//******************************************************************************************
//                COPYRIGHT ENDSLEIGH INSURANCE SERVICES LIMITED 2007
//******************************************************************************************
//   PROJECT         :   insurance.net
//   LANGUAGE        :   JavaScript
//   FILENAME        :   WebServiceProxy.js
//   ENVIRONMENT     :   Microsoft Visual Studio
//******************************************************************************************
//   FILE FUNCTION   :   
//   EXECUTABLE TYPE :   JS
//   SPECIFICATION   :   None
//
//   RELATED DOCUMENTATION : None
//
//******************************************************************************************
//   ABSTRACT        :   JavaScript object that encapsulates the functionality 
//						 supported by the XmlHttpRequest object, in order to support
//						 synchronous/asynchronous web service calls from
//						 client-side code i.e. AJAX.
//                       
//   AUTHOR          :   C. Newton       CREATION DATE : 26-Nov-2007
//
//******************************************************************************************
//   BUILD INFORMATION   :   Endsleigh Build System
//   EXECUTABLE NAME     :   
//   MAIN ENTRY POINTS   :   
//
//   EVENTS              :   
//
//******************************************************************************************
//   PVCS SECTION:
//   ~~~~~~~~~~~~~
//   PVCS FILENAME: $Logfile:   Z:\INETBase\Res\JavaScript\WebServiceProxy.js  $
//   PVCS REVISION: $Revision:   1.0  $
//
//   $Log:   Z:\INETBase\Res\JavaScript\WebServiceProxy.js  $
//
//******************************************************************************************

function WebServiceProxy(errorPage)
{
	// Construct objects
	this.ErrorPage = errorPage;
	this.XmlHttp = null;
	this.ResponseXml = '';

	this.Initialise = function()
	{
		if (this.XmlHttp != null)
				return this.XmlHttp;
		else
		{
			// Attempt to create XmlHttp object
			/*@cc_on
			@if (@_jscript_version >= 5)
			try 
			{
				this.XmlHttp = new ActiveXObject("Msxml2.XmlHttp");
			} 
			catch (e) 
			{
				try 
				{
					this.XmlHttp = new ActiveXObject("Microsoft.XmlHttp");
				} 
				catch (e) 
				{
				}
			}
			@end @*/
			
			if (this.XmlHttp == null && typeof XMLHttpRequest != 'undefined') 
			{
				try 
				{
					this.XmlHttp = new XMLHttpRequest();
					this.XmlHttp.overrideMimeType("text/xml");
				} 
				catch (e) 
				{
					alert(e.message);
				}
			}
			
			if (this.XmlHttp == null)
				return false;
			else
				return true;
		}
	}

	// Initialise the request object
	this.Initialised = this.Initialise();

	// Sends a POST request message to the specified Web Service method
	this.SendMessage = function(url, soapAction, requestMessage, async)
	{
		try
		{
			this.XmlHttp.open("POST", url, async);
			this.XmlHttp.setRequestHeader("SOAPAction", soapAction);
			this.XmlHttp.setRequestHeader("Content-Type", "text/xml");
			this.XmlHttp.send(this.GetSOAPEnvelopeFirstPart() + requestMessage + this.GetSOAPEnvelopeLastPart());
		}
		catch (e)
		{
			this.ErrorHandler(e);
		}
	}

	// Whether we have a valid response from the Web Service
	this.ResponseReceived = function()
	{
		var retVal = false;
		
		if (this.XmlHttp != null)
		{                    
			try
			{
				if (this.XmlHttp.readyState === 4 && this.XmlHttp.status === 200)
				{
					// Get the response message
					this.ResponseXml = this.XmlHttp.responseXML;
					
					// Clear onreadystatechange so action is not repeated
					this.XmlHttp.onreadystatechange = function () {}
					
					retVal = true;
				}
			}
			catch (e)
			{
			}
		}
		
		return retVal;
	}

	// Returns the start of the SOAP envelope
	this.GetSOAPEnvelopeFirstPart = function()
	{
		var soapEnvelope =  "<soap:Envelope xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +

							" xsd=\"http://www.w3.org/2001/XMLSchema\"" +

							" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +

							"	<soap:Body>";
		return soapEnvelope;
	}

	// Returns the end of the SOAP envelope
	this.GetSOAPEnvelopeLastPart = function()
	{
		var soapEnvelope =	"	</soap:Body>" +

							"</soap:Envelope>";
		return soapEnvelope;
	}

	// Direct request to the user-defined error page
	this.ErrorHandler = function(error)
	{
		// Simply direct user to message page
		window.location = this.ErrorPage;
	}
}
