/**
 * Javascript
 * 
 * @package    Forum
 * @author     banhthidiem <banhthidiem@gmail.com>
 * @copyright  2007 Bach Khoa Computer Inc.
 * @version    $Id: XMLAjax.js, v1.0 2007/09/13
 * var obj = new Object();
 * obj.showData = function(data)
 * {// noi dung xu ly
 *		alert(data);
 * }
 * ex: var xmlObj = new XMLAjax(obj, true);
 *	var xmlObj.ajaxDo('page.asp', "a=123&b=456");
 */
 
var AjaxLoadingStatus = 
{
	getSpan : function()
	{
		var o = utilObj.getElById("spanStatusAjax");
		if (!o)
		{
			o = utilObj.createEl("SPAN");
			o.id = "spanStatusAjax";
			o.style.position = "absolute";
			o.style.display = "none";
			o.style.top = "-1000px";
			o.style.left = "-1000px";
			o.style.visibility = "hidden";
			o.style.margin = "2px 2px 2px 2px";
			o.style.padding = "2px 2px 2px 2px";
			o.style.zIndex = 1000;
			o.style.border = "1px solid #669900";
			o.style.backgroundColor = "#FFFFFF";
			utilObj.d.body.appendChild(o);
		}
		return o;
	},

	display : function(value)
	{
		var content = "<table cellpadding='0' cellspacing='5' border='0'>" +
			"<tr><td class='AjaxLoaderImg'></td><td style='font-size:13px; font-weight:bold'>" + value + "</td></tr>" +
			"</table>";
		var o = this.getSpan();
		var pos = utilObj.getElPosCenterWebPage(o);
		o.innerHTML = content;
		o.style.top = pos.Y + "px";
		o.style.left = pos.X + "px";
		o.style.visibility = "visible";
		o.style.display = "";
		try
		{
			setTimeout("AjaxLoadingStatus.hide()", 10000);
		}
		catch (e) {}
	},

	hide : function()
	{
		var o = this.getSpan();
		o.style.top = "-1000px";
		o.style.left = "-1000px";
		o.style.visibility = "hidden";
		o.style.display = "none";
	}
 };
 
/**
 * New object
 */
function XMLAjax(callObject, displayLoader)
{
	this.u = utilObj;
	
	try
	{
		this.XMLHandler = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			this.XMLHandler = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			return false;
		}
	};
	
	var self = this;
	this.responseResult = function() {
		if (self.getReadyState() == 1) {
			//You can add animated gif while loading //
			if (displayLoader) AjaxLoadingStatus.display("Loading...");
		}
		else if (self.getReadyState() == 4) {
			if (self.getStatus() == 200) {
				var data = self.getResponseText();
				if (data != "") {
					callObject.showData(data);
				}
			}
			if (self.getStatus() != 0) {
				if (displayLoader) AjaxLoadingStatus.hide();
			}
		}
	};
};

/**
 * Actually send data
 */
XMLAjax.prototype.process = function(url, type, post)
{
	type = (type.toUpperCase() == "POST") ? "POST" : "GET";

	if(! this.readyStateIsNotReady())
	{
		this.XMLHandler.open(type, url, true);
		
		if(type == "GET")
		{
			this.XMLHandler.send(null);
		}
		else
		{
			if(typeof(this.XMLHandler.setRequestHeader) != "undefined")
			{
				this.XMLHandler.setRequestHeader("Pragma", "cache=yes");
				this.XMLHandler.setRequestHeader("Cache-Control", "no-transform");
				this.XMLHandler.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			this.XMLHandler.send(post);
		}
	}

	return false;
};

/**
 * Takes an array of: array[ field ] = value and returns a nice encoded POST string
 */
XMLAjax.prototype.formatForPost = function(arrayfields)
{
	var str = "";
	try
	{
		
		for(var i in arrayfields)
		{
			if (str == "")
				str += i + "=" + arrayfields[i];
			else
				str += "&" + i + "=" + arrayfields[i];
		}
	}
	catch(e)
	{
	}
	return str;
};

/**
 * Check to ensure ready state-ness
 */
XMLAjax.prototype.readyStateIsNotReady = function()
{
	return (this.XMLHandler.readyState && (this.XMLHandler.readyState < 4));
};

/**
 * Get value Ready State
 */
XMLAjax.prototype.getReadyState = function()
{
	return this.XMLHandler.readyState;
};

/**
 * Get value Status
 */
XMLAjax.prototype.getStatus = function()
{
	return this.XMLHandler.status;
};

/**
 * Get text response
 */
XMLAjax.prototype.getResponseText = function()
{
	return this.XMLHandler.responseText;
};

/**
 * Onready state change event handler
 */
XMLAjax.prototype.onReadyStateChange = function(fn)
{
	if(typeof(fn) == "function")
	{
		this.XMLHandler.onreadystatechange = fn;
	}
};

/**
 * Do ajax
 */
XMLAjax.prototype.ajaxDo = function(url, fields)
{
	this.onReadyStateChange(this.responseResult);
	this.process(url, "POST", this.formatForPost(fields));
};