
//creates an XMLHttpRequestObject instance
var xmlHttp = createXmlHttpRequestObject();
var responseText = '';
var XMLResponse = null;

function wait(millis) {
  var date = new Date();
  var currentDate = null;
  do { currentDate = new Date(); } 
  while(currentDate - date < millis);
}

function createXmlHttpRequestObject() {
	var xmlHttp;
	//this should work for all browsers except IE6 and older
	try {
	  //try to create XMLHttpRequestObject
	  xmlHttp = new XMLHttpRequest(); 
	} catch(e) {
	  //assume IE6 or older
		var xmlHttpVersion = new Array("MSXML2.XMLHTTP.6.0",
																	 "MSXML2.XMLHTTP.5.0",
																	 "MSXML2.XMLHTTP.4.0",
																	 "MSXML2.XMLHTTP.3.0",
																	 "MSXML2.XMLHTTP",
																	 "Microsoft.XMLHTTP");
		for(var i = 0;i < xmlHttpVersion.length && !xmlHttp;i++) {
		  try {
			  //try to create XMLHttpRequestObject
			  xmlHttp = new ActiveXObject(xmlHttpVersion[i]);
			} catch(e) {}
		} 
	}
	
	if(!xmlHttp) {
	  window.alert("Error creating the XMLHttpRequest object.\nPlease contact your system administrator.");
	} else {
	  return xmlHttp;
	}
}

function handleRequestStateChange() {
  //when ready state is 4, we are ready to read the server response
  if(xmlHttp.readyState == 4) {
    //continue only if HTTP status is "OK"
	  if(xmlHttp.status == 200) {
		  try {
  			responseText = xmlHttp.responseText;
  			XMLResponse = xmlHttp.responseXML;
			} catch(e) {
			  window.alert("Error reading the response: " + e.toString());
			}
		} else {
		  window.alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
		}
	}  
}
