// SOAPProxyD.js
// Author: R. Cunnings

// SOAP proxy implementation...this function makes the SOAP invocation
// and returns the DOM object containing the parsed response message
// along with the request and response message XML.

// constructor for parameter class
function classParameter(strName, strContent, strAttributes)
{
	this.strName = strName;
	this.strContent = strContent;
	this.strAttributes = strAttributes;
}

// constructor for header class
function classHeader(strName, strNS, strContent, strAttributes)
{
	this.strName = strName;
	this.strNamespace = strNS;
	this.strContent = strContent;
	this.strAttributes = strAttributes;
}

// SOAP method invocation over HTTP
// parameters:
// (1) strURL... the URL for the HTTP 'POST' request.
// (2) strSOAPAction... the value of the 'SOAPAction" HTTP header sent along with the 'POST' request.
// (3) strMethod... the name of the method being invoked via SOAP.
// (4) strMethodNS... the namespace to be associated with the method element in the SOAP request message.
// (5) arrParameters... an array of type classParameters containing the method parameter names and values.
// (6) arrHeaders... an array of type classHeader containing any headers to be sent in the SOAP request message.
// (7) objTextAreaReq... a text area object into which the request message XML will be placed.
// (8) objTextAreaResp... a text area into which the response message XML will be placed.
//
function SOAPInvoke(strURL, strSOAPAction, strMethod, strMethodNS, arrParameters, arrHeaders, objTextAreaReq, objTextAreaResp)
{
	var objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	var strRequestMsg = "";
	var index = 0;
	var elemName = "";
	var elemContent = "";
	var objParam;

	objXMLHTTP.Open("POST", strURL, false, "", "");
	objXMLHTTP.setRequestHeader("SOAPAction", '"' + strSOAPAction + '"');
	objXMLHTTP.setRequestHeader("Content-Type:", 'text/xml; charset="utf-8"');
	strRequestMsg=  '<?xml version="1.0" encoding="UTF-8"?>\r\n';
	strRequestMsg=strRequestMsg + '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">\r\n';

	// construct any header elements required
	if (arrHeaders.length > 0)
	{
		strRequestMsg=strRequestMsg + '<SOAP-ENV:Header>\r\n';
		for (index = 0 ; index < arrHeaders.length; index++)
		{
			objHeader = arrHeaders[index]; 
			strRequestMsg = strRequestMsg + '<h:' + objHeader.strName + ' xmlns:h="' + objHeader.strNamespace + '"';
			if (objHeader.strAttributes != "")
			{
				strRequestMsg = strRequestMsg + ' ' + objHeader.strAttributes;
			}
			strRequestMsg = strRequestMsg + '>';
			strRequestMsg = strRequestMsg + objHeader.strContent;
			strRequestMsg = strRequestMsg + '</h:' + objHeader.strName + '>\r\n'; 
		}
		strRequestMsg=strRequestMsg + '</SOAP-ENV:Header>\r\n';
	}
	strRequestMsg=strRequestMsg + '<SOAP-ENV:Body>\r\n';
	strRequestMsg=strRequestMsg + '<m:' + strMethod + ' xmlns:m="' + strMethodNS + '" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">\r\n';

	// construct parameter elements

	for (index = 0 ; index < arrParameters.length; index++)
	{
		objParam = arrParameters[index]; 
		strRequestMsg = strRequestMsg + '<' + objParam.strName;
		if (objParam.strAttributes != "")
		{
			strRequestMsg = strRequestMsg + ' ' + objParam.strAttributes;
		}
		strRequestMsg = strRequestMsg + '>'; 
		strRequestMsg = strRequestMsg + objParam.strContent;
		strRequestMsg = strRequestMsg + '</' + objParam.strName + '>\r\n'; 
	}
	strRequestMsg=strRequestMsg + '</m:' + strMethod + '>\r\n';
	strRequestMsg=strRequestMsg + '</SOAP-ENV:Body>\r\n';
	strRequestMsg=strRequestMsg + '</SOAP-ENV:Envelope>';
	objXMLHTTP.Send (strRequestMsg);
    
	if (objXMLHTTP.ResponseXML.xml == "")
	{
	      alert(objXMLHTTP.ResponseText);
	}
	objTextAreaReq.value = strRequestMsg;
	objTextAreaResp.value = objXMLHTTP.ResponseText;
	return objXMLHTTP.ResponseXML;
}
