/*
 * This function creates an XMLHttpRequest object
 * and stores it in a global variable called request.
 * This method tries to create the object different ways
 * to support Mozilla-based and IE browsers
 */
function createXMLHttpRequest() {
	request = null;
	if (window.XMLHttpRequest) {        // Mozilla-Based Browsers
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {  // newer IE browsers
		request = new ActiveXObject("Msxml2.XMLHTTP");
		if (request == null) {           // older IE Browsers
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	//The request could still be null
	if (request == null) {
		alert("Your browser does not support AJAX");
	}
}

/*
 * This function will send the URL passed in as an AJAX call
 * using the HTTP GET method.
 */
function sendHttpGetRequest(theURL) {
	//alert("Sending GET to "+theURL);
	request.open('GET', theURL, true);
	request.onreadystatechange=handleHttpResponse;
	request.send(null);
}

/*
 * This function will send the URL passed in as an AJAX call
 * using the HTTP POST method.
 * The postData variable should contain all the name=value
 * pairs to be sent to theURL.
 */
function sendHttpPostRequest(theURL, postData) {
	//alert("Sending POST to "+ theURL);
	request.onreadystatechange=handleHttpResponse;
	request.open('POST', theURL, true);
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	request.send(postData);
}

/*
 * This method is called by the browser when the AJAX response is
 * received from the server
 */
function handleHttpResponse() {
	//alert("handle response, ready state = "+request.readyState);
	if (request.readyState == 4) {     // ReadyState of 4 means finished
		if (request.status == 200) {    // HTTP status of 200 means ALL OK
			// Get the response as plain text
			ajaxResultText = request.responseText;
			
			// Call displayResults. This method must be implemented
			// for each page to do the page specific logic.
			displayResults(ajaxResultText);
		} else {
			alert("An error occurred: "+request.status + ":"+request.statusText);
		}
	}
}


