//Author: AnhDT6
//Created Date: 2008-March-26
function parseParams(params)
{
   var r = '';   
   for(var property in params)
   {
      r += property + '=' + params[property] + "&";
   }      
   return r.substring(0, r.length - 1);
}

//-------- AjaxObject ---------

AjaxObject = function(config)
{
    this.config = {
        url: '',
        params: '',
        method: 'POST',
        success: null,
        failure: null,
        asynchronous: true,
        options: null
    };
    Object.extend(this.config, config || {});
    
    //init
	this.url 			= this.config.url;
	this.query 			= (typeof(this.config.params) == "string") ? this.config.params : parseParams(this.config.params);
	this.uri			= this.url + "?" + this.query;
	this.init 			= false;
	this.http 			= false;
	this.onsuccess      = (typeof(this.config.success) == "function") ? this.config.success : function() { };
	this.onfailure      = (typeof(this.config.failure) == "function") ? this.config.failure : function() { };
	this.options        = this.config.options;

	this.setup();
}
AjaxObject.prototype.debug = false;
AjaxObject.prototype.setup = function()
{
	if (typeof XMLHttpRequest != 'undefined')
	{
		this.http = new XMLHttpRequest();
	}
	if (!this.http && window.ActiveXObject)
	{
		try
		{
			this.http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try
			{
				this.http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { this.http = false; }
		}
	}
	if (this.http) this.init = true;
	if (this.debug) alert(this.http);
}

AjaxObject.handler = function(o)
{
	var obj = o;
	if (obj.debug) alert("readyState: " + obj.http.readyState);
	
	if (obj.http.readyState == 4)
	{
		// properties
		obj.responseText	= obj.http.responseText;
		obj.responseXML		= obj.http.responseXML;
		obj.readyState 		= obj.http.readyState;
		obj.status	 		= obj.http.status;
		obj.statusText		= obj.http.statusText;
		obj.headers 		= obj.http.getAllResponseHeaders();
		
		// methods
		obj.getHeader		= function(aHeader) { return obj.http.getResponseHeader(aHeader); }
		
		//if (obj.status != 200) obj.init = false;

		if (obj.debug) alert("status: " + obj.status + "\ncalling onsuccess()");
		
		obj.onsuccess(obj, obj.options);
	}
}

AjaxObject.prototype.renderTo = function(itemId)
{
	if (document.getElementById(itemId)) document.getElementById(itemId).innerHTML = this.responseText;
}

AjaxObject.prototype.go = function(v)
{
	if (!this.init) return false;
	var me = this;
	try
	{
		this.http.onreadystatechange = function() { AjaxObject.handler(me); }
		
		if (v == "GET")
		{
			if (this.debug) alert("getting: " + this.uri);
			this.http.open("GET", this.uri, true);
			this.http.send(null);
		}
		else if (v == "POST")
		{
			if (this.debug) alert("posting: " + this.url + "\npost data: " + this.query);
			this.http.open("POST", this.url, true);
			// todo: set this as an option so we can actually do a web-service type thing
			this.http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.http.send(this.query);
		}
	}
	catch(e)
	{
		if (this.debug) alert("failed to connect");
		this.init = false;
		this.onfailure();
	}
}
AjaxObject.prototype.get = function() { this.go("GET"); }
AjaxObject.prototype.post = function() { this.go("POST"); }
AjaxObject.prototype.request = function() { (this.config.method == "GET") ? this.get() : this.post(); }
