var ajax_component = {};
var ad_delay = 0;

//---------------------

function addLoadEvent(func) 
{
	var oldonload = window.onload;
 	if (typeof window.onload != 'function') 
	{
    	window.onload = func;
  	} 
	else 
	{
    	window.onload = function() 
		{
      		if (oldonload) 
			{
        		oldonload();
      		}
      		func();
    	}
  	}
}

//---------------------

function Init_Ajax_Components()
{
	for (id in ajax_component) 
	{
	    switch(typeof(ajax_component[id])) 
		{
			case 'string':
				ajax_component[id] 				= new window[ajax_component[id]](id);
				break;
				
			case 'function':
				ajax_component[id].prototype 	= new ajax();
				ajax_component[id] 				= new ajax_component[id]();
				ajax_component[id].Init();
				break;
		  }
	  }
}

//---------------------

addLoadEvent(Init_Ajax_Components);

//---------------------

function ajax()
{
	//---------------------
	// Private Declarations
	//---------------------
	var _request 	= null;
	var _this 		= null;
	var ElementID 	= '';
	var LoadingIMG	= 'http://www.pjio.com/images/ajax-loader.gif';

	//--------------------
	// Public Declarations
	//--------------------
	this.GetResponseXML = function()
	{
		return (_request) ? _request.responseXML : null;
	}

	this.GetResponseText = function()
	{
		return (_request) ? _request.responseText : null;
	}

	this.GetRequestObject = function()
	{
		return _request;
	}

	this.InitializeRequest = function(Method, Uri)
	{
		_InitializeRequest();
		_this = this;
		
		switch (arguments.length)
		{
			case 2:
				_request.open(Method, Uri);
				break;

			case 3:
				_request.open(Method, Uri, arguments[2]);
				break;
		}

		if (arguments.length >= 4) _request.open(Method, Uri, arguments[2], arguments[3]);
		this.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	}

	this.SetRequestHeader = function(Field, Value)
	{
		if (_request) _request.setRequestHeader(Field, Value);
	}

	this.Commit = function(Data)
	{
		if (_request) _request.send(Data);
	}
       
	this.Close = function()
	{
		if (_request) _request.abort();
	}

	this.ShowLoading = function()
	{
		var height 	= (parseInt(document.getElementById(this.ElementID).offsetHeight)-25) / 2;
		var	width	= (parseInt(document.getElementById(this.ElementID).offsetWidth) -25) / 2;
		document.getElementById(this.ElementID).innerHTML = "<img src='"+LoadingIMG+"' style='position:relative; top:"+height+"px; left:"+width+"px;'>";
	}

	this.OnSuccess = function()  // Over-ride this function with you own OnSuccess Function.
	{
		this.Show();		
	}
	
	this.Show = function()
	{
		document.getElementById(this.ElementID).innerHTML = this.GetResponseText();
		//eval(document.getElementById(this.ElementID).innerHTML);
		document.getElementById(this.ElementID).style.visibility = 'visible';
		//alert(this.GetResponseText());
	}

	//---------------------------
	// Public Event Declarations.
	//---------------------------
	this.Init				= function() { };
	this.OnUninitialize		= function() { };
 	this.OnLoading			= function() { };
	this.OnLoaded			= function() { };
	this.OnInteractive		= function() { };
	//this.OnSuccess		= function() { };
	this.OnFailure			= function() { };
	

	//---------------------------
	// Private Event Declarations
	//---------------------------
	function _OnUninitialize() 	{ _this.OnUninitialize(); 	};
	function _OnLoading() 		{ _this.OnLoading(); 		};
	function _OnLoaded() 		{ _this.OnLoaded(); 		};
	function _OnInteractive() 	{ _this.OnInteractive(); 	};
	function _OnSuccess() 		{ _this.OnSuccess(); 		};
	function _OnFailure() 		{ _this.OnFailure(); 		};

	//------------------
	// Private Functions
	//------------------
	function _InitializeRequest()
	{
		_request = _GetRequest();
		_request.onreadystatechange = _StateHandler;
	}

	function _StateHandler()
	{
		switch (_request.readyState)
		{
			case 0:
				window.setTimeout("void(0)", 100);
				_OnUninitialize();
				break;

			case 1:
				window.setTimeout("void(0)", 100);
				_OnLoading();
				break;

			case 2:
				window.setTimeout("void(0)", 100);
				_OnLoaded();
				break;

			case 3:
				window.setTimeout("void(0)", 100);
				_OnInteractive();
				break;

			case 4:
				if (_request.status == 200)
					_OnSuccess();
				else
					_OnFailure();
	
			return;
		break;
		}
	}

	function _GetRequest()
	{
		var obj;

	  	var tryThese = [
   			function () { return new XMLHttpRequest(); },
   			function () { return new ActiveXObject('Msxml2.XMLHTTP'); },
   			function () { return new ActiveXObject('Microsoft.XMLHTTP'); },
   			function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); },
   			function () { alert("Browser does not support XMLHttpRequest");}
   		];

		for (var i = 0; i < tryThese.length; i++) 
   		{
   			var func = tryThese[i];
   			try 
			{
        		obj = func;
				return func();
			}
			catch (e) 
			{
				// pass
			}
		}
		return obj;
   	}
}

ajax_component['generic'] = function()
{
	this.GetData = function(element,data)
	{
		this.ElementID = element;
		this.InitializeRequest('GET', data);
		this.Commit(null);
	}
	this.OnSuccess = function()
	{
		FadeIn(this.ElementID,500);
		window.setTimeout("ajax_component['generic'].Show()",100);
	}
}

