//
// Generic Ajax Handler
// @author DJ Squires :)
//
function Ajax_Handler (_newUrl, _newMethod)
{

	//Configuration.
	this.url           = null;
	this.method        = 'POST';
	this.requestData   = new Array();
	
	//On-action functions. These do things when other things do things.
	this.onStart       = null;
	this.onLoading     = null;
	this.onLoaded      = null;
	this.onInteractive = null;
	this.onCompletion  = null;
	this.onError       = null;
	this.onCreateFail  = null;
	this.onFail        = null;
	
	//Internal config. Seriously, don't touch this.
	this.xmlHttp       = null;
	this.queryPrefix   = '?';
	this.argSeparator  = '&';
	this.requestString = null; //Contains the entire request for reusage.
	this.encodeString  = true;
	this.status        = new Array(null, null);
	this.thread        = null;
	this.responseText  = null;
	this.responseXML   = null;

	var self           = null; //MSIE wants this. var (not this) required.

	/******************
	Set requested url
	******************/
	this.setUrl = function (url)
	{
		this.url = url;	
	}
	
	/******************
	Set method type, POST and GET valid
	******************/
	this.setMethod = function (method)
	{
		if (method == null)
		{
			return;
		}

		var method = method.toUpperCase();
		
		if (method == 'POST' || method == 'GET')
		{
			this.method = method;	
		}
	}
	
	/*****************
	Create ajax object
	*****************/
	this._createXmlHttp = function ()
	{
		//Try IE first
		if (window.ActiveXObject)
		{
			try
			{
				this.xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
			}
			catch (e)
			{
				try
				{
					this.xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
				}
				catch (e2)
				{
					//Pass
				}
			}
		}
		//Now try Mozilla
		else if (window.XMLHttpRequest)
		{
			try
			{
				this.xmlHttp = new XMLHttpRequest();
			}
			catch (e)
			{
				//Pass
			}
		}

		if (this.xmlHttp == null)
		{
			this.onCreateFail();	
		}
	}

	/******************
	Handle variables
	******************/

	this.addRequestVar = function (name, value)
	{
		this.requestData.push(new Array(name, value));
	}

	this.clearRequestVars = function ()
	{
		this.requestData = new Array();
	}
	
	/******************
	Run this thing
	******************/
	
	this.run = function ()
	{
		this.onStart();
		this._createXmlHttp();

		if (this.xmlHttp == null)
		{
			this.onFail();
			return false;
		}
		else
		{

			//We start our magic here
			self = this;
			
			this.xmlHttp.onreadystatechange = this._readyState;

			if (this.method == 'GET')
			{
				this.xmlHttp.open(this.method, this._buildUrl() + this.queryPrefix + this._buildQuery(), true);
				this.xmlHttp.send(null);
			}
			else if (this.method == 'POST')
			{
				this.xmlHttp.open(this.method, this._buildUrl(), true);
				this.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				this.xmlHttp.send(this._buildQuery());
			}
		}	
	}
	
	/******************
	Ready state for xmlHttp
	******************/
	
	this._readyState = function ()
	{
		switch (self.xmlHttp.readyState)
		{
			case 1:
				self.onLoading();
				break;
		
			case 2:
				self.onLoaded();
				break;
				
			case 3:
				self.onInteractive();
				break;
				
			case 4:
				self.responseText = self.xmlHttp.responseText;
				self.responseXML  = self.xmlHttp.responseXML;
                
				try
				{
					self.status = new Array(self.xmlHttp.status, self.xmlHttp.statusText);
				}
				catch (e)
				{
					//Pass
				}
				
				if (self.status[0] == '200')
				{
					self.onCompletion();
				}
				else
				{
					self.onError();
				}
				break;
		}
	}

	/******************
	Build url string
	******************/
	this._buildUrl = function ()
	{
		return this.url;
	}

	/******************
	Build query string
	******************/

	this._buildQuery = function ()
	{
		var requestQuery = '';

		if (this.requestData.length > 0)
		{
			var parts = new Array();
			for (key in this.requestData)
			{
				parts.push(this.requestData[key][0] + '=' + this.requestData[key][1]);
			}
			requestQuery += parts.join(this.argSeparator);		
		}

		return requestQuery;
	}
	
	/******************
	Initialize on-action handlers.
	******************/

	this._resetFunctions = function ()
	{
		this.onStart       = function () {}
		this.onLoading     = function () {}
		this.onLoaded      = function () {}
		this.onInteractive = function () {}
		this.onCompletion  = function () {}
		this.onError       = function () {}
		this.onCreateFail  = function () {}
		this.onFail        = function () {}
	}
	
	
	/*********************
	Setup url / method
	*********************/
	this._init = function (newUrl, newMethod)
	{
		this._resetFunctions();
		this.setUrl(newUrl);
		this.setMethod(newMethod);
	}

	/*********************
	Constructor(s)
	*********************/
	this._init(_newUrl, _newMethod);

}
