var isIE=navigator.appVersion.indexOf('MSIE')>-1;

Object.prototype.isArray = function( ) {
	return this.constructor == Array ;
}

function WebServiceRequest(url, method, ns) {
    this.url=url;
    this.method=method;
    this.ns=ns;
    this.parameters=[];
    
    this.AddParameter = function(key, value) {
        this.parameters.push(key, value);
    }
    
    this.BuildRequest = function() {
		var header=	'<?xml version="1.0" encoding="utf-8"?>\n' +
					'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">\n' +
					'  <soap12:Body>\n' +
					'    <{METHOD} xmlns="{NAMESPACE}">\n';
		
		var footer=	'    </{METHOD}>\n' +
					'  </soap12:Body>\n' +
					'</soap12:Envelope>';
					
		header=header.replace(/{METHOD}/g, this.method);
		header=header.replace(/{NAMESPACE}/g, this.ns);
		footer=footer.replace(/{METHOD}/g, this.method);
		
		var params='', paramTemplate='      <{KEY}>{VALUE}</{KEY}>\n';
		for(var i=0; i<this.parameters.length; i+=2) {
			var k=this.parameters[i];
			var v=this.parameters[i+1];
			
			if (v.isArray()) {
				params+='      <{KEY}>\n'.replace(/{KEY}/g, k);
				var t=v[0];
				
				if (t=='string')
					for(var j=1; j<v.length; j++)
						params+='      <string><![CDATA[{VALUE}]]></string>\n'.replace(/{VALUE}/g, v[j]);
				else
					for(var j=1; j<v.length; j++)
						params+='      <{TYPE}>{VALUE}</{TYPE}>\n'.replace(/{TYPE}/g, t).replace(/{VALUE}/g, v[j]);
						
				params+='      </{KEY}>\n'.replace(/{KEY}/g, k);
			} else {
				params+=paramTemplate.replace(/{KEY}/g, k).replace(/{VALUE}/g, v);
			}
		}
		
		return header+params+footer;
    }
    
    this.SendRequest = function(onSuccessFunction, onFailureFunction) {
		if (onSuccessFunction) this.onSuccessFunction=onSuccessFunction;
		if (onFailureFunction) this.onFailureFunction=onFailureFunction;
		
		var methodName=this.method;
		
		var myAjax=new Ajax.Request(this.url,
		{
			method: 'post',
			contentType: 'text/xml; charset="utf-8"',
			// requestHeaders: ['Connection', 'close'],
			postBody: this.BuildRequest(),
			onSuccess: function(o) {
				var tagname="{METHOD}Result".replace(/{METHOD}/g, methodName);
				var xmldoc;
				
				if (isIE) {
					// IE does not take responseXML as a DOM Document.
					xmldoc = new ActiveXObject("Microsoft.XMLDOM");
					xmldoc.async=false;
					xmldoc.loadXML(o.responseText);
				} else {
					xmldoc=o.responseXML;
				}
				
				var element=xmldoc.documentElement;
				while(element.tagName!=tagname && element.hasChildNodes())
					element=element.childNodes[0];
					
				if (element.hasChildNodes())
					onSuccessFunction(o, element.childNodes[0].nodeValue);
				else
					onSuccessFunction(o, null);
			},
			onFailure: function(o) {
				onFailureFunction(o, null);
			}
		});
    }
}

function escapeForm(v) {
	var r='';
	for(var i=0; i<v.length; i++) {
		if (v[i]==' ') {
			r+='+´';
		} else {
			c=v.charCodeAt(i);
			h='00' + c.toString(16);
			h='%' + h.substring(h.length-2, h.length);
			r+=h;
		}
	}
	return r;
}

