//web service functions
var AKA_XMLNS = "http://tempuri.org/";
//var AKA_URL = "http://www.akanda.com";

var WS_ACCESS_MANAGER	= "../Admin/Services/AccessManager.asmx";
var WS_ADMIN_ONLY		= "../Admin/Services/AdminOnly.asmx";
var WS_CONFIG_MANAGER	= "../Config/Services/ConfigManager.asmx";
var WS_PUBLIC			= "../WebServices/PublicServices.asmx";
var WS_LOGIN			= "../Admin/Services/Login.asmx";

var SOAP_READY_STATE_UNITIALIZED = 0; 
var SOAP_READY_STATE_LOADING = 1; 
var SOAP_READY_STATE_LOADED=2; 
var SOAP_READY_STATE_INTERACTIVE = 3; 
var SOAP_READY_STATE_COMPLETE = 4; 

function BuildXmlString(name, value) {
	var doc = new ActiveXObject("Microsoft.XMLDOM");
	var node = doc.createElement(name);
	node.text = value;
	return node.xml;
}

function SoapHtmlEncode(s) {
	var re = /</g;
	s = s.replace(re, "#lt;");
	re = />/g;
	return s.replace(re, "#gt;");
}

function SoapErrorMsg(retString) {
	if (retString == "") return true;
	var doc = new ActiveXObject("Microsoft.XMLDOM");
	doc.loadXML(retString);
	if (doc.xml == "" && retString != "") return retString;
	var faultNode = doc.selectSingleNode("//faultstring");
	if (faultNode == null) return true;
	return faultNode.text;
}

var gNoWSAccess = false;
function SoapInvokeService(serviceUrl, methodName, paramXml, async, xmlns, callback, noMessage) {
	var ns = AKA_XMLNS;
	if (xmlns) ns = xmlns;
	var env = SoapBuildEnvelope(methodName, paramXml, xmlns);
	var oHttp = new ActiveXObject("Microsoft.XMLHTTP");
	async = (async == true);
	//alert(serviceUrl);
	oHttp.open("POST", serviceUrl, async);
	oHttp.setRequestHeader("Content-Type", "text/xml; charset='utf-8'"); //utf-8 is not default, need to set explicitely, otherwise SP and FR won't send correctly - bug in .NET, they'll fix in a next release
	var hdr = ns + methodName;
	//alert(hdr);
	oHttp.setRequestHeader("SOAPAction", hdr);
//alert("Envelope:\n\n" + env);
	if (callback != null) {
		oHttp.onreadystatechange = function() { eval(callback + "(oHttp);") }
	}	
	oHttp.send(env);
	if (async) return true;
	if (oHttp.status == 200) {
		//alert(oHttp.responseXml.xml);
		var ret = SoapGetResult(oHttp.responseXml, methodName);
		if (ret == null) ret = true;
		return ret;
	} else {	
		//If there is an internal string (error message) surrounded by "[[" and "]]" (e.g. "Error:ORA-251 [[...my message...]]"), 
		//only this message will be displayed - this used to suppress system messages from SOAP, Oracle, etc.
		var s = SoapErrorMsg(oHttp.responseText);
		var k = s.toString().indexOf("[[");
		if (s.toString().indexOf("User is undefined") >= 0) {
			var msg = "Web service access denied.\n\nYour session was closed on a server.\nPlease login again.";
			if (gNoWSAccess) {
				window.status = msg;
			} else {
				alert(msg);
			}
			gNoWSAccess = true;
			return false;
		} else {
			if (s.toString().toLowerCase().indexOf("access denied") >= 0) {
				if (!noMessage) alert("You don't have an access to the requested Web Service:\n\n" + serviceUrl); //:\n\n\n" + s);
				//return false;
			}	
		}
		
		if (k >= 0) {
			var k2 = s.toString().indexOf("]]");
			var msg = s.toString().substring(k+2, k2);
			alert(msg);
		} else {
			alert("Web Service Error:\n\n" + s);
		}
		return false;
	}	
}





//version without alerts (for watch dog )
function SoapInvokeService2(serviceUrl, methodName, paramXml, async, xmlns) {
	var ns = AKA_XMLNS;
	if (xmlns) ns = xmlns;
	var env = SoapBuildEnvelope(methodName, paramXml, xmlns);
	var oHttp = new ActiveXObject("Microsoft.XMLHTTP");
	async = (async == true);
	oHttp.open("POST", serviceUrl, async);
	oHttp.setRequestHeader("Content-Type", "text/xml; charset='utf-8'"); 
	var hdr = ns + methodName;
	oHttp.setRequestHeader("SOAPAction", hdr);
	oHttp.send(env);
	if (async) return true; 
	if (oHttp.status == 200) {
		var ret = SoapGetResult(oHttp.responseXml, methodName);
		if (ret == null) ret = true;
		return ret;
	} else {	
		return false;
	}	
}

function SoapBuildEnvelope (methodName, parXml, xmlns) {
	var ns = AKA_XMLNS;
	if (xmlns) ns = xmlns;
	var env = "<?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>";
	env += "<" + methodName + " xmlns='" + ns + "'>";
	var re = /--/g;
	parXml = parXml.replace(re, "");
	//alert("XML=\n\n" + parXml);
	//re = /</g;
	env += parXml;  //sample parXml: <a>1</a><b>2</b>
	env += "</" + methodName + "></soap:Body></soap:Envelope>";
	//alert("Envelope:\n\n" + env);
	return env;
}

function SoapBuildInputParam(name, value, doEncode) {
	if (doEncode) value = SoapHtmlEncode(value);
	return "<" + name + ">" + value + "</" + name + ">";
}

function SoapGetReturnValue(xml, xPath) {
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.loadXML(xml);
	var node = xmlDoc.selectSingleNode(xPath);
	if (node) {
		return node.text;
	} else {
		alert("Cannot find " + xPath + " in: \n\n" + xml);
		return null;
	}	
}

function SoapGetResult(xmlDoc, methodName) {
	var node = xmlDoc.selectSingleNode("//" + methodName + "Result");
	//if (node == null) node = xmlDoc.DocumentElement;
	if (node) {
		return node.text;
	} else {
		return null;
	}
}
