/******************************************************************************
Function: ProcessAJAX
Arguments:
	serverPage = the name of the .php to call to do the processing
	objId = HTML element that gets updated by the resonse from calling serverPage
	method = either "POST" or "GET"
	async = either "true" or "false"
	str = the list of things to process (or NULL)
Returns: Response text (only useful if async is false)  Note if using the responseText
as a string it will have prepended CR/LF characters.  Use the following function to remove
these leading characters.  
Author: Alex Clarke
Revisions:
JP - 3/12/09 - Added Async parameter to control pend or not pend on Open call
JP - 5/29/09 - Discovered that returning the response text when in Async mode causes a 
javascript error in the calling script.  Modified so a value is only returned on pended calls
(i.e. Async parameter is false).
*******************************************************************************/
// JP - use the following function to strip the leading CR/LF
// NewString = ReturnString.replace(/[^(\x20-\x7F)]*/,"");

function ProcessAJAX(serverPage,objId,method,async,params) {
	// Instanciate an xmlhttprequest object
	try {//alert("In ProcessAJAX");
		var xmlhttpreq = new ActiveXObject('MSXML2.XMLHTTP'); }
	catch (e) 
		{
		try {var xmlhttpreq = new ActiveXObject('Microsoft.XMLHTTP'); }
		catch (e) 
			{	
			try {var xmlhttpreq = new XMLHttpRequest();}			
			catch(e) { alert ("Error creating XMLHttpRequest object"); }
			}
	}

	// define a callback function
		xmlhttpreq.onreadystatechange = function() {
		//alert("ready state " + xmlhttpreq.readyState + " stat " + xmlhttpreq.status);
		if (xmlhttpreq.readyState == 4 && xmlhttpreq.status == 200) {
			//alert( "proc ajax ready state 4 ");
			if(objId) objId.innerHTML = xmlhttpreq.responseText;
				
		}
	}
	
	// Microsoft caching hack - see this link http://www.ilovemyjournal.com/?action=view_entry&eid=4182
	var random_number = Math.floor(Math.random()*30000000000);
	if (params.length == 0) 
		params = "random=" + random_number;
	else
		params = params + "&random=" + random_number;

	// Make request - note Request Header must be called after open and before send.
	if (method.toUpperCase() == "POST") {
		xmlhttpreq.open(method,serverPage,async);
		xmlhttpreq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlhttpreq.send(params);
	}
	else if (method.toUpperCase() == "GET") {
		xmlhttpreq.open(method,serverPage + "?" + params,async );
		xmlhttpreq.send(null);

	}
	
	//Return values only make sense if the calling script is waiting for them (i.e. pended mode)
	if (!async)		{
		return xmlhttpreq.responseText;
		//alert("proc ajax returning " + xmlhttpreq.responseText);
	}
}
//
function GetFormValues(fobj) {
	var str = "";
	for (ii = 0; ii < fobj.elements.length; ii++) {
		str += fobj.elements[ii].name + "=" + encodeURI(fobj.elements[ii].value) + "&";
	}
	return str;
}
//
function ProcessForm(formId,serverPage,formDIVID) {
	str = GetFormValues(document.getElementById(theform)); // direction = LOAD
 	formDIV = document.getElementById(formDIVID);
 	ProcessAJAX(serverPage,formDIV,"POST",true,str);
}
//