/*
	This file is used for handling Web services from the client side
	using protocol SOAP for call Master SP.
	Created By		:		Ramu
	Created On		:		28 Oct 2004
*/

function SPMaster(namespace, url, spName) {
	this.namespace = namespace;	//	Namespace where webservice is declared
	this.url = "webservices/Services.asmx";	//	URL for webservice path
	this.spName = spName;	// SPname to be executed
	this.method = "Execute";	//	Which method has to be called in the above URL. This method is defined in webservice

	this.params = new Array();	//	All the parameter constructed using this array
	this.xslParams = new Array();

	this.isError = false;	//	0 -> No error; 1 -> Error 
	this.responseText;

	//	All method prototype declared follows
	this.addParam = addParam;	//	Function to add parameter
	this.getParamValue = getParamValue;	//	Get the funciton parameter
	this.addXslParam = addXslParam;
	
	this.getSoapRequest = getSoapRequest;	//	Getting soap request consist of the following 2 function
	this.getBody = getBody;// Get body of the Soap

	this.send = send;	//	It will send the soap request to the weh service
	this.execute = execute;	//	This will parse the returned result and give back to the caller
	this.executeTransform = executeTransform;
	this.setResponse = setResponse;
	this.getSpXml = getSpXml;
	this.encodeXml = encodeXml;
	this.encryptXml = encryptXml;

	//	Encryption is required or not
	this.isSpEncrypted = false;
	this.isParamEncrypted = false;
	this.isXslTransform = false;
	this.xslFile;
	
	
	this.searchResult = searchResult;
	this.extraParam = new Array();
	
	/**
		This function is used to add parameter dynamically in the SOAP Request
		@@Input
			@paramName		:		Parameter name to be added
			@paramValue		:		Value of that parametere
		@@Output
			@type			:		void
	*/
	function addParam(paramName, paramValue) {
		this.params[paramName] = paramValue;
	}
	
	function addXslParam(paramName, paramValue) {
		this.xslParams[paramName] = paramValue;
	}

	/* -----	SOAP request string is created dynamically using the following function ------ */

	function getSoapRequest() {
		var str = "<soap:Envelope xmlns:xsi='http://wwww.w3.org/XMLSchema-instance' ";
		
		str += "xmlns:xsd='http://www.w3.org/XMLSchema' ";
		str += "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
		str += "<soap:Body>";
		str += this.getBody();	//	Body is added	
		str += "</soap:Body>";
		str += "</soap:Envelope>";	//	Closing the envelope
		return str;
	}

	//	Body is being created
	function getBody() {
		var str = "<s:" + this.method + " xmlns:s='"+this.namespace+"'>";	
		for(var i in this.extraParam) {
			str += "<s:" + i + ">" + this.extraParam[i] + "</s:" + i + ">";
		}
		
		str += "<s:xml>" + this.getSpXml() + "</s:xml>";
		str += "</s:" + this.method + ">";
		return str;
	}

	//	Construct xml for Stored procedure xml
	function getSpXml() {
		var str = "<Root>";
		if(!this.isSpEncrypted) {
			str += "<SP name='" + this.spName + "'>";
		}else {
			str += "<SP>" + this.encryptXml("name", this.spName);
		}
		for (var i in this.params) {
			if(!this.isParamEncrypted) {
				str += "<Param ";
				str += "name='" + i + "' ";
				str += "value='" + this.params[i] + "' ";
				str += "/>";
			}else {
				str += "<Param ";
				str += "value='" + this.params[i] + "' ";
				str += ">";
				str += this.encryptXml("name", i);
				str += "</Param>";
			}
		}
		str += "</SP>";
		if(this.isXslTransform) {
			str += "<XslTransform path='" + this.xslFile + "'>";
			for (var i in this.xslParams) {
				str += "<Param ";
				str += "name='" + i + "' ";
				str += "value='" + this.xslParams[i] + "' ";
				str += "/>";
			}
			str += "</XslTransform>";
		}
		str += "</Root>";
		return this.encodeXml(str);
	}

	//	Encrypted xml construction
	function encryptXml(name, value) {
		var str = "<EncryptedChiper type='attribute' name='" + name + "'>";
		str += "<ChiperText>";
		str += "<ChiperValue>";
		str += value;
		str += "</ChiperValue>";
		str += "</ChiperText>";
		str += "</EncryptedChiper>";
		return str;
	}


	function encodeXml(str) {
		str = str.replace(/&/gi, "&amp;");
		str = str.replace(/</gi, "&lt;");
		str = str.replace(/>/gi, "&gt;");
		return str;
	}
	
	function searchResult(pageId, xmlFile) {
		this.method = "GetSearchResult";
		this.extraParam["pageId"] = pageId;
		this.extraParam["xmlFile"] = xmlFile;
		this.send();
		return this.responseText;
	}

	//	Constructing parameter First 2 bytes define parameter length
	function getParamValue(param, value) {
		var length = param.length.toString();
		if (length.length == 1) {
			length = "0" + length;
		}
		return length + param + value;
	}

	/*---- Sending and evaluating response ---*/
	function send() {
		var objHttp = getXmlHttpObject();
		//alert("This is new JS" + this.url);
		//alert(objHttp)
		objHttp.open("POST", this.url, false);
		objHttp.setRequestHeader("SOAPAction", this.namespace + this.method);
		objHttp.setRequestHeader("Content-Type", "text/xml");
		//alert(this.getSoapRequest());
		objHttp.send(this.getSoapRequest());
		//alert(objHttp.responseText);
		this.setResponse(objHttp.responseText);
		return objHttp.responseText;
	}

	/*	----------------------------------   Manipualting result ---------------------- */
	function setResponse(xmlResult) {
		xmlResult = xmlResult.replace('<?xml version="1.0" encoding="utf-8"?>',"");	//	For avoid encoding switching. This may not support by some browser
		var xmlDom = new JXmlDom(xmlResult, false);	//	Creating dom for the return result
		var root = xmlDom.dom.documentElement;
		var element = root.childNodes[0].childNodes[0].childNodes[0];	//	Getting the result from the soap response
		
		if (element) {
			if (element.nodeName == this.method + "Result") {
				if(element.childNodes.length > 0 ) {
					this.responseText = element.childNodes[0].nodeValue;
				}else {
					this.isError = true;
				}
			}else {
				this.responseText = "<root/>";
			}
		}else {
			this.isError = true;	//	Set error
		}
		
	}

	/*	-------------------------- Function to parsing the returned result --------------------- */
	function execute() {
		this.send();
		return "<root>" + this.responseText + "</root>";
	}

	function executeTransform(xslFile) {
		this.isXslTransform = true;
		this.xslFile = xslFile;
		this.send();
		return this.responseText;
	}



}