// These are the core functions needed to write an "AJAJ" application, which is what some
// people are calling AJAX that uses JSON instead of XML.
 
 // The ONLY difference between this and the "ajaxcore.js" file is ONE LINE OF CODE. It is the very
 // last instruction of this file! Your parseResponseXML function must now be parseResponseJSON, 
 // which receives the responseText instead of responseXML.
 
// 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 parseResponseJSON which will receive the text response, which
// we hope is valid JSON (JavaScript Object Notation) code. This  function will be called by the AJAXreceive function 
// below. Here is a sample of a basic parseResponseJSON function:

// This function parses the AJAX JSON response.  
//function parseResponseJSON(answerJSON)
//{
//	var response = eval( '(' + answerJSON + ')' );
//      now use object notation to extract the properties of your object.
//}

//////////////////////////////////////////// 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 responseText TO YOUR FUNCTION TO PROCESS
			parseResponseJSON(myRequest.responseText);  ///////// THE ONLY CHANGE FOR "AJAJ" ///////////////
	}
	//alert(myRequest.readyState);
}
