// page = page to call
// box = id to replace with returned text
// parameters = eg. var1=10&var2=20 - this format for GET and POST calls
// method = GET or POST
// loadmessage = html to display while loading, if unset nothing happens
// call = function to call on completion
function ajax(page, box, parameters, method, loadmessage, call, callparas) {  
	ajax_responsetext="";
	// set defauls
	var silent=false; // default
	if ((typeof box == "undefined") || (typeof box == "")) silent = true; 	// if box isn't set nothing will be displayed
	if ((typeof method == "undefined") || (typeof method == "")) method = "POST";	// set default
	if (typeof callparas == "undefined") callparas = "";			// set default
	if (typeof loadmessage == "undefined") loadmessage = "";		// set default

	function new_request() {
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
				}
			}
		}
	}
	
	if (!document.getElementById(box)){
		getbox="";
	}else{
	var getbox = document.getElementById(box);
	}

	var xmlhttp = new_request();
	// xmlhttp.overrideMimeType('text/html'); // kills IE, how rude
	var page = escape(page);
	if(page !== "") {
		if (method=="POST"){
			var url = page + '?lfghnv=' + Math.random();
			xmlhttp.open("POST", url, true);
		    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    xmlhttp.setRequestHeader("Content-length", parameters.length);
		    xmlhttp.setRequestHeader("Connection", "close");  
		    xmlhttp.send(parameters);
		} else {
			if (parameters==""){
				var url = page + '?jhdfbhg=' + Math.random();
			} else {
				var url = page + '?' + parameters + '&jhdfbhg=' + Math.random();
			}
			xmlhttp.open('GET', url, true);
			xmlhttp.setRequestHeader("Content-Type", "text/html; Charset=ISO_8859-1" );
			xmlhttp.send(null);
		}

		if(loadmessage != "") getbox.innerHTML = loadmessage;
		xmlhttp.onreadystatechange = function(){
			// xmlhttp.overrideMimeType('text/html; charset=ISO_8859-1'); // set char set so we dont hav probs with £ etc - breaks IE
			if(xmlhttp.readyState == 4 || xmlhttp.readyState=="complete"){
				if(xmlhttp.status == 200){
					
					// alert("Page: "+page);
					// alert("Box: "+box);
					// alert("Parameters: "+parameters);
					// alert("Method: "+method);
					// alert("Loadmessage: "+loadmessage);
					// alert("Call: "+call);
					// alert("Callparas: "+callparas);
					
					if (getbox){
						if(silent==false) getbox.innerHTML = xmlhttp.responseText;
					}
					
					ajax_responsetext = xmlhttp.responseText;
					
					if ((call!="") && (call!=undefined)){ // call external function now completed
						call( callparas );
					}
				} else {
					if (getbox){
						if(silent==false) getbox.innerHTML = 'Error Fetching Page';
					}
					//alert(url);
				}
			}
		}

	} else if(page == "") {
		if(silent==false) getbox.innerHTML = 'Error: No Page Specified';
	}
	
}