/**
 */
Ajax = {
 request : function(options) {
	options = options || {};
	options.args = options.args || {};
	options.args.json = true;
	
	var jsonDone = options.done;
	
	options.done = function(response) {
		if(jsonDone) {
			jsonDone( eval('(' + response + ')') );
		}
	};
	
	return Ajax.load(options);
},

 load : function(options) {

    var self = { };
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var query = options.query ? options.query : '';
    
    for (var i in options.args) {
    	if(query.length > 0) { query += '&'; };
    	query += i + '=' + encodeURIComponent(options.args[i]);
    }

    self.xmlHttpReq.open(options.method ? options.method : 'POST', options.url, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4 /* self.xmlHttpReq.DONE */) {
        	if(options.target) {
        		var target = (typeof options.target == 'string') ? $(options.target) : options.target;
        		target.innerHTML = self.xmlHttpReq.responseText;
        	}
        	if(options.done) {
	            options.done( self.xmlHttpReq.responseText );
        	}
        }
    };
 
    self.xmlHttpReq.send(query);
    
    return self.xmlHttpReq;
}
};
