// method that sets up a cross-browser XMLHttpRequest object
function getHTTPObject() {
	var http_object;

	// MSIE Proprietary method

	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			http_object = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				http_object = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (E) {
				http_object = false;
			}
		}
	@else
		xmlhttp = http_object;
	@end @*/

	  /*if(window.XMLHttpRequest)  http = new XMLHttpRequest(); 
	  else 
		if (window.ActiveXObject)  http = new ActiveXObject(Microsoft.XMLHTTP);*/

	// Mozilla and others method

	if (!http_object && typeof XMLHttpRequest != 'undefined') {
		try {
			http_object = new XMLHttpRequest();
		}
		catch (e) {
			http_object = false;
		}
	}

	return http_object;
}
var http = getHTTPObject();
var NORMAL_STATE = 4;
var loadContentArea = null;

// The ajaxQ is meant to make it so ajax requests can't overlap, they will just que up and run in sequence

var ajaxQ = Array();
var ajaxQnum = 0;
var ajaxDoNum = 0;
var ajaxArgs;

function changeContent(args){
	//alert(http.responseText);
	getObj(args[2]).innerHTML = http.responseText;
}
function changeValue(args){
	//alert(http.responseText);
	//getObj(args[2]).value = "";
	getObj(args[2]).value = http.responseText;
}

function populateForm(args){
	var fields = http.responseText.split(",");
	var myFields = new Object();
	for(key in fields){
		var parts = fields[key].split("=");
		var name = parts[0];
		var value = parts[1];
		//alert(name+"="+value);
		myFields[name] = value;
	}	
	var myForm = getObj(args[2]);
	var formLength = myForm.length;
	for (i=0; i<formLength;i++){
		var thisField = myForm[i];
		if(myFields[thisField.name]){
			thisField.value=myFields[thisField.name];
		}
	}
}

function ajaxQnext(){
	ajaxArgs = ajaxQ[ajaxDoNum].split("<ajax>");
	//swapClass("working","show");
	http.open('GET', ajaxArgs[0], true);
	http.onreadystatechange = ajaxContentLoaded;
	http.send(null);	
}

function ajaxRequest(url, onLoadFunction, args){
	ajaxQ[ajaxQnum] = url+"<ajax>"+onLoadFunction+"<ajax>"+args;
	if(ajaxQnum==ajaxDoNum) ajaxQnext();
	ajaxQnum++;
	if(onLoadFunction == "changeContent"){
		var argParts = args.split(",");
		//insertFlash("media/waiting.swf","20","20",args);
		getObj(args).innerHTML = "Processing...";
	}
}

function getFormValues(formName){
	var myForm = getObj(formName);
	var formLength = myForm.length;
	var passVars = "";
	for (i=0; i<formLength;i++){
		passVars += "&"+myForm[i].name+"="+myForm[i].value;
	}
	return passVars;
}

function ajaxContentLoaded(args){
	if (http.readyState == NORMAL_STATE) {
		//alert(http.responseText);
		//alert(ajaxArgs[1]+" | "+ajaxArgs[2]);
		if(ajaxArgs[1] != "none") eval(ajaxArgs[1])(ajaxArgs);
		ajaxDoNum++;
		//swapClass("working","hide");
		if(ajaxQnum > ajaxDoNum) ajaxQnext();
	}
}

function validateAndSubmit(formName,respondDiv,processingPage){
	if(validateForm(formName)) ajaxSubmitForm(formName,respondDiv,processingPage);
	return false;
}

function ajaxSubmitForm(formName,respondDiv,processingPage){ // sends all form values to processingPage as $_GET (PHP) variables
	//alert(processingPage+","+formName+","+respondDiv);
	ajaxRequest(processingPage+"?"+getFormValues(formName), "changeContent", respondDiv);
	return false;
}