$(document).ready(function(){
	$('#loginSubmit').click(function(){
		var userICCardInfo = null;
		var userPassword = $('#loginPassword').val();
		
		if (userPassword == 0)
		{
			alert("密码不能为空!");
			$('#loginPassword').focus();
			return false;
		}
		
		if (len(userPassword)!=8)
		{
			alert("密码必须为8位!");
			$('#loginPassword').focus();
			return false;
		}
		
		try
		{
			spwebOcx.InitCpuCard();
		}
		catch(e)
		{
			alert("打开数字证书失败，可能的原因是:\n1)没有插入数字证书;\n2)插入的数字证书不正确;\n3)没有下载正确的数字证书;");
			return false;
		}
		
		if (!getUserICCardInfo())
			return false;
			
		spwebOcx.UserPWD = userPassword;
		spwebOcx.verifyPassWD();
		
		if (spwebOcx.nResult != 0)
		{
			if(spwebOcx.nResult==51560 || spwebOcx.nResult==51570)
			{
				alert("您的数字证书口令已被锁定");
			}
			else if(spwebOcx.nResult>51570 && spwebOcx.nResult<=51580)
			{
				var t=spwebOcx.nResult-51570;
				alert("输入口令错误，您还可以重试"+t+"次");
			}
			else
				alert(spwebOcx.strResult);
				
			spwebOcx.CloseCpuCard();
			return false;	
		}
		else
		{
			if (userPassword == '88888888')
			{
				alert("您数字证书当前的密码是默认密码,为了保证您使用的安全性,请尽快修改您的密码.");
			}
			spwebOcx.CloseCpuCard();
		}	
		
		return true;
	});
	
	function getUserICCardInfo()
	{
		spwebOcx.getItemUserInfo();
		if (spwebOcx.nResult != 0)
		{
			alert(spwebOcx.strResult);
			spwebOcx.CloseCpuCard();
			return false;
		}
		
		userICCardInfo = splitString(spwebOcx.multiUserCertInfo,"||");
		$('#userICCode').val(userICCardInfo[5]);
		return true;
	}
	
	function splitString(source, split)
	{
		if (source == null || source.length < 1)
			return new Array();
			
		var strArray;
		if (split == null || split.length < 1)
		{
			strArray = new Array(1);
			strArray[0] = source;
			return strArray;
		}
		else
		{
			strArray = source.split(split);
			return strArray;
		}	
	}
	
	function len(s) { 
var l = 0; 
var a = s.split(""); 
for (var i=0;i<a.length;i++) { 
if (a[i].charCodeAt(0)<299) { 
l++; 
} else { 
l+=2; 
} 
} 
return l; 
}
	$('#updateSubmit').click(function(){
		if ($('#oldpassword').val() == 0)
		{
			alert("旧的密码不能为空!");
		    $('#oldpassword').focus();
			return false;
		}
		if ($('#newpassword1').val() == 0)
		{
			alert("新的密码不能为空!");
		    $('#newpassword1').focus();
			return false;
		}
		if (len($('#newpassword1').val())!=8)
		{
			alert("密码必须为8位!");
			$('#newpassword1').focus();
			return false;
		}
		if ($('#newpassword2').val() != $('#newpassword1').val())
		{
			alert("前后两次新密码不一致,请重新输入!");
		    $('#newpassword2').focus();
			return false;
		}			
				
		try
		{
			spwebOcx.InitCpuCard();
		}
		catch(e)
		{
			alert("打开数字证书失败，可能的原因是:\n1)没有插入数字证书;\n2)插入的数字证书不正确;\n3)没有下载正确的数字证书;");
			return false;
		}
		
		spwebOcx.UserPWD = $('#oldpassword').val();
		spwebOcx.NewUserPWD = $('#newpassword2').val();
		spwebOcx.changePasswd();	
		
		if (spwebOcx.nResult != 0)
		{
			alert("修改密码失败,错误代码:"+spwebOcx.nResult);
			spwebOcx.CloseCpuCard();
			return false;
		}	
		
		return true;
	});
});
