Ext.namespace("MicroPacific.Net");

MicroPacific.Net.MpXmlResponse = function(pParams, cUrl, oScope, fnCallback, fnXmlExceptionCallback, fnResponseErrorCallback)
{
	this.pParams = pParams;
	this.cUrl = cUrl;
	this.oScope = oScope;
	this.fnCallback = fnCallback;
	this.fnXmlExceptionCallback = fnXmlExceptionCallback;
	this.fnResponseErrorCallback = fnResponseErrorCallback;
	this.rspResponse = null;
	
	// MDR 06/09/2007 - Not exactly sure what to call these arguments, check them out in firebug debug
	// MDR 06/09/2007 - Be very careful about 'this' in javascript. In this case, 'this' actually refers to the error event not the owner of the error handler.
	this.XmlExceptionCallback = function(e, f, g)
	{
		//this.fnXmlExceptionCallback.call(oScope);
		// MDR 06/09/2007 - It seems that 'f.request.scope.oScope' is equivalent to what should be 'this'
		// MDR 06/09/2007 - The problem being that our loadexception handler is called with a scope of request
		var eError = { ErrorCode: "-1", ErrorMessage: "Server Error: " + g.status, Response: g, Params: pParams };
		f.request.scope.fnXmlExceptionCallback.call(f.request.scope.oScope, eError);
	}
	this.InnerCallback = function(e, f, g)
	{

		//console.log("Inner callback.");
		
		// MDR 06/09/2007 - Error should have been handled by loadexception handler
		if (!this.rspResponse) return;
		if (this.rspResponse.responseXML == null)
		{
			this.fnResponseErrorCallback.call(this.oScope, { ErrorCode: "", ErrorMessage: "ResponseXML is null.", Params: this.pParams, ResponseText: this.rspResponse.responseText, Response: this.rspResponse }, false);
			return;
		}
		// MDR 05/08/2007 - Check for errors
		var xmlErrorCode = this.rspResponse.responseXML.getElementsByTagName("ErrorCode")[0];
		var xmlErrorMessage = this.rspResponse.responseXML.getElementsByTagName("ErrorMessage")[0];
		
		if (!xmlErrorCode || !xmlErrorMessage)
		{
			this.fnResponseErrorCallback.call(this.oScope, { ErrorCode: "", ErrorMessage: "Missing response codes.", Params: this.pParams, ResponseText: this.rspResponse.responseText, Response: this.rspResponse }, false);
			return;
		}
		
		var nErrorCode = parseInt(xmlErrorCode.firstChild.nodeValue, 10);
		var cErrorMessage = xmlErrorMessage.firstChild.nodeValue;
		
		if (nErrorCode != 0)
		{
			var oResponseError = { ErrorCode: nErrorCode, ErrorMessage: cErrorMessage, Params: this.pParams, ResponseText: this.rspResponse.responseText, Response: this.rspResponse };
			this.fnResponseErrorCallback.call(this.oScope, oResponseError, false);
			return;
		}
		
		
		var nodeResponseData = this.rspResponse.responseXML.getElementsByTagName("ResponseData")[0];
		
		if (!nodeResponseData)
		{
			this.fnResponseErrorCallback.call(this.oScope, { ErrorCode: "", ErrorMessage: "Missing ResponseData node.", Params: this.pParams, ResponseText: this.rspResponse.responseText }, false);
			return;
		}
		
		// MDR 05/08/2007 - Should be all good.
		
		var xmlResponseData = StringToXmlDocument(nodeResponseData.firstChild.nodeValue);
		this.fnCallback.call(this.oScope, xmlResponseData, false);
	}
	
	var xml = new Ext.data.HttpProxy({url:this.cUrl});
	// MDR 06/09/2007 - Don't subscribe to this.
	var _mpxr = this;
	xml.on('loadexception', this.XmlExceptionCallback);
	
	//var _mpxr = this;
	
	//xml.on('loadexception', this.XmlExceptionCallback);
	xml.load(
		this.pParams,
		{ read: function(response) { _mpxr.rspResponse = response } },
		this.InnerCallback,
		_mpxr		
	);
}