/**
	AJAX library 
*/

function AjaxCall(url)
{
	this._xmlHttpObj = null;
	this._callBackMethod = null;
	this._synchronousCall = false;
	this._url = url;
	if(this._url == null)
	{
		this._url = "AjaxCallHandler.php";
	}

}

AjaxCall.prototype._xmlHttpObj;
AjaxCall.prototype._callBackMethod;
AjaxCall.prototype._synchronousCall;
AjaxCall.prototype._url;

AjaxCall.prototype._GetXmlHttpObj = function()
{
	if(this._xmlHttpObj != null)
	{
		return;
	}

	try
	{
		// Firefox, Opera 8.0+, Safari
		this._xmlHttpObj=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			this._xmlHttpObj=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			this._xmlHttpObj=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
}

AjaxCall.prototype.SetCallback = function(callBack)
{
	this._callBackMethod = callBack;
}

AjaxCall.prototype.SendRequest = function(query, additionalArgs, synchronous)
{
	if (this._xmlHttpObj == null)
	{
		this._GetXmlHttpObj();
		if(this._xmlHttpObj == null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		}
	} 

	var l_url = this._url;
	var l_parameters = "query="+query;
	l_parameters = l_parameters + "&" + additionalArgs;

	if(synchronous)
	{
		this._xmlHttpObj.open("POST", l_url, false);
		this._xmlHttpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this._xmlHttpObj.setRequestHeader("Content-length", l_parameters.length)
		this._xmlHttpObj.setRequestHeader("Connection", "close");

		this._xmlHttpObj.send(l_parameters);
		return this._xmlHttpObj.responseText;
	}
	else
	{
		var me = this;
		this._xmlHttpObj.onreadystatechange = function () {
			if(me._callBackMethod == null || me._callBackMethod == "undefined")
			{
				return;
			}
			if (me._xmlHttpObj.readyState == 4 || me._xmlHttpObj.readyState == "complete")
			{ 
				if(me._callBackMethod != null)
				{
					var xmlDoc=GetXmlObject(me._xmlHttpObj.responseText);
					if(xmlDoc == null)
					{
						xmlDoc = me._xmlHttpObj.responseXml;
						if(xmlDoc == null)
						{
							alert('XMLDom is not supported on your browser'); return;
						}
					}
					//call the registered callback method
					me._callBackMethod(xmlDoc); 
				}
			} 
		}

		this._xmlHttpObj.open("POST", l_url, true);
		this._xmlHttpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this._xmlHttpObj.setRequestHeader("Content-length", l_parameters.length)
		this._xmlHttpObj.setRequestHeader("Connection", "close");

		this._xmlHttpObj.send(l_parameters);
	}
}

function GetXmlObject(text)
{
	var xmlDoc = null;
	// code for IE
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(text);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else
	{
		//return null;
		var parser=new DOMParser();
		xmlDoc = parser.parseFromString(text,"text/xml");
		//xmlDoc = document.implementation.createDocument("", "", null) ;
		//xmlDoc.loadXML(text);

	}
	return xmlDoc;
}



