/*
Parameters:
	url—	The string that contains the URL of the server-side program to call.
	obj— 	The object reference to an HTML element on the page to update.
	func — 	The name of a JS function page that will update the page contents
			with the obj parameter and the AJAX response.
	isAsync-	Is the request made async or block the browser
	isGet -	Is the request using 'GET' (true) or 'POST' (false). Note currently
			other methods such as 'PUT' are not supported
			
Notes:

	Other overload variants provided with devaults to full update function
	
	If an obj with no func is given then the obj will be updated with the
	response (the obj element inner HTML will be set to the response.
	
	XMLHttpRequest also supports open with username, password for security.
	Versions with arameters will be included in a future release.
	
	We could check for readyState=3 which is receiveing results and if there is a
	status object then update it with "Receiving..." or some such.
*/

function AJAXUpdate(url, obj, func)
{
	// Assumes url is call via GET
	return AJAXUpdate(url, obj, func, true, true);
}

function AJAXUpdate(url, obj, func, isAsync, isGet)
{
	return AJAXUpdate(url, obj, func, true, isGet);
}

function AJAXUpdate(url, obj, func, isAsync, isGet)
{
	return AJAXUpdate(url, obj, func, true, isGet, null);
}

function AJAXUpdate(url, obj, func, isAsync, isGet, statusObj)
{
	// Don't run if missing the url parm.
	if (!url) return false;
	
	// instantiate xmlhttp object
	//  global variable: holds type of ActiveX toinstantiate

	var _ms_AJAX_Request_ActiveX = "";
	if (window.XMLHttpRequest) {
		// code for Mozilla, etc.
		var xmlhttp=new XMLHttpRequest();
		}
	else if (window.ActiveXObject) {
		// Code for IE. Instantiate the latest MS ActiveX Objects
		if (_ms_AJAX_Request_ActiveX){
			xmlhttp = new ActiveXObject(_ms_AJAX_Request_ActiveX);
		}
		else {
			// loops through the various versions of XMLHTTP to ensure the latest	
			var versions = [
						"MSXML2.XMLHTTP.12.0",
						"MSXML2.XMLHTTP.11.0",
						"MSXML2.XMLHTTP.10.0",
						"MSXML2.XMLHTTP.9.0",
						"MSXML2.XMLHTTP.8.0",
						"Msxml2.XMLHTTP.7.0",
						"Msxml2.XMLHTTP.6.0",
						"Msxml2.XMLHTTP.5.0",
						"Msxml2.XMLHTTP.4.0",
						"MSXML2.XMLHTTP.3.0",
						"MSXML2.XMLHTTP",
						"Microsoft.XMLHTTP"
						];
			for(var i = 0; i < versions.length ; i++) {	
				try {
					// Try to create object.  If it doesn't work, try again
					// if it does work, save a reference to the proper one to
					// speed up future instantiations
					xmlhttp  = new ActiveXObject(versions[i]);
					if (xmlhttp) {
						_ms_AJAX_Request_ActiveX = versions[i];
						break;
					}
				}	
				catch (objException) { 
					// trap -  try next one
				}
			}
		}
	}
	
	// If we didn't get a xmlhttp object then bail out.
	// This should be changed to test if there is a status object then
	// post the fact we didn't get on to that html object
	if (!xmlhttp) return false;

	xmlhttp.onreadystatechange =
		function() {
			if (xmlhttp.readyState != 4) return;
			// If we have a wait status message object, remove it
			if (statusObj) {
				document.getElementById(statusObj).innerHTML = "";
			}
			// If successful then either call the function the caller wants,
			// or update the document object passed in, or do nothing if neither
			// was passed in
			if (xmlhttp.status == 200)
				if (func)
					func(obj, xmlhttp.responseText);
				else
					if (obj) document.getElementById(obj).innerHTML = responseText;
			else
				if (statusObj)
					document.getElementById(statusObj).innerHTML = "An error occurred" + http.status";
				else
					alert("An error occurred" + http.status);
		}
		
	if (isGet)
		xmlhttp.open('GET', url, isAsync);
	else
		xmlhttp.open('POST', url, isAsync);
		
	xmlhttp.send(null);
	return true;
}