// Send a GET asynchronous request to the server
function ajaxAsynchronousRequest(url, clientFunction) {
	var xhr = ajaxNewXMLHttpRequest();
	
	xhr.open("GET", url);

	xhr.onreadystatechange = function() {
		if (xhr.readyState != 4) return;
		if (xhr.status != 200) return;
    if (clientFunction) {
		  clientFunction(xhr.responseText);
    }
	}

	xhr.send(null);
}

/*
An example

<script type="text/javascript">
function clientUpdate(responseText) {
	document.getElementById("myElementId").innerHTML = responseText;
}
</script>
<div id="myElementId"></div>
<button onclick="ajaxAsynchronousRequest('service.php', clientUpdate);">Click Me</button>
*/

// Send a POST asynchronous request to the server
function ajaxAsynchronousPOSTRequest(url, params, clientFunction) {
	var xhr = ajaxNewXMLHttpRequest();
	
	xhr.open("POST", url);

  // Create the params string
  var strParams = '';
  for (var key in params) {
    if (strParams.length > 0) {
      strParams += "&";
    }
    strParams += key + "=" + params[key];
  }

  // Send the proper header information along with the request
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhr.setRequestHeader("Content-length", strParams.length);
  xhr.setRequestHeader("Connection", "close");

	xhr.onreadystatechange = function() {
		if (xhr.readyState != 4) return;
		if (xhr.status != 200) return;
		clientFunction(xhr.responseText);
	}

	xhr.send(strParams);
}

// Create an XMLHttpRequest object
function ajaxNewXMLHttpRequest() {
	var xhr;

	try {
		xhr = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	   try {
	        xhr = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (E) {
	        xhr = false;
	    }
	}
	
	if (!xhr && typeof XMLHttpRequest != 'undefined') {
		xhr = new XMLHttpRequest();
	}
	
	return xhr;
}

