// 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;
}


function compare(){
if(xmlHttp){
    
    corp1=document.getElementById("corp1").value;
    corp2=document.getElementById("corp2").value;
    var prm="";
    try{    
        prm+="corp1="+corp1;
        prm+="&corp2="+corp2;
        send_Request(prm);
    }catch(e){};
} 
} 


function send_Request(param){
try
    {
        // initiate reading a file from the server
      xmlHttp.open("POST", "/ajax/compare.php", true);
      xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      xmlHttp.onreadystatechange = RequestStateChange;
      xmlHttp.send(param);
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }

}
// function called when the state of the HTTP request changes
function RequestStateChange() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        ServerResponse();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
 
    }
  }
}

// handles the response received from the server
function ServerResponse()
{
 var xmlResponse = xmlHttp.responseXML;
  // catching potential errors with IE and Opera
  if (!xmlResponse || !xmlResponse.documentElement)
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  // catching potential errors with Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror") throw("Invalid XML structure");
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;
  try{
    id1= xmlRoot.getElementsByTagName("id1");
    id2= xmlRoot.getElementsByTagName("id2");
  
    itemname1= xmlRoot.getElementsByTagName("itemname1");
    itemname2= xmlRoot.getElementsByTagName("itemname2");
    phones1=document.getElementById("phone1");
    phones2=document.getElementById("phone2");
    j1=id1.length;
    j2=id2.length;
    phones1.length=0;
    phones2.length=0;
    for(i=0;i<j1;i++) phones1.options[i] = new Option(itemname1[i].childNodes[0].nodeValue, id1[i].childNodes[0].nodeValue,  true, true);
    for(i=0;i<j2;i++)phones2.options[i] = new Option(itemname2[i].childNodes[0].nodeValue, id2[i].childNodes[0].nodeValue,  true, true);
  } catch(e){};
}

function do_select(checkg,val)
{
form=document.getElementById("chooser");
alert(form.checkg.length);
//for (i=0; i<form.checkgroup.length; i++){
//if (document.chooser.checkgroup[i].value==val)  document.chooser.checkgroup[i].checked=true;
//}
}


