/*
AJAXlib.js

A function like the following must be coded in the HTML page
to handle data returned from the AJAX call.

	function cback(sText)
	{
		document.getElementById("RESP").innerHTML = sText
	}

Error handling has been added:
200 - OK				The request succeeded.
204 - No Content		The document contains no data.
301 - Moved Permanently	The resource has permanently moved to a different URI.
401 - Not Authorized	The request needs user authentication.
403 - Forbidden			The server has refused to fulfill the request.
404 - Not Found			The requested resource does not exist on the server.
408 - Request Timeout	The client failed to send a request in the time allowed by the server.
500 - Server Error		Due to a malfunctioning script, server configuration error or similar.

*/

function createREQ() {
try {
     req = new XMLHttpRequest(); /* e.g. Firefox */
     } catch(err1) {
       try {
       req = new ActiveXObject('Msxml2.XMLHTTP'); /* some versions IE */
       } catch (err2) {
         try {
         req = new ActiveXObject("Microsoft.XMLHTTP"); /* some versions IE */
         } catch (err3) {
          req = false;
         }
       }
     }
     return req;
}
function requestGET(url, query, req) {
myRand=parseInt(Math.random()*99999999);
req.open("GET",url+'?'+query+'&rand='+myRand,true);
req.send(null);
}
function requestPOST(url, query, req) {
req.open("POST", url,true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(query);
}
function doCallback(callback,item) {
eval(callback + '(item)');
}

function doAjax(url,query,callback,reqtype,getxml) {
	// create the XMLHTTPRequest object instance
	var myreq = createREQ();

	myreq.onreadystatechange = function() {
	if(myreq.readyState == 4) {
	   if(myreq.status == 200) {
		  var item = myreq.responseText;
		   // This turns OFF a DIV layer with ID of "AJAXWAIT" in the HTML page
		  // to signal the user that the page is no longer tied up - see AJAXwait.js
		  document.getElementById("AJAXWAIT").style.visibility = "hidden";
		  if(getxml==1) {
			 item = myreq.responseXML;
		  }
		  doCallback(callback, item);
		}
		else {
			alert ("HTTP Resp Code: " + myreq.status);
		}
	  }
	  else {
		   // This turns on a DIV layer with ID of "AJAXWAIT" in the HTML page
		  // to let the user know that the page is "busy" - see AJAXwait.js
		  document.getElementById("AJAXWAIT").style.visibility = "visible";
	  }
	}

	if(reqtype=='post') {
		requestPOST(url,query,myreq);
	} else {
		requestGET(url,query,myreq);
	}
}
