/*
 * 处理:设计到请求页面，等待页面，结果页面的交替
 */
function CTrans()
{
	this.CommitByForm = CTrans_CommitByForm;
	this.CommitByStr = CTrans_CommitByString;
	this.Callback = CTrans_Callback;
	this.Disable = CTrans_Disable;
	
	this.ClearInput = CTrans_ClearInput;
	
	this.ShowWaiting = CTrans_ShowWaiting;
	this.ShowComplete = CTrans_ShowComplete;
	
	this.GenRequest = CTrans_GenReqData;
	this.DoResult=null;
	this.sReqDat = "";
	this.sActUrl = "";
	
	this.bDoing	= false;
	this.bNoDisable=false;
	
	this.objCanvan = 0;

	
	
}
function CTrans_CommitByString(str,acturl,bNoDisable, DoResult)
{
  var reqdat = str;
this.sReqDat = reqdat;
this.sActUrl = acturl;
this.bNoDisable=bNoDisable;
//	alert(DoResult);
	if(DoResult){
//alert("ture");
		this.DoResult=DoResult;
		var http = new CHttpRequest(this.Callback,true,2);
	}else{
		this.DoResult=null;
		var http = new CHttpRequest(this.Callback,false,2);
	}
	this.Disable(true);
	http.Get(acturl,reqdat);
}
function CTrans_CommitByForm(frm, DoResult, bNoDisable,method)
{	
	this.sReqDat = this.GenRequest(frm, method);
	this.sActUrl = frm.action;
	this.bNoDisable=bNoDisable;
	if(DoResult){
		this.DoResult=DoResult;
		var http = new CHttpRequest(this.Callback,true,2);
	}else{
		this.DoResult=null;
		var http = new CHttpRequest(this.Callback,false,2);
	}
	

	if(this.bDoing)
		return false;
	
	this.bDoing = true;
	this.Disable(true);
	
	if(method && method=="post"){
		http.Post(frm.action,this.sReqDat);
	}else{
		http.Get(frm.action,this.sReqDat);
	}
	//http.Post(frm.action,reqdat);
	//frm.submit();
	
	return true;
	
}
function CTrans_ShowWaiting()
{
	//this.Disable(true);
	//更改这里，在等待的时候显示友好界面
	
}
function CTrans_ShowComplete()
{
	//this.Disable(false);
	if(this.m_iRetcode == 0)
	{
    ;
	}
	else
	{
	  if(this.m_iRetcode == 1)
	  {  
	    alert("您未登录或登录超时，请先登录财付通系统。");
	    top.location.href="/zft/login.shtml?u1=" + encodeURIComponent(window.location.href);
	  }
	  else
	  {

		  if(this.m_sRetmsg=="")
		  	this.m_sRetmsg="系统繁忙";
		  
		  var sLoadUrl=window.document.URL.replace(/#$/g, "").split("?")[0];
		  if(sLoadUrl.indexOf("pay_new.cgi")<0 && sLoadUrl.indexOf("pay.cgi")<0 && sLoadUrl.indexOf("register_1.shtml")<0)
		  {
			  if(this.m_sRetmsg.indexOf("1197")>=0){
				  if(confirm(this.m_sRetmsg+"\n是否重新发送激活邮件到您的邮箱")){
					  g_CReSendEmail.Send(this.m_xmlRes.GetValue("u"), "2");
				  }
			  }else{
				alert(this.m_sRetmsg);
			  }
			  
		  if(this.m_sRetmsg.indexOf("密码")>=0 && this.m_sRetmsg.indexOf("密码保护")<0 &&  this.m_sRetmsg.indexOf("1064")<0)
		  {

				
				if(sLoadUrl.indexOf("register_1.shtml")>=0 && sLoadUrl != "" && this.sReqDat != "")
					sLoadUrl += "?" + this.sReqDat;
				else
					sLoadUrl = "";
				
				if(sLoadUrl=="")
					window.location.reload();
				else
				{
						window.location.href=sLoadUrl;
				}
		  }
		  }
	    
	  }
	}
}
function CTrans_ClearInput()
{
	var coll = document.all;
	if (coll!=null) 
	{
		for (i=0; i < coll.length; i++ )
	 	if((coll.item(i).tagName.toUpperCase() == 'INPUT' ) ||
	 	   (coll.item(i).tagName.toUpperCase() == 'TEXTAREA' )||
	 	   (coll.item(i).tagName.toUpperCase() == 'IMG' )||
	 	   (coll.item(i).tagName.toUpperCase() == 'OBJECT' ))
	 	{	 		
	 		if((coll.item(i).name) && coll.item(i).type)
	 		{
		 		if( (coll.item(i).name != "") && 
		 			(coll.item(i).type.toUpperCase() != "HIDDEN") &&
		 			(coll.item(i).type.toUpperCase() != 'SUBMIT') && 
		 			(coll.item(i).type.toUpperCase() != 'BUTTON') )
		 			coll.item(i).value = "" ;
	 		}
	 	}
	}
}
function CTrans_Disable(flag)
{
	if(this.bNoDisable)
		return;
	var coll = document.all;
	if (coll!=null) 
	{
		for (i=0; i < coll.length; i++ )
	 	if((coll.item(i).tagName.toUpperCase() == 'INPUT' ) ||
	 	   (coll.item(i).tagName.toUpperCase() == 'TEXTAREA' )||
	 	   (coll.item(i).tagName.toUpperCase() == 'IMG' )||
	 	   (coll.item(i).tagName.toUpperCase() == 'OBJECT' ))
	 	{
	 		coll.item(i).disabled = flag ;
	 	}
	}
}
/*
 * 定义全局变量g_CCftUser
 */
var g_CTrans =null ;
if(!g_CTrans)
	g_CTrans = new CTrans();

function CTrans_Callback(state,sts,str)
{
	
	g_CTrans.bDoing	= false;

	switch(state)
	{
	case 0://initting		
//		break;
	case 1://reading
//		break;
	case 2://readed
//		break;
	case 3://interact
		g_CTrans.ShowWaiting();
		break;
	case 4://complete
	g_CTrans.Disable(false);
		switch(sts)
		{
		case 200://success
		  g_CTrans.m_xmlRes = new CXml(str);	
			g_CTrans.m_iRetcode = parseInt(g_CTrans.m_xmlRes.GetValue("retcode"),10);
			g_CTrans.m_sRetmsg = g_CTrans.m_xmlRes.GetValue("retmsg");
			g_CTrans.m_sTid = g_CTrans.m_xmlRes.GetValue("tid");
			
			if(!g_CTrans.DoResult)
				g_CTrans.ShowComplete();	
			else
				g_CTrans.DoResult(g_CTrans.m_xmlRes);
			break;				
		case 404://notfound
		default:
		  g_CTrans.m_iRetcode = 99;
			g_CTrans.m_sRetmsg = "系统故障：访问的页面不存在！";
			g_CTrans.m_xmlRes = new CXml("<?xml version=\"1.0\" encoding=\"gb2312\" ?><root><retcode>"+sts+"</retcode> <retmsg>["+sts+"]系统故障：访问的页面出错</retmsg> </root>");
			if(!g_CTrans.DoResult)
				g_CTrans.ShowComplete();	
			else
				g_CTrans.DoResult(g_CTrans.m_xmlRes);
			break;
		}
	default:
		break;
	}			
}

function CTrans_GenReqData(form, method)
{
	var reqdat = "";
	var first_flag = true;
	var bEncodeGb = false;
	if(method && method=="post")
		bEncodeGb = true;
	for(var i=0; i < form.length; i++)
	{
		if(form.elements[i].type.toUpperCase() == "CHECKBOX" && !form.elements[i].checked)
			continue;
			
		if(form.elements[i].type.toUpperCase() == "RADIO" && !form.elements[i].checked)
			continue;

		
		if(form.elements[i].name!="" && form.elements[i].value !=""){
			if(first_flag){
				first_flag = false;
				if(bEncodeGb)
					reqdat = reqdat + form.elements[i].name + "="+ encodeURIComponent(form.elements[i].value);
				else
					reqdat = reqdat + form.elements[i].name + "="+ (form.elements[i].value).replace(/%/g,"%25").replace(/\r\n/g,"%0D%0A").replace(/=/g,"%3D").replace(/&/g,"%26").replace(/\?/g,"%3F").replace(/\+/g,"%2B");
			}else{		
				if(bEncodeGb)
					reqdat = reqdat + "&" + form.elements[i].name + "="+ encodeURIComponent(form.elements[i].value);
				else
					reqdat = reqdat + "&" + form.elements[i].name + "=" + (form.elements[i].value).replace(/%/g,"%25").replace(/\r\n/g,"%0D%0A").replace(/=/g,"%3D").replace(/&/g,"%26").replace(/\?/g,"%3F").replace(/#/g,"%23").replace(/\+/g,"%2B");
			}
		}
	}
	//alert(reqdat);
	return reqdat;
}


