var xmlHttp, objectID = ''; 
var functionsArray = new Array(); 
var jsonObj = new Object; 

function runArrayFunction() { 

	if ( functionsArray.length > 0 ) {
		var firstElement = functionsArray.shift(); 
		
		// alert("About to run function "+firstElement+"\n\n(Array len = "+functionsArray.length+")"); 
		
		eval(firstElement);
	} 	
}


function displayInfo() {
	
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) {
			document.body.style.cursor = 'default';
			document.getElementById(objectID).innerHTML = xmlHttp.responseText; 
			runArrayFunction(); 
	} else {
		document.body.style.cursor = 'wait';
		//document.getElementById(objectID).innerHTML = '<div style="text-align:center; color:red; background-color:white; font-weight:bold; padding:2px;">Loading ('+xmlHttp.readyState+')...</div>';
	} 
		
}

function displayEvalInfo() {
	
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) {
		
		document.getElementById(objectID).innerHTML='';
			
		if (xmlHttp.status == 200) {
			document.body.style.cursor = 'default'; 
			eval(xmlHttp.responseText); 
			runArrayFunction(); 
		} else { 
			alert("Error: " + returnHTTPErrorMsg(xmlHttp.status)); 
			document.body.style.cursor = 'default'; 
		}
		
	} else { 
		document.body.style.cursor = 'wait';
		//document.getElementById(objectID).innerHTML = '<div style="text-align:center; color:red; background-color:white; font-weight:bold; padding:2px;">Loading ('+xmlHttp.readyState+')...</div>';
	} 
		
} 

function displayJSONInfo() { 
	
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) { 
		
		document.getElementById(objectID).innerHTML='';
		
		if (xmlHttp.status == 200) {
			document.body.style.cursor = 'default'; 
			jsonObj = eval("(" + xmlHttp.responseText + ")"); 
			runArrayFunction(); 
		} else {
			alert("Error: " + returnHTTPErrorMsg(xmlHttp.status)); 
			document.body.style.cursor = 'default'; 
		}
		
	} else { 
		document.body.style.cursor = 'wait';
		//document.getElementById(objectID).innerHTML = '<div style="text-align:center; color:red; background-color:white; font-weight:bold; padding:2px;">Loading ('+xmlHttp.readyState+')...</div>';
	} 
		
} 

function returnHTTPErrorMsg ( httpStatus ) {
	
	errorMsg = "There was an unknown error trying to access this page on the server (A "+httpStatus+" error was returned)";  //  http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
	if ( httpStatus==0 ) 
		errorMsg = "No connection to the internet can be detected at this time. Try testing your connection first."; 
	else if ( httpStatus==400 ) 
		errorMsg = "Your request was syntactically incorrect. Some code may need to be changed before you attempt this operation again."; 
	else if ( httpStatus==404 ) 
		errorMsg = "The page the application was trying to access could not be found at this point. (404 - File missing error was returned)"; 
	else if ( httpStatus==500 ) 
		errorMsg = "Undefined server error has occured. Either try your request again or contact your Web Hoster for possible details on this problem."; 
			
	return errorMsg; 
	
}

function getDBInfo (url, changeFunction, newIDVar, postGetOption, formName, varArray, valuesArray) {
	
	xmlHttp = GetXmlHttpObject();
	
	if ( testHTTPCompatible() ) {
		
		objectID = newIDVar; 
		
		if ( !document.getElementById(objectID) ) {
			
			alert('Developer Error: There is no form element on the page with the id of "'+objectID+'". This needs to be created before continuing...'); 
			
		} else {
		
		if ( postGetOption.toUpperCase() == 'GET' ) { 
			
			if ( varArray ) {
				for ( var ind=0; ind<varArray.length; ind++ ) {
					if ( url.indexOf('?')==-1 ) url += '?';  
					url += encodeURIComponent(varArray[ind]) + '=' + encodeURIComponent(valuesArray[ind]) + '&'; 
				}
			}
			
			var asynchAjax = ( typeof synchUpJscript!='undefined' ) ? synchUpJscript : true; 
			
			if ( asynchAjax ) xmlHttp.onreadystatechange = changeFunction; 
			
			url = url+"&sid="+Math.random();
			xmlHttp.open("GET", url, asynchAjax);
			xmlHttp.send(null); 
			document.getElementById(objectID).innerHTML = '<img src="http://www.yesit.com.au/images/ajax-loader.gif" width="16" height="16" />'; 
			
			if ( !asynchAjax ) { 
				changeFunction(); 
			}
			
		} else if ( postGetOption.toUpperCase() == 'POST' ) { 
			
			var docForm = formName.elements, params = ''; 
			var saveFrmChecksRadios = ( typeof(formName.saveChecksAndRadios)!='undefined' && formName.saveChecksAndRadios ) ? true : false; 
			for ( var fl = 0; fl < docForm.length; fl++ ) {
				
				elType = docForm[fl].type.toLowerCase(); 
				
				if ( elType == 'checkbox' && (docForm[fl].checked || saveFrmChecksRadios) || 
					elType == 'radio' && (docForm[fl].checked || saveFrmChecksRadios) || 
					elType != 'checkbox' && elType != 'radio' && elType != "button" ) 
				params += encodeURIComponent(docForm[fl].name) + "=" + encodeURIComponent(docForm[fl].value) + "&";
				
			} 
			
			// alert("About to POST info now..."); 
			
			xmlHttp.open("POST", url, false);
			xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlHttp.send(params+"&sid="+Math.random()); 
			document.getElementById(objectID).innerHTML = '<img src="http://www.yesit.com.au/images/ajax-loader.gif" width="16" height="16" />'; 
			
			changeFunction(); 
			
		} else alert('Developer Error: GET or POST option is not set properly.'); 
		
		}
		
	}
	
}


function abortLastAjaxCall () {
	if ( xmlHttp!='' ) xmlHttp.abort(); 
}


function GetXmlHttpObject() { 

	var objXMLHttp = null;
	
	if ( window.XMLHttpRequest ) {
		objXMLHttp = new XMLHttpRequest();
	} else if ( window.ActiveXObject ) {
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	}
	
	return objXMLHttp; 
}

function testHTTPCompatible () {
	var testComp = true;
	if ( xmlHttp == null ) {
		alert ("This browser does not support HTTP Request. You will most likely need to use I.E. or Firefox/Mozilla.");
		testComp = false;
	} 
	return testComp;
}
