/* new AJAXHelper().post("foo.cgi", {}, function() {alert("Received!")}); */
function AJAXHelper() {
	this.build_onreadystatechange=function(xmlhttp, callback) {
		return function() {
			if (xmlhttp.readyState==4) {
				if (xmlhttp.status==200) {
					callback(xmlhttp);
				} else {
					// Failure. FIXME
				}
			}
		}
	};
	this.post = function(url, args, callback) {
		var xmlhttp;
		if (window.XMLHttpRequest) {
			xmlhttp=new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if(!xmlhttp) {
			alert("Your browser does not support AJAX. Please consider upgrading.");
			return false;
		}
		var post_content = "";
		for(var i in args) {
			if(args[i] === undefined) continue;
			if(args[i] instanceof Array) {
				for(var j=0; j<args[i].length; j++)
					post_content += i + "=" + encodeURIComponent(args[i][j]) + ";";
			} else {
				post_content += i + "=" + encodeURIComponent(args[i]) + ";";
			}
		}

		xmlhttp.onreadystatechange=this.build_onreadystatechange(xmlhttp, callback);
		xmlhttp.open("POST",url,true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", post_content.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(post_content);
		return true;
	};
}

