var request;
var destination;
var instances=0;
var objs=[];

function processStateChange() {
	switch (request.readyState) {
		case 0: // uninitialized
		case 1: // loading
		case 2: // loaded
			contentDiv = document.getElementById(destination);
	        if (contentDiv) {
				contentDiv.style.cursor = "wait";
	        }
    	    break;
		
		case 3: // interactive
			break;
			
		case 4: // complete
			contentDiv = document.getElementById(destination);
			if (contentDiv) {
				if (request.status == 200 || request.status == 0) {
					response = request.responseText;
					contentDiv.innerHTML = response;
				} else {
					contentDiv.innerHTML = "<p style=\"color: red;\">Error: Status " + request.status + ", Message: " + request.statusText + "</p>";
				}
				contentDiv.style.cursor = "auto";
			} else {
				window.status = "No Content-DIV found";
			}
			break;
			
		default:
			contentDiv = document.getElementById(destination);		
			if (contentDiv) {
				contentDiv.innerHTML = "<p style=\"color: red;\">Error: Unknown Ready State</p>";
			} else {
				window.status = "No Content-DIV found and unknown Ready State";
			}
			contentDiv.style.cursor = "auto";
			break;
	}
}

function loadHTML(url, dest, data) {
	destination = dest;
	if (window.XMLHttpRequest) {
		// Non-IE browsers
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		// IE browser
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	} else {
		request = new XMLHttpRequestOld();
	}
	
	request.onreadystatechange = processStateChange;
	request.open("POST", url, true);
	
	if (data) {
		request.setRequestHeader("Content-Type", "text/xml");
	}

	request.send(data);
}

function XMLHttpRequestOld() {
	var i = 0;
	var url = '';
	var responseText = '';
	var responseXML = null;
	
	this.onreadystatechange = function() {
		return false;
	}
	
	this.open = function(method, url, b) {
		// TODO: POST methods
		this.i = ++instances; // id number of this request
		this.url = url;
		var iframe = document.createElement("<iframe id=\"Rahn_iframe_" + this.i + "\" style=\"display:none\" type=\"text/plain\"></iframe>");
		document.body.appendChild(iframe);
	}
	
	this.send = function(postdata) {
		// TODO: use the postdata
		var el = document.getElementById('Rahn_iframe_' + this.i);
		el.src = this.url;
		objs[this.i] = this;
		setTimeout('XMLHttpRequestOld_checkState(' + this.i + ')', 500);
	}
	
	return true;
}

function XMLHttpRequestOld_checkState(inst) {
	var el = document.getElementById('Rahn_iframe_' + inst);
	if (el.readyState == 'complete') {
		var responseXML = window.frames['Rahn_iframe_' + inst].document.body.childNodes[0];
		objs[inst].responseXML = responseXML;
		objs[inst].responseText = responseXML.data;
		objs[inst].readyState = 4;
		objs[inst].status = 200;
		objs[inst].onreadystatechange();
		el.parentNode.removeChild(el);
	} else {
		setTimeout('XMLHttpRequestOld_checkState(' + inst + ')', 500);
	}
}
