/**
 * Using Object Detection for a Cross-Browser Solution
 */
function getXmlHttpObject()
{
	var obj = null;

	try
	{
		obj = new XMLHttpRequest();
		// e.g. Firefox
	}
	catch(err1)
	{
		try
		{
			obj = new ActiveXObject("Msxml2.XMLHTTP");
			// some versions IE
		}
		catch(err2)
		{
			try
			{
				obj = new ActiveXObject("Microsoft.XMLHTTP");
				// some versions IE
			}
			catch(err3)
			{
			}
		}
	}

	return obj;
}



/**
 * XML HTTP object
 */
var xmlHttp;



/**
 * Send Ajax request using POST
 *
 * @param string url the URL to post the request to
 * @param string parameters the parameters to send with the request
 * @param string callback the name of the function to use for callback
 */
function postAjax(url, parameters, callback)
{
	xmlHttp = getXmlHttpObject();

	if(xmlHttp)
	{
		try
		{
			rnd = (new Date().getTime()) + parseInt(Math.random()*99999999);

			xmlHttp.onreadystatechange = callback;
			xmlHttp.open("POST", url, true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttp.send(parameters + '&random=' + rnd);
		}
		catch(e)
		{
		}
	}
}



/**
 * Ajax callback function
 *
 * @param string element the name of the element to print the reponse in
 *
 * @return boolean status true if callback complete, false if not
 */
function callbackAjax(element)
{
	var status = false;

	//check if state is "loaded"
	if(xmlHttp.readyState == 4)
	{
		//check if server HTTP response is "OK"
		if(xmlHttp.status == 200)
		{
			document.getElementById(element).innerHTML = xmlHttp.responseText;
			status = true;
		}
		else
		{
			document.getElementById(element).innerHTML = 'ERROR';
		}
	}
	else
	{
		//document.getElementById(element).innerHTML = 'LOADING';
		document.getElementById(element).innerHTML = '';
	}

	return status;
}
