var _xmlHttp;
var _xmlHttpQueue = Array();
var _xmlSending = 0;
var _xmlTimeoutTimer;

function getXMLHTTP() {
	var A = null;
	try {
		A = new ActiveXObject("Msxml2.XMLHTTP")
	} catch(e) {
	try {
		A = new ActiveXObject("Microsoft.XMLHTTP")
		} catch(oc) {
			A = null;
		}
	}
	if(!A && typeof XMLHttpRequest != "undefined") {
		A = new XMLHttpRequest();
	}

	return A;
}

function addToXMLqueue(path, mode, flags, timeout, timeout_callback, method) {
	var newObj = new Object();
	newObj.path = path;
	newObj.mode = mode;
	newObj.flags = flags;
	newObj.timeout = timeout;
	newObj.timeoutCallback = timeout_callback;
	
	if (!method) {
		method = 'POST';
	}
	
	newObj.method = method;

	_xmlHttpQueue.push(newObj);
}

function runXMLqueue() {
	if (_xmlSending == 0) {
		if (_xmlHttpQueue.length > 0) {
			var queueObj = _xmlHttpQueue.shift();
			getData(queueObj.path, queueObj.mode, queueObj.flags, queueObj.timeout, queueObj.timeoutCallback, queueObj.method);
		}
	}
}

function getData(path, mode, flags, timeout, callback, method) {
	_xmlHttp = getXMLHTTP();
	if(_xmlHttp){
		_xmlSending = 1;
		//_xmlHttp.open("GET", path + '?mode=' + mode + '&' + flags, true);
		_xmlHttp.open(method, path, true);
		if (timeout) {
			if (_xmlTimeoutTimer) {clearTimeout(_xmlTimeoutTimer);}
			_xmlTimeoutTimer = setTimeout('handleXMLtimeout();' + callback, timeout);
		}
		_xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		_xmlHttp.onreadystatechange=function() {
			if (_xmlHttp.readyState==4) {
				if (_xmlHttp.responseText) {
					eval(_xmlHttp.responseText);
				}
				if (_xmlTimeoutTimer) {clearTimeout(_xmlTimeoutTimer);}
				_xmlSending = 0;
				runXMLqueue();
			}
		}
	}

	_xmlHttp.send('mode=' + mode + '&' + flags)
}

function generalHTTPSend(path) {
	_xmlHttp = getXMLHTTP();
	if(_xmlHttp){
		_xmlSending = 1;
		_xmlHttp.open("GET", path, true);
		_xmlHttp.onreadystatechange=function() {
			if (_xmlHttp.readyState==4&&_xmlHttp.responseText) {
				eval(_xmlHttp.responseText);
			}
			_xmlSending = 0;
		}
	}

	_xmlHttp.send(null)
}

function handleXMLtimeout() {
	clearTimeout(_xmlTimeoutTimer);
	
	if (_xmlSending && _xmlHttp) {
		_xmlHttp.abort();
		_xmlSending = 0;		
		runXMLqueue();
	}
}

function clearXMLqueue() {
	clearTimeout(_xmlTimeoutTimer);
	_xmlHttp.abort();
	_xmlSending = 0;
	_xmlHttpQueue = Array();
}
