/*
	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(spName) {

	this.namespace = "http://tempuri.org/";	//	Namespace where webservice is declared
	this.url = (arguments.length > 1)?arguments[1]:"../WebService/Services.asmx";
//	this.url = "../WebService/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.isXslTransform = false;	// Transformation required on server side
	this.xslFile = '';

	this.params = new Array();	//	All the parameter constructed using this array
	this.xslParams = new Array();	// Used to store xsl argument list

	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;
	this.execute = execute;
	this.executeTransform = executeTransform;
	this.setResponse = setResponse;
	this.getXmlSp = getXmlSp;
	this.encodeXml = encodeXml;
	
	/**
		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 = "<" + this.method + " xmlns='http://tempuri.org/'>";	
	    str += "<xml>" + this.encodeXml(this.getXmlSp()) + "</xml>";
		str += "</" + this.method + ">";
		return str;
	}

	//	Constructing xml string 
	function getXmlSp() {
		var strXml = "<Root>";
		strXml += "<SP name='" + this.spName + "'>";
		for (var i in this.params) {
			strXml += "<Param name='" + i + "' value='" + this.params[i] + "'/>";
		}
		strXml += "</SP>";
		if(this.isXslTransform) {
			strXml += "<Transform xslFileName='" + this.xslFile + "'>";
			for(var i in this.xslParams) {
				strXml += "<Param name='" + i + "' value='" + this.xslParams[i] + "'/>";
			}
			strXml += "</Transform>";
		}
		strXml += "</Root>";
		return strXml;
	}

	//	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;
	}

	function encodeXml(xml) {
		xml = xml.replace(/&/g, "&amp;");
		xml = xml.replace(/</g, "&lt;");
		xml = xml.replace(/>/g, "&gt;");
		return xml;

	}

	/*---- Sending and evaluating response ---*/
	function send() {
		var objHttp = getXmlHttpObject();
		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"?>',"");
		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];
		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.isError = true;	//	Set error
		}
		
	}

	function execute() {
		this.method = "Execute";
		this.send();
		return "<root>" + this.responseText + "</root>";
	}

	function executeTransform(xslFile) {
		this.isXslTransform = true;
		this.xslFile = xslFile;
		this.method = "ExecuteTransform";
		this.send();
		this.responseText = this.responseText.replace('<?xml version="1.0" encoding="utf-8"?>',"");
		return this.responseText;
	}




}
