// ajax helper functions

function AjaxRequest() {
	var me = this;
	if (navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
		try {
			new ActiveXObject("Msxml2.XMLHTTP"); 
			try {
				this.conn = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch (e) {
	        	this.conn = null;
	        }
		} catch (e) {
			this.conn = null;
	    }
	}
    if (this.conn == null && typeof XMLHttpRequest != 'undefined') {
		this.conn = new XMLHttpRequest();
	}
	this.handleResponse = function() {
	    if(me.conn.readyState == 4){
	    	me.eventHandler(me.conn.responseText);
	    	me.eventHandler = null;
	    }
	}
}

AjaxRequest.prototype.post = function(address,data,eventHandler) {
	var async = eventHandler !== undefined;
    if (eventHandler) {
		this.eventHandler = eventHandler;
	    this.conn.onreadystatechange = this.handleResponse;
	}
	this.conn.open('POST',address,async);
	this.conn.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    this.conn.send(data);
    if (!async) {
    	this.response = this.conn.responseText;
    }
}

AjaxRequest.prototype.get = function(address,eventHandler) {
	var async = eventHandler !== undefined;
    if (eventHandler) {
		this.eventHandler = eventHandler;
	    this.conn.onreadystatechange = this.handleResponse;
	}
    this.conn.open('GET',address,async);
    this.conn.send(null);
    if (!async) {
    	this.response = this.conn.responseText;
    }
}

var ajaxRequest = new AjaxRequest();


