/*
Method:

 - Ajax(string serverSidePage,string ajaxKey,string post)*;
 
Parameters: 
 
 *a function called 'AjaxResponse(response, key)' must be declared
where the response and key are passed from the asyncronous method.
The 'key' should be used to identify which set of ajax code should
be executed (using a switch or if statements) and the 'response' will
contain the response from your server side page. The 'AjaxResponse'
method can be left out when the ajax call does not require response.
the post is optional.

Example:

Ajax("getCurrentTime.aspx","getTime");

function AjaxResponse(response, key)
{
	switch(key)
	{
		case "getTime":
		{
			string timeFromServer = response;
			break;
		}
	}
}

*/

function Ajax(page, key, post)
{
	var xmlHttp;
	
	try
	{  
		// All Browsers that are not IE 
		xmlHttp = new XMLHttpRequest();  
	}
	catch (e)
	{  
		// Internet Explorer  
		try
		{    
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");    
		}
		catch (e)
		{    
			try
			{      
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");      
			}
			catch (e)
			{      
				alert("Your browser does not support AJAX!");      
				return false;      
			}    
		}  
	}  
	
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			try
			{
				AjaxResponse(xmlHttp.responseText,key);
			}
			catch (e)
			{
				//The function AjaxResponse(response, key) is missing
			}
		}
	};
	
	try
	{
		if(post != null)
		{
			xmlHttp.open("POST",page,true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", post.length);
			xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.send(post);
		}
		else
		{
			//xmlHttp.open("GET",page,true);
			//xmlHttp.send(null);
			//changed to post not to cache
			xmlHttp.open("POST",page,true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", 1);
			xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.send(' ');
		}
	}
	catch (e)
	{
		alert("AJAX Error: " + e.message);
	}
}