﻿var xmlreqs = new Array();

function CXMLReq(freed) {
	this.freed = freed;
	this.xmlhttp = false;
	if (window.XMLHttpRequest) {
		this.xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function load(url,target,callback) {
	var pos = -1;
	for (var i=0; i<xmlreqs.length; i++) {
		if (xmlreqs[i].freed == 1) { pos = i; break; }
	}
	if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new CXMLReq(1); }
	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		xmlreqs[pos].target = target;
		xmlreqs[pos].callback = callback;
		xmlreqs[pos].xmlhttp.open("GET",url,true);
		xmlreqs[pos].xmlhttp.onreadystatechange = function() {
			if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
		}
		if (window.XMLHttpRequest) {
			xmlreqs[pos].xmlhttp.send(null);
		} else if (window.ActiveXObject) {
			xmlreqs[pos].xmlhttp.send();
		}
	}
}

function xmlreqPOST(url,data) {
	var pos = -1;
	for (var i=0; i<xmlreqs.length; i++) {
		if (xmlreqs[i].freed == 1) { pos = i; break; }
	}
	if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new CXMLReq(1); }
	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		xmlreqs[pos].xmlhttp.open("POST",url,true);
		xmlreqs[pos].xmlhttp.onreadystatechange = function() {
			if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
		}
		xmlreqs[pos].xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlreqs[pos].xmlhttp.send(data);
	}
}

function xmlhttpChange(pos) {
	if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4) {
		if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) {
			target = xmlreqs[pos].target;
			callback = xmlreqs[pos].callback;

			if (document.getElementById(target) && target != undefined && target != '') {
				document.getElementById(target).innerHTML = xmlreqs[pos].xmlhttp.responseText;
			}
			
			if (callback != undefined && callback != '') {
				callback(xmlreqs[pos].xmlhttp.responseText);
			}
		} else {
			//document.getElementById(target).innerHTML = 'There was an error loading the content.  Pleas try again.';
		}
		xmlreqs[pos].freed = 1;
	}
}

function ajaxLoaderCreate() {
	if (document.getElementById('loaderDiv')) {
		fBody = '<table width="100%" cellpadding="0" cellspacing="0" height="100%"><tr><td align="center"><div><table cellpadding="0" cellspacing="0"><tr><td width="13" height="34"><img src="/net/images/boxify3/tl.png" width="13" height="34"></td><td background="/net/images/boxify3/t.png"><div id="loaderContenttitle" style="float: left; padding-left: 6px; padding-top: 4px; font-family: Arial; font-size: 12px; color: rgb(73, 73, 73); font-weight: bold;">Loading Content...</div><div style="float: right; font-size: 18px; color: rgb(73, 73, 73);"></div></td><td width="13" height="34"><img src="/net/images/boxify3/tr.png" width="13" height="34"></td></tr><tr><td width="13" background="/net/images/boxify3/l.png"><img src="/net/images/spacer.gif" width="1" height="1"></td><td id="loaderContentholder" style="padding-left: 5px; padding-top: 10px; padding-bottom: 10px; background-color: rgb(255, 255, 255);"><div id="loaderContent" style="text-align: center;"><img src="/net/images/processing.gif"> </div></td><td width="13" background="/net/images/boxify3/r.png"><img src="/net/images/spacer.gif" width="1" height="1"></td></tr><tr><td width="13" height="21"><img src="/net/images/boxify3/bl.png" width="13" height="21"></td><td background="/net/images/boxify3/b.png"><img src="/net/images/spacer.gif" width="1" height="1"></td><td width="13" height="21"><img src="/net/images/boxify3/br.png" width="13" height="21"></td></tr></table></div></td></tr></table>';
		newDiv = document.getElementById('loaderDiv');
		newDiv.innerHTML = fBody;
		newDiv.style.position = 'absolute';
		newDiv.style.width = '100%';
		newDiv.style.height = '100%';
		newDiv.style.zIndex = '10';
		newDiv.style.top = '0px';
		newDiv.style.left = '0px';
		newDiv.id = 'loaderDiv';
		newDiv.style.display = 'none';
	}
}

function ajaxLoaderOn() {
	if (document.getElementById('loaderDiv')) {
		document.getElementById('loaderDiv').style.display = 'block';
	}
}

function ajaxLoaderOff() {
	if (document.getElementById('loaderDiv')) {
		document.getElementById('loaderDiv').style.display = 'none';
	}
}
