// JavaScript Document
function getHTTPObject() {
	var xmlhttp;
	var browser=	 navigator.appName;
	if ( browser == "Microsoft Internet Explorer" ) {
		xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		xmlhttp= new XMLHttpRequest();
		xmlhttp.overrideMimeType('text/xml');
	}
	return xmlhttp;
}

var http = getHTTPObject();  //We create the HTTP Object

function doAJAX(param, callback, url, loadscreen) {
	if((url == null) || (url == false)) { url = "auto.php"; }
	if(loadscreen == null) { loadscreen = false; }
	param = param + '&xml=true';
	http.open("POST", url, true);
	http.onreadystatechange = function () { handleAJAX(callback, loadscreen); };
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(param);
}

function handleAJAX(callback, loadscreen) {
	var height = document.body.offsetHeight;
	var width =  document.body.offsetWidth;
	var overlay = document.getElementById('ajax_overlay');
	
	if ( http.readyState == 4 ) {
		if(loadscreen) {
			//hide the loading screen
			overlay.style.display = "none";
			document.getElementById('ajax_loading_div').style.display = "none";
		}
		
		//console.log(http.responseText);
		//alert(http.responseText);
		var xml = http.responseXML.getElementsByTagName('result')[0];
		//console.log(xml);
		if(callback == null) {
			//deprecated code for old badly written ajax callbacks.
			var action = http.responseXML.getElementsByTagName('result')[0].getAttribute("action");
			switch(action) {

			}
		} else {
			callback(xml);
		}
	} else {
		if(loadscreen) {
			//show the loading screen.
			overlay.style.height = height + 'px';
			overlay.style.width = width + 'px';
			overlay.style.display = 'block';
			document.getElementById('ajax_loading_div').style.display = "block";
			document.getElementById('ajax_loading_div').style.margin = "0 0 0 100px";
		}
	}
}
