// Copyright(c)2005 Daniel Pupius - http://pupius.co.uk/
function XMLHttpHandler() {
	var _this = this;
	var _xmlobj;
	var _busy = false;
	var _queue = new Array();

	var _activeRequest = "";
	var _responseText = "";

	this.send = function(url) {
		if(!_busy) {
			
			this.onsend({ url: url,
				      time: new Date(),
				      content: ""});

			_busy = true;
			_activeRequest = url;
			_responseText = "";

			if(url.indexOf("?")>=0) url += "&" + new Date().valueOf();
			else url += "?" + new Date().valueOf();

			if(typeof XMLHttpRequest == "undefined") _xmlobj = new ActiveXObject("Microsoft.XMLHTTP");
			else _xmlobj = new XMLHttpRequest();
			_xmlobj.onreadystatechange = this.recieve;
			_xmlobj.open("GET", url, true);
			_xmlobj.send(null);
		} else {
			_queue.push(url);
		}
	}

	this.recieve = function() {
		if (_xmlobj.readyState == 4) {

			_this.onrecieve({ url: _activeRequest,
					  time: new Date(),
					  content: _xmlobj.responseText});

			_busy = false;
			_activeRequest = "";
			_responseText = _xmlobj.responseText;

			if(_queue.length>0) _this.send(_queue.shift());
		}
	}

	this.getActiveRequest = function() { return _activeRequest; }
	this.getResponseText = function() { return _responseText; }
	this.getQueueLength = function() { return _queue.length; }
	this.isBusy = function() { return _busy; }

	this.onsend = function(e) { }
	this.onrecieve = function(e) { }
}