/**********************************************************
Author:
Adam Barry
www.ebound.dk

Date: June 11 2008

© 2008 Adam Barry, all rights reserved
-----------------------------------------------------------

Name:
xmlhttp script

-----------------------------------------------------------
Description:
Function that enables dynamic loading of dynamic content

-----------------------------------------------------------
Usage:
Simply place a link to the this script in the head-section
of the XHTML page.

<script type="text/javascript" src="xmlhttp.js"></script>

-----------------------------------------------------------
Example:

<script type="text/javascript" src="xmlhttp.js"></script>

<script>
	callServer(myFunction,"result.html");	//executes the request and passes the result to a callback function
	callServer(false,"result.html");		//executes the request and then does nothing

	function myFunction(response) {
		alert(response);
	}
</script>


**********************************************************/

function callServer(callbackFunction,url) {
	try {
		var xmlhttp;

		/* Initialize the xmlhttp object */
		if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest();}
		else if (window.ActiveXObject) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
		if (!xmlhttp) return false;

		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				response = xmlhttp.responseText;
				if (!callbackFunction == false) {
					callbackFunction(response);
				}
			}
		}

		xmlhttp.open("GET",url,true);
		xmlhttp.send('');
	}

	/* íf xmlhttp throws an exception */
	catch (exception) {

		var name = exception.name;
		var message = exception.message;
		var fileName = exception.fileName;
		var lineNumber = exception.lineNumber;
		var stack = exception.stack;
		var tip = "Please make sure that the website executing the\ncallServer function is running on a webserver, and\nnot as a folder on a local hard drive";

		callbackFunction("XMLHTTP error: "+ name +"\nDescription: " +message + "\nFile name: "+ fileName + "\nLine number: "+ lineNumber +"\nStack: "+stack + "\n\nTip:\n"+ tip);
	}
}