/*
	This file is used for handling Web services from the client side
	using protocol SOAP.
	Created By		:		Ramu
	Created On		:		11 Oct 2004
*/

function Soap(namespace, url, method) {
	this.namespace = namespace;	//	Namespace where webservice is declared
	this.url = url;	//	URL for webservice path
	this.method = method;	//	Which method has to be called in the above URL

	this.params = new Array();	//	All the parameter constructed using this array

	this.isError = false;	//	0 -> No error; 1 -> Error 
	this.responseText;

	//	All method prototype declared follows
	this.addParam = addParam;	//	Function to add parameter

	this.getSoapRequest = getSoapRequest;	//	Getting soap request consist of the following 2 function
	this.getBody = getBody;// Get body of the Soap

	this.send = send;
	this.setResponse = setResponse;

	/**
		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;
	}

	/* -----	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='"+this.namespace+"'>";	
		for (var i in this.params)	{
			str += "<" + i + ">" + this.params[i] + "</" + i + ">";
		}
		str += "</" + this.method + ">";
		return str;
	}

	/*---- Sending and evaluating response ---*/
	function send() {
		var response;
		var objHttp = getXmlHttpObject();
		objHttp.open("POST", this.url, false);
		objHttp.setRequestHeader("SOAPAction", this.namespace + this.method);
		objHttp.setRequestHeader("Content-Type", "text/xml");
		objHttp.send(this.getSoapRequest());
		alert(this.getSoapRequest());
		
		response = objHttp.responseText;
		alert(objHttp.responseText);
		this.setResponse(response);
		return this.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 eturn result
		var root = xmlDom.dom.documentElement;
		var element = root.childNodes[0].childNodes[0].childNodes[0];
		if (element.nodeName == this.method + "Result") {
			if(element.childNodes.length > 0) {
				this.responseText = element.childNodes[0].nodeValue;
				alert(element.childNodes[0].nodeValue);
			}else {
				this.responseText = "";
			}
		}else {
			this.isError = true;	//	Set error
		}
		
		
	}


}