var AjaxController = {

	__CFGInterval : 500,
	__CFGTimeout : 20000,

	__aRequests : new Array(),

	fnMakeRequest : function( oJSONData )
		{
		iIndex = AjaxController.fnGetIndex();
		AjaxController.fnLoadParams(iIndex, oJSONData);
		AjaxController.fnLoadFunctions(iIndex, oJSONData);
		
		if (!AjaxController.fnCreateObject(iIndex)) {
			AjaxController.fnClearFinished(iIndex);
			return; 
			}
		if (AjaxController.__aRequests[ iIndex ][ 'sMethod' ] == "GET") {
			AjaxController.fnSendGetMethod(iIndex);
			}
		else {
			AjaxController.fnSendPostMethod(iIndex);
			}
		},

	fnLoadParams : function( iIndex, oJSONData )
		{
		AjaxController.__aRequests[ iIndex ][ 'sURL' ] = oJSONData[ 'sURL' ];
		AjaxController.__aRequests[ iIndex ][ 'sMethod' ] = ( ( oJSONData[ 'sMethod' ] == "POST" || oJSONData[ 'sMethod' ] == "GET" ) ? oJSONData[ 'sMethod' ] : "POST" );
		AjaxController.__aRequests[ iIndex ][ 'sParams' ] = AjaxController.fnFormatParams( iIndex, oJSONData[ 'aParams' ] );
		},

	fnLoadFunctions : function( iIndex, oJSONData )
		{
		AjaxController.__aRequests[ iIndex ]['fnOnInit'] = ( typeof oJSONData.fnOnInit == "function" ? oJSONData.fnOnInit : function(){} );
		AjaxController.__aRequests[ iIndex ]['fnOnSuccess'] = ( typeof oJSONData.fnOnSuccess == "function" ? oJSONData.fnOnSuccess : function(){} );
		AjaxController.__aRequests[ iIndex ]['fnOnFail'] = ( typeof oJSONData.fnOnFail == "function" ? oJSONData.fnOnFail : function(){} );
		AjaxController.__aRequests[ iIndex ]['fnOnTimeout'] = ( typeof oJSONData.fnOnTimeout == "function" ? oJSONData.fnOnTimeout : function(){} );
		},

	fnCreateObject : function( iIndex )
		{
		AjaxController.__aRequests[ iIndex ][ 'XMLHTTPObject' ] = AjaxController.fnCreateXMLHttpObject();
		if( AjaxController.__aRequests[ iIndex ][ 'XMLHTTPObject' ] ) { return true; }
		else { return false; }
		},

	fnSendPostMethod : function( iIndex )
		{
		AjaxController.__aRequests[ iIndex ][ 'XMLHTTPObject' ].open( AjaxController.__aRequests[ iIndex ][ 'sMethod' ], AjaxController.__aRequests[ iIndex ][ 'sURL' ], true );
		AjaxController.__aRequests[ iIndex ][ 'XMLHTTPObject' ].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		AjaxController.__aRequests[ iIndex ][ 'XMLHTTPObject' ].send( AjaxController.__aRequests[ iIndex ][ 'sParams' ] );
		AjaxController.fnOnInit( iIndex );
		},

	fnSendGetMethod : function( iIndex )
		{
		AjaxController.__aRequests[ iIndex ][ 'XMLHTTPObject' ].open( AjaxController.__aRequests[ iIndex ][ 'sMethod' ], AjaxController.__aRequests[ iIndex ][ 'sURL' ] + AjaxController.__aRequests[ iIndex ][ 'sParams' ], true );
		AjaxController.__aRequests[ iIndex ][ 'XMLHTTPObject' ].send( null );
		AjaxController.fnOnInit( iIndex );
		},

	fnOnInit : function( iIndex )
		{
		AjaxController.fnLaunchState( iIndex, "init" );
		AjaxController.fnSetTimeStamps( iIndex );
		AjaxController.fnOnProgress( iIndex );
		},

	fnOnProgress : function( iIndex )
		{
		if( AjaxController.__aRequests[ iIndex ][ 'XMLHTTPObject' ].readyState == 4 )
			{
			if( AjaxController.__aRequests[ iIndex ][ 'XMLHTTPObject' ].status == 200 )
				{ AjaxController.fnOnSuccess( iIndex ); }
			else
				{ AjaxController.fnOnFail( iIndex ); }
			}
		},

	fnOnSuccess : function( iIndex )
		{
		AjaxController.fnClearTimeStamps( iIndex );
		AjaxController.fnLaunchState( iIndex, "success" );
		AjaxController.fnClearFinished( iIndex );
		},

	fnOnFail : function( iIndex )
		{
		AjaxController.fnClearTimeStamps( iIndex );
		AjaxController.fnLaunchState( iIndex, "fail" );
		AjaxController.fnClearFinished( iIndex );		
		},
	
	fnOnTimeout : function( iIndex )
		{
		AjaxController.fnClearTimeStamps( iIndex );
		AjaxController.fnLaunchState( iIndex, "timeout" );
		AjaxController.fnClearFinished( iIndex );
		},

	fnLaunchState : function( iIndex, sState )
		{
		switch( sState )
			{
			case "init" :
				AjaxController.__aRequests[ iIndex ][ 'sRequestState' ] = "init";
				AjaxController.__aRequests[ iIndex ].fnOnInit();
				break;
			case "success" :
				AjaxController.__aRequests[ iIndex ][ 'sRequestState' ] = "success";
				AjaxController.__aRequests[ iIndex ].fnOnSuccess( AjaxController.__aRequests[ iIndex ][ 'XMLHTTPObject' ].responseText );
				break;
			case "timeout" :
				AjaxController.__aRequests[ iIndex ][ 'sRequestState' ] = "timeout";
				AjaxController.__aRequests[ iIndex ].fnOnTimeout();
				break;
			case "fail" :
				AjaxController.__aRequests[ iIndex ][ 'sRequestState' ] = "fail";
				AjaxController.__aRequests[ iIndex ].fnOnFail( AjaxController.__aRequests[ iIndex ][ 'XMLHTTPObject' ].status );
				break;
			}
		},

	fnSetTimeStamps : function( iIndex )
		{
		AjaxController.__aRequests[ iIndex ][ 'iIntervalId' ] = window.setInterval( "AjaxController.fnOnProgress( " + iIndex + " )", AjaxController.__CFGInterval );
		AjaxController.__aRequests[ iIndex ][ 'iTimeoutId' ] = window.setTimeout( "AjaxController.fnOnTimeout( " + iIndex + " )", AjaxController.__CFGTimeout );		
		},

	fnClearTimeStamps : function( iIndex )
		{
		window.clearInterval( AjaxController.__aRequests[ iIndex ][ 'iIntervalId' ] );
		window.clearTimeout( AjaxController.__aRequests[ iIndex ][ 'iTimeoutId' ] );
		},

	fnClearFinished : function( iIndex )
		{
		if( AjaxController.__aRequests[ iIndex ] )
			{delete AjaxController.__aRequests[ iIndex ]; }
		},

	fnCreateXMLHttpObject : function()
		{
		oNewRequest = false;
		if( window.XMLHttpRequest ) 
			{ 
			try {
				oNewRequest = new XMLHttpRequest(); 
				if( oNewRequest.overrideMimeType ) { oNewRequest.overrideMimeType( 'text/xml' ); }
				}
			catch( oError ) {}
			} 
		else if( window.ActiveXObject ) 
			{
			var aSignatures = [ "MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp" ];     
			for( var i = 0; i < aSignatures.length; i++ ) 
				{ 
				try { 
					oNewRequest = new ActiveXObject( aSignatures[ i ] ); 
					} 
				catch ( oError ) {} 
				}	
			}
		return oNewRequest;
		},
	
	fnFormatParams : function( iIndex, oJSONParams )
		{
		sParams = "";
		if( AjaxController.__aRequests[ iIndex ][ 'sMethod' ] == "GET" )
			{
			if( sParams == "" ) { sParams += "?"; }
			for( i in oJSONParams )
				{
				if( sParams.length > 1 ) { sParams += "&" }
				sParams += encodeURIComponent( i ) + "=" + encodeURIComponent( oJSONParams[ i ] );
				}
			}
		else
			{
			for( i in oJSONParams )
				{
				if( sParams.length > 0 ) { sParams += "&" }
				sParams += encodeURIComponent( i ) + "=" + encodeURIComponent( oJSONParams[ i ] );
				}
			}
		return sParams;
		},

	fnGetIndex: function() 
		{
		iIndex = null; i = 0;
		do { iIndex = i; i++; }
		while( AjaxController.__aRequests[ iIndex ] );
		AjaxController.__aRequests[ iIndex ] = {};
		return iIndex;
		}
		
	};

