﻿function getWebServiceResults(params, values, serviceName, methodName, stateChangedFunction)
{
	var xmlHttp;
	if (window.ActiveXObject)
	{
		// Create the XML HTTP object
		var bHttp = false;
		var aszHttpProgIDs = [ "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP" ];

		for(var i = 0; !bHttp && i < aszHttpProgIDs.length; i++)
		{
			try
			{
				xmlHttp = new ActiveXObject(aszHttpProgIDs[i]);
				bHttp = true;
			} 
			catch (objException)
			{
			}
		}
		
		// If we failed to create objects, then throw an exception and return
		if (!bHttp) 
		{
			throw "MSXML not found on your computer.";
			return;
		}
		
		var strEnvelope = "<?xml version='1.0' encoding='utf-8'?> ";
		strEnvelope += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> ";
		strEnvelope += "<soap:Body> ";
		strEnvelope += "<"+methodName+" xmlns='http://Farabar/'> ";
		if(params != null && values != null && params.length == values.length)
		{
			for(var j = 0; j < params.length ; j++)
			{
				strEnvelope +="<"+params[j]+">"+values[j]+"</"+params[j]+">";
			}
		}
		strEnvelope += "</"+methodName+"> ";
		strEnvelope += "</soap:Body> ";
		strEnvelope += "</soap:Envelope>";
		xmlHttp.onreadystatechange = stateChanged;
		xmlHttp.open("post", GetUrlPrefix()+"WebServices/"+serviceName, true);
		xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
		xmlHttp.setRequestHeader("SOAPAction", "http://Farabar/"+methodName);
		xmlHttp.send(strEnvelope);
	}
	
	function stateChanged()
	{
		if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete")
		{
			try
			{
				if(stateChangedFunction != null) stateChangedFunction(xmlHttp.responseXML.text, false);
			}
			catch(e)
			{
				stateChangedFunction("", true);
			}
		}
	}
}
