//======================================================
// Soap client class
//======================================================
function SoapClient()
{
	var THIS = this;

	THIS.Name = 'SoapClient';

	//==========================================
	// Add parameter.
	//==========================================
	this.AddParameter = function AddParameter(Name, Value)
	{
		// Check if the parameters array has been defined.
		if (THIS.Parameters == undefined)
			THIS.Parameters = new Array();

		THIS.Parameters.push(new Array(Name, Value));
		return;
	}
	
	//==========================================
	// Dispose.
	//==========================================
	this.Dispose = function Dispose()
	{
		for(Element in THIS)
		{
			if (typeof(THIS[Element]) != 'function')
			{
				if (Element != 'Name')
				{
					THIS[Element] = null;
					delete THIS[Element];
				}
			}
		}
		
		delete THIS.Parameters;
		delete THIS;
		return;
	}

	//==========================================
	// Send the XML request to the server.
	//==========================================
	this.SendRequest = function SendRequest(URL, Xmlns, Method, CallBack, Object)
	{
		var blnAsync = (((CallBack == undefined) || (CallBack == null)) ? false : true);
		var strReturnValue = '';
		
		THIS.URL = URL + '?op=' + Method;
		THIS.Xmlns = Xmlns;
		THIS.Method = Method;
		THIS.CallBack = CallBack;
		THIS.Object = Object;
		strReturnValue = THIS.BuildSoapRequest();
		
		// Check if an error occure building the soap request.
		if (strReturnValue == 'ERROR')
			return;
		
		THIS.CreateHTTPRequestObject();

		// Check if the HTTP request object has been created.
		if (THIS.HTTPRequest != null)
		{
			try
			{
				THIS.HTTPRequest.open('POST', THIS.URL, blnAsync);
				THIS.HTTPRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");				
				
				if (blnAsync)
				{
					// Set up the state changed event handler.
					THIS.HTTPRequest.onreadystatechange = THIS.StateChange;
					THIS.HTTPRequest.send(THIS.SoapMessage);
				}
				else
				{
					THIS.HTTPRequest.send(THIS.SoapMessage);
					strReturnValue = THIS.ProcessResponse(THIS.HTTPRequest.responseText);
					THIS.Dispose();
				}
			}
			catch(e)
			{
				if (blnAsync)
				{					
					CallBack(e.message);
				}
				else
				{
					alert(e.message);
				}
				
				THIS.Dispose();
				return;
			}
		}
		else
		{
			// HTTP request object has not been created.
			alert('Your browser does not support XMLHTTP.');
		}
		
		return strReturnValue;
	}

	//==========================================
	// Create a new HTTP request object.
	//==========================================
	this.CreateHTTPRequestObject = function CreateHTTPRequestObject()
	{
		// Mozilla etc...
		if (window.XMLHttpRequest)
		{
			THIS.HTTPRequest = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			// Internet explorer.
			THIS.HTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		return;
	}

	//============================================
	// Build the soap request.
	//============================================
	this.BuildSoapRequest = function BuildSoapRequest()
	{
		try
		{
			strReturnValue = 'OK';
			
			THIS.SoapMessage = '';
			THIS.SoapMessage += '<?xml version="1.0" encoding="utf-8"?>';
			THIS.SoapMessage += '<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">';
			THIS.SoapMessage += '<soap12:Body>';
			THIS.SoapMessage += '<' + THIS.Method  + ' xmlns="' + THIS.Xmlns + '">';

			// Build the parameters.
			if (THIS.Parameters != undefined)
			{
				for(var intCounter = 0; intCounter < THIS.Parameters.length; intCounter++)
				{
					if (THIS.Parameters[intCounter][1] == undefined)
					{
						strReturnValue = 'ERROR';
						alert('Unfortunately an error has occured, please contact your system administrator if you continue to receive this message: -\n\nParameter: \'' + THIS.Parameters[intCounter].toString().replace(',', '') + '\' is ' + THIS.Parameters[intCounter][1] + '\nMethod: ' + THIS.Method);
						break;
					}
					
					THIS.SoapMessage += '<' + THIS.Parameters[intCounter][0] + '>' + THIS.Parameters[intCounter][1] + '</' + THIS.Parameters[intCounter][0] + '>';
				}
			}

			THIS.SoapMessage += '</' + THIS.Method  + '>';
			THIS.SoapMessage += '</soap12:Body>';
			THIS.SoapMessage += '</soap12:Envelope>';
		}
		catch(e)
		{
			alert(e.message);
		}
		return strReturnValue;
	}

	//============================================
	// Response has been received from the server, process the response.
	//============================================
	this.StateChange = function StateChange()
	{
		// Check if the HTTP request object shows that something has been loaded.
		if (THIS.HTTPRequest.readyState == 4)
		{
			// The HTTP request object contains data.
			if ((THIS.HTTPRequest.status == 200) || (THIS.HTTPRequest.status == 0))
			{
				// Check if there was a problem with the request.
				if (THIS.HTTPRequest.responseText == 'ERROR')
				{
					// There was a problem retrieving the XML.
					THIS.CallBack('An error has occured in the soap request the request.\n\n' + THIS.ProcessErrorResponse(THIS.HTTPRequest.responseText));
					THIS.Dispose();
				}
				else
				{
					THIS.CallBack(THIS.ProcessResponse(THIS.HTTPRequest.responseText), THIS.Object);
					THIS.Dispose();
				}
			}
			else
			{
				// There was a problem retrieving the XML.
				try
				{
					THIS.CallBack('ERROR|There was a problem processing the request (' + THIS.HTTPRequest.status + ').\n\n' + THIS.ProcessErrorResponse(THIS.HTTPRequest.responseText));
					THIS.Dispose();
				}
				catch (e)
				{
					alert(e.message);
				}
			}
		}
		return;
	}
	
	//============================================
	// Process the response.
	//============================================
	this.ProcessResponse = function ProcessResponse(Response)
	{
		var strResponse = (Response == undefined ? '' : Response);
		strResponse = strResponse.substring(strResponse.indexOf('<' + THIS.Method + 'Result>') + ('<' + THIS.Method + 'Result>').length, strResponse.indexOf('</' + THIS.Method + 'Result>'));
		strResponse = strResponse.replace(/&lt;/gi, '<');
		strResponse = strResponse.replace(/&gt;/gi, '>');
		return strResponse;
	}
	
	//============================================
	// Process the error response.
	//============================================
	this.ProcessErrorResponse = function ProcessErrorResponse(Response)
	{
		var strResponse = (Response == undefined ? '' : Response);
		
		strResponse = strResponse.substring(strResponse.indexOf('<soap:Text xml:lang="en">') + ('<soap:Text xml:lang="en">').length, strResponse.indexOf('</soap:Text>'));
		strResponse = strResponse.replace(/&lt;/gi, '<');
		strResponse = strResponse.replace(/&gt;/gi, '>');
		return strResponse;
	}

	//============================================
	// Request details.
	//============================================
	this.RequestDetails = function RequestDetails(SoapMessage)
	{
		var strRequestDetails = '\n\n';

		strRequestDetails += 'URL: ' + THIS.URL + '\n';
		strRequestDetails += 'Xmlns: ' + THIS.Xmlns + '\n';
		strRequestDetails += 'Method: ' + THIS.Method + '\n';
		strRequestDetails += 'Message: ' + THIS.SoapMessage + '\n';

		return strRequestDetails;
	}

	return;
}
