﻿/** 
 * @author WELL
 * @description 获取背景
 * @param {String} psDirect "Prev"-上一张，"ChangeByTime"-按时间，"Next"-下一张。
 * @param {String} psBg psDirect="Prev"或psDirect="Next"时，代表当前背景。
 * @version 
 */
function sGetChangeBg(psDirect,psBg){
	var laBg = new Array(
		"url(bg_1.jpg)",
		"url(bg_2.jpg)",
		"url(bg_3.jpg)",
		"url(bg_4.jpg)",
		"url(bg_5.jpg)",
		"url(bg_6.jpg)",
		"url(bg_default.jpg)"
	);
	var liIndex;
	if(psDirect == "ChangeByTime"){
		var laTime = new Array(
			"08:00:00-12:00:00",
			"12:00:00-14:00:00",
			"14:00:00-15:00:00",
			"15:00:00-17:00:00",
			"17:00:00-19:00:00",
			"19:00:00-22:00:00"
		);
		liIndex = 6;	//默认
		var lsCurTime = (new Date()).Format("hh:mm:ss");
		var liCurTime = parseInt(lsCurTime.replace(/:/g,""), 10);
		for(var i=0; i<laTime.length; i++){
			var liStart = parseInt(laTime[i].substr(0,8).replace(/:/g,""), 10);
			var liEnd = parseInt(laTime[i].substr(9,8).replace(/:/g,""), 10);
			if(liCurTime >= liStart && liCurTime < liEnd){
				liIndex = i;
				break;
			}
		}
	}
	else{
		var lsBg = ($null(psBg)||psBg=="")? "url(bg_default.jpg)": psBg;
		liIndex = laBg.indexOf(lsBg);
		if(psDirect=="Prev"){
			liIndex--;
			if(liIndex < 0) liIndex = laBg.length-1;
		}
		else if(psDirect=="Next"){
			liIndex++;
			if(liIndex >= laBg.length) liIndex=0;
		}
	}
	return laBg[liIndex];
}

/** 
 * @author WELL
 * @description 切换背景
 * @param {String} psDirect "Prev"-上一张，"Default"-默认，"Next"-下一张。
 * @version 
 */
function ChangeBg(psDirect){
	var lsBg;
	var loMiddle = $Id("ID_Middle");
	if(psDirect == "ChangeByTime"){
		bSetCookie("LoginBgImg", "ChangeByTime", 1000);
		loMiddle.style.backgroundImage = sGetChangeBg("ChangeByTime");
	}
	else{
		var lsBg = sGetChangeBg(psDirect,loMiddle.style.backgroundImage)
		loMiddle.style.backgroundImage = lsBg;
		bSetCookie("LoginBgImg",lsBg,1000);
	}
}

/** 
 * @author WELL
 * @description 关闭浏览器
 * @version 
 */
function selfClose(){
	var loBrowser = new Browser();
	if(loBrowser.IsIE && loBrowser.Version < 5.5){
		self.focus();
		document.all.hAutoClose.Click();
	}else{
		top.opener = window;
		top.close();
	}
}

/** 
 * @author WELL
 * @description 验证IE浏览器版本是否通过
 * @param {Boolean} pbNotPromptTrue 如不通过是否弹出提示，false-弹出，true-不弹出。
 * @return {Boolean} 布尔型 true通过，false不通过
 * @version 
 */
function bCheckIEVersion(pbNotPromptTrue){
	var loBrowser = new Browser();
	if(loBrowser.IsIE && loBrowser.Version >= 6){
		if (pbNotPromptTrue!=true) alert(sGetLang("SYSRES_BROWSERISVALID"));
		return true;
	}
	else{
		alert(sGetLang("SYSRES_BROWSERNOTVALID"));
		return false;
	}
}

/** 
 * @author WELL
 * @description 下载验证码图片
 * @version 
 */
function loadimage(){
    document.getElementById("ID_RandImage").src = "/servlet/CreateJPG?"+Math.random();
}

/** 
 * @author WELL
 * @description 验证验证码
 * @return {Boolean} 布尔型 true通过，false不通过
 * @version 
 */
function bValidCode(){
	if (typeof document.forms[0].Code!="object") return true;
	if (document.forms[0].Code.value=="" || document.forms[0].Code.value.length!=4){
		alert(sGetLang("SYSRES_VALIDCODEINVALID"));
		document.forms[0].Code.focus();
		return false;
	}
	var xmlhttp = oGetXmlHttpRequest();
	if (xmlhttp==null) return true;
	xmlhttp.open("GET","/servlet/CreateJPG?rand="+document.forms[0].Code.value, false);
	xmlhttp.send();
	var lsText = xmlhttp.responseText;
	xmlhttp = null;
	if (lsText!=null && lsText.indexOf("SUCCESS")!=-1)
		return true;
	else{
		alert(sGetLang("SYSRES_VALIDCODEINVALID"));
		loadimage();
		document.forms[0].Code.focus();
		return false;
	}
}

/** 
 * @author WELL
 * @description 验证Rockey
 * @return {Boolean} 布尔型 true通过，false不通过
 * @version 
 */
function bValidRockey(){
	var lsUserName = goForm.Username.value;
	var loRockey = new CRockey();
	if(!loRockey.bServerVertify(lsUserName,4))return true;	//判断用户是否需要身份认证锁登录
	return bCheckRockey(lsUserName);
}

/** 
 * @author WELL
 * @description 提交表单
 * @return {Boolean} 布尔型 true提交成功，false提交不成功
 * @version 
 */
function bSubmitForm(){
	if(bCheckIEVersion(true)==false) return false;
	
	if($Id("ID_MessagePane").style.display == "") return false;
	
	if(goForm.Username.value == ""){
		alert(sGetLang("SYSRES_USERNAMEISEMPTY")); 
		return false;
	}
	if(goForm.Password.value == ""){
		alert(sGetLang("SYSRES_PASSWORDISEMPTY")); 
		return false;
	}
	LoginCount();
	if (bValidCode()==false) return false;

	bSetCookie("InputUsername",goForm.Username.value,0,"/");

	if (goForm.rUserName.checked)
		bSetCookie("LoginName",goForm.Username.value,10000,"/");
	else
		bDelCookie("LoginName");
	
	if (goForm.rPassword.checked)
		bSetCookie("LoginPW",goForm.Password.value,10000,"/");
	else
		bDelCookie("LoginPW");
	
	if(typeof(document.all.Rockey) == "object" && Rockey.innerText == "1")
		if(!bValidRockey())return false;
	
	if(!goForm.rFullScreen.checked)
		bSetCookie("FullScreen","0",10000,"/");
	else
		bSetCookie("FullScreen","1",10000,"/");
	bDelCookie("LogoutFullScreen");
	
	goForm.submit();
}

/** 
 * @author WELL
 * @description 键盘按下事件
 * @version 
 */
function CheckKeyPress(e){
	var lins=(window.Event)?1:0;
	var liCode=(lins)?e.which:event.keyCode;
	if(liCode=="13"){		
		bSubmitForm();
		return false;
	}
}

/** 
 * @author WELL
 * @description document的键盘按下事件
 * @version 
 */
function CheckKeyDown(e){
	var lins=(window.Event)?1:0;
	var liCode=(lins)?e.which:event.keyCode;
	if(liCode=="9"){
		if($Id("ID_MessagePane").style.display == "") return false;
	}
}


/** 
 * @author WELL
 * @description 硬件登陆
 * @version 
 */
function RockeyLogin(){
	var lsUsername="",lsPassword="";
	var goForm = document.forms[0];
	var loEPass = document.forms[0].ePass;
	try{
		loEPass.OpenDevice(1,0);
	}
	catch(e){
		alert(sGetLang("SYSRES_INSERTROCKEY"));
		return false;
	}
	var loRockey = new CRockey(loEPass);
	try{
		loRockey.sSerialNum = loEPass.GetStrProperty(7,0,0);
		loEPass.ChangeDir(0x300,0,"WELLSOFT");
		loEPass.OpenFile(0,5);
		loEPass.GetFileInfo(0,3,5,0);
	}
	catch(e){
		loEPass.CloseDevice();
		alert(sGetLang("SYSRES_READROCKEYERROR"));
		return false;
	}
	LoginCount();
	try{
		//读取锁里的用户名、密码
		var liLength1 = loRockey.HexToDec(loEPass.Read(0,1,0,1));
		var liLength2 = loRockey.HexToDec(loEPass.Read(0,1,1,1));
		var liLength3 = loRockey.HexToDec(loEPass.Read(0,1,2,1));
		
		for(var i = 1;i<liLength2/2+1;i++){
			liOffSet = 8+liLength1+2*(i-1);
			lsLow = loEPass.Read(0,1,liOffSet,1);
			lsHigh = loEPass.Read(0,1,liOffSet+1,1);
			lsUsername = lsUsername + String.fromCharCode(loRockey.HexToDec(lsHigh+lsLow));
		}
		
		for(var i = 1;i<liLength3/2+1;i++){
			liOffSet = 8+liLength1+liLength2+2*(i-1);
			lsPassword = lsPassword + loEPass.Read(0,0,liOffSet,2);
		}
		
	}
	catch(e){
		loEPass.CloseDevice();
		alert(sGetLang("SYSRES_ISROCKEYUSERINVALID"));
		return false;
	}
	
	var loXmlDoc = loRockey.oRockeyLogin(lsUsername,lsPassword);
	
	loEPass.CloseDevice();
	
	if(loXmlDoc != null && loXmlDoc.selectSingleNode("/root/valid")!=null && loXmlDoc.selectSingleNode("/root/valid").text == "1"){
		goForm.Username.value = lsUsername;
		goForm.Password.value = loXmlDoc.selectSingleNode("/root/password").text;
		if (goForm.rUserName.checked)
			bSetCookie("LoginName",goForm.Username.value,10000,"/");
		else
			bDelCookie("LoginName");
		goForm.submit();
	}
	else{
		ID_ErrorMsg.innerText = sGetLang("SYSRES_INSERTROCKEY");
		return false;
	}
}

/** 
 * @author WELL
 * @description 发送密码到邮箱
 * @version 
 */
function SendPassword(){
	if(document.forms[0].Username.value == ""){
		alert(sGetLang("SYSRES_USERNAMEISEMPTY")); 
		return false;
	}
	if(window.confirm(sGetLang("SYSRES_SENDPASSWORDTOMAIL"))==false) return false;
	var lsURL = "/zConfig.nsf/SendUserPW?openpage";
	window.RunAgent.location.replace(lsURL);
}


/** 
 * @author WELL
 * @description 记录登陆次数
 * @version 
 */
function LoginCount(){
	var liTime=sGetCookie("LoginTime")-0;
	liTime++;
	bSetCookie("LoginTime",""+liTime,0,"/");
}

/** 
 * @author WELL
 * @description 表单加载事件
 * @version 
 */
function LoginOnLoadEvent(){
	if (typeof top.document.all.ID_LoginFormMark!="object" 
		&& top.IntervalName!=null 
		&& typeof top.document.all.hPromptDoingExt=="object"){
		top.location.reload(true);
		return false;
	}
	if (bCheckIEVersion(true)==false) selfClose();
	
	var loMsgPane = $Id("ID_MessagePane");
	var loLoginPane = $Id("ID_LoginPane");
	loMsgPane.style.width = loLoginPane.offsetWidth;
	$Id("ID_LoginCover").style.height = loLoginPane.offsetHeight;
	try{
		document.forms[0].WebInstall.Install("/icons/icons/CInstall.ini");
		loMsgPane.style.display="none";
	}
	catch(e){
		loMsgPane.style.display="";
		return false;
	}
	
	if(sGetCookie("LoginTime")-0>=3){
		alert(sGetLang("SYSRES_LOGINERROROVER"));
		selfClose();
	}
	
	var lsFullScreen = sGetCookie("FullScreen");
	if(lsFullScreen == "0") goForm.rFullScreen.checked = false;
	var lsLogoutFullScreen = sGetCookie("LogoutFullScreen");
	if(lsLogoutFullScreen == "1") document.getElementById("ID_FullScreen").style.display = "none";
	
	lsName=sGetCookie("LoginName");
	if (lsName!=null && lsName!=""){
		goForm.Username.value=lsName;
		goForm.rUserName.checked=true;
		goForm.Password.focus();
		goForm.Password.select();
	}
	else{
		goForm.rUserName.checked=false;
		lsName=sGetCookie("InputUsername");
		if(lsName!=null && lsName!="" && ID_ErrorMsg!=null && ID_ErrorMsg.reasonType=="2"){
			goForm.Username.value=lsName;
			goForm.Password.focus();
			goForm.Password.select();
		}else{
			goForm.Username.focus(); 
			goForm.Username.select();
		}
	}
	lsPassword=sGetCookie("LoginPW");
	if (lsPassword!=null && lsPassword!=""){
		goForm.Password.value=lsPassword;
		goForm.rPassword.checked=true;
	}
	else
		goForm.rPassword.checked=false;
}

/** 
 * @author WELL
 * @description 点击关闭按钮事件
 * @version 
 */
function LoginOncloseEvent(){
	if((event.clientX + 20) > document.body.clientWidth && event.clientY < 0 || event.altKey) bDelCookie("LogoutFullScreen");
}

document.body.loadEditor[document.body.loadEditor.length] = LoginOnLoadEvent;
document.body.onbeforeunload = LoginOncloseEvent;
