// A static object to which we attach methods
function AjaxClient() {}

// This method calls a web service method
AjaxClient.invokeGet = function(url, async, callback)
{
	// send request
	var xmlHttp = AjaxClient._getXmlHttp();
	xmlHttp.open("GET", url, async);

	if(async) 
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4)
				AjaxClient._onSendAjaxRequest(async, callback, xmlHttp);
		}
	}

	xmlHttp.send(null);	
	
	if (async){
		return xmlHttp;
	}
	else { // if (!async)
		return AjaxClient._onSendAjaxRequest(async, callback, xmlHttp);
	}
}

// This method calls a web service method
AjaxClient.invokePost = function(url, parameter, async, callback)
{
	// send request
	var xmlHttp = AjaxClient._getXmlHttp();
	xmlHttp.open("POST", url, async);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
	if(async) 
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4)
				AjaxClient._onSendAjaxRequest(async, callback, xmlHttp);
		}
	}
	xmlHttp.send(parameter);
	if (async)
		return xmlHttp;
	else // if (!async)
		return AjaxClient._onSendAjaxRequest(async, callback, xmlHttp);
}

// This method calls a web service method
AjaxClient.invoke = function(url, parameter, async, callback)
{
	// send request
	var xmlHttp = AjaxClient._getXmlHttp();
	xmlHttp.open("POST", url, async);
	xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
	if(async) 
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4)
				AjaxClient._onSendAjaxRequest(async, callback, xmlHttp);
		}
	}
	xmlHttp.send(parameter);
	if (async)
		return xmlHttp;
	else // if (!async)
		return AjaxClient._onSendAjaxRequest(async, callback, xmlHttp);
}

AjaxClient._onSendAjaxRequest = function(async, callback, req)
{
	var responseXML = req.responseText;//GetOuterXML(req.responseXML);

	if(callback){
		callback(responseXML);
	}
	if(!async)
		return responseXML;
}

// private: xmlhttp factory
AjaxClient._getXmlHttp = function() 
{
	try
	{
		if(window.XMLHttpRequest) 
		{
			var req = new XMLHttpRequest();
			// some versions of Moz do not support the readyState property and the onreadystate event so we patch it!
			if(req.readyState == null) 
			{
				req.readyState = 1;
				req.addEventListener("load", 
									function() 
									{
										req.readyState = 4;
										if(typeof req.onreadystatechange == "function")
											req.onreadystatechange();
									},
									false);
			}
			return req;
		}
		if(window.ActiveXObject) {
			return new ActiveXObject(AjaxClient._getXmlHttpPrefix() + ".XmlHttp");
		}
	}
	catch (ex) {}
	throw new Error("Your browser does not support XmlHttp objects");
}

AjaxClient._getXmlHttpPrefix = function()
{
	if(AjaxClient._getXmlHttpPrefix.prefix)
		return AjaxClient._getXmlHttpPrefix.prefix;
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for(var i = 0; i < prefixes.length; i++)
	{
		try
		{
			o = new ActiveXObject(prefixes[i] + ".XmlHttp");
			return AjaxClient._getXmlHttpPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	throw new Error("Could not find an installed XML parser");
}
