// Constants for XmlHttp status

var			REQ_UNINITIALISED = 0;
var			REQ_LOADING = 1;
var			REQ_LOADED = 2;
var			REQ_INTERACTIVE = 3;
var			REQ_COMPLETE = 4;

function AjaxRequest()
{
	
	
	this.mRequest = this.getHttpRequest();
	this.mHandlers = new Array();

	// Need to assign 'this' to a variable so we can put it into the anonymous function
	
	var self = this;
	
	this.mRequest.onreadystatechange = function()
	{
		if(	self.mHandlers[ self.mRequest.readyState ] != undefined )
		{
			for( i = 0 ; i < self.mHandlers[ self.mRequest.readyState ].length ; i++ )
			{
				self.mHandlers[ self.mRequest.readyState ][ i ]( self );				
			}
		}
	}
}

AjaxRequest.prototype.addEventListener = function( pEventType, pFunction )
{
	if(	this.mHandlers[ pEventType ] == undefined )
	{
		this.mHandlers[ pEventType ] = new Array();
	}
	
	this.mHandlers[ pEventType ].push( pFunction );
}

AjaxRequest.prototype.getHttpRequest = function()
{
	// List of Microsoft XMLHTTP versions - newest first

	var MSXML_XMLHTTP_PROGIDS = new Array
	(
		'MSXML2.XMLHTTP.5.0',
		'MSXML2.XMLHTTP.4.0',
		'MSXML2.XMLHTTP.3.0',
		'MSXML2.XMLHTTP',
		'Microsoft.XMLHTTP'
	);

	// Do we support the request natively (eg, Mozilla, Opera, Safari, Konqueror)

	if( window.XMLHttpRequest != null )
	{
		return new XMLHttpRequest();
	}
	else
	{
		// Look for a supported IE version

		for( i = 0 ; MSXML_XMLHTTP_PROGIDS.length > i ; i++ )
		{
			try
			{
				return new ActiveXObject( MSXML_XMLHTTP_PROGIDS[ i ] );
			}
			catch( e )
			{
			}
		}
	}
	
	return( null );
}


//added by Stephen Rowe to allow a loading period
/*
load_display is args for the display options
func_name is the forwarding function name
2.. = args
*/
function ajaxLoadingInit( load_display )
{

	ajaxLoadDisplay( load_display );
	
	var methodArgs = ( typeof load_display.method == 'undefined' ) ? null : load_display.method;

	if( methodArgs )
	{
		var func_name = ( typeof methodArgs.func_name == 'undefined' ) ? null : methodArgs.func_name;
		
		if( func_name )
		{		
			var newArgs = [];

		    for(var i in methodArgs )
		    	if( methodArgs[i] != func_name )
		        	newArgs.push(methodArgs[i]);

			if( window[func_name] ) window[func_name].apply( this, newArgs );
		}
	}
	
}

function ajaxLoadDisplay( load_display )
{
		
	var mType = ( typeof load_display.type == 'undefined' ) ? 'image' : load_display.type;
   	
   	var mValue = ( typeof load_display.value == 'undefined' ) ? null : load_display.value;
   	
   	var mElement = ( typeof load_display.element == 'undefined' ) ? null : load_display.element;
   	
   	var mCss = ( typeof load_display.css == 'undefined' ) ? null : load_display.css;

   	var loadResult = null;
   	
   	if( mType && mValue && mElement )
   	{
	   	if ( mType == 'image')
	   	{
	   		loadResult = document.createElement('img');
	   		loadResult.src = mValue;
	   		   		   		
	   	}
	   	else if( mType == 'text')
	   	{
	   		loadResult = document.createElement('p');
	   		loadResult.innerHTML = mValue ;

		}
	
		if( loadResult && mCss )
	   			loadResult.className = mCss;
	   	
	   		
	   	if( document.getElementById(mElement) )
	   	{
	   		document.getElementById( mElement ).innerHTML = "";
	   		document.getElementById( mElement ).appendChild(loadResult);
	   	}
	   		
   	}
}