function isIE()  
{  
	if(window.ActiveXObject || "ActiveXObject" in window){
		return true;
	}
   return false;
} 

var sendMobileMsgCnt = 0;
var importSignObjected = false;

var clickTime=0;
var validTime = 60;
var timerId = "";

var _selAuthTool = null;

var toolList;

/*元素显示切换*/
function setElemDisplay(elemId, flag) {
    document.getElementById(elemId).style.display = flag ? "block" : "none";
}

/*隐藏别名输入框*/
function hiddenAlias() {
	document.getElementById("cardNoDiv").style.display = "none";
}

/*显示别名输入框*/
function showAlias() {
    document.getElementById("cardNoDiv").style.display = "block";
}

/*校验别名知否可用*/
function cardAliasCheck(isServerCheck) {
    var cardAlias = document.getElementById("cardAlias").value;
    document.getElementById("cardAliasHint").style.display="";
    if(cardAlias == null || cardAlias == ""){
        alert(PAGES_ALERT_INPUTPAYCARDNO);//请输入支付卡号
        return false;
    }
    if(cardAlias.length < 5 || cardAlias.length > 19){
        alert(PAGES_ALERT_INPUTPAYCARDNOLEN);//设置支付卡号长度不符，请输入5~19位的数字或字母（至少含1个字母）
        return false;
    }
    if (cardAlias.match("[a-zA-Z]") == null){
        alert(PAGES_ALERT_INPUTPAYCARDNOALT);//支付卡号至少应含有一个字母
        return false;
    }
    if (cardAlias.match("^[a-zA-Z0-9_\.@]{5,19}$") == null) {
        alert(PAGES_ALERT_INPUTPAYCARDNOFMT);//合法的支付卡号应由数字、字母、下划线、.号、@符号组成，且至少包含一个字母
        return false;
    }
    //发送后台检查
    if("true" == isServerCheck) {
        try {
            var url = requiredHtmlParams2 + "&cardAlias=" + cardAlias + "&cardNo=" + cardNo + "&channel=0";
            var respXML = sendXMLHttpRquest(url, "Post", "xml");
            var errMsg = getValueFromXML(respXML, "errMsg");
            alert(errMsg);
       } catch (Exception) {
       }
    }
    return true;
}

/*日限额校验*/
function dayQuotaCheck() {
    var dayQuota = $('#dayQuota').val();
    var regx = "^([1-9][0-9]{0,4})+(\.\[0-9]{0,2})?$";

    if(dayQuota.match(regx) == null) {
        alert(PAGES_ALERT_INPUTVALIDLIMIT);//限额只能为整数
        return false;
    }
    if(parseFloat(dayQuota) > parseFloat(_dayQuota)) {
       alert(PAGES_ALERT_EXCEEDLIMIT + _dayQuota + PAGES_TAG_MONEY_YUAN);//限额不能超过50000元
        return false;
    }
    if(parseFloat(amount) > parseFloat(dayQuota)) {
    	alert(PAGES_ALERT_INPUTORDERLMT);//您的本次订单金额已超过该卡今日支付额度
   	return false;
   }
    return true;
}


/*安全信息校验*/
function welcomeMsgCheck() {
    var welcomeMsgValue = document.getElementById("welcomeMsgId").value;
    document.getElementById("welcomeMsgHint").style.display='none';
    if(welcomeMsgValue == null || welcomeMsgValue.length == 0) {
        alert(PAGES_ALERT_INPUTEXAMINFO);//安全信息不能为空
        return false;
    }
    var msgLen = welcomeMsgValue.replace(/[^\x00-\xff]/g, "**");
    if(msgLen.length > 30) {
        alert(PAGES_ALERT_INPUTAUTHINFO);//安全信息长度不能超过30个字母或15个汉字
        return false;
    }
    return true;
}


/*修改日限额*/
function alterDayQuota(){
//$('#dayQuota').attr('readOnly',false).css('color','black').focus();	
   var r = document.getElementById("dayQuota");
    r.removeAttribute("readOnly");
    r.focus();
    //window.document.from.dayQuota.css('color','black');
    
    
}

/*校验卡验证码*/
function cvvCheck() {
    var cvvVal = document.getElementById("cardCVV").value;
    if(cvvVal == null || cvvVal == 0) {
		alert(PAGES_ALERT_EMPTYCARDCVV2);//卡校验码不能为空
		return false;
	}
    if(cvvVal.match("^[0-9]{3}$") == null) {
        alert(PAGES_ALERT_WRONGCVV2);//卡校验码位数只能为3位数字
        return false;
    }
	return true;
}

function clickChgAuthTool()
{
	var jqDiv = $(this).parent().next();
	
	if(jqDiv.css('display')!='none')
	{
		return;
	}
	
	var thiz = $(this).unbind('click').addClass('disabled');
	
	thiz.hover(function(){
		$(this).css('color', '#aaa');
	},function(){
		$(this).css('color', '#aaa');
	});
	
	$.ajax({url:getAuthToolReqUrl+"&selAuthTool="+_selAuthTool,dataType:'text',success:function(data){
		var arr = data.split('\n');
		var result = {};
		
		try{
			result = $.parseJSON(arr[arr.length-2]);
		}catch(e)
		{
			document.write(data);
			return;
		}
		
		if(result['state']=='0')
		{
			toolList = result['authTools'];
			createAuthToolListItem(jqDiv);
		}
		else
		{
			alert(PAGES_ALERT_GETAUTHTOOL);
		}
		thiz.hover(function(){
			$(this).css('color','#cf0000');
		},function(){
			$(this).css('color','#0e7e3d');
		});
		thiz.click(clickChgAuthTool).removeClass('disabled').css('color','#0e7e3d');
	}});
}

function clickAuthToolItem()
{
	var clickItem = $(this).attr('value');

	
	if(clickItem=='-1')
	{
		return;
	}
	
	if(isIE()==false && (clickItem=='2' || clickItem=='4' || clickItem=='5'))
	{
	   
		alert(PAGES_ALERT_CHNGBROWSER);
		return false;
	}
	
	_selAuthTool = clickItem;
	var defValue = getValue(toolList, 'id', _selAuthTool, 'defValue');
	$('#netBankTotal').val(defValue);
	
	var name = getValue(toolList, 'id', _selAuthTool, 'name');
	$('#authToolName').text(name);
	
	$('.listItem').hide();
	
	updateDisplay();
}

function updateDisplay()
{
    $('.displayabled').hide();
    $('#netBankTotalDiv').toggle(defAuthTool!=_selAuthTool);
    
    var offsetX = 0;
    switch(_selAuthTool)
    {
    	case '1':
    		$('#mobileDiv').show();
    		$('#dynPwdDiv').show();
    		if(sendMobileMsgCnt==0)
    		{
    			validTime = 0;
    			reSendMobile();
    			validTime = 60;
    			refresh();
    		}
    		var text = '(' + PAGES_TAG_PWDSEQNO + ':' +verifyCodeSequence + ')'; 
    		$('#verifyCodeSequence').text(text);
    		offsetX = 6;
    		break;
    	case '2':
    		offsetX = 0;
    		if(importSignObjected==false)
    		{
    			importSignObject(webAppPath);
    			importSignObjected = true;
    		}
    		$('#usbTip').show();
    		break;
    	case '4':
    		if(importSignObjected==false)
    		{
    			importSignObject(webAppPath);
    			importSignObjected = true;
    		}
    		$('#usbTip').show();
    		offsetX = 34;
    		break;
    	case '3':
    		$('#chanomDiv').show();
    		offsetX = 40;
    		break;
    	case '5':
    	  if(importSignObjected==false)
    		{
    			importSignObject(webAppPath);
    			importSignObjected = true;
    		}
    		$('#usbTip').show();
    		offsetX = 40;
    		break;  	  		
    }
    $('.auth_span').css('left',160+offsetX);
    $('.listItem').css('left',364+offsetX);
}

function pushs(flag){
	var isKeyBoard = $('#isKeyBoard').val();
	if(isKeyBoard==0)
	{
		$('#isKeyBoard').val('1');
		$('#passwordtxt1').hide();
		try{
			$('#passwordtxt1').get(0).clear();
		}catch(e){}
 		$('#controlPassword').hide();
		$('#tranPass1').show();
		$('#buttonT').html(PAGES_ALERT_CHGSAFEINPUT);
		$('#pwdTypeChange').html(PAGES_ALERT_CHGSAFEINPUT);    //换为安全输入控件";
	}
	else
	{
		if(flag==0)
		{
			$('#passwordtxt1').show();
			initPwdNpApiObject(initPwdObjData.id,initPwdObjData.fieldName,initPwdObjData.maxLen,initPwdObjData.minLen,initPwdObjData.uniqueID,initPwdObjData.rule,initPwdObjData.language);
			$('#isKeyBoard').val('0');
		    	$('#tranPass1').hide();
			$('#softkeyboard').hide();
			$('#buttonT').html(PAGES_ALERT_CHGSAFTKEY);	//换为图形键盘
		}
	}
}

/*校验卡有效期*/
function cardExpDateCheck() {
    var cardExpMM = $('#cardExpMM').val();
    var cardExpYY = $('#cardExpYY').val();
    $('#cardExpDate').val(cardExpYY + "" + cardExpMM);
	if(cardExpMM == null || cardExpMM.length == 0) {
	    alert(PAGES_ALERT_EMPTYCARDEXPIR);//卡有效期月份不能为空
	    return false;
	}else if(cardExpYY == null || cardExpYY.length == 0) {
		alert(PAGES_ALERT_INPUTCARDEXPIR);//卡有效期年份不能为空
		return false;
    } else {
	    return true;
    }
}

/*校验证件号码*/
function certNoCheck() {
    var certNo = document.getElementById("certNo").value;
    var certType = document.getElementById("dbCertType").value;

    if(certNo == null || certNo.length == 0) {
        alert(PAGES_ALERT_INPUTCERTIFICATENO);//请输入证件号码
        return false;
    }
    if(certType == 15){
        if(!(certNo.length==15||certNo.length==18)){
            alert(PAGES_ALERT_WRONGDIGITSIDNO);//您输入的证件位数不正确
            return false;
        }
    }
    return true;
}

function refresh()
{
	if (validTime > 0)
	{
		validTime--;
		timerId = window.setTimeout("refresh()", 1000);
	}
	
	try{
		$('#reSendButton').html(PAGES_ALERT_RESENDAFTER.replace("XX",validTime));
	}catch(e){}

	if (validTime <= 0){
		try{
			$('#reSendButton').html(PAGES_ALERT_RESENDDYNPWD);
		}catch(e){}
	}
}

/*交易密码校验*/
function tranPasswordCheck() {
	var tranPassword = '';
    var isKeyBoard = document.getElementById("isKeyBoard").value;
    if(isKeyBoard == 1) {
    	tranPassword = document.getElementById("tranPass1").value;
    	if(tranPassword == null || tranPassword.length == 0) {
    	    alert(PAGES_ALERT_EMPTYPWD);//交易密码不能为空
    	    return false;
        }
        if(tranPassword.length!=6) {
            alert(PAGES_ALERT_PWDLENGTHERR);//交易密码长度不合法
            return false;
        }
        document.getElementById("tranPass").value = tranPassword;
    }else {
    	//校验交易密码
		if(isValid("passwordtxt1")==0)
		{
			alert(PAGES_ALERT_INPUTPWD);	//请输入数字密码！	
			return;
		}
    }
    return true;
}

/*协议校验*/
function agreeCheck() {
	var dymPassword = document.getElementById("isAgreeProt").checked;
	if(!dymPassword) {
	    alert(PAGES_ALERT_INPUTAGREE);//不同意协议则不能进行签约支付交易
	    return false;
    }
	return true;
}

/*显示协议*/
function showProtocol() {
    var top = (window.screen.height - 750)/2;
    var left = (window.screen.width - 1000)/2;
    top = top > 0 ? top : 0;
    left = left > 0 ? left : 0;
    var protocolLink = "netbankpay/protocol.jsp?" + sidPar;
	window.open(protocolLink,'签约并支付协议','top='+top+',left='+left+',width=1000,height=750,resizable=yes,scrollbars=1,menubar=no,status=no,toolbar=no,location=no');
}

function pwdCheck(){
	if(_selAuthTool == "0" || _selAuthTool == "1"){
		if(window.document.form.sms.value.length!=6)
		{
			alert(PAGES_ALERT_INPUTDYNPWD);		//请输入完整的动态密码
			window.document.form.sms.focus();
			return false;
		}
	}else if(_selAuthTool == "3")
	{
		if(window.document.form.dyCntsign.value.length!=6)
		{
			alert(PAGES_ALERT_INPUTDYCNTSIGN);		//请输入完整的动态口令
			window.document.form.dyCntsign.focus();
			return false;
		}
	}else{
		var signData = attachedSign(signDataRe);
		if(signData==undefined || signData==null ||signData=='')
		{
			alert(PAGES_ALERT_SIGNCANCEL);
			return false;
		}
		document.form.signData.value = signData;
	}
	return true;
}
function isValid(id)
{
	if(isIE()) {
		return document.getElementById(id).IsValid();
	}else{
		return document.getElementById(id).isValid;
	}
}

var isSubmit = 0;
/*提交签约并支付*/
function submitMainForm() {
	if(cardType=='2') {
		if(!cardExpDateCheck()) {return false;}
		//信用卡分期支付表单检查
		if(!submitStageCheck(document.getElementById("form"))){
		   return false;
		}
	}
	
	if("-1"==signType){
		if(cardType=='2') {
			if(!cvvCheck()) {return false;}
		}
		var cardAliasFlag = document.getElementsByName("cardAliasFlag")[1].checked;
		var isKeyBoard = document.getElementById("isKeyBoard").value;
	    if(!certNoCheck()) {return false;}
	    if(!dayQuotaCheck()){return false;}
	    if(cardAliasFlag){
	        if(!cardAliasCheck("false")){return false;}
	    }
	    if(!welcomeMsgCheck()){return false;}
	   
	    
	    if(!agreeCheck()){return false;}
	}
	 if(!tranPasswordCheck()){return false;}
	if(!pwdCheck()){return false;}
    $('#selAuthTool').val(_selAuthTool);
    
    if (clickTime==0){
		if (form.isKeyBoard .value=="1"){
			form.tranPass.value =form.tranPass1.value;
			window.document.form.submit();
		}else if (form.isKeyBoard.value=="0"){
			submitSafeControl('passwordtxt1','form','zh_CN');
		}
	}else{
      alert(PAGES_ALERT_GOBACK);
      return false;  
	}
    
    clickTime =1;
}

function reSendMobile()
{
	if(validTime>0)
	{
		return false;
	}
	if(sendMobileMsgCnt<2)
	{
		try{
			
			var act = requiredHtmlParams;
			if(sendMobileMsgCnt==0){
				act += "&generateCode=true";
			}
			
			var resXML = sendXMLHttpRquest(act, "Post", "xml");
			var errMsg = getValueFromXML(resXML, "errMsg");
			var seq = getValueFromXML(resXML, "verifyCodeSequence");
			
			if(seq!=null && $.trim(seq).length>0){
				var text = '(' + PAGES_TAG_PWDSEQNO + ':' +verifyCodeSequence + ')'; 
				$('#verifyCodeSequence').text(text);
				verifyCodeSequence = seq;
			}
		}catch(Exception){}
		
		sendMobileMsgCnt++;
		if(sendMobileMsgCnt==2)
		{
			alert(errMsg);
			$('#reSendButton').hide();
		}
	}
	else
	{
		alert(PAGES_ALERT_CANNOTRESENDDYNPWD);
	}
}

function goBack()
{
    if(clickTime==1){
      alert(PAGES_ALERT_GOBACK);
      return false;  
    }
	window.location=fullJspUrl;
}

function createAuthToolListItem(parent)
{
	parent.empty();
	
	if(toolList.length==1 && toolList[0]['id']=='-1'){
		alert(toolList[0]['name']);
		return;
	}
	
	var item = $('<div class="item1"><span class="close"><a href="###">[X]</a></span></div>');
	item.appendTo(parent);
	item.find('a').click(function(){
		$('.listItem').hide('slow');
	});
	
	$(toolList).each(function(i, n){
		var id = n['id'];
		var name = n['name'];
		if(id==_selAuthTool)
		{
			return true;
		}
		
		var item = $('<div class="item"><a href="###" value="'+id+'">'+name+'</a></div>');
		item.appendTo(parent);
		
		item.find('a').click(clickAuthToolItem);
	});
	parent.show('slow');
}

function getValue(arr, pro, proValue, key)
{
	var value = null;
	$(arr).each(function(i, n){
		if(n[pro]!=undefined && n[pro]==proValue)
		{
			value = n[key];
			return false;
		}
	});
	return value;
}

function init()
{
	_selAuthTool = defAuthTool;
	
	if(defAuthTool=='1'){
		sendMobileMsgCnt = 1;
		refresh();
	}
	
	if(language=='zh_CN')
	{
		$('.auth_span').show();
		$('.auth_span a').click(clickChgAuthTool);
		
		if(cardType==2)
		{
			$('#tranPass1').parent().prev().children().attr('color', 'red');
			$('#tranPass1').parent().next().next().css({'clear':'none','float':'left'});
			$('#creditCardDiv').children('div').last().css({'clear':'none','float':'left'});
		}
	}
	else
	{
		$('.auth_span').remove();
		$('#usbTip').remove();
		$('#showSelAuthToolDiv').remove();
		$('#netBankTotalDiv').remove();
		
		$('.nr_xx lanse').find('div span:first').css('text-align','left');
	}
	 
	try{
		parent.document.getElementById("welcomeMsg").innerHTML = PAGES_ALERT_VINFO + '：'+ welcomeMSG+ '&nbsp;&nbsp;&nbsp;' + PAGES_ALERT_PLEASECHECK;
	}catch(e){}
	
	$("#softkeyboard img").mousedown(function(e){
		$(this).attr("style","cursor:hand;filter:progid:DXImageTransform.Microsoft.Chroma(color='white')");
	});
	$("#softkeyboard img").mouseup(function(e){
		$(this).attr("style","");
	});
	createSafeCommitControl(webAppPath);
	
	$('#passwordtxt1').hide();
	
	$('#goBack').click(goBack);
	
	$('#submit').click(submitMainForm);
	
	if("-1"==signType){	
		$('#welcomeMsg').hide();
	} 
	updateDisplay();
}

function mobnote(){
alert(PAGES_TAG_MOBNOTEINFO);

}

 