
// httpRequest performs an xmlHttpRequest
// url - URL to which request is made
// method - currently only GET or POST
// handler - callback function. receives the request object
// args - variables to pass to call in form of (foo=moo&cat=dog&...)

function httpRequest(url, method, handler, args)
{
	var xmlReq = null;
	var ajax = new AjaxHandler(handler); 
	
	if (window.ActiveXObject) {
		xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
		xmlReq.onreadystatechange = ajax.register(xmlReq);
	}
	else {
		xmlReq = new XMLHttpRequest();
		xmlReq.onload = ajax.register(xmlReq);
	}
	
	if (xmlReq) {
		switch(method) {
			case 'GET':
				url += args ? '?' + args : '';
				xmlReq.open('GET', url, true);
				xmlReq.send(null);
				break;
			
			case 'POST':
				xmlReq.open('POST', url, true);
				xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
				xmlReq.send(args);
				break;
		}
		
		return xmlReq;
	}
}

// AjaxHandler is a custom event handler to handle multiple asynchronous requests
function AjaxHandler(strHandler) {
	var request = null;
	var handler = strHandler;
	
	this.register = function(req) {
		request = req;
		return this.change;
	};
	
	this.change = function() {
		if ( request.readyState == 4 && request.status == 200 )
		eval(handler + '( request )');
	};
}
