// These are the core functions needed to write an AJAX application. 
// Include this js file in your web page, and call the AJAXsend function by sending it the URL of a script that will
// return an XML response. The URL must include a querystring, even if you must make up a key=value pair, as follows:
//   AJAXsend('myscript.php?key=value');

// You MUST also provide your own function named parseResponseXML, which will receive the XML response. This 
// function will be called by the AJAXreceive function below. Here is a sample of a basic parseResponseXML function:

// This function parses the AJAX XML response. This sample uses just one XML tag: response. 
//function parseResponseXML(answerXML)
//{
//	var responses = answerXML.getElementsByTagName('response');
//	var response = responses[0].firstChild.nodeValue;
//	alert(response);
//}

//////////////////////////////////////////// START AJAX //////////////////////////////////////////////////

function getXMLHTTPRequest()
{
	var request = false;
	try { request = new XMLHttpRequest();} // Firefox
	catch (err1)
	{
		try {request = new ActiveXObject("Msxml2.XMLHTTP"); } // Some versions IE
		catch (err2)
		{
			try {request = new ActiveXObject("Microsoft.XMLHTTP");} // other versions IE
			catch (err3) { request = false; }
		}
	}
	return request;
}

var myRequest = getXMLHTTPRequest();

// Call a PHP script that performs an action.
function AJAXsend(url)
{
	//alert(url);
	var random = parseInt(Math.random()*99999999); // trick browsers into not caching the response that will follow
	myRequest.open("GET", url+"&rand="+random, true); // true: asynchronous
	myRequest.onreadystatechange = AJAXreceive;
	myRequest.send(null); // null for GET requests, encoded stuff for POST requests
}

// This function is called by the XMLHTTPRequest object when the state of the object changes
// readystate values: 0=uninitialized, 1=loading, 2=loaded, 3=interactive, 4=completed
function AJAXreceive()
{
	if (myRequest.readyState == 4)
	{
		if (myRequest.status != 200)
			alert("AJAXreceive error: " + myRequest.statusText);
		else  // Success! Send the response to the function that processes it.
			// SEND responseXML TO YOUR FUNCTION TO PROCESS
			parseResponseXML(myRequest.responseXML);
	}
	//alert(myRequest.readyState);
}