// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// call server asynchronously
function process(form, step, brand_name, man_model)
{
/*  var prod_type = form.prod_type.value;
  if(form.brand_name){
    var brand_name = form.brand_name.value;
  }
  if(form.man_model){
    var man_model = form.man_model.value;
  } */
  
  try{
  var prod_type = document.getElementById('prod_type').value;
  var brand_name = document.getElementById('brand_name').value;
  var man_model = document.getElementById('man_model').value;
  } 
  catch (e){}
  
  
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {    
        xmlHttp.open("GET", 'abc.php'+ "?" + 'step=' + step + '&prod_type=' + prod_type + '&brand_name=' + brand_name + '&man_model=' + man_model, true);
        xmlHttp.onreadystatechange = displayResult;
        xmlHttp.send(null);
      }
 }
 
function process_boxes(prod_type, step, brand_name, form, box)
{
  
  try{
  var brand_name = document.getElementById('brand_name').value;
  var man_model = document.getElementById('man_model').value;
  } 
  catch (e){}
  
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {    
        xmlHttp.open("GET", 'abc.php'+ "?" + 'step=' + step + '&prod_type=' + prod_type + '&brand_name=' + brand_name + '&man_model=' + man_model + '&box=' + box, true);
        xmlHttp.onreadystatechange = function(){
            displayResultBox(box);
        }
        xmlHttp.send(null);
 }
}
// handles the response received from the server
function displayResult()
{
  if (xmlHttp.readyState != 4) {
  document.getElementById("wait").innerHTML = '<img src="images/ajax-loader.gif" width="16" height="16">';
  } else {
	document.getElementById("wait").innerHTML = '<img src="images/pixel_trans.gif" width="16" height="16">';
  // retrieve the server's response 
  var response = xmlHttp.responseText;
  /*
  document.getElementById("wait").innerHTML = '<a href="javascript:void(0);" onmouseover="return overlib(\'You can easily find the battery your looking for here. If you know the model of your Camcorder, Digital Camera or Cordless phone, use the model option. If you know the original manufacurers battery code then select the battery option.\', AUTOSTATUS, WIDTH, 400);" onmouseout="nd();"><img src="images/info.gif" width="16" height="16" border="0"></a>'; 
  */
  //'<img src="images/wait_blank.gif" width="16" height="16">';
  document.getElementById("battery_result").innerHTML = response; 
  }
}

function displayResultBox(box)
{
          if (xmlHttp.readyState != 4) {
          document.getElementById("wait").innerHTML = '<img src="images/ajax-loader.gif" width="16" height="16">';
          } else {
          // retrieve the server's response 
		  document.getElementById("wait").innerHTML = '<img src="images/pixel_trans.gif" width="16" height="16">';
          var response = xmlHttp.responseText;
          /*
		  document.getElementById("wait").innerHTML = '<a href="javascript:void(0);" onmouseover="return overlib(\'You can easily find the battery your looking for here. If you know the model of your Camcorder, Digital Camera or Cordless phone, use the model option. If you know the original manufacurers battery code then select the battery option.\', AUTOSTATUS, WIDTH, 400);" onmouseout="nd();"><img src="images/info.gif" width="16" height="16" border="0"></a>';
		  */
          //'<img src="images/wait_blank.gif" width="16" height="16">';
          document.getElementById('box['+box+']').innerHTML = response; 
        }
      }

function find_battery(brand){
  var man_model = document.getElementById('man_model').value;
  window.location = 'index.php?display_battery=true&man_model='+man_model+'&brand_name='+brand;
}
