var myajax={
	
	//当前页面的url
	curUrl:"",
	
	//当前页面的参数
	curData:"",

	//上一页面的url
	historyUrl:"",

	//上一页面的参数
	historyData:"",
	
	//默认div的id
	historyTarget:"",
	
	curTarget:"",
	
	//访问允许的最长时间
	timeout:10000,

	//通过ajax提交，不直接调用
	post:function(url,data,callback,dataType,isHiddeTips){

		if(!isHiddeTips) this.showTips();//显示提示：数据加载中
	
		$.ajax({
			type: "POST",
			url: url,
			dataType: dataType,
			data: data+"&isAjax=1",			
			cache: false,
			timeout:myajax.timeout,
			error: function(e,error){
				if(!isHiddeTips) myajax.hidTips();
				alert(error);
			},  
			success:function(html){
				if(!isHiddeTips) myajax.hidTips();//隐藏提示				
				flag=html;
				if(flag==-2){
					alert("你没有操作此功能的权限！");
					//html="没有权限";
				}else if(flag==-1){//没有登录
					//alert(flag);
					waLogin.loginBox(isHiddeTips);	
					waLogin.loginOnCB=function(){
						//设置登录区块
						waLogin.loginBlock();
						myajax.post(url,data,callback,dataType,isHiddeTips);
					}
				}else{			 
					if(callback!=null&&callback!='') callback(html);
				}
			}
		});
	},
	
	/**
	表单提交，不直接调用
	*/
	submitform:function (formId,callback,dataType,isHiddeTips){
		if(document.getElementById(formId)==null) return;
		
		if(!isHiddeTips) this.showTips();
		var actionURL=$("#"+formId).attr("action");
		actionURL+=actionURL.indexOf("?")==-1?"?isAjax=1":"&isAjax=1";
	
		var options = { 
			//target:        '#output1',   // target element(s) to be updated with server response 
			//beforeSubmit:  showRequest,  // pre-submit callback 
			cache: false,
			timeout:myajax.timeout,
			error: function(e,error){
				if(!isHiddeTips) myajax.hidTips();
				alert(error);
			}, 			
			success:function(html){
				if(!isHiddeTips) myajax.hidTips();//隐藏提示
				flag=html;
				if(flag==-2){
					alert("你没有操作此功能的权限！");
					//html="没有权限";
				}else if(flag==-1){
					waLogin.loginBox();	
					waLogin.loginOnCB=function(){
						myajax.submitform(formId,callback,dataType,isHiddeTips);
						//设置登录区块
						waLogin.loginBlock();
					}
				}else{
					if(callback!=null&&callback!='') callback(html);
				}
			}, 
			// other available options: 
			url:       actionURL,         // override for form's 'action' attribute 
			//type:      type        // 'get' or 'post', override for form's 'method' attribute 
			dataType:  dataType        // 'xml', 'script', or 'json' (expected server response type) 
			//clearForm: true        // clear all form fields after successful submit 
			//resetForm: true        // reset the form after successful submit 
			//,iframe:true
	 
			// $.ajax options can be used here too, for example: 
			//timeout:   3000 
		};
	
		$('#'+formId).ajaxSubmit(options);
	},

	/**
	表单提交，返回json数据
	*/
	submitformJSON:function(formId,callback,isHiddeTips){
		this.submitform(formId,callback,"json",isHiddeTips);
	},

	/**
	表单提交，返回json数据
	*/
	submitformHTML:function(formId,callback,isHiddeTips){
		this.submitform(formId,callback,"html",isHiddeTips);
	},

	//通过ajax提交改变目标div的html,使用form提交
	ajaxlinkForm:function(formId,target){
		if(target==null) return;
		if(document.getElementById(target)==null) return;

		data=$("#"+formId).formSerialize();
		url=$("#"+formId).attr("action");
		this.setBackUrl(url,data,target);

		this.submitformHTML(
			formId,
			function(html){
				myajax.changeHtmlById(html,target);
			},
			false
		);
	},

	/**
	*通过ajax提交改变目标div的html
	target 省略的话使用默认的target
	*/
	ajaxlink:function(url,data,target,isHiddeTips){
		if(target==null) return;
		if(document.getElementById(target)==null) return;

		this.setBackUrl(url,data,target);
		
		this.postHTML(
			url,
			data,
			function(html){
				myajax.changeHtmlById(html,target);
			},
			isHiddeTips
		);
	},
		
	//通过ajax提交，返回html代码
	postHTML:function(url,data,callback,isHiddeTips){
		this.post(url,data,callback,'html',isHiddeTips);
	},

	//通过ajax提交，返回json代码
	postJSON:function(url,data,callback,isHiddeTips){
		this.post(url,data,callback,'json',isHiddeTips);
	},

	//更改目标的html
	changeHtmlById:function(html,target){
		$('#'+target).empty();
		$('#'+target).html(html);
	},
	
	/**
	设置返回的url
	@param url 当前的url
	@param data 当前的参数数据
	*/
	setBackUrl:function(url,data,target){
			this.historyUrl=this.curUrl;
			this.historyData=this.curData;
			this.historyTarget=this.curTarget;
			this.curData=data;
			this.curUrl=url;
			this.curTarget=target;
	},	
	
	
	/**
	返回按钮执行的函数
	目前只支持一级返回
	*/
	ajaxBack:function(){
		target=this.historyTarget;
		if(this.historyUrl==null||this.historyUrl=="") return;
		
		this.curData=this.historyData;
		this.curUrl=this.historyUrl;
		
		this.postHTML(
			this.historyUrl,
			this.historyData,
			function(html){
				myajax.changeHtmlById(html,target);
			},
			false
		);
	},
	
	showTips:function(){
		$.blockUI({ message: "<img src='/images/wait.gif' /> <font style='font-size: 12px'>数据处理中，请稍候...</font>",css: { border: '2px solid #aaa',color: '#000',background:'#ffffcc'} });	
	},
	
	hidTips:function(){
		$.unblockUI();
		//$().ajaxStop($.unblockUI);
	}	
	
};
