// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function process(URI)
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
	// execute the respective page from the server with all the relevant getParams
    xmlHttp.open("GET", URI, true);  
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('process()', 1000);
}

function updateTBL(caller,tbl,ID,OID)
{
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
	var theVal = trim(caller.value);
	if (caller.type=="checkbox" || caller.type=="radio")
		theVal=Number(caller.checked);
	var callerID = caller.getAttribute('Id');
	var URI = "simpleUpdate.php?caller="+callerID+"&value="+theVal+"&ID="+ID+"&tbl="+tbl+"&OID="+OID;
	// execute the respective page from the server with all the relevant getParams
    xmlHttp.open("GET", URI , true); 
	xmlHttp.send(null);
	var pseudoDate = new Date();
	if (document.getElementById("changed_"+ID))
	{
	var Jahr = pseudoDate.getFullYear();
	var Monat = pseudoDate.getMonth();
	if (Monat <10) Monat="0"+Monat;
	var Tag = pseudoDate.getDate();
	if (Tag <10) Tag="0"+Tag;
	var Stunde = pseudoDate.getHours();
	if (Stunde <10) Stunde="0"+Stunde;
	var Minute = pseudoDate.getMinutes();
	if (Minute <10) Minute="0"+Minute;
	var Sec = pseudoDate.getSeconds();
	if (Sec <10) Sec="0"+Sec;
	document.getElementById("changed_"+ID).innerHTML = Jahr +"-"+Monat+"-"+Tag+" "+Stunde+":"+Minute+":"+Sec;
	} 
	
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('updateTBL('+caller+','+tbl+','+ID+')', 1000);
}

function cascadingUpdate(callerval,tbl,uptbl)
{
	// callerval: the current id of the calling element's ENTRY
	// tbl: the table opened when the call was issued i.e. the DB context in which the calling element is embedded in
	// uptbl: the table where the the calling element takes its values from
	// *** IMPORTANT: there are (at least one) more arguments passed to this function = dependent elements to be updated by the call
	var URI = "cascadingUpdate.php?tbl="+tbl+"&uptbl="+uptbl+"&id="+callerval+"&up=";
	var upEls = new Array();
	for(i=3;i<cascadingUpdate.arguments.length;i++)
	{
		upEls.push(cascadingUpdate.arguments[i]);
	}
	URI = URI + upEls.join(",");
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
    	///alert (URI);
		xmlHttp.open("GET", URI , true);
		xmlHttp.onreadystatechange = doCascadingUpdate;
		xmlHttp.send(null);
	}
    else 
    {
       setTimeout('cascadingUpdate('+cascadingUpdate.arguments+')', 1000);
    }
  
}

function doCascadingUpdate()
{
 if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlRoot = xmlResponse.documentElement;
      elementsArray = xmlRoot.getElementsByTagName("idname");
	  valueArray = xmlRoot.getElementsByTagName("value");
	  for (var i=0;i<elementsArray.length;i++)
	  {
	  	objID = elementsArray.item(i).firstChild.data;
		
		if (document.getElementById(objID))
		{
			optionsArray = valueArray.item(i).getElementsByTagName("option")
			document.getElementById(objID).options.length=null;
			document.getElementById(objID).options.length=optionsArray.length;
			for (var j=0;j<optionsArray.length;j++)
			{
			var Opt = new Option(optionsArray.item(j).firstChild.data,optionsArray.item(j).attributes.getNamedItem("value").value,false,false);
			document.getElementById(objID).options[j]=Opt;
			}
		}
	  }
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
	
}

// executed automatically when a message is received from the server
function handleServerResponse() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlDocumentElement = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element
      helloMessage = xmlDocumentElement.firstChild.data;
      // update the client display using the data received from the server
      document.getElementById("divMessage").innerHTML = 
                                            '<i>' + helloMessage + '</i>';
      // restart sequence
      setTimeout('process()', 1000);
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

