// include our JavaScript stuff for Ajax
// the ajaxml object we need to make our calls

// need way to keep track things already added to a cart!

var ajax_info_r = make_new_ajax_request();
var ajax_info_busy = false;
var showing_info_request = false;

function info_request_callback()
{
   if (ajax_info_r.readyState == 4) {
      if (ajax_info_r.status == 200) {
         // Get the response from the server - conveniently already
         //  as an XML tree (requires some subtle header settings in 
         //  the server script
         var d = ajax_info_r.responseXML;
         node = d.getElementsByTagName('Result')[0];
         if (node)
         {  if (node.childNodes[0].nodeValue == 'Success')
            {  
               showAlert("Your message has been received");
               el = document.getElementById("info_comment");
               el.value = "";
               jump_to_last_breadcrumb();
            }
            else
            {  showAlert('Sorry, something went wrong with our messaging system');
            }
         }
      }       
   }


/*  el = document.getElementById("search_count");   
  el.innerHTML = out;
  document.getElementById("intro_text").innerHTML = '';
   */   
}

function show_info_request(show, cue_id)
{  
   el = document.getElementById("info_section");
   if (!el) // for standalone case
   {  return;
   }
   el.style.display = (show ? "block" : "none"); 
   showing_info_request = show;

   if (show != true) 
   {  // all done now 
      return;
   }

   show_login(false);
   show_cart(false);
   show_my_bins(false); 
   show_bin(false, 0); 
   show_order_summary(false, null);
   show_search(false);
   show_search_form(false);
   show_share(false, null);
   show_register(false);
   if (standalone)
   {  show_prefs(false);
      show_updater(false);
   }
   show_needledrop(false);
   show_data_find(false);
   show_new_releases(false);
   show_cue_sheets(false);

   update_breadcrumb("INFO", false);

   update_cart_status_line();
   if (cue_id != 0)
   {
      cue_data = all_cue_data[cue_id];

      el = document.getElementById("info_request_track");
      el.innerHTML = cue_data.cue_code + " ( " + cue_data.cue_title + " ) ";

      el = document.getElementById("info_cue_id");
      el.value = cue_data.cue_id;

      el = document.getElementById("info_cue_code");
      el.value = cue_data.cue_code;

      if (user.pretty_name)
      {  el = document.getElementById("info_name");
         el.value = user.pretty_name;
      }
      if (user.company)
      {  el = document.getElementById("info_company");
         el.value = user.company;
      }
      if (user.email)
      {  el = document.getElementById("info_email");
         el.value = user.email;
      }
      if (user.phone)
      {  el = document.getElementById("info_phone");
         el.value = user.phone;
      }
   }
}

function handle_info_request_submit()
{
   radios = document.getElementsByName("prefer_group");
   
   prefer_phone = null;

   for (i = 0; i < radios.length; i++)
   {  if (radios[i].checked == true)
      {  if (radios[i].value == "phone")
         {  prefer_phone = true;
         }
         else
         {  prefer_phone = false;
         }
         break;
      }
   }
 
   el = document.getElementById("info_name");
   name_in = trim(el.value);

   el = document.getElementById("info_company");
   company_in = trim(el.value);

   el = document.getElementById("info_email");
   email_in = trim(el.value);

   el = document.getElementById("info_phone");
   phone_in = trim(el.value);

   el = document.getElementById("info_comment");
   comment_in = trim(el.value);

   el = document.getElementById("info_cue_id");
   cue_id_in = trim(el.value);

   el = document.getElementById("info_cue_code");
   cue_code_in = trim(el.value);

   if (prefer_phone == null)
   {  showAlert("Please choose whether you'd like to be contacted by phone or email");
      return;
   }
   else if (prefer_phone == true)
   {  if (!phone_in)
      {  showAlert("Please provide a phone number");
         return;
      }
   }
   else 
   {  if (!email_in)
      {  showAlert("Please provide an email address");
         return;
      }
   }
   request_info(name_in, company_in, email_in, phone_in, prefer_phone, 
      cue_id_in, cue_code_in, comment_in);
}

function request_info(name, company, email, phone, prefer_phone, 
   cue_id, cue_code, comment)
{ 

   xml = "<\?xml version=\"1.0\"\?><InfoRequest>";
   xml += "<Name>" + xml_escape(name) + "</Name>";
   xml += "<Company>" + xml_escape(company) + "</Company>";
   xml += "<Email>" + xml_escape(email) + "</Email>";
   xml += "<Phone>" + xml_escape(phone) + "</Phone>";
   xml += "<CueKey>" + xml_escape(cue_id) + "</CueKey>";
   xml += "<CueCode>" + xml_escape(cue_code) + "</CueCode>";
   xml += "<Comment>" + xml_escape(comment) + "</Comment>";
   xml += "<PreferredContact>" + (prefer_phone ? "Phone" : "Email") + "</PreferredContact>";
   xml += "</InfoRequest>";
   xml = "xml=" + escape(xml);

   ajax_info_r.open("POST", "/info_request_backend.php", true);
   
ajax_info_r.onreadystatechange = info_request_callback;
   ajax_info_r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   ajax_info_r.send(xml);
}



















