var xmlHttps = new Array();

function ajaxUpdate(str,updateURLp,updateObjectIDp) {
	//Calls an AJAX update
	// str - string that you want to pass, labelled as "q"
	// updateURLp - the URL that will be called
	// updateObjectIDp - the ID of the HTML object that will receive the AJAX info
	//GetXmlHttpObject.prototype.updateObjectID = updateObjectIDp;

	//Creates a unique ID XMLHTTP object for each object,
	//but allows older versions of the request object to be overwritten in each HTML object
	if(xmlHttps[updateObjectIDp]) {
		xmlHttps[updateObjectIDp].close;
	}
	xmlHttps[updateObjectIDp] = GetXmlHttpObject();

	//var xmlHttp = xmlHttps[updateObjectIDp];
	
	if(typeof loadingText != 'undefined')
		document.getElementById(updateObjectIDp).innerHTML = loadingText+document.getElementById(updateObjectIDp).innerHTML;//"Loading...";
	if (xmlHttps[updateObjectIDp]==null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url=updateURLp;
	if(url.indexOf("?")==-1)
		url = url+"?";
	url=url+"&q="+str;
	url=url+"&sid="+Math.random();
	xmlHttps[updateObjectIDp].open("GET",url,true);
	xmlHttps[updateObjectIDp].onreadystatechange=stateChanged;
	xmlHttps[updateObjectIDp].send("");
	
	function stateChanged() { 
		if (xmlHttps[updateObjectIDp].readyState==4) { 
			var updObj = document.getElementById(updateObjectIDp);
			if(updObj) {
				updObj.innerHTML = xmlHttps[updateObjectIDp].responseText;
				//Eval any scripts within the returned values
				var scripts = updObj.getElementsByTagName('script');
				for (var i=0;i<scripts.length;i++) {
					eval(scripts[i].innerHTML);
/*
					if(window.parent.execScript) {
						window.parent.execScript(scripts[i].innerHTML);
					} else if(window) {
						window.eval(scripts[i].innerHTML);
					} else {
						eval(scripts[i].innerHTML);
					}
					*/
				}
			} else
				alert("Obj Name:'"+updateObjectIDp+"' not found");
			xmlHttps[updateObjectIDp].close;
		}
	}
} 


function GetXmlHttpObject() {
	var xmlHttp=null;
	try { 
	// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
	// Internet Explorer
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP.6.0"); } 
		catch (e) { }
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP.3.0"); } 
		catch (e) { }
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch (e) { }
		try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } 
		catch (e) { }
	}
	return xmlHttp;
}


function getFormValues(fobj,valFunc) {
/* http://www.devarticles.com/c/a/XML/XML-in-the-Browser-Submitting-forms-using-AJAX/5/
This function accepts two arguments. The first is a reference to a form object, the other is the name of a validating 
function in the form of a string. The elements array of the form object is looped through, and a switch construct decides 
what to do with each of the relevant types of form elements (in this case just text and select-one). If the element is a 
text field, the eval function is used to call the validating function that is specified by argument two, which is sent the 
value of the form element.
*/

	var str = "";
	var valueArr = null;
	var val = "";
	var cmd = "";
	for(var i = 0;i < fobj.elements.length;i++) {
	    switch(fobj.elements[i].type) {
			case "checkbox":
			case "radio":
				if(!fobj.elements[i].checked)
					continue;
			case "submit" :
	        case "text" :
			case "hidden" :
			case "textarea" :
				if(fobj.elements[i].disabled)
					continue;
	             if(valFunc) {
	                 //use single quotes for argument so that the value of
	                 //fobj.elements[i].value is treated as a string not a literal
	                 cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
	                 val = eval(cmd)
	             }
	             str += fobj.elements[i].name +
	              "=" + escape(fobj.elements[i].value) + "&";
	              break;
	        case "select-one":
				if(fobj.elements[i].selectedIndex < 0 || !fobj.elements[i].options[fobj.elements[i].selectedIndex])
					break;
	             str += fobj.elements[i].name +
	             "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
	             break;
	    }
	}
	str = str.substr(0,(str.length - 1));
	return str;
}
