/*
This object allows us to make SOAP calls to .NET objects using Javascript
*/
function SOAP()
{
    this.ErrorMessage = 'N/A';
    this.Namespace = 'http://tempuri.org/';
    this.Operation = '';
    this.Parameters = new Array();
    this.PostedXml = '';
    this.ResponseText = '';
    this.ResponseXml = '';
    this.Url = '';
    
    if (window.XMLHttpRequest)
    {
	    this.Request = new XMLHttpRequest();
    } 
    else if (window.ActiveXObject)
    {
	    try
	    {
		    this.Request = new ActiveXObject("Msxml2.XMLHTTP");
	    } 
	    catch (e) 
	    {
		    try 
		    {
			    this.Request = new ActiveXObject("Microsoft.XMLHTTP");
		    } 
		    catch (e) 
		    {
		        this.ErrorMessage = e;
		    }
	    }
    }
}

SOAP.prototype.AddParameter = function(name, value)
{
    var idx;
    
    idx = this.Parameters.length;
    
    this.Parameters[idx] = new Array();
    
    this.Parameters[idx][0] = name;
    this.Parameters[idx][1] = value;
}

SOAP.prototype.Post = function()
{
    var data, xml, idx, parameter, error, doc, text;    
    
    xml = '<?xml version="1.0" encoding="utf-8"?>\r\n';
    xml += '<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">\r\n';
    xml += '\t<soap12:Body>\r\n';
    xml += '\t\t<' + this.Operation + ' xmlns="' + this.Namespace + '">\r\n';

    for(idx=0; idx<this.Parameters.length; idx++)
    {
        parameter = this.Parameters[idx];        
        xml += '\t\t\t<' + parameter[0] + '>' + parameter[1] + '</' + parameter[0] + '>\r\n';
    }

    xml += '\t\t</' + this.Operation + '>\r\n';
    xml += '\t</soap12:Body>\r\n';
    xml += '</soap12:Envelope>';
    
	this.Request.open("POST", this.Url, false);
	this.Request.setRequestHeader("Content-Type", "application/soap+xml; charset=utf-8");
	this.Request.setRequestHeader("Content-Length", xml.length);
	this.Request.send(xml);	
		
    this.PostedXml = xml;
	
    if (this.Request.readyState == 4) 
    {
	    if (this.Request.status == 200) 
	    {
		    doc = this.StringToXMLDoc(this.Request.responseText);
		    
		    if (doc.text) text = doc.text;
		    else text = doc.documentElement.textContent
		    
		    this.ResponseText = text;
		    this.ResponseXml = this.StringToXMLDoc(text);		    		    
            this.Successful = true;
	    }
	    else 
	    {
	        this.ErrorMessage = "Status Number: " + this.Request.status + "\n\n" + this.Request.statusText + "\n\n" + this.Request.responseText;		    
	        this.Successful = false;
	    }
    }        
}

SOAP.prototype.StringToXMLDoc = function(str)
{
	var xmlDoc = null;

	try
	{
		var xmlDOMObj = new ActiveXObject("Microsoft.XMLDOM");
		xmlDOMObj.async = false;
		xmlDOMObj.loadXML(str);
		xmlDoc = xmlDOMObj;
	}
	catch (e)
	{
		try
		{
			var domParser = new DOMParser;
			xmlDoc = domParser.parseFromString(str, 'text/xml');
		}
		catch (e)
		{
			xmlDoc = null;
		}
	}

	return xmlDoc;
};

SOAP.prototype.constructor = SOAP; 