function SoapMakeEnvelope(sFunc, sNs, Body)
{
	return "<?xml version=\"1.0\" encoding=\"utf-8\"?><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/\"><soap:Body><" +	
			sFunc + " xmlns=\"" + sNs + "\">" +	Body + "</" + sFunc + "></soap:Body></soap:Envelope>"
}

//Constructs each parameter as <ParamName>ParamValue</ParamName>
function MakeParameter(ParamName, ParamValue)
{
	return "<" + ParamName + ">" + ParamValue + "</" + ParamName + ">";
}

function SoapSyncCallPost(URL, Action, sBody)
{
    var oHTTP
    if (window.XMLHttpRequest) {
        oHTTP = new XMLHttpRequest();
        //oHTTP.onreadystatechange = function() {//Call a function when the state changes.
        //    if (http.readyState == 4 && http.status == 200) {
        //        alert(http.responseText);
        //    }
        //}
        oHTTP.open("POST", URL, false);//
    }
    else {
        oHTTP = new ActiveXObject("MSXML2.XMLHTTP");
        oHTTP.open("POST", URL, false);//I need sync call, this is okay for IE
    }
    oHTTP.setRequestHeader("Content-Length", sBody.length);
    oHTTP.setRequestHeader("Content-Type", "text/xml");
    oHTTP.setRequestHeader("SOAPAction", Action);
    oHTTP.send(sBody);
   
	return oHTTP;
}

function SoapSyncCall(URL, Action, sBody, sRetXPath)
{
	var oHTTP = SoapSyncCallPost(URL, Action, sBody);
	var status
	if (window.XMLHttpRequest)
	    status = oHTTP.status;
	else
	    status = oHTTP.Status
	
	if (status == 200)
	{
		if (sRetXPath != null)
			try {
			    if (document.all == null) // if (window.XMLHttpRequest)
			        return oHTTP.responseXML.getElementsByTagName(sRetXPath.split("/")[1])[0].textContent;
			    else
			        return oHTTP.responseXML.selectSingleNode("/soap:Envelope/soap:Body/" + sRetXPath).text;
			}
			catch(e)
			{
				return null;
			}
	}
	else
	{
		var e = new Error(oHTTP.Status, oHTTP.responseXML.selectSingleNode("/soap:Envelope/soap:Body/soap:Fault/faultstring").text);
		
		throw(e);
	}
}

function SelectSingleNode(xmlDoc, elementPath) {
    if (window.ActiveXObject) {
        return xmlDoc.selectSingleNode(elementPath);
    }
    else {
        var xpe = new XPathEvaluator();
        var nsResolver = xpe.createNSResolver(xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
        var results = xpe.evaluate(elementPath, xmlDoc, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        return results.singleNodeValue;
    }
}

function SoapSyncCallXML(URL, Action, sBody)
{
	var oHTTP = SoapSyncCallPost(URL, Action, sBody);
	
	if (oHTTP.Status == 200)
	{
		try
		{
			return oHTTP.responseXML;
		}
		catch(e)
		{
			return null;
		}
	}
	else
	{
		var e = new Error(oHTTP.Status, oHTTP.responseXML.selectSingleNode("/soap:Envelope/soap:Body/soap:Fault/faultstring").text);
		
		throw(e);
	}
}


//
//	sService:	string, WebServiceName.MethodName, ie "DataService.InsertFilter"
//	sParams:	string, Parameter List made from MakeParameter function call
//	bPath:		bool, true for return path, null or false to return as xmlDocument
//	
function CallWebService( sService, sParams, bPath) {
//debugger
	// parse service name in "ServiceName.MethodName" format
	var sSvcName = sService.split(".");
	
	if (sSvcName.length != 2) {
		throw new Error("Invalid service name argument.");
	}
	var sTmp;
    //12/14/2010. Now Firefox 3.6 also has the same as IE. so comment the following code
	//if (document.all == null) //if (IsNetScape == true)
	//    sTmp = this.srcElementOrTarget.baseURI; //http://localhost:1403/Products.aspx
	//else
	//    sTmp = this.location.href; //http://localhost/Shop/Products.aspx
	sTmp = this.location.href; //http://localhost/Shop/Products.aspx

	var oArr = sTmp.split("/");
	var sBody = new String();
	var sServiceRoot = "Services"; //"/ApacheScoutDBA/Services"; 	// ***
//	var sServiceRoot = sTmp.replace(oArr(oArr.length-1),"")
//	var url = window.location.protocol + "//" + window.location.hostname
//				+ sServiceRoot + "/" + sSvcName[0] + ".asmx";
//	var url = sTmp.replace(oArr[oArr.length - 1], "")
//				+ sServiceRoot + "/" + sSvcName[0] + ".asmx";

	var url = oArr[0]+"//"+oArr[2]+"/" + sServiceRoot + "/" + sSvcName[0] + ".asmx";

	var sAction = "http://tempuri.org/" + sSvcName[1];
	sBody = SoapMakeEnvelope(sSvcName[1], "http://tempuri.org/", sParams);
	var sRelRetXPath = sSvcName[1] + "Response/" + sSvcName[1] + "Result";
	
	if (bPath == true) {		
		return SoapSyncCall(url, sAction, sBody, sRelRetXPath);
	}
	else {
		return SoapSyncCallXML(url, sAction, sBody).selectSingleNode("/soap:Envelope/soap:Body/" + sRelRetXPath);
	}
	
	return;
}


