
var _ajaxObjects	= new Array();

function ajaxClass (callback)
{
	this.callback		= callback;
	this.busy		= false;
	this.killed		= false;
	this.request		= _ajax_request;
	this.kill		= _ajax_kill;
	this.createObject	= _ajax_createObject;
	this.onAjaxResponse	= _ajax_onAjaxResponse;
	this.pos		= -1;
	this.createObject();
}

function _ajax_request (url, postData)
{
	if (this.busy) {
		return false;
	}
	this.busy = true;
	this.object.open("GET", url, true);  
	this.object.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	this.object.onreadystatechange = _ajax_onAjaxResponse;
	this.object.send (postData);
}

function _ajax_kill ()
{
	this.killed = true;
}

function _ajax_onAjaxResponse ()
{
	for (var i=0; i<_ajaxObjects.length; i++) {
		var obj = _ajaxObjects [i];
		if (obj == null) continue;
		if (obj.busy == false) continue;
		if (obj.object.readyState != 4) continue;
		obj.busy = false;
		if (obj.object.status != 200) {
			if (obj.callback) obj.callback (null);
			continue;
		}
		var text = obj.object.responseText;
		if (!obj.killed) {
			if (obj.callback) obj.callback (text);
		}
	}
}

function _ajax_createObject()
{  
	var	xmlHttp;
	var	n = _ajaxObjects.length;
	var p = -1;
	for (var i=0; i<n; i++) {
		if (_ajaxObjects [i] == null) {
			p = i; break;
		}
	}
	if (p == -1) {
		p = n;
	}

	try {
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		var xmlHttpVersions = new Array ("MSXML2.XMLHTTP.6.0",
										 "MSXML2.XMLHTTP.5.0",
										 "MSXML2.XMLHTTP.4.0",
										 "MSXML2.XMLHTTP.3.0",
										 "MSXML2.XMLHTTP",
										 "Microsoft.XMLHTTP");
		for (var i=0; i < xmlHttpVersions.length && !xmlHttp; i++) {
			try {
				xmlHttp = new ActiveXObject (xmlHttpVersions [i]);
			}
			catch (e) { }
		}
	}

	if (!xmlHttp) {
		return false;
	}

	this.pos = p;
	this.object = xmlHttp;
	_ajaxObjects [p] = this;

	return true;
}

