var AJAX = new Object();

AJAX.get = function(url, method, ms) {
    return AJAX.send("GET", url, method, ms);
}

AJAX.post = function(url, method, ms) {
    return AJAX.send("POST", url, method, ms);
}

AJAX.send = function(type, url, method, ms) {
   	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
   	if (window.XMLHttpRequest)
       	self.xmlHttpReq = new XMLHttpRequest();
    // IE
   	else if (window.ActiveXObject)
       	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    // timeout
    if (ms > 0) {
	    var onTimeout = function() {
    		self.xmlHttpReq.abort();
	    }
    	var timeout = setTimeoutHelper(onTimeout, ms);
    }
    // parse URL and data
    var href = url;
    var data = null;
    if (type == "POST") {
        var sep = url.indexOf("?");
        if (sep >= 0) {
            href = url.substring(0, sep);
            data = url.substring(sep + 1);
        } 
    }
    // AJAX
    if (method != null && typeof(method) == "function") {
	   	self.xmlHttpReq.open(type, href, true);
    	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	   	self.xmlHttpReq.onreadystatechange = function() {
    	   	if (method && self.xmlHttpReq.readyState == 4) {
    	   		if (ms > 0) clearTimeoutHelper(timeout);
        	   	method(self.xmlHttpReq.responseText);
        	}
	    }
       	self.xmlHttpReq.send(data); // query string is argument
	} 
	// SJAX
	else {
	   	self.xmlHttpReq.open(type, href, false);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	   	try {
	       	self.xmlHttpReq.send(data); // query string is argument
   			if (ms > 0) clearTimeoutHelper(timeout);
			return self.xmlHttpReq.responseText;
		} catch(ex) {
			return null;
		}
	}
}
