// Written by Ian Henderson in 2005.  Copy and modify freely, as long as this line is present and unmodified.

function safeEscape(string)
{
	if (encodeURIComponent) return encodeURIComponent(string);
	if (escape) return escape(string);
}

var xmlHttpLoading = false;
var xmlHttpRequest = null;
var xmlHttpRequestString = "";
var xmlHttpCallback = null;
var xmlHttpCallbackArgument = null;
var pendingRequests = [];

function dequeueHttpRequest()
{
	if (loading()) {
		window.setTimeout("dequeueHttpRequest()", 500);
		return;
	}
	if (pendingRequests.length <= 0) {
		return;
	}
	var request = pendingRequests[0];
	pendingRequests.splice(0, 1);
	xmlHttpRequestPage(request.page, request.args, request.method, request.callbackFunctionName, request.callbackArgument);
}

function queueHttpRequest(page, args, method, callbackFunctionName, callbackArgument)
{
	pendingRequests.push({page:page, args:args, method:method, callbackFunctionName:callbackFunctionName, callbackArgument:callbackArgument});
}

function dropQueuedHttpRequests()
{
	pendingRequests = [];
	xmlHttpRequest.abort();
	xmlHttpCallback = false;
	setLoading(false);
}

function setLoading(loading)
{
	xmlHttpLoading = loading;
}

function loading()
{
	return xmlHttpLoading;
}

function createXMLHttpRequest()
{
	var newRequest = null;
	try {
		newRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (error) {
		try {
			newRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (anotherError) {
			newRequest = null;
		}
	}
	if (!newRequest && XMLHttpRequest) {
		newRequest = new XMLHttpRequest();
	}
	return newRequest;
}


function dataReceived()
{
	if (!xmlHttpRequest) {
		return;
	}
	if (xmlHttpRequest.readyState == 4) {
		setLoading(false);
		if (xmlHttpRequest.status == 200) {
			if (xmlHttpCallback) {
				xmlHttpCallback(xmlHttpRequest.responseText, xmlHttpCallbackArgument);
			}
		} else {
			alert("A problem occurred while processing request for '" + xmlHttpRequestString + "':\n" + xmlHttpRequest.status + " " + xmlHttpRequest.statusText);
		}
		dequeueHttpRequest();
	}
}

function argumentsToString(args)
{
	var string = "";
	for (i in args) {
		if (string != "") {
			string += "&";
		}
		string = string + safeEscape(i) + "=" + safeEscape(args[i]);
	}
	return string;
}


function xmlHttpRequestPage(page, args, method, callbackFunctionName, callbackArgument)
{
	if (loading() == true) {
		queueHttpRequest(page, args, method, callbackFunctionName, callbackArgument);
		return;
	}
	setLoading(true);
	xmlHttpCallback = eval(callbackFunctionName);
	xmlHttpCallbackArgument = callbackArgument;
	xmlHttpRequest = createXMLHttpRequest();
	xmlHttpRequest.onreadystatechange = dataReceived;
	if (method == "GET" || method == "get") {
		page += "?" + argumentsToString(args);
	}
	xmlHttpRequestString = method + " " + page;
	xmlHttpRequest.open(method, page, true);
	if (method == "POST" || method == "post") {
		xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttpRequest.send(argumentsToString(args));
	} else {
		xmlHttpRequest.send(null);
	}
}

