
function SOAPClientParameters()
{

	var _pl = new Array();
	var _readingtype="kilometers";
	
	this.setReadingType = function(type)
	{
	   _readingtype=type;
	}
	this.add = function(name, value) 
	{
		_pl[name] = value; 
		return this; 
	}
	this.toXml = function()
	{
		var xml = "";
		for(var p in _pl)
		{
			if(typeof(_pl[p]) != "function")
			{
			if(p=='mileage')
				xml += "  <" + p + " readingtype=\""+_readingtype+"\">" + _pl[p].toString().replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") + "</" + p + ">\n";
			else
				xml += "  <" + p + ">" + _pl[p].toString().replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") + "</" + p + ">\n";
			}
		}
		return xml;	
	}
}

function SOAPClient() {}

SOAPClient.genereateXML = function(method, parameters,xmltype,readingtype)
{
	var soapNamespace;
    if(xmltype=="rawxml")
    	soapNamespace=null;
    else if(xmltype=="soap11")
    	soapNamespace="http://schemas.xmlsoap.org/soap/envelope/";
    else if(xmltype=="soap12")
        soapNamespace="http://www.w3.org/2003/05/soap-envelope";
    
    
	var xml ="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
	if(soapNamespace!=null)
	{
		xml+="<soap:Envelope xmlns:soap=\""+soapNamespace+"\">\n" +
		"<soap:Body>\n";
	}
	xml+="<" + method + ">\n" +
	   parameters.toXml() +
	   "</" + method + ">";
    if(soapNamespace!=null)
	{
		xml+="\n</soap:Body>\n</soap:Envelope>";
	}
	return xml;
}

SOAPClient.sendSoapRequest = function(url, xml, async, callback)
{
	var xmlHttp = SOAPClient._getXmlHttp();
	xmlHttp.open("POST", url, async);
	xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
	if(async) 
	{
		xmlHttp.onreadystatechange = function() 
		{
			if(xmlHttp.readyState == 4)
				SOAPClient.onSendSoapRequest(async, callback, xmlHttp);
		}
	}
	xmlHttp.send(xml);
	if (!async)
		return SOAPClient.onSendSoapRequest(async, callback, xmlHttp);
}

SOAPClient.sendGET = function(url, async, callback)
{
  var xmlHttp = SOAPClient._getXmlHttp();
	xmlHttp.open("GET", url , async);
	if(async) 
	{
		xmlHttp.onreadystatechange = function() 
		{
			if(xmlHttp.readyState == 4)
				SOAPClient.onSendSoapRequest(async,callback,xmlHttp);
		}
	}
	
	xmlHttp.send(null);
    if (!async)
		return SOAPClient.onSendSoapRequest(async,callback,xmlHttp);

}
SOAPClient.sendHtmlGET = function(url, async, callback)
{
  var xmlHttp = SOAPClient._getXmlHttp();
	xmlHttp.open("GET", url , async);
	if(async) 
	{
		xmlHttp.onreadystatechange = function() 
		{
			if(xmlHttp.readyState == 4)
				SOAPClient.onSendHtmlRequest(async,callback,xmlHttp);
		}
	}
	
	xmlHttp.send(null);
    if (!async)
		return SOAPClient.onSendHtmlRequest(async,callback,xmlHttp);

}
SOAPClient.onSendHtmlRequest = function(async, callback, req)
{
	if(callback)
		callback(req.responseText);
	if(!async)
		return req.responseText;		
}

SOAPClient.onSendSoapRequest = function(async, callback, req)
{
	if(callback)
		callback(req.responseXML);
	if(!async)
		return req.responseXML;		
}

// private: utils
SOAPClient._getElementsByTagName = function(document, tagName)
{
	try
	{
		// trying to get node omitting any namespaces (latest versions of MSXML.XMLDocument)
		return document.selectNodes(".//*[local-name()=\""+ tagName +"\"]");
	}
	catch (ex) {}
	// old XML parser support
	return document.getElementsByTagName(tagName);
}
// private: xmlhttp factory
SOAPClient._getXmlHttp = function() 
{
	try
	{
		if(window.XMLHttpRequest) 
		{
			var req = new XMLHttpRequest();
			// some versions of Moz do not support the readyState property and the onreadystate event so we patch it!
			if(req.readyState == null) 
			{
				req.readyState = 1;
				req.addEventListener("load", 
									function() 
									{
										req.readyState = 4;
										if(typeof req.onreadystatechange == "function")
											req.onreadystatechange();
									},
									false);
			}
			return req;
		}
		if(window.ActiveXObject) 
			return new ActiveXObject(SOAPClient._getXmlHttpProgID());
	}
	catch (ex) {}
	throw new Error("Your browser does not support XmlHttp objects");
}
SOAPClient._getXmlHttpProgID = function()
{
	if(SOAPClient._getXmlHttpProgID.progid)
		return SOAPClient._getXmlHttpProgID.progid;
	var progids = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
	var o;
	for(var i = 0; i < progids.length; i++)
	{
		try
		{
			o = new ActiveXObject(progids[i]);
			return SOAPClient._getXmlHttpProgID.progid = progids[i];
		}
		catch (ex) {};
	}
	throw new Error("Could not find an installed XML parser");
}