// requires file js_xmlhttprequest.js to be called 1st
// if browser is not right version and var cannot by created, rest of fns on this page will not run
var receiveReq = createXMLHttpRequest();
// set a global variable which defines the element id to be changed
var globalTargetID = "";
// theForm is the form id, weburl is the url to be posted to and processed, target_element is the final element to be updated, target_type is the
// type of element to be updates, eg, "div_innerhtml", "form_action"
function getParams(theForm,weburl,target_element,target_type)
{
//Set up the parameters of our AJAX call
var postStr = '';
var el = document.getElementById(theForm);
// update the global variable with the id of the target element
globalTargetID = target_element;
for (var i=0; i < el.length; i++)
	{
	if (i > 0)
		{postStr = postStr + "&";}
	postStr = postStr + el.elements[i].name + "=" + encodeURIComponent(el.elements[i].value);
	}
 //Call the function that initiate the AJAX request
 sendRequest(weburl, postStr, target_type);
}

function sendRequest(url, param, typ)
{
//If our readystate is either not started or finished, initiate a new request
if (receiveReq.readyState == 4 || receiveReq.readyState == 0)
	{
	//Set up the connection to the displayed html file (or page containing form). True sets the request to asyncronous(default) 
	receiveReq.open("POST", url, true);
	//Set the function that will be called when the XmlHttpRequest objects state changes
	if (typ == "div_innerhtml")
		{receiveReq.onreadystatechange = updateInnerHTML;}
	//else if (typ == "form_action")
	//	{receiveReq.onreadystatechange = updateFormACTION;}
	else
		{receiveReq.onreadystatechange = updateFormACTION;}
	receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	receiveReq.setRequestHeader("Content-length", param.length);
	receiveReq.setRequestHeader("Connection", "close");
	receiveReq.send(param);
	}   
}

//Called every time the response is requested and our XmlHttpRequest objects state changes
function updateInnerHTML()
{
//Check if our response is ready
if (receiveReq.readyState == 4)
	{
	//Set the content of the DIV element with the response text
	document.getElementById(globalTargetID).innerHTML = receiveReq.responseText;
	}
}

function updateFormACTION()
{
//Check if our response is ready
if (receiveReq.readyState == 4)
	{
	//Set the form action to responseText
	var x=document.getElementById(globalTargetID);
	x.action=receiveReq.responseText;
	}
}