var http_request = false;
var http_newstr = "";
var http_func = null;
var http_fail = null;
var http_array = [];
var http_sending = 0;

function ajaxReset() {
	if (http_newstr.length > 0) { alert("Developer Warning: Resetting non-empty AJAX parameter string.  Check for unsubmitted ajax query.  String='"+http_newstr+"'."); }
	http_newstr = "";
}

function ajaxArg(name, value) {
	if (http_newstr.length > 0) { http_newstr += "&"; }
	if (name.length <= 0 || value.length <= 0) { alert("Developer Warning.  Your ajax arg or value is empty.  string='"+http_newstr+"'"); }

	http_newstr += encodeURIComponent(name);
	http_newstr += '=';
	http_newstr += encodeURIComponent(value);
}

function ajaxRequest(url, func, failfunc) {
	if (http_newstr.length == 0) { alert("Developer Warning: AJAX parameter string is empty."); }
	if (!func) { alert("Developer Warning: You havent passed in the name of the callback function."); }
	else {
		if (http_array.length == 0 && http_sending == 0) {
			ajaxSend(url, http_newstr, func, failfunc);
		}
		else {
			var i = http_array.length;
			http_array[i] = [];
			http_array[i][0] = url;
			http_array[i][1] = http_newstr;
			http_array[i][2] = func;
			http_array[i][3] = failfunc;
		}
		http_newstr = "";
	}
}

function ajaxDebugCheck() {
	if (http_array.length > 10) {
		alert("Ajax queue is " + http_array.length + ".  The server might not be responding.");
	}
}

function ajaxSend (url, str, func, failfunc) {

	if (http_request != false) {
		alert("Site error.  please report this problem to an administrator, and quote this value [AJX-4599].  Thankyou.");
	} else {
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject) { // IE
			try { http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try { http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if (!http_request) {
			alert('Cannot create XMLHTTP instance.   This web-application requires AJAX functionality, please upgrade your browser if possible.');
			return false;
		}

		http_sending ++;
		http_func = func;
		http_fail = failfunc;
		http_request.onreadystatechange = ajaxReturn;
		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Connection",   "close");
		http_request.send(str);
	}
}

function ajaxReturn() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			if (http_func != null) {
				xml = http_request.responseXML.documentElement;
				var func = http_func;
				setTimeout(function() {func(xml);}, 0);
			}
			else {
				alert("Ajax developer warning.  What is our callback function?  http_func is empty.");
			}
			http_func = null;
			http_fail = null;
			ajaxDone();
		}
		else {
			if (http_fail != null) {
				var func = http_fail;
				var code = http_request.status;
				setTimeout(function() {func(code);}, 0);
			}
			http_func = null;
			http_fail = null;
			ajaxDone();
		}
	}
}

function ajaxDone () {
	http_request = false;

	if (http_sending == 0) { alert("Ajax Developer Warning.  Why is http_sending == 0?"); }
	http_sending--;

	if (http_func != null) {
		alert("Ajax Developer Warning.  http_func should not have a value at this point");
	}

	if (http_array.length > 0) {
		var url = http_array[0][0];
		var str = http_array[0][1];
		var func = http_array[0][2];
		var failfunc = http_array[0][3];
		http_array.splice(0,1);
		ajaxSend(url, str, func, failfunc);
	}
}

function xmlData(xml, id) {
	var x = xml.getElementsByTagName(id);
	var l = x.length;
	if (l > 0) {
		if (x[0].firstChild != null) { return(x[0].firstChild.data); }
		else { return(""); }
	} else { return (""); }
}

// from http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/
function getElementsByClassName(strClass, strTag, objContElm) {
	strTag = strTag || "*";
	objContElm = objContElm || document;
	var objColl = objContElm.getElementsByTagName(strTag);
	if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
	var arr = new Array();
	var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
	var arrClass = strClass.split(delim);
	for (var i = 0, j = objColl.length; i < j; i++) {
		var arrObjClass = objColl[i].className.split(' ');
		if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
		var c = 0;
comparisonLoop:
		for (var k = 0, l = arrObjClass.length; k < l; k++) {
			for (var m = 0, n = arrClass.length; m < n; m++) {
				if (arrClass[m] == arrObjClass[k]) c++;
				if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
					arr.push(objColl[i]);
					break comparisonLoop;
				}
			}
		}
	}
	return arr;
}
// To cover IE 5.0's lack of the push method
Array.prototype.push = function(value) {
  this[this.length] = value;
}

function show (id) {
	var vv = document.getElementById(id);
	if (vv != null) { vv.style.display = 'block'; }
}
function hide (id) {
	var vv = document.getElementById(id);
	if (vv != null) { vv.style.display = 'none'; }
}

function LoadImage(id, s) {
var img = document.getElementById(id);
if (img == null) { alert("Element " + id + " doesnt seem to exist."); }
else { img.src = s; }
}

function dhtmlLoadScript(url) {
var e = document.createElement("script");
e.src = url;
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
}

function getCookie( name ) {
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
return null;
}
if ( start == -1 ) {
return null;
}
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) {
end = document.cookie.length;
}
return unescape( document.cookie.substring( len, end ) );
}

function setCookie (name, value, expires, domain) {
expdate = new Date();
if (expires) expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * expires));
var curCookie = name + "=" + escape(value) + "; expires="+expdate.toGMTString()+"; path=/" +
      ((domain) ? "; domain=" + domain : "");
document.cookie = curCookie;
}
