// For use with pages which don't define the function loginpage.  Only declares the function
// if the function has not already been declared.
if (typeof(loginpage) == 'undefined') {
	loginpage = function(msg) {};
}
var warned = false;
var CATS = new Object();
CATS.READY_STATE_UNINITIALIZED = 0;
CATS.READY_STATE_LOADING = 1;
CATS.READY_STATE_LOADED = 2;
CATS.READY_STATE_INTERACTIVE = 3;
CATS.READY_STATE_COMPLETE = 4;
CATS.RequestMaker = function(url, onload, onerror, post, showProgress, reqTimeout, async) {
	if(async == null){
		this.async = true;
	}else{
		this.async = async;
	}
	if(reqTimeout == null)
		reqTimeout = 120000;
	this.timeout = reqTimeout;
	if(showProgress == null) showProgress = false;
	this.showProgress = showProgress;
	this.url = url;
	this.onload = onload;
	this.onerror = onerror;
	this.post = post;
}
CATS.RequestMaker.prototype = {

	loadXMLDoc:function() {
		// Browser - IE
		if ( window.ActiveXObject ) {
			try { this.req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e)	{
				try { this.req = new ActiveXObject("Microsoft.XMLHTTP"); }
				catch (e) {
				}
			}
		} else if (window.XMLHttpRequest) {
			this.req = new XMLHttpRequest();
		}
		if(!this.req){
			if(!warned)
				alert("Critical functionality for this page needs Active X turned on.\n  Without this you will not get full advantage of this site.");
			warned = true;
			return;
		}
		var loader = this;
		this.req.onreadystatechange = function() {
			loader.onReadyState.call(loader);
		}
		var id = this.requestID;
		if(this.onerror != null)
			this.requestTimer = setTimeout(this.onerror, this.timeout);
		if(this.req != null){
			if(this.post == null){
				this.req.open('GET', this.url, this.async);
				this.req.send("");
			}else{
				this.req.open('POST', this.url, this.async);	
	      		this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.req.send("xml="+encodeURI(this.post));
			}
			if(!this.async){
					this.onReadyState();
			}
		}
	},

	onReadyState:function() {
		var req = this.req;
		var ready = req.readyState;
		// callback after refresh, hitting page before cats init.
		if(typeof CATS == "undefined" || typeof CATS.READY_STATE_COMPLETE == "undefined") return;
		if (ready == CATS.READY_STATE_COMPLETE) {
			clearTimeout(this.requestTimer);
			var status = "";
			try{
			  status = req.statusText;
			    }
			catch(e){
			  status = "Not Ready Yet";
			  return;
			}
			var httpStatus = req.status;
			if ((httpStatus == 200) || (httpStatus == 0)) { 
				if(this.req.responseXML != null){
					var ers = this.req.responseXML.getElementsByTagName("Error");
					if(ers.length > 0) ers = ers[0]; else ers = null;
					if(ers != null){
						var message = ers.attributes.getNamedItem('message').value;
						if(message == "LOGIN"){
							var description = ers.attributes.getNamedItem('description').value;
							loginpage(description);
							return;
						}
						var callMethod = ers.attributes.getNamedItem('continueProcessing');
							if(callMethod.value != null && callMethod.value == "true")
								this.onload.call(this);
						var color;
						if(ers.attributes.getNamedItem('color') != null)
							color = ers.attributes.getNamedItem('color').value;
						displayAjaxError(color, message, 
							(ers.attributes.getNamedItem('bold') != null && 
								ers.attributes.getNamedItem('bold').value == "true"))
						if(!callMethod && this.onerror != null)
							this.onerror.call();
						this.req = null;
						return;
					}
				}
				if(this.onload != null)
					this.onload.call(this);
				this.req = null;
				return;
			}
			if(this.onerror != null){
				this.onerror.call();
			}
			this.req = null;
		}
	}
}
function hideMessage(){
	var statusBox = document.getElementById("StatusMessageTable");
	var statusRow = document.getElementById("StatusRow");
	if(statusBox != null)
		statusBox.style.display = 'none';
	if(statusRow != null)
		statusRow.style.display = 'none';
}
function displayAjaxError(color, message, bold){
	if(color == null)
		color = "black";
	var statusBox = document.getElementById("StatusMessageTable");
	var statusRow = document.getElementById("StatusRow");
	if(statusBox != null){
		statusBox.style.display = '';
		if(statusRow != null)
			statusRow.style.display = '';
			setTimeout(hideMessage, 10000);
		color = "<font color='"+color+"'>";
		message = color + message+"</font>";
		if(bold)
			message = "<b>"+message+"</b>";
		statusBox.innerHTML = message;
		return this.statusBox != null;
	}
}