

function ukValidate(){
	var ukType = 0;
	try{
		var instance = new AInstance();
  		//初始化UK全局变量
		instance.InitResource();
		//查找UK相应设备
		var ret = instance.FindDevice(0,1,'Ed@!J*kl');
		if(ret>=0){
			//打开UK
			ret = instance.open();
			if( ret >= 12 )
			{
			
			//is_Hex=0表示直接读字符串，is_Hex=1表示读字符的16进制字符串
			var serialNum = instance.getserialnumfuntion(0);
			if(serialNum<0){
				ret = instance.close();
				instance.CloseResource();
				alert("读取序列号发生错误！");
				url="/ukErrorMessageForwordIndex.jhtml";
				window.location.href=url;
				return false;		
			}
			
			//获取普通权限
			ret = instance.passwd(1, 'Ed@!J*kl');
			//获取usbkeysn
			var usbkeysn = instance.readfunction(0,0,4,20);
			
			//根据序列号查询PIN
			$.ajax({
					url:"/aguserlockedUsbkeyAjax/getUsbkeyPinForInf.jhtml",
					data:"usbkeysn="+usbkeysn+"&number="+Math.random(),
					type: "POST",
					async:false,
					cache: false,
					success: function(resPin){
						var usbkeyPin = resPin;
						
						//获取权限
						ret = instance.passwd(2,usbkeyPin);
						if(ret == 0)
						{
							var usbkeysn = instance.readfunction(0,0,4,20);
							 var urlvalue1="/checkUsbkeySn.jhtml?SN="+usbkeysn;
							$.ajax({
								type: "post",
					            async:false,
					            cache:false,  
					            url: encodeURI(encodeURI(urlvalue1)),
								success: function(res){
									ret = instance.close();
									instance.CloseResource();
									
									var result1 = res;
						         	if(result1!=1){
						         	   ukType = 1;
					         		}					         		
								}
							});
						}else{
							ret = instance.close();
							instance.CloseResource();
							alert("权限获取错误");
							url="/ukErrorMessageForwordIndex.jhtml";
							window.location.href=url;
							return false;
						}
					}
				});
			}else{
				instance.CloseResource();
				alert("打开设备错误");				
				url="/ukErrorMessageForwordIndex.jhtml";
				window.location.href=url;
				return false;
			}
		}else{	
			//未找到新版UK，关闭新版UK
			instance.CloseResource();
			ukType = 1;
		}
	}catch(e)
  	{
  		ukType = 1;
  	}
	if(ukType == 1){
		  //校验usbkey
	      try{
		    //取到控件
			/* 是否安装驱动 */
		  	try{
		  		//取到控件
				var ePass = new ActiveXObject("ET99_FULL.ET99Full.1");
				}catch(error){		
				alert("请先安装驱动!");
				url="/ukErrorMessageForwordIndex.jhtml";
				window.location.href=url;
				return false;
			}
			//usbkeyID(硬件id号)
			var ePassPID = "FFC5EB78";	
			var soPIN="FFC5EB786A2F0D73";	
			
			/* 是否插入UK */
			try{
				//插入的usbkey个数
				var ePass_Num = ePass.FindToken(ePassPID);
				}catch(error){		
				alert("请插入USBKEY!");
				url="/ukErrorMessageForwordIndex.jhtml";
				window.location.href=url;
				return false;
			}									      
			//usbkey PIN值
			if(ePass_Num!=1){
				alert("请插入1个USBKEY!");
				url="/ukErrorMessageForwordIndex.jhtml";
				window.location.href=url;
				return false;
			}else{
			    //打开硬件
			   	 try{
					var ss=ePass.OpenToken(ePassPID,1);
				 }catch(error){
					alert("打开usbkey设备错误!.");
					url="/ukErrorMessageForwordIndex.jhtml";
					window.location.href=url;
					return false;
				 }
				 //校验pin值是否正确
			    try{
					ePass.VerifyPIN(1,soPIN);
						 //取出usbkey SN号
						 try{
							var sn = ePass.Read(0,2,11);
						 }catch(error){
							alert("打开usbkey设备错误!");
							url="/ukErrorMessageForwordIndex.jhtml";
							window.location.href=url;
							return false;
						 }
			   	 }catch(error){
			   	    alert("检验usbkeyPIN值错误!");
			   	    url="/ukErrorMessageForwordIndex.jhtml";
					window.location.href=url;
					return false;
			   	 } 
			   	 //获取密钥
			   	/* var userKeyRandom = "${sessionScope.userKeyRandom}";
					try{
						var clientDigest = ePass.MD5HMAC(1,userKeyRandom,12);															
					}catch(error){
						alert("获取userKey错误!");
	   								url="/index.do?method=logout";
					    window.location.href=url;
				    }*/
			   	  //验证用户绑定的usbkey是否正确
			   	  var urlvalue1="/checkUsbkeySn.jhtml?SN="+sn;
			   	  $.ajax({
						url:encodeURI(encodeURI(urlvalue1)),
						type: "POST",
						cache: false,
						success: function(res1){
							var result1 = res1;
				         	if(result1!=1){
				         	   alert("您插入usbkey的序号不对,请核实!");
			         		   url="/ukErrorMessageForwordIndex.jhtml";
							   window.location.href=url;
							   return false;
			         		 }
						}
						});					   	 
			}
		  }catch(error){
				alert("USBKEY未加载或USBKEY没有插入!");
				url="/ukErrorMessageForwordIndex.jhtml";
				window.location.href=url;
				return false;
		  }
	}										  
}
