/**
 * @author Simon Jia
 * @copyright 2006
 * @I'm not responsible for any damages caused by mis-using this script.
 * @you are free to copy this code and use it for any purpose as long as you keep the credits somewhere on in the code
 *
 * Documentaion:
 * SvrResObj (string) required - Name of the obj to be used for svr side responds
 * URL (string) required - URL where the remote call is sent to
 * Req (obj) optional - requests sent to the remote URL
 *                      property name is the name of the request parameter
 *                      property value is the value of the request parameter
 * Loading (function) optional - function that will be trigger during loading (no arguments passed in)
 * Finishing (function) optional - function that will be trigger after loading (svr response obj passed in as argument)
 *
 * Example:
 * var $$=new XHRAjax({
 *		URL			:	"http://www.yahoo.com/test.php",
 *		Req			:	{"var1":"value1", "var2":"value2"},
 *		Loading		:	function(str) {
 *			//show loading state in a node
 *		},
 *		Finish	:	function(str) {
 *			//do some DOM change based on the svr responde str
 *		},
 *		Fail:	function(str) {
 *			//do some Fail codes
 *		}
 * )};
 */

function XHRAjax(Obj) {
	this.Loading=Obj.Loading;
	this.Finish=Obj.Finish;
	this.Fail=Obj.Fail;
	if (Obj.URL==null || Obj.Req==null) {
		this.ShowFail("Error in request");
		return false;
	}		
	this.URL=Obj.URL;
	this.ReqStr="";
	if (Obj.Req!=null) {
		for (property in Obj.Req) {
			this.ReqStr+="&"+property+"="+escape(Obj.Req[property]);
		}
	}
	this.ReqStr=this.ReqStr.substr(1);
	
	if (window.XMLHttpRequest) {
		// Mozilla, Safari,...
		this.HTTPRequest = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {// IE
			this.HTTPRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.HTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!this.HTTPRequest) {
		this.ShowFail();
		return false;
	}
	this.SendRequest();
}

XHRAjax.prototype = {
	
	SendRequest: function() {
		var CurObj=this;
		this.HTTPRequest.onreadystatechange = function() {
			CurObj.GetResponse();
		};

		this.HTTPRequest.open('POST', this.URL, true);
		this.HTTPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.HTTPRequest.setRequestHeader("Content-length", this.ReqStr.length);
		this.HTTPRequest.setRequestHeader("Connection", "close");
		this.HTTPRequest.send(this.ReqStr);
	},
	
	GetResponse: function() {
		if (this.HTTPRequest.readyState == 4) {
			if (this.HTTPRequest.status == 200) {
				this.ShowFinish(this.HTTPRequest.responseText);
			} else {
				this.ShowFail(this.HTTPRequest.responseText);
			}
		} else {
			this.ShowLoading();
		}
	},
	
	ShowFinish: function(Str) {
		if (this.Finish!=null) {
			this.Finish(Str);
		}
	},
	
	ShowLoading: function() {
		if (this.Loading!=null) {
			this.Loading(this.ReqStr);
		}
	},
	
	ShowFail: function(Str) {
		if (this.ShowFail!=null) {
			this.Fail(Str);
		}
	}
}
