<!--

/*/-------------------------------------------------------/#/
|| Cross-browser Ajax Class (Ajax)
|| --------------------
||
|| Copyright © 2006 Stephan Massin. All Rights Reserved.
|| Released under Velegant, LLC. 
|| http://www.velegant.com
|| ...Work in progress....
/#/-------------------------------------------------------/*/

/*/-------------------------------------------------------/#/
|| Notes
|| -----
|| 1. The readyStateHandler must be set after open() it is
||    intended to be reused.
/#/-------------------------------------------------------/*/

Ajax = function()
{
}

Ajax.prototype = {

	xmlHttpRequest : null,
	busy : false,
	readyStateHandler : null,
	loadStateHandler : null,

	XmlHttpRequest : new function()
	{
		try { 
			if(new ActiveXObject("Msxml2.XMLHTTP")) return function() { return new ActiveXObject("Msxml2.XMLHTTP"); } } catch (e) {
			try	{ if(new ActiveXObject("Microsoft.XMLHTTP")) return function() { return new ActiveXObject("Microsoft.XMLHTTP"); } } catch (e) {
				try	{ if(new XMLHttpRequest()) return function() { return new XMLHttpRequest(); } } catch (e) {
					return null;
				}
			}
		}
	},

	request : function(method, url, async, user, pass, content) {
		this.open(method, url, async, user, pass);
		this.xmlHttpRequest.send(content);
	},

	onreadystatechange : function()
	{
		this.readystatechange();
		if(this.xmlHttpRequest.readyState == 4)
		{
			try
			{
				if(this.xmlHttpRequest.status == 200)
					this.loadstatechange();
				this.busy = false;
			}
			catch (e)
			{
			}
		}
	},

	
	// Custom Events
	readystatechange : function() { if(typeof this.readyStateHandler == 'function') this.readyStateHandler(new AjaxState(this.xmlHttpRequest)); },
	loadstatechange : function() { if(typeof this.loadStateHandler == 'function') this.loadStateHandler(new AjaxResponse(this.xmlHttpRequest)); },

	// Handler Setters
	setReadyStateHandler : function(func) { this.readyStateHandler = func; },
	setLoadStateHandler : function(func) { this.loadStateHandler = func; },

	// Wrapped client functions
	open : function(method, url, async, user, pass)
	{
		if(this.xmlHttpRequest)
			this.xmlHttpRequest.abort();
		
		this.xmlHttpRequest = this.XmlHttpRequest();/*new function()
		{
			try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
				try	{ return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {
					try	{ return new XMLHttpRequest(); } catch (e) {
						return null;
					}
				}
			}
		}*/

		var ajax = this;	// So that is can be added to the onreadystatechange function
		//this.xmlHttpRequest.onreadystatechange = function(){ajax.onreadystatechange()};
		this.busy = true;
		this.xmlHttpRequest.open(method, url, async, user, pass);
		this.xmlHttpRequest.onreadystatechange = function(){ ajax.onreadystatechange(); };
	},
	abort : function() { this.xmlHttpRequest.abort(); },
	send : function(content) { this.xmlHttpRequest.send(content); },
	setRequestHeader : function(name,value) { this.xmlHttpRequest.setRequestHeader(name,value); },
	getAllResponseHeaders : function() { return this.xmlHttpRequest.getAllResponseHeaders(); },
	getResponseHeader : function(name) { return this.xmlHttpRequest.getResponseHeader(name); }
};

AjaxState = function(xmlHttpRequest)
{
	this.readyState = xmlHttpRequest.readyState;
}

AjaxState.prototype = {
	readyState : null
};

AjaxResponse = function(xmlHttpRequest)
{
	this.status = xmlHttpRequest.status;
	this.statusText = xmlHttpRequest.statusText;
	this.responseText = xmlHttpRequest.responseText;
	this.responseXML = xmlHttpRequest.responseXML;
	this.responseBody = xmlHttpRequest.responseBody;
}

AjaxResponse.prototype = {
	status : null,
	statusText : null,
	responseText : null,
	responseXML : null,
	responseBody : null
}

//-->