
function Ajax()
{
	this.mReqURL 		= "";
	this.mQryString 	= "";
	this.mMethod 		= "POST";
	this.mCompletion 	= "";
	this.mTargetElement = "";
	this.mTargetObj 	= "";
	this.mCompletion    = "";
	this.mLoading		= "";
	this.mLoaded		= "";
	this.mInteractive   = "";
	this.mCallMe		= "";
	this.mIsIE 			= (document.all && navigator.userAgent.search(/MSIE/i) != -1)|| false;
	//	-----------------------------------------------------------------------------------------
	//	Cretae a HTTP request.
	//	-----------------------------------------------------------------------------------------
	this.CreateRequest = function() 
	{
		try 
		{
			this.HttpObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) 
		{
			try 
			{
				this.HttpObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err) 
			{
				this.HttpObj = null;
			}
		}
		if(!this.HttpObj && typeof XMLHttpRequest != "undefined")
			this.HttpObj = new XMLHttpRequest();
		if (!this.HttpObj)
		{
			this.failed = true; 
		}
	};
	//	-----------------------------------------------------------------------------------------
	//	To check given variable is decleared or not.
	//	-----------------------------------------------------------------------------------------
	this.IsSet = function(pmString)
	{
		if(pmString == "" || pmString == "undefined")
			return 0;
		else
			return 1;
	};
	//	-----------------------------------------------------------------------------------------
	//	To call a user defined function on the time of request loading.
	//	-----------------------------------------------------------------------------------------
	this.Loading = function() 
	{
		vStatus = this.IsSet(this.mLoading);
		if(vStatus == 1)
		{
			eval(this.mLoading);
		}	
	};
	//	-----------------------------------------------------------------------------------------
	//	To call a user defined function on the time of request loaded.
	//	-----------------------------------------------------------------------------------------
	this.Loaded = function() 
	{ 
		vStatus = this.IsSet(this.mLoaded);
		if(vStatus == 1)
		{
			eval(this.mLoaded);
		}
	};
	//	-----------------------------------------------------------------------------------------
	//	To call a user defined function on the time of request interactive state.
	//	-----------------------------------------------------------------------------------------
	this.Interactive = function() 
	{ 
		vStatus = this.IsSet(this.mInteractive);
		if(vStatus == 1)
		{
			eval(this.mInteractive);
		}
	};
	//	-----------------------------------------------------------------------------------------
	//	To call a user defined function on the time of request completed.
	//	-----------------------------------------------------------------------------------------
	this.Completion = function() 
	{ 
		vStatus = this.IsSet(this.mCompletion);
		if(vStatus == 1)
		{
			eval(this.mCompletion);
		}
	};
	//	-----------------------------------------------------------------------------------------
	//	To diaplay the response.
	//	-----------------------------------------------------------------------------------------
	this.ShowResponse = function(me)
	{
		if(this.mCallMe != "")
			eval(this.mCallMe);
		else
		{
			var vTargetNode = me.mTargetObj.nodeName;
			vTargetNode.toLowerCase();
			if (vTargetNode == "input" || vTargetNode == "select" || vTargetNode == "option" || vTargetNode == "textarea")
				me.mTargetObj.value = me.mResponse;
			else 
				me.mTargetObj.innerHTML = me.mResponse;
		}
	};
	//	-----------------------------------------------------------------------------------------
	//	To display URL not found error.
	//	-----------------------------------------------------------------------------------------
	this.ShowNotFoundError = function()
	{
		//alert("Requseted URL is not found.");
		var er_ = '';
	};
	//	-----------------------------------------------------------------------------------------
	//	To display the UNKNOWN error.
	//	-----------------------------------------------------------------------------------------
	this.ShowUnknownError = function()
	{
		//alert("Unknown Error.");
		var er_ = '';
	};
	//	-----------------------------------------------------------------------------------------
	//	To send the HTTP request to given URL.
	//	-----------------------------------------------------------------------------------------	
	this.SendRequest = function(pmURL)
	{
		this.mResponseStatus = new Array(2);
		if (this.mTargetElement) 
		{ 
			    if(typeof(this.mTargetElement) != "object" ) 
                     this.mTargetObj = document.getElementById(this.mTargetElement); 
                else
                     this.mTargetObj = this.mTargetElement
		}
		if (this.HttpObj) 
		{
			var me = this;
			if (this.mMethod == "GET") 
			{
				var vGetURL = this.mReqURL + "?" + this.mQryString;
				this.HttpObj.open(this.mMethod, vGetURL, true);
			} 
			else 
			{
				this.HttpObj.open(this.mMethod, this.mReqURL, true);
			}
			if (this.mMethod == "POST")
			{
					this.HttpObj.setRequestHeader('Content-Type','text/html');  
			}
			//this.HttpObj.setRequestHeader('Content-Length','522820'); 
			//this.HttpObj.setRequestHeader('Accept-Ranges','bytes');
			this.HttpObj.send(this.mQryString);
			this.HttpObj.onreadystatechange = function() 
			{
				me.ReadyState = me.HttpObj.readyState
				switch (me.HttpObj.readyState)
				{
					case 1:
						me.Loading();	//	ready state 1, Object is loading its data.
						break;
					case 2:
						me.Loaded();	//	ready state 2, Object has finished loading its data.
						break;
					case 3:
						me.Interactive(); // ready satate 3, User can interact with the object even though it is not fully loaded.
						break;
					case 4:
						//	ready sate 4, response loaded completely in to client.
						me.mResponse 		= me.HttpObj.responseText;
						me.mResponseXML 	= me.HttpObj.responseXML;
						me.mResponseHeader  = me.HttpObj.getAllResponseHeaders();
						me.Completion();
						//	401: Unauthorized, 403: Forbidden(Access denied)
						if(me.HttpObj.status == 200 && me.HttpObj.statusText.toUpperCase() == "OK")	
							me.ShowResponse(me);
						else if(me.HttpObj.status == 404 && me.HttpObj.statusText.toUpperCase() == "NOT FOUND")
							me.ShowNotFoundError();
						else
							me.ShowUnknownError();
						me.mQryString = "";
						break;
				}
			};
		}
	};
	this.CreateRequest();
}
