/*--------------------------------------------------------------------------
 *  PAjax
 *
 *  Author: 	xqiang
 *  Version: 	1.3.0
 *	Date:		2009-05-28 22:35
 *--------------------------------------------------------------------------*/ 
WQ.PAjax = {
	method:'POST',			//发送方法	
	updateTip:'Loading data ...',	//后台处理中提示信息	
	target:'AjaxResult',	//提示信息对象
	showTip:true,	 // 是否显示提示信息，默认开启
	isAsync:true, // 是否异步发送请求
	evalScripts:true, // 是否执行其中的脚本块
	debug:false,
	params:"",
	processResponse : function (request) {
		var responseText = request.responseText;
		if (this.debug) {
			alert(responseText);
		}
		eval(responseText);
	},	
	reportError : function (request) {
		alert('Sorry. There was an error.');
	},
	encodeURI : function (value) {
		if (!value) return "";
		return encodeURIComponent(value);
	},
	addParam : function (field,value,isEncodeURI) {
		if (!field || field == "") return false;
		if (!isEncodeURI) isEncodeURI = true;
		if (this.params.length > 0) this.params += "&";
		this.params += field + "=" + (isEncodeURI?this.encodeURI(value):value);
	},
 	// 显示正在更新
	displayTip : function (target) {
		this.showTip = false;
		if (target == undefined || target == "") target = "result";
		//if (!$(target)) return false;
		//$(target).style.display = 'block';
		//$(target).innerHTML	= this.updateTip;
		var elements = document.getElementsByTagName("div");
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].id == target || elements[i].name == target) {						
				elements[i].innerHTML	= this.updateTip;
				elements[i].style.display = 'block';	
			}	
		}
		return true;
	},
	// 更新完成, 隐藏正在更新层
	hiddenTip : function (target) {
		this.showTip = true;
		if (target == undefined || target == "") target = "result";
		//if (!$(target)) return false;
		//$(target).style.display = 'none';
		//$(target).innerHTML	= "";
		var elements = document.getElementsByTagName("div");
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].id == target || elements[i].name == target) {						
				elements[i].style.display = 'none';		
				elements[i].innerHTML	= "";	
			}				
		}
		return true;		
	},
	loading:function (target,tips){
		if ($(target)) {				
			$(target).innerHTML	= tips;
			$(target).style.display = 'block';		
		}
	},
	// 发送Ajax请求
	updater:function(url,pars,target,response,tips)
	{	
		if (target == undefined)	target = this.target;		
		if (tips == undefined) tips = this.updateTip;
		if (response == undefined || response == '') response = this.reportError;
		if (pars && pars != "") this.params = pars;
		if (this.debug) {
			alert("url = [" + url + "]\n" 
				+ "params = [" + this.params + "]\n"
				+ "target = [" + target + "]\n"
				+ "response = [" + response + "]\n"
				+ "tips = [" + tips + "]\n"
				);
		}
		if (this.showTip)
		{
			this.loading(target,tips);
		}		
		try {
			var ajax = new Ajax.Updater(
				{success: target},
				url,
				{
					method: this.method, 
					parameters: this.params, 
					onFailure: response,
					// 是否执行其中的脚本块
					evalScripts: this.evalScripts,
					// 是否异步发送请求
					asynchronous: this.isAsync
				});
		} catch (e) { 
			if (this.debug) alert(e.message);
			return false; 
		}
		this.params = "";
	},
	// 发送表单Ajax操作
	updaterForm:function(formId,url,target,response,tips)
	{
		vars = Form.serialize(formId);
		this.updater(url,vars,target,response,tips);
	},
	// 发送Ajax请求
	request:function(url,pars,target,response,tips)
	{	
		if (target == undefined)	target = this.target;		
		if (tips == undefined) tips = this.updateTip;
		if (response == undefined || response == '') response = this.processResponse;
		if (pars && pars != "") this.params = pars;
		if (this.debug) {
			alert("url = [" + url + "]\n" 
				+ "params = [" + this.params + "]\n"
				+ "target = [" + target + "]\n"
				+ "response = [" + response + "]\n"
				+ "tips = [" + tips + "]\n"
				);
		}
		if (this.showTip)
		{
			this.loading(target,tips);
		}		
		try {
			var ajax = new Ajax.Request(
				url,
				{
					//请求方式：POST
					method: this.method,
					//请求参数
					parameters: this.params,
					//指定回调函数
					onComplete: response,
					//是否异步发送请求
					asynchronous: this.isAsync
				});
		} catch (e) { 
			if (this.debug) alert(e.message);
			return false; 
		}
		this.params = "";
	},
	// 发送表单Ajax操作
	requestForm:function(formId,url,target,response,tips)
	{
		vars = Form.serialize(formId);
		this.request(url,vars,target,response,tips);
	}
};

var PAjax = WQ.PAjax;