//*******************************************************************
// NOTE NOTE NOTE - this script has a dependency on webtoolkit.md5.js
//*******************************************************************

function xmlHttpRequestObject() {
	this.xmlHttp = null;
	
	try {
		this.xmlHttp = new XMLHttpRequest();
	}
	catch(e) {
		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		for( var i = 0; i < xmlHttpVersions.length && !this.xmlHttp; i++ ) {
			try {
			  this.xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
    if( !this.xmlHttp )
	  alert( "Unable to create XMLHttpRequest object");

    this.process = function( page, async )
	{
		if( !this.xmlHttp )
			return false;
		var time = new Date().getTime();
		page += "&ms=" + time + "&key=" + MD5( String( time ) );
		this.xmlHttp.open( "POST", page, false );
		try
		{
			this.xmlHttp.send(null);
			return true;
		}
		catch(e)
		{
			alert( "Error connecting to server: " + e.toString() );
			return false;
		}
	}
		
    this.async = function( page, callback )
	{
		if( !this.xmlHttp )
			return false;
		var time = new Date().getTime();
		page += "&ms=" + time + "&key=" + MD5( String( time ) );

		this.xmlHttp.open( "POST", page, true );
		this.xmlHttp.onreadystatechange = callback; // doing this after the 'open' allows re-use of the object
		try
		{
			this.xmlHttp.send(null);
			return true;
		}
		catch(e)
		{
			alert( "Error connecting to server: " + e.toString() );
			return false;
		}
	}

	this.getStatus = function()
	{
		var xmlDoc = this.xmlHttp.responseXML;
		return xmlDoc ? parseInt( xmlDoc.getElementsByTagName( 'code' )[0].childNodes[0].nodeValue ) : -1;
	}
	
	this.getStatusText = function()
	{
		var xmlDoc = this.xmlHttp.responseXML;
		return xmlDoc ? xmlDoc.getElementsByTagName( 'info' )[0].childNodes[0].nodeValue : 'no status info provided';
	}
	
	this.getXMLResponse = function()
	{
		return this.xmlHttp.responseXML;
	}
	
	this.getResponseByTag = function( tag )
	{
		var data;
		var xmlDoc = this.xmlHttp.responseXML;
		try {
		  data = xmlDoc.getElementsByTagName( tag )[0].childNodes[0].nodeValue;
		}
		catch( e )
		{
			data = '';
		}
		return data;
	}
	
	this.getElementsByTag = function( tag )
	{
		var data;
		var xmlDoc = this.xmlHttp.responseXML;
		try {
		  data = xmlDoc.getElementsByTagName( tag );
		}
		catch( e )
		{
			data = null;
		}
		return data;
	}
		
	this.getResponseText = function()
	{
		return this.xmlHttp.responseText;
	}
}
