/*--------------------------------------------------------------------------
 *  SyaAjax
 *
 *  Author: 	xqiang
 *  Version: 	1.1.1
 *	Date:		2008-11-30 16:10
 *--------------------------------------------------------------------------*/
/* document.write("<div id='AjaxResult'></div>"); */
WQ.SysAjax = {
	method:'POST',			//发送方法
	bComplete:false,			//是否完成
	updateTip:'数据处理中～',	//后台处理中提示信息
	updateEffect:{'opacity': [0.1,0.85]},			//更新效果
	target:'AjaxResult',	//提示信息对象
	showTip:true,	 // 是否显示提示信息，默认开启
	timeOut:5000,	// 提示信息显示时间, 毫秒
	status:0, //返回状态码
	info:'',	//返回信息
	data:'',	//返回数据
	intval:0,
	debug:false,
	activeRequestCount:0,
	m : {
		'\b': '\\b',
		'\t': '\\t',
		'\n': '\\n',
		'\f': '\\f',
		'\r': '\\r'
	},
	// Ajax连接初始化
	getTransport: function() {
		return Try.these(
		 function() {return new XMLHttpRequest()},
		  function() {return new ActiveXObject('Msxml2.XMLHTTP')},
		  function() {return new ActiveXObject('Microsoft.XMLHTTP')}
		 
		) || false;
	},
	loading:function (target,tips,effect){
		if ($(target))
		{
			//var arrayPageSize = getPageSize();
			var arrayPageScroll = WQ.util.getPageScroll();
			$(target).style.display = 'block';
			$(target).style.top = (arrayPageScroll[1] +  'px');
			$(target).style.right = '0px';
			// 显示正在更新
			if ($('loader'))
			{
				$('loader').style.display = 'none';
			}
			$(target).innerHTML = tips;
			//使用更新效果
			//var myEffect = $(target).effects();
			//myEffect.custom(effect);
		}
	},
	// 显示正在更新
	displayTip : function (target,tagName) {
		this.showTip = false;
		if (target == undefined || target == "") target = "result";
		if (tagName == undefined || tagName == "") tagName = "div";
		
		var elements = document.getElementsByTagName(tagName);
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].id != target && elements[i].name != target) continue;
			elements[i].innerHTML	= this.updateTip;
			elements[i].style.display = 'block';		
		}
		return true;
	},
	// 更新完成, 隐藏正在更新层
	hiddenTip : function (target,tagName) {
		this.showTip = true;
		if (target == undefined || target == "") target = "result";
		if (tagName == undefined || tagName == "") tagName = "div";
		
		var elements = document.getElementsByTagName(tagName);
		var msg = this.info;
		if (this.status == 0) {
			msg = "<font color='red'>" + msg + "</font>";	
		}
		var isExists = false;
		for (var i = 0; i < elements.length; i++) {
			var element = elements[i];
			if (element.id != target && element.name != target) continue;
			isExists = true;
			if (this.info) {
				element.innerHTML	= msg;						
			} else {
				element.style.display = 'none';
				element.innerHTML	= "";	
			}			
		}
		if (isExists && this.info) {
			window.setTimeout(function () {
				for (var i = 0; i < elements.length; i++) {
					var element = elements[i];
					if (element.id != target && element.name != target) continue;
					element.style.display = 'none';		
					element.innerHTML	= "";
				}
			},this.timeOut);
		}
		if (!isExists && this.info) alert(this.info);
		return true;
	},
	ajaxResponse:function(request,target,response){
		// 获取后台返回Ajax信息和数据
		//alert(request.responseText);
		var str	=	request.responseText;
		str  = str.replace(/([\x00-\x1f\\"])/g, function (a, b) {
                    var c = WQ.SysAjax.m[b];
                    if (c) {
                        return c;
                    }else{
						return b;
					}
                     }) ;
		if (this.debug) {
			// 调试模式下面输出eval前的字符串
			alert(str);
		}
		try {
			$return =  eval('(' + str + ')');
			this.status = $return.status;
			this.info	 =	 $return.info;
			this.data = $return.data;
		} catch (e) {
			this.status = 0;
			this.info	 =	 "";
			this.data = "";
		}
		// 处理返回数据
		// 需要在客户端定义ajaxReturn方法
		if (response == undefined)
		{
			try	{(ajaxReturn).apply(this,[this.data,this.status,this.info]);}
			catch (e){}
			 
		}else {
			try	{ (response).apply(this,[this.data,this.status,this.info]);}
			catch (e){}
		}
		if ($(target))
		{
			// 显示提示信息
			if (this.showTip && this.info!= undefined && this.info!='') {
				if (this.status == 0) {
					$(target).innerHTML	= "<font color='red'>" + this.info + "</font>";
				} else {
					$(target).innerHTML	= this.info;
				}
			}
			// 提示信息停留5秒
			if (this.showTip)
			this.intval = window.setTimeout(function (){
				//var myFx = new Fx.Style(target, 'opacity',{duration:1000}).custom(1,0);
				$(target).style.display='none';
				},this.timeOut);
		} else {
			// 显示提示信息
			if (this.showTip && this.info!= undefined && this.info!='') {
				alert(this.info);
				if (this.data && $(this.data)) {
					try {
						$(this.data).focus();
					} catch (e) {}
				}
			}
		}
	},
	// 发送Ajax请求
	send:function(url,pars,response,target,tips,effect)
	{
		if (!url) {
			alert("url is empty.");
			return false;
		} else {
			url = url.replace(/(\/){2,}/g, "/");	
		}
		var xmlhttp = this.getTransport();
		if (target == undefined)	target = this.target;
		if (effect == undefined)	effect = this.updateEffect;
		if (tips == undefined) tips = this.updateTip;
		if (this.debug) {
			alert("url = [" + url + "]\n" 
				+ "pars = [" + pars + "]\n"
				+ "target = [" + target + "]\n"
				+ "response = [" + response + "]\n"
				+ "tips = [" + tips + "]\n"
				);
		}
		if (this.showTip)
		{
			this.loading(target,tips,effect);
		}
		if (this.intval)
		{
			window.clearTimeout(this.intval);
		}
		this.activeRequestCount++;
		this.bComplete = false;
		try {
			if (this.method == "GET")
			{
				xmlhttp.open(this.method, url+"?"+pars, true);
				pars = "";
			}
			else
			{
				xmlhttp.open(this.method, url, true);
				xmlhttp.setRequestHeader("Method", "POST "+url+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			var _self = this;
			xmlhttp.onreadystatechange = function (){
				if (xmlhttp.readyState == 4 ){
					if( xmlhttp.status == 200 && !_self.bComplete)
					{
						_self.bComplete = true;
						_self.activeRequestCount--;
						_self.ajaxResponse(xmlhttp,target,response);
					}
				}
			}
			xmlhttp.send(pars);
		}
		catch(z) { return false; }
	},
	// 发送表单Ajax操作，暂时不支持附件上传
	sendForm:function(formId,url,response,target,tips,effect)
	{
		vars = Form.serialize(formId);
		this.send(url,vars,response,target,tips,effect);
	},
	// 绑定Ajax到HTML元素和事件
	// event 支持根据浏览器的不同 
	// 包括 focus blur mouseover mouseout mousedown mouseup submit click dblclick load change keypress keydown keyup
	bind:function(source,event,url,vars,response,target,tips,effect)
	{
		var _self = this;
	   $(source).addEvent(event,function (){_self.send(url,vars,response,target,tips,effect)});
	},
	// 页面加载完成后执行Ajax操作
	load:function(url,vars,response,target,tips,effect)
	{
		var _self = this;
	   window.addEvent('load',function (){_self.send(url,vars,response,target,tips,effect)});
	},
	// 延时执行Ajax操作
	time:function(url,vars,time,response,target,tips,effect)
	{
		var _self = this;
		myTimer =  window.setTimeout(function (){_self.send(url,vars,response,target,tips,effect)},time);
	},
	// 定制执行Ajax操作
	repeat:function(url,vars,intervals,response,target,tips,effect)
	{
		var _self = this;
		_self.send(url,vars,response,target,effect);
		myTimer = window.setInterval(function (){_self.send(url,vars,response,target,tips,effect)},intervals);
	}
};

var SysAjax = WQ.SysAjax;