
function CAS2_GetUrlParms()   {
	 var args=new Object();  
	 var query=location.search.substring(1);         //获取查询串  
	 var pairs=query.split("&");                              //在逗号处断开  
	 for(var i=0;i<pairs.length;i++)  
	 {  
		 var pos=pairs[i].indexOf('=');                      //查找name=value  
			if(pos==-1)   continue;                            //如果没有找到就跳过  
			 var argname=pairs[i].substring(0,pos); //提取name  
			var value=pairs[i].substring(pos+1);      //提取value  
			args[argname]=unescape(value);         //存为属性  
	}
	 return args;
}

function CAS2_login(){
if (document.CAS2_LOGINFORM.userLoginNo.value == '') {
		alert('用户名不得为空！');
		document.CAS2_LOGINFORM.userLoginNo.focus();
		return false;
	}
	
	if (document.CAS2_LOGINFORM.userLoginPsd.value == '') {
		alert('密码不得为空！');
		document.CAS2_LOGINFORM.userLoginPsd.focus();
		return false;
	}
	if (document.CAS2_LOGINFORM.ccode.value == '') {
		alert('验证码不得为空！');
		document.CAS2_LOGINFORM.ccode.focus();
		return false;
	}
	if(CAS2_getRaidoValue("IS_CA_LOGIN")=="1"){
		if(document.all.caPassword.value==""){
			alert("CA密码不能为空");
			document.CAS2_LOGINFORM.caPassword.focus();
			return false;
		}
		
		var sno ;
		try{
			sno = CAS2_getKeySeqNo();
		}catch(e){
			alert("获取CA信息发生异常"+e.message);
			return false;
		}
		if(sno==false){
			return false;
		}
		document.all.CA_NUM.value = sno;
	}
	document.CAS2_LOGINFORM.submit();
	
}



function CAS2_init(){
	//alert(userLoginNo);
	//alert(userLoginNoCa);

	var args = new Object();
	args =CAS2_GetUrlParms();
	var errorCode=args['CAS2_ERROR_CODE'];
	//var userLoginNoFinal=args['userLoginNo'];
	//alert('ff--'+userLoginNo);undefined
	if(errorCode!=null){
		if("M1000"==errorCode) {
			alert("数字验证码校验失败！");
		} else if("M1001"==errorCode) {
			alert("验证码超时！");
		}  else if("M1002"==errorCode) {
			alert("帐号不存在或者密码错误,请重新登录！");
		}  else if("M1003"==errorCode) {
			alert("用户帐号和密码不能为空!");
		}  else if("M1004"==errorCode) {
			alert("该页面需要登录后访问!");
		}  else if("M2001"==errorCode) {
			alert("特批会员已过期!");
		}  else{
			alert("未知错误!");
		} 
	}
}
	
function CAS2_caSelect(va){
	if(va==1){
		document.getElementById("CAS2_CAPSD").style.display = "block";
	}else{
		document.getElementById("CAS2_CAPSD").style.display = "none";
	}
}


function CAS2_getKeySeqNo(){
	//SafeEngineCtl.SEH_InitialSession( 0x2,"","",0,0x9,"","" );
	SafeEngineCtl.SEH_InitialSession( 0x9,'com1',document.CAS2_LOGINFORM.caPassword.value,0,0x9,"","" );
	if(SafeEngineCtl==""){
		alert("读取 USB KEY 失败! 请安装客户端驱动程序!");
		return false;
	}
	if(SafeEngineCtl.ErrorCode!=0)
	{
	 
		if(SafeEngineCtl.ErrorCode== -2113667069){
			alert("密码错误! 请重新输入!");
 		}else if(SafeEngineCtl.ErrorCode== -2113667019){
			alert("证书初始化失败! 请检查客户段安装文件是否正确!");
 		}else{
			alert("证书初始化失败! 请检查私钥设备!");
		}
		SafeEngineCtl.SEH_ClearSession();
		return false;
	}else{	
		/* 获取自己证书 */
		strCert = SafeEngineCtl.SEH_GetSelfCertificate(0x9,"", "");
		if(SafeEngineCtl.ErrorCode!=0)
		{
			alert("获取证书失败!");
			SafeEngineCtl.SEH_ClearSession();
			return false;
		}
		strcertSeqNo = SafeEngineCtl.SEH_GetCertDetail(strCert,2);
		//document.writeln(strCert);
		//alert(SafeEngineCtl.SEH_GetCertValidDate(strCert));
		//alert(SafeEngineCtl.SEH_VerifyCertificate(strCert));
		//ff.keySeqNo.value=strcertSeqNo;


		try{
			caValidDateEndStr = SafeEngineCtl.SEH_GetCertDetail(strCert,12);//12
			var nowDate=new Date();
			//nowDate.setMonth(nowDate.getMonth()+1);
			var pattern=/^(\d{4})(\d{2})(\d{2})(\d{6})$/;
			var outcome1=pattern.exec(caValidDateEndStr);
			var targetDate = new Date(outcome1[1],outcome1[2]-1,outcome1[3]);
			var flag = (nowDate.getTime()>targetDate.getTime())?true:false;//true：正常，false:超了
			//alert(nowDate.toLocaleString()+"/"+targetDate.toLocaleString()+"="+flag);
			var errorMessage = "";
			//66055 ： 企业证书， 65799 ：个人证书
			if(SafeEngineCtl.SEH_GetCertClass (strCert)=="66055"){
				errorMessage = "您登录的CA证书已于"+targetDate.toLocaleString()+"失效，您不能参加需CA认证的网上竞价及网上招标业务，请您及时续办CA证书，续办流程请见eps.baosteel.net.cn的\"重要通知\"栏的提示信息。";
			}else if(SafeEngineCtl.SEH_GetCertClass (strCert)=="65799"){
				errorMessage = "您登录的CA证书已于"+targetDate.toLocaleString()+"失效，您不能参加网上评标业务，请您及时续办CA证书，续办流程请见eps.baosteel.net.cn的\"重要通知\"栏的提示信息。";
			}
			if(false && flag){
				alert(errorMessage);
				return false;
			}
		}catch(e){
			//alert(e);
		}
		
		

		return strcertSeqNo;
	}
}

function CAS2_getRaidoValue(objName){
	var objs = document.getElementsByName(objName);
	var i,j;
	for(i=0,j=objs.length;i<j;i++){
		if(objs[i].checked){
			return (objs[i].value);
		}
	}
}


function CAS2_login_Ca(){
if (document.CAS2_LOGINFORM_CA.userLoginNo.value == '') {
		alert('用户名不得为空！');
		document.CAS2_LOGINFORM_CA.userLoginNo.focus();
		return false;
	}
	
	if (document.CAS2_LOGINFORM_CA.userLoginPsd.value == '') {
		alert('密码不得为空！');
		document.CAS2_LOGINFORM_CA.userLoginPsd.focus();
		return false;
	}
	if (document.CAS2_LOGINFORM_CA.ccode.value == '') {
		alert('验证码不得为空！');
		document.CAS2_LOGINFORM_CA.ccode.focus();
		return false;
	}

	if(document.all.caPassword.value==""){
		alert("CA密码不能为空");
		document.CAS2_LOGINFORM_CA.caPassword.focus();
		return false;
	}
		
	var sno ;
	try{
		sno = CAS2_getKeySeqNo_Ca();
	}catch(e){
		alert("获取CA信息发生异常"+e.message);
		return false;
	}
	if(sno==false){
		return false;
	}
	document.all.CA_NUM.value = sno;
	
	document.CAS2_LOGINFORM_CA.submit();
	
}


function CAS2_getKeySeqNo_Ca(){
	//SafeEngineCtl.SEH_InitialSession( 0x2,"","",0,0x9,"","" );
	SafeEngineCtl.SEH_InitialSession( 0x9,'com1',document.CAS2_LOGINFORM_CA.caPassword.value,0,0x9,"","" );
	if(SafeEngineCtl==""){
		alert("读取 USB KEY 失败! 请安装客户端驱动程序!");
		return false;
	}
	if(SafeEngineCtl.ErrorCode!=0)
	{
	 
		if(SafeEngineCtl.ErrorCode== -2113667069){
			alert("密码错误! 请重新输入!");
 		}else if(SafeEngineCtl.ErrorCode== -2113667019){
			alert("证书初始化失败! 请检查客户段安装文件是否正确!");
 		}else{
			alert("证书初始化失败! 请检查私钥设备!");
		}
		SafeEngineCtl.SEH_ClearSession();
		return false;
	}else{	
		/* 获取自己证书 */
		strCert = SafeEngineCtl.SEH_GetSelfCertificate(0x9,"", "");
		if(SafeEngineCtl.ErrorCode!=0)
		{
			alert("获取证书失败!");
			SafeEngineCtl.SEH_ClearSession();
			return false;
		}
		strcertSeqNo = SafeEngineCtl.SEH_GetCertDetail(strCert,2);
		//document.writeln(strCert);
		//alert(SafeEngineCtl.SEH_GetCertValidDate(strCert));
		//alert(SafeEngineCtl.SEH_VerifyCertificate(strCert));
		//ff.keySeqNo.value=strcertSeqNo;


		try{
			caValidDateEndStr = SafeEngineCtl.SEH_GetCertDetail(strCert,12);//12
			var nowDate=new Date();
			//nowDate.setMonth(nowDate.getMonth()+1);
			var pattern=/^(\d{4})(\d{2})(\d{2})(\d{6})$/;
			var outcome1=pattern.exec(caValidDateEndStr);
			var targetDate = new Date(outcome1[1],outcome1[2]-1,outcome1[3]);
			var flag = (nowDate.getTime()>targetDate.getTime())?true:false;//true：正常，false:超了
			//alert(nowDate.toLocaleString()+"/"+targetDate.toLocaleString()+"="+flag);
			var errorMessage = "";
			//66055 ： 企业证书， 65799 ：个人证书
			if(SafeEngineCtl.SEH_GetCertClass (strCert)=="66055"){
				errorMessage = "您登录的CA证书已于"+targetDate.toLocaleString()+"失效，您不能参加需CA认证的网上竞价及网上招标业务，请您及时续办CA证书，续办流程请见eps.baosteel.net.cn的\"重要通知\"栏的提示信息。";
			}else if(SafeEngineCtl.SEH_GetCertClass (strCert)=="65799"){
				errorMessage = "您登录的CA证书已于"+targetDate.toLocaleString()+"失效，您不能参加网上评标业务，请您及时续办CA证书，续办流程请见eps.baosteel.net.cn的\"重要通知\"栏的提示信息。";
			}
			if(false && flag){
				alert(errorMessage);
				return false;
			}
		}catch(e){
			//alert(e);
		}
		
		

		return strcertSeqNo;
	}
}