
function CreateRequestObject() {
	if (navigator.appName == "Microsoft Internet Explorer") {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return new XMLHttpRequest();
	}
}

var http = CreateRequestObject();
var errorelement = "";

function ExBlock(obj) {
	i = obj.s.indexOf(":");
	if ((i == -1) || (i > 5)) {
		obj.errno = 1;
		return;
	}
	x = obj.s.substring(0,i);
	l = parseInt(x);
	obj.bk = obj.s.substring(i+1,i+l+1);
	obj.s = obj.s.substring(i+l+1,obj.s.length);
}

function bker(s) {
	this.s = s;
	this.bk = "";
	this.errno = 0;
}

function AxRequest(sURL,errelement) {
	if ((http.readyState == 0) || (http.readyState == 4)) {
		errorelement = errelement;
		http = CreateRequestObject();
		http.open("get",sURL);
		http.onreadystatechange = HandleResponse; 
		http.send(null);
	} else {
		bContinu = confirm("Previous click is not finished processing. Click \'Ok\' to allow it to continue, click \'Cancel\' to cancel it.");
		if (bContinu == false) {
			http.abort();
			alert("Operation aborted.");
		}
	}
}

function invoke_ajax_javascript(comd) {
	eval(comd);
}

function HandleResponse() {
	if (http.readyState == 4) {
		try {
			stx = http.status;
		} catch (e) { alert (e.name + ": " + e.message); stx = -1; }
		if (stx == 200) {
			var sResponse = http.responseText;
			o = new bker(sResponse);
			while (o.s != "") {
				ExBlock(o);
				if (o.errno == 1) {
					document.getElementById(errorelement).innerHTML = sResponse;
					return;
				}
				sFirst = o.bk;
				ExBlock(o);
				sSecond = o.bk;
				if (sFirst == 'javascript') {
					invoke_ajax_javascript(sSecond);
				} else {
					document.getElementById(sFirst).innerHTML = sSecond;
				}
			}
		} else {
			if (stx == -1) {
				alert("Unable to contact the server. Please check your network connection.");
			} else {
				alert("Request failed: Server returned error code " + http.status + ": " + http.statusText);
			}
		}
	}
}

function make_ajax_request_via_runtime_params(ajaxurlbase,replaceelement,runtime_params) {
	axparamurl = "";
	for (var runkey in runtime_params) {
		axparamurl += "&" + runkey + "=" + runtime_params[runkey];
	}
	axparamurl = ajaxurlbase + "?" + axparamurl.substring(1);
	AxRequest(axparamurl,replaceelement);
}

