// stores the reference to the XMLHttpRequest object
var xmlHttp = createxmlhttprequestobject(); // retrieves the XMLHttpRequest object
function createxmlhttprequestobject(){
	var xmlHttp;
	try {		
		xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
	} catch (e) {
		// Internet Explorer CRAP!
		xmlHttp = false;
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
			xmlHttp = false;
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlHttp = false;
			}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp){	
		alert("Your browser does not support AJAX!");
	} else {
		return xmlHttp;
	}	
}

// make asynchronous HTTP request using the XMLHttpRequest object
var function_callback;
var last_geturl;
function processAJAX(getstr, callBack){
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		function_callback = callBack; //call this function after receive data from server.
		// execute the check_availability.php page from the server
		last_geturl = getstr;
		xmlHttp.open("GET", last_geturl, true);
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleAJAXServerResponse;
		// make the server request
		xmlHttp.send(null);
	}
}

// executed automatically when a message is received from the server
function handleAJAXServerResponse(){
	// move forward only if the transaction has completed
	if (xmlHttp.readyState == 4){
		// status of 200 indicates the transaction completed successfully
		if (xmlHttp.status == 200){
			function_callback(xmlHttp.responseText);
			// extract the XML retrieved from the server
			/*xmlResponse = xmlHttp.responseXML;
			// obtain the document element (the root element) of the XML structure
			xmlDocumentElement = xmlResponse.documentElement;
			// get the text message, which is in the first child of
			// the the document element
			helloMessage = xmlDocumentElement.firstChild.data;*/
		} else { // a HTTP status different than 200 signals an error
			alert("There was a problem accessing the server: " + xmlHttp.statusText + "\n last url: "+last_geturl);
		}
	}
} 