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)
{
//debugger
//	alert("SoapSyncCallPost URL="+URL)
	var oHTTP
    try {
    //  if(navigator.AppName == "Netscape") 
		if(navigator.appName == "Netscape") {
    		oHTTP = new XMLHttpRequest();
       	}	
		else {
       		oHTTP = new ActiveXObject("MSXML2.XMLHTTP");
    	}	
    }
	catch (e)
	{
//    	alert("new ActiveXObject e.message="+e.message +" "+navigator.appName)
	}
//	alert("SoapSyncCallPost oHTTP="+oHTTP)
   
	try
	{
		oHTTP.open("POST", URL, false);
//	alert("SoapSyncCallPost oHTTP.open")
		oHTTP.setRequestHeader("Content-Length", sBody.length);
		oHTTP.setRequestHeader("Content-Type", "text/xml");
		oHTTP.setRequestHeader("SOAPAction", Action);
		oHTTP.setRequestHeader("SOAPHeader", getSoapHeader());
		//oHTTP.Timeout = 5000;

//	alert("SoapSyncCallPost before oHTTP.send")
		oHTTP.send(sBody);
//	alert("SoapSyncCallPost after oHTTP.send")
	}
	catch (e)
	{
//    	alert("SoapSyncCallPoste.message="+e.message)
	}
	return oHTTP;
}

function SoapSyncCall(URL, Action, sBody, sRetXPath)
{
	var oHTTP = SoapSyncCallPost(URL, Action, sBody);
	var status , sXPath, node
	if(navigator.appName == "Netscape") 
	  status = oHTTP.status
	else
	  status = oHTTP.Status
	
	//alert("after oHTTP = SoapSyncCallPost(URL, Action, sBody) oHTTP.status ="+oHTTP.status )
	
	if (status == 200) //(oHTTP.Status == 200)
	{
		if (sRetXPath != null)
			try
			{
	            if(navigator.appName == "Netscape") {
        	       //alert("oHTTP.responseText="+oHTTP.responseText)
                   //return oHTTP.responseXML.getElementsByTagName(sRetXPath.split("/")[1])[0].nodeValue
                   return Parse_responseText(oHTTP.responseText, sRetXPath.split("/")[1])
                }   
                else   
				   return oHTTP.responseXML.selectSingleNode("/soap:Envelope/soap:Body/" + sRetXPath).text;
			}
			catch(e)
			{
			    //alert("e.message="+e.message)
				return null;
			}
	}
	else
	{
		var e = new Error(oHTTP.Status, oHTTP.responseXML.selectSingleNode("/soap:Envelope/soap:Body/soap:Fault/faultstring").text);
//alert("throw e")
		throw(e);
	}
}

function SoapSyncCallXML(URL, Action, sBody)
{
//alert("SoapSyncCallXML")
	var oHTTP = SoapSyncCallPost(URL, Action, sBody);
	var status , sXPath, node
	if(navigator.appName == "Netscape") 
	  status = oHTTP.status
	else
	  status = oHTTP.Status
	
//alert("SoapSyncCallXML status="+status)
	if (status == 200)
	{
		try
		{
		    if(navigator.appName=="Netscape") {
		        //alert("oHTTP.responseText="+oHTTP.responseText)
		        //The alert is executed, but seems the return not work properly
			    return oHTTP.responseText
			}    
		    else
				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)
{
	// parse service name in "ServiceName.MethodName" format
	var sSvcName = sService.split(".");
	
	if (sSvcName.length != 2) {
		throw new Error("Invalid service name argument.");
	}
	
	var sBody = new String();
	var sServiceRoot = "/FLS/FLSLAN/Services";		// ***
	var url = window.location.protocol + "//" + window.location.hostname
				+ 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;
}

function CallWebService2( sService, sParams, bPath, sServiceRoot)
{//This one is to use WebService from other projects.
	// parse service name in "ServiceName.MethodName" format
//	alert("CallWebService2 sService="+sService)
	var sSvcName = sService.split(".");
	
	if (sSvcName.length != 2) {
		throw new Error("Invalid service name argument.");
	}
	
	var sBody = new String();
	//var sServiceRoot = "/FLS/FLSLAN/Services";		// ***
	var url = window.location.protocol + "//" + window.location.hostname
				+ 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";
	
//	alert("CallWebService2 bPath="+bPath)
	if (bPath == true) {		
		return SoapSyncCall(url, sAction, sBody, sRelRetXPath);
	}
	else {
		return SoapSyncCallXML(url, sAction, sBody).selectSingleNode("/soap:Envelope/soap:Body/" + sRelRetXPath);
	}
	
	return;
}

function CallWebService3( sService, sParams, bPath, sDomainName, sServiceRoot)
{//This one is to use WebService from other projects.
	// parse service name in "ServiceName.MethodName" format
	var sSvcName = sService.split(".");
	
	if (sSvcName.length != 2) {
		throw new Error("Invalid service name argument.");
	}
	
	var sBody = new String();
	//var sServiceRoot = "/FLS/FLSLAN/Services";		// ***
	//var url = window.location.protocol + "//" + window.location.hostname
	var url = window.location.protocol + "//" + sDomainName
				+ 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;
}

function getSoapHeader()
{
  var ret, sUserName, sPassword
  //The default window. enough parent to point to the default window
  try {
	 sUserName = window.parent.parent.parent.parent.document.getElementById("UserName").value //"test"
	 sPassword = window.parent.parent.parent.parent.document.getElementById("Password").value //"test" 
/*    if(navigator.appName == "Netscape") {
		sUserName = window.parent.parent.parent.parent.document.getElementById("UserName").value //"test"
		sPassword = window.parent.parent.parent.parent.document.getElementById("Password").value //"test" 
    }
    else {
		sUserName = window.parent.parent.parent.parent.document.all.item("UserName").value //"test"
		sPassword = window.parent.parent.parent.parent.document.all.item("Password").value //"test" 
    } */
  }
  catch(ex){
    sUserName=""
    sPassword=""
  }
  ret = "<ROOT>"
  ret += "<UserName>" + sUserName + "</UserName>"
  ret += "<Password>" + sPassword + "</Password>"
  ret += "</ROOT>"
  return ret
}
// Evaluate an XPath expression aExpression against a given DOM node
// or Document object (aNode), returning the results as an array
// thanks wanderingstan at morethanwarm dot mail dot com for the
// initial work.
//results = evaluateXPath(people, "//person[address/@city='denver']");
function evaluateXPath(aNode, aExpr) {
  var xpe = new XPathEvaluator();
  var nsResolver = xpe.createNSResolver(aNode.ownerDocument == null ?
    aNode.documentElement : aNode.ownerDocument.documentElement);
  var result = xpe.evaluate(aExpr, aNode, nsResolver, 0, null);
  var found = [];
  var res;
  while (res = result.iterateNext())
    found.push(res);
  return found;
}

function Parse_responseText(responseText, tag)
{
  var ret
  var StartTag = "<" + tag + ">"
  var EndTag = "</" + tag + ">"
  var pos1, pos2
  pos1 = responseText.indexOf(StartTag) + StartTag.length
  pos2 = responseText.indexOf(EndTag)
  //http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:substring
  ret = responseText.substring(pos1, pos2)
  return ret
}

		//	sXpath = "/soap:Envelope/soap:Body/" + sRetXPath
			//getElementsByTagName
	    //alert("oHTTP.responseXML="+oHTTP.responseXML)
	    //alert("oHTTP.responseXML.getElementsByTagName="+oHTTP.responseXML.getElementsByTagName("Envelope"))
	    //alert("oHTTP.responseXML.TSInfoResult="+oHTTP.responseXML.getElementsByTagName("TSInfoResult"))
	    //alert("oHTTP.responseXML.TSInfoResult.length="+oHTTP.responseXML.getElementsByTagName("TSInfoResult").length)
	    //alert("oHTTP.responseXML.TSInfoResult.nodeValue="+oHTTP.responseXML.getElementsByTagName("TSInfoResult")[0].nodeValue)
	    //alert("oHTTP.responseXML.TSInfoResult.nodeValue="+oHTTP.responseXML.getElementsByTagName("TSInfoResult")[0].nodeText)
	    //alert("oHTTP.responseXML.TSInfoResult.nodeValue="+oHTTP.responseXML.getElementsByTagName("TSInfoResult")[0].innerXML)
	    //alert("oHTTP.responseXML.TSInfoResult.nodeValue="+oHTTP.responseXML.getElementsByTagName("TSInfoResult")[0].Text)
	    //alert("oHTTP.responseXML.TSInfoResult.nodeName="+oHTTP.responseXML.getElementsByTagName("TSInfoResult")[0].nodeName)
	    //alert("oHTTP.responseXML 2="+oHTTP.responseXML.evaluate(sXPath,oHTTP.responseXML,null,0,null))
	    //var xpathResult = oHTTP.responseXML.evaluate(sXPath, oHTTP.responseXML, null, 0, null); 
//alert("xpathResult="+xpathResult);
//alert("xpathResult.resultType="+xpathResult.resultType);
//while ((node = xpathResult.iterateNext())) { 
//alert("node.nodeValue="+node.nodeValue);
//   alert(node + '; nodeName: ' + node.nodeName); 
//   lastNode = node; 
//} 
//        var node = xpathResult.iterateNext(); 
//alert(sRetXPath.split("/")[1]);
//alert(oHTTP.responseXML.getElementsByTagName(sRetXPath.split("/")[1])[0].nodeValue)
	    //alert("oHTTP.responseXML 2innerHTML="+oHTTP.responseXML.evaluate(sXPath,oHTTP.responseXML,null,0,null).innerHTML));//responseText)
	    //alert("oHTTP.responseXML 2innerXML="+oHTTP.responseXML.evaluate(sXPath,oHTTP.responseXML,null,0,null).innerXML));//responseText)
	    //alert("oHTTP.responseXML 2="+oHTTP.responseXML.evaluate(sXPath,oHTTP.responseXML,null,0,null).nodeValue))
	    //alert( oXML.evaluate( sXPath, oXML, null, 0, null).nodeValue ); 
	    //alert(oHTTP.responseXML.getElementsByTagName("Envelope")[0].nodeName + " = " + oHTTP.responseXML.getElementsByTagName("Envelope")[0].nodeValue)
	    //alert(oHTTP.responseXML.getElementsByTagName("Body")[0].nodeName + " = " + oHTTP.responseXML.getElementsByTagName("Body")[0].innerXML)
	    //alert(oHTTP.responseXML.getElementsByTagName("Body")[0].nodeName + " = " + oHTTP.responseXML.getElementsByTagName("Body")[0].innerXml)
//	    alert(oHTTP.responseXML.getElementsByTagName("Envelope")[0].nodeName + " = " + oHTTP.responseXML.getElementsByTagName("Envelope")[0].innerXML)
	    
//	    results = evaluateXPath(oHTTP.responseXML, "/");
//	    alert("results.length="+results.length + " nodeName="+results[0].nodeName + " node value="+results[0].value + " or " +results[0].Value)
//
//		sXpath = "//soap:Envelope/soap:Body/" + sRetXPath
//	    var results
//	    results = evaluateXPath(oHTTP.responseXML, sXPath);
//	    alert("results.length="+results.length)// + "   nodeName="+results[0].nodeName)
//	    alert("results[0].value="+results[0].value)

