//Ajax generic implementation, NSH 2006/7
//Version 2.0, 20th June 2007

/*------
To call AJAX, parameters are:

1) the URL to be read (eg. http://www.google.co.uk or /test.php?foo=bar etc.)

2) object IDs to update with results, comma-separated if more than one.
* IF LEFT EMPTY, ajaxRead sets itself to syncronous mode rather than asyncronous mode
** IF SET TO ZERO, doesn't update anything, but runs asyncronously anyway

3) the name of a function to run when the ajax response comes in (eg. "updatePage();")

4) post data to be sent with the ajax call, URL encoded . eg. "foo=bar&test=my+string"

5) return object - if true and running syncronously, function returns an object

* IF LEFT EMPTY, ajaxRead defaults to GET mode, otherwise POST mode. *

------*/
function ajaxRead(file,objNames,runfunc,postData,returnObject) {
	var xmlObj = null;
	if(window.XMLHttpRequest){
		xmlObj = new XMLHttpRequest();
	} else if(window.ActiveXObject){
		xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return;
	}
	var Sync=false;
	if (objNames) {
		if (objNames=="") {
			Sync=true;
		}
	} else {
		Sync=true;	
	}
	//Remove dummy object name of "0"
	if (objNames=="0") {
		objNames="";
	}
		
	if (!Sync) {
		//Say "please wait" nicely...
		updateObj(objNames,"Please wait...");
		
		//Work out ID of first object name
		var tobjArr=new Array;
		tobjArr=objNames.split(",");
		
		//Add ID field to filename to avoid 
		if (file.match("\\?")) {
			file=file+"&_idfield_="+tobjArr[0];
		} else {
			file=file+"?_idfield_="+tobjArr[0];
		}
	}
	
	//Add unique identifier to filename to disable cacheing
	var now = new Date();
	if (file.match("\\?")) {
		file=file+"&_UID_="+now.getTime();
	} else {
		file=file+"?_UID_="+now.getTime();
	}
	file=file+"&_UID_="+now.getTime();

	if (!Sync) {
		xmlObj.onreadystatechange = function() {
			if(xmlObj.readyState == 4){
				//Update all the objects
				updateObj(objNames, xmlObj.responseText);
				
				if (runfunc) {
					eval(runfunc);
				}
			}
		}
	}
	
	//Get or post?
	if (postData) {
		//POST
		xmlObj.open('POST', file, !Sync);
		
		//Send the proper header information along with the request
		xmlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlObj.setRequestHeader("Content-length", postData.length);
		xmlObj.setRequestHeader("Connection", "close");

		//Send the post data
		xmlObj.send(postData);
		
	} else {
		//GET
		xmlObj.open ('GET', file, !Sync);
		xmlObj.send ('');
	}	
	if (Sync) {
		if (returnObject) {
			//ie hack
			if (xmlObj.responseXML.childNodes.length == 0) {
				tempNode = new ActiveXObject('Microsoft.XMLDOM');
				tempNode.async = false;
				tempNode.loadXML(xmlObj.responseText);
				
				return tempNode;
			} else {
				return xmlObj.responseXML;
			}
		} else {
			return xmlObj.responseText;
		}
	}

}

function updateObj(objn, data) {
	var objArr=new Array;
	objArr=objn.split(",");
	//Update all the objects
	for (var n = 0; n < objArr.length; n++) {
		var myObj=document.getElementById(objArr[n]);
		if (myObj) {
			var myType;
			
			myType=myObj.type;
			if (myType=='submit' || myType=='hidden') {
				myObj.value = data;
			} else {
				myObj.innerHTML = data;
			}
		}
	}
}
