// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
var cache = new Array();
// 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;
}

// read a file from the server
function process()
{
	params='';
	params+='C8='+document.formular.C8.value;
	params+='&C9='+document.formular.C9.value;
	params+='&C10='+document.formular.C10.value;
	params+='&C11='+document.formular.C11.value;
	cache.push(params);
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate reading a file from the server
    	if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && cache.length > 0){
      	while (cache.length > 0)
      		var cacheEntry = cache.shift();
	      xmlHttp.open("GET", "../ajax/xml_vis_calculate.php?"+cacheEntry, true);
	      xmlHttp.onreadystatechange = handleRequestStateChange;
	      xmlHttp.send(null);
    	}
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function calculateRecords()
{
	document.formular.C9.value = document.formular.C8.value * 2;
	document.formular.C10.value = document.formular.C8.value * 0.9;
}

// function called when the state of the HTTP request changes
function handleRequestStateChange() 
{
  // 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
        handleServerResponse();
      }
      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 handleServerResponse()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;  
  // obtain arrays with book titles and ISBNs 
  valArray = xmlRoot.getElementsByTagName("val");
  varArray = xmlRoot.getElementsByTagName("variable");
  // generate HTML output
  // iterate through the arrays and create an HTML structure
  for (var i=0; i<varArray.length; i++){
    value = valArray.item(i).firstChild.data;
    elementName = "div"+varArray.item(i).firstChild.data;
    if (elementName == "divC83")
      value = "$ "+value;
    myDiv = null;
    myDiv = document.getElementById(elementName);
    if (myDiv != null)
      myDiv.innerHTML = value;
  }
}

function handleKey(e) 
{
  // get the event
  e = (!e) ? window.event : e;
  // get the code of the character that has been pressed        
  code = (e.charCode) ? e.charCode :
         ((e.keyCode) ? e.keyCode :
         ((e.which) ? e.which : 0));
  // handle the keydown event       
  if (e.type == "keydown") 
  {
    // if enter (code 13) is pressed
    if(code == 13)
    {
      // send the current message  
      sendMessage();
    }
  }
}
