/***************************************************************************
 *                            ajax.js
 *                            -------------------
 *   copyright            : (C) 2003 DXSolutions Ltd
 *   email                : support@dxsolutions.co.uk
 *
 *   Created              : 2/5/2006
 *
 ***************************************************************************/

function DXSChildNode(sNodeText)
{
//---------------------------------------------------
// CLASS : Implements simple DOM friendly Node
//---------------------------------------------------
	this.text = sNodeText;
	return this;
}

function DXSNodeList()
{
//---------------------------------------------------
// CLASS : Implements simple DOM friendly Nodelist
//---------------------------------------------------
	this.childNodes = new Array();

	function Add(sNodeText)
	{
		this.childNodes[this.childNodes.length] = new DXSChildNode(sNodeText);
	}

	this.Add = Add;		
}

// this object encapsulates our http request and groups data required in our onresponse function
function XMLRequestOb(oHttpReq, fn, bIsXML)
{
	this.m_ob = oHttpReq;
	this.m_fnAction = fn; 	
	this.m_bIsXML = bIsXML;
	
	return this;
}

// creates a null function, which is later used to prevent cyclic dependacies causing memory leaks
var Constant=	{ NULL:function() { } }

function AjaxOb(url, fnToDo, bXML, bAsync /*, bIsPost - default = false */) 
{
//---------------------------------------------------
// CLASS : Making an xml HTTPRequest (AJAX stylee)
//---------------------------------------------------
//	url	: page to request
//   fnToDo 	: response handler function
//     bXML 	: true => return XML response ob, 	false => return text response  
// bAsyncMode 	: true => asynchronus mode, 		false => synchronous mode 
// bIsPost      : true => post request,                 false => GET request [DEFAULT]
//----------------------------------------------
// RETURNS	: true if success, else false
//----------------------------------------------

	this.m_url = url;
	this.m_fnAction = fnToDo; 	
	this.m_bIsXML = bXML;
	this.m_bAsyncMode = bAsync; // asynchronous mode	
	this.m_bIsPOST = false;
	if(arguments[4] && (arguments[4] == true)) this.m_bIsPOST = true;

	
	//----------------------------------------------
	// FUNCTION : Start
	//
	// RETURNS	: n/a
	//----------------------------------------------
	function Start(/* optional POST string i.e: attr1=value1&attr2=value2 */)
	{
		var str_POST = null;
		if(arguments[0] && (arguments[0] != "")) str_POST = arguments[0];

		var oHTTPRequest = null;

		if (window.XMLHttpRequest) 
		{ 
			// Mozilla, Safari,...
			oHTTPRequest = new XMLRequestOb(new XMLHttpRequest(), this.m_fnAction, this.m_bIsXML);
			if (oHTTPRequest.m_ob.overrideMimeType) 
			{
				oHTTPRequest.m_ob.overrideMimeType('text/xml');
			}
		} 
		else if (window.ActiveXObject) 
		{ // IE
			try 
			{
				oHTTPRequest = new XMLRequestOb(new ActiveXObject("Msxml2.XMLHTTP"), this.m_fnAction, this.m_bIsXML);
			} catch (e) 
			{
				try 
				{
					oHTTPRequest = new XMLRequestOb(new ActiveXObject("Microsoft.XMLHTTP"), this.m_fnAction, this.m_bIsXML);
				} catch (e) {}
			}
		}

		if (!oHTTPRequest.m_ob) 
		{
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
		}
		oHTTPRequest.m_ob.onreadystatechange = 
			function OnResponse() 
			{	
				if (oHTTPRequest.m_ob.readyState == 4) 
				{
					try
					{
						if (oHTTPRequest.m_ob.status == 200) 
						{			
						} 
						else if (oHTTPRequest.m_ob.status == 404) 
						{
							//alert('Page specified in xml request NOT FOUND.');
						}
						else 
						{
							// ie runs this bit on error
							//alert('There was a problem with the xml request: status='+oHTTPRequest.m_ob.status);
						}
					}
					catch(e)
					{
						// FF runs this bit on error
					}
					
					// only run response handler if not null
					if (oHTTPRequest.m_fnAction != null) oHTTPRequest.m_fnAction(oHTTPRequest.m_ob);
					
					oHTTPRequest.m_ob.onreadystatechange = Constant.NULL;
				}
			}

		if (this.m_bIsPOST)
		{
			oHTTPRequest.m_ob.open('POST', this.m_url, this.m_bAsyncMode);
			oHTTPRequest.m_ob.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		}
		else
		{
			oHTTPRequest.m_ob.open('GET', this.m_url, this.m_bAsyncMode);
		}
		
		try 
		{
			oHTTPRequest.m_ob.send(str_POST);
		}
		catch (ee) {};
	}	

//----------------------------------------------
// instantiation of public methods
//----------------------------------------------
	this.Start = Start;

}

