<!--
/**
 * login.js
 * @fileoverview 登陆功能
 * @author Lynk Li
 */

/**
 * @class PersonSession 客服登陆信息类
 * @constructor
 * @author Lynk Li
 */
function PersonSession() {
	/**
	 * 客服ID
	 */
	this.id = "";
	/**
	 * 客服帐号
	 */
	this.account = "";
	/**
	 * 客服昵称
	 */
	this.nick = "";
	/**
	 * 客服所在公司ID
	 */
	this.company = "";
	/**
	 * 欢迎语-问候语
	 */
	this.hello = "";
	/**
	 * 欢迎语-道别语
	 */
	this.bye = "";
	/**
	 * 客服类型，"A"-管理员
	 */
	this.type = 0;
	/**
	 * 客服职称
	 */
	this.position = "";
	/**
	 * 客服手机号
	 */
	this.phone = "";
	/**
	 * 客服电话号
	 */
	this.telephone = "";
	/**
	 * 客服Email
	 */
	this.email = "";
}

/**
 * @class Login 登陆类
 * @constructor
 * @author Lynk Li
 */
function Login() {
	/**
	 * 登陆公司帐号
	 */
	this.company = "";
	/**
	 * 登陆客服帐号
	 */
	this.account = "";
	/**
	 * 登陆密码
	 */
	this.passwd = "";
	/**
	 * 是否保存登陆密码，0-不保存；1-保存
	 */
	this.savePassword = 0;
	/**
	 * 默认登陆状态，A:离线B:隐身C:在线D:在线不交谈，如果没有设置那么使用"C"
	 */
	this.defaultStatus = "C";
	this.login = Login_login;
	this.loadServerList = Login_loadServerList;
	this.doLogin = Login_doLogin;
	this.checkLogin = Login_checkLogin;
	this.logout = Login_logout;
	this.gotoLoginInterface = Login_gotoLoginInterface;
	this.setLoginAccount = Login_setLoginAccount;
	this.setMyStatus = Login_setMyStatus;
	this.gotoWaitInterface = Login_gotoWaitInterface;
	this.changeMyStatus = Login_changeMyStatus;
	this.saveInfo = Login_saveInfo;
	this.getInfo = Login_getInfo;
	/**
	 * 是否弹出提示框标志
	 */
	this.alertFlag = true;
	/**
	 * 是否重新登陆标志
	 */
	this.reloginFlag = false;
	this.alertFn = Login_alert;
	/**
	 * 登陆错误信息
	 */
	this.error = {
			FSError0011 : _langPackage.login.fsError.FSError0011,
			FSError0012 : _langPackage.login.fsError.FSError0012,
			FSError0015 : _langPackage.login.fsError.FSError0015,
			FSError0019 : _langPackage.login.fsError.FSError0019,
			FSError0025 : _langPackage.login.fsError.FSError0025,
			FSError2 	: _langPackage.login.fsError.FSError2,
			FSError3 	: _langPackage.login.fsError.FSError3,
			FSError4 	: _langPackage.login.fsError.FSError4,
			FSError6 	: _langPackage.login.fsError.FSError6,
			FSError7 	: _langPackage.login.fsError.FSError7,
			FSError9995 : _langPackage.login.fsError.FSError9995
		};
	this.relogin = Login_relogin;
	this.reloginOk = Login_reloginOk;
	this.connectStatus = Login_connectStatus;
}

/**
 * 提示访客信息
 * @param String str 提示内容
 */
function Login_alert(str) {
	if (this.alertFlag) {
		alert(str);
	}
}

/**
 * 登陆函数
 * @param String ip 服务器IP字符串（备用）
 * @param int port 服务器端口（备用）
 * @param String companyid 公司帐号
 * @param String username 客服帐号
 * @param String userpswd 客服登陆密码
 * @param #gotoWaitInterface
 * @see #loadServerList
 */
function Login_login(ip, port, companyid, username, userpswd) {
	if (_common.myTrim(companyid) == "") {
		this.alertFn(_langlogin.getLang('InfoLack', 'Company'));
		return false;
	}
	
	if (_common.myTrim(username) == "") {
		this.alertFn(_langlogin.getLang('InfoLack', 'Account'));
		return false;
	}
	
	this.company = companyid;
	this.account = username;
	this.passwd = userpswd;
	if (this.alertFlag) {
		this.defaultStatus = document.getElementById("status").value;
		if (document.getElementById("savePassword").checked) this.savePassword = 1;
		else this.savePassword = 0;
	}
	
	if (userpswd == "") {
		this.alertFn(_langlogin.getLang('InfoLack', 'Password'));
		return false;
	}
	if (this.alertFlag) this.gotoWaitInterface();
	this.loadServerList(_setting.xmlsPath + "server.xml", companyid.toLowerCase(), username, hex_md5(userpswd));
}
/**
 * 获取配置文件中的LoginServer列表，并顺序连接，直到连上一个服务器为止。如果所有都连不上，提示用户连接失败。
 * @param String path 服务器列表XML URL
 * @param String companyid 公司帐号
 * @param String username 客服帐号
 * @param String userpswd MD5加密后的客服登陆密码
 * @see #doLogin
 */
function Login_loadServerList(path, companyid, username, userpswd) {
	var xmlhttp = xmlhttpInit();
	xmlhttp.open("GET", path, true); 
	xmlhttp.onreadystatechange = function() { 
		if (xmlhttp.readyState == 4) {//异步模式
			var xmlObj = xmlhttp.responseXML;
			var longinservers = xmlObj.getElementsByTagName("LoginServer");
			var arrLoginServers = new Array();
			for (var i = 0; i < longinservers.length; i++) {
				if (longinservers[i].childNodes && longinservers[i].childNodes.length == 2) {
					var lsip = longinservers[i].childNodes.item(0).childNodes.item(0).nodeValue;
					var lsport = longinservers[i].childNodes.item(1).childNodes.item(0).nodeValue;
					if (lsip && lsip.length > 0 && lsport && lsport.length > 0) {
						var loginServer = {ip:lsip, port:lsport};
						arrLoginServers.push(loginServer);
					}
				}
			}
			_login.doLogin(arrLoginServers, companyid, username, userpswd);
		} 
	}  
	xmlhttp.send(null);
}

/**
 * 按照LS列表中的顺序，依次登陆
 * @param Array serverList 服务器列表数组
 * @param String companyid 公司帐号
 * @param String username 客服帐号
 * @param String userpswd MD5加密后的客服登陆密码
 * @see #checkLogin
 */
function Login_doLogin(serverList, companyid, username, userpswd) {
	//alert("Login_doLogin(serverList, '"+companyid+"', '"+username+"', '"+userpswd+"')");
	if (serverList.length == 0) {
		if (this.alertFlag) {
			document.getElementById("loginDiv").style.display = "";
			document.getElementById("loginWaitDiv").style.display = "none";
			this.alertFn(_langlogin.getLang('InfoFailed', 'Failed'));
			if (document.loginform.companyid.value == "") document.loginform.companyid.focus();
			else if (document.loginform.username.value == "") document.loginform.username.focus();
			else document.loginform.userpswd.focus();
		} else {
			this.relogin();
		}
		return false;
	}
	
	if (this.alertFlag && (typeof webcl == "undefined" || webcl == null)) {
		document.getElementById("loginDiv").style.display = "";
		document.getElementById("loginWaitDiv").style.display = "none";
		this.alertFn(_langlogin.getLang('InfoFailed', 'OcxError'));
		if (document.loginform.companyid.value == "") document.loginform.companyid.focus();
		else if (document.loginform.username.value == "") document.loginform.username.focus();
		else document.loginform.userpswd.focus();
		return false;
	}
	
	var loginServer = serverList.shift();
	//alert(loginServer.port+"@"+loginServer.ip + " " + companyid + " " + username + " " + userpswd);
	webcl.Login(loginServer.ip, loginServer.port, companyid, username, userpswd);
	if (this.alertFlag) {
		var i = 20;
		setTimeout(function(){_login.checkLogin(serverList, companyid, username, userpswd, i);}, 1000);
	}
}

/**
 * 判断是否成功登陆
 * 如果是连接服务器失败，那么继续连接下一个服务器。
 * 如果是登陆失败，提示登陆失败，例如用户名错误
 * @param Array serverList 服务器列表数组
 * @param String companyid 公司帐号
 * @param String username 客服帐号
 * @param String userpswd MD5加密后的客服登陆密码
 * @param String timeout 超时数
 */
function Login_checkLogin(serverList, companyid, username, userpswd, timeout) {
	var loginRs = webcl.GetToWebMsg();
	if (!loginRs || loginRs == null || loginRs == "") {
		timeout--;
		if (timeout == 0) {
			this.doLogin(serverList, companyid, username, userpswd);
		} else {
			setTimeout(function(){_login.checkLogin(serverList, companyid, username, userpswd, timeout);}, 500);
		}
	} else {
		var msgString = new HJWebClMsgString(loginRs);
		//if (loginRs != "") this.alertFn(loginRs);
		//alert(loginRs);
		for (var i = 0; i < msgString.messages.length; i++) {
			switch (parseInt(msgString.messages[i].msgId)) {
				case 9000:
					var exception = msgString.messages[i].commands[0];
					if (exception == "0") {
						if (this.alertFlag) {
							try {
								var data = {
									images_path	: _setting.imagesPath,
									wait_info	: _langPackage.login.infoConnect.lsSucess
									};
								var str = TrimPath.processDOMTemplate("main_wait_tpl", data);
								document.getElementById("loginWaitDiv").innerHTML = str;
							} catch (e) {
								_common.alertDebug(e);
							}
						}
					} else {
						if (exception == "2") {
							if (this.alertFlag) {
								document.getElementById("loginDiv").style.display = "";
								document.getElementById("loginWaitDiv").style.display = "none";
								this.alertFn(_langPackage.login.lsError.LSError2);
								if (document.loginform.companyid.value == "") document.loginform.companyid.focus();
								else if (document.loginform.username.value == "") document.loginform.username.focus();
								else document.loginform.userpswd.focus();
							}
							return false;
						} else {
							this.doLogin(serverList, companyid, username, userpswd);
							return false;
						}
					}
					break;
				case 9001:
					var exception = msgString.messages[i].commands[0];
					//9001;ErrorID[错误ID];CompanyID[公司ID];UserID[客服ID];[客服列表版本号];[类别列表版本号];[监控页面列表版本号];[客服状态列表版本号];[请求类别与客服关系列表];[请求预存消息列表];[请求域名绑定列表];CompanyType;InviteNum;InviteUseNum;WatchNum;WatchUseNum;是否开通电话
					//this.alertFn(msgString.messages[0].commands);
					if (exception == "0") {
						_safeToLogout = false;
						_common.loginParam = msgString.messages[i].commands;
						if (this.alertFlag) {
							_personSession.account = username;
							_personSession.company = msgString.messages[i].commands[1];;
							this.saveInfo();
							_common.showFrame();
							//_monitor.gotoMonitorInterface();
							_common.loadDatas();
							_monitor.sendMsgLoadedList();
						} else {
							if (!_login.reloginFlag) return;
							if (msgString.messages[i].commands[0] != "0" && msgString.messages[i].commands[0] != "102" && msgString.messages[i].commands[0] != "0011") _login.relogin();
							else if (msgString.messages[i].commands[0] == "0") {
								_common.send("<M><C>9002</C></M>");
								_login.reloginFlag = false;
							}
						}
						return true;
					} else {
						if (this.alertFlag) {
							document.getElementById("loginDiv").style.display = "";
							document.getElementById("loginWaitDiv").style.display = "none";
							if (eval("this.error.FSError" + exception)) eval("this.alertFn(this.error.FSError" + exception + ");");
							else this.alertFn(_langPackage.login.infoConnect.failed);
							if (document.loginform.companyid.value == "") document.loginform.companyid.focus();
							else if (document.loginform.username.value == "") document.loginform.username.focus();
							else document.loginform.userpswd.focus();
						}
						return false;
					}
					break;
				case 100:
					this.doLogin(serverList, companyid, username, userpswd);
					return;
			}
		}
		setTimeout(function(){_login.checkLogin(serverList, companyid, username, userpswd, timeout);}, 500);
	}
}

/**
 * 设置客服登陆帐号信息
 * @param String account 客服帐号
 * @param String nick 客服昵称
 */
function Login_setLoginAccount(account, nick) {
	var div = document.getElementById("loginAccountDiv");
	div.innerHTML = _langPackage.login.infoAccount.account + account + "[" + nick + "]&nbsp;";
}

/**
 * 设置客服登陆后状态
 * @param Stirng status 客服状态值
 */
function Login_setMyStatus(status) {
	var div = document.getElementById("myStatusDiv");
	var statusSelect = "<select id='myStatusSelect' onChange='_login.changeMyStatus();'>";
	for (var i = 0; i < _common.arrOperatorStatus.length; i++) {
		var stObj = _common.arrOperatorStatus[i];
		if (stObj.value == "A") continue;
		var sel = "";
		//if (stObj.id == status) sel = "selected";
		if (stObj.value == this.defaultStatus) sel = "selected";
		statusSelect += "<option value='" + stObj.id + "#" + stObj.value + "' " + sel + ">" + stObj.label + "</option>";
	}
	statusSelect += "</select>";
	div.innerHTML = statusSelect;
}

/**
 * 当前客服改变状态，相应消息&lt;M>&lt;C>9008;" + ID + ";" + Value + "&lt;/C>&lt;/M>
 */
function Login_changeMyStatus() {
	try {
		var myStatus = document.getElementById("myStatusSelect").value;
		var aStatus = myStatus.split("#");
		_common.send("<M><C>9008;" + aStatus[0] + ";" + aStatus[1] + "</C></M>");
	} catch (exc) {
		_common.alertDebug(exc);
	}
}

/**
 * 退出系统
 */
function Login_logout() {
	if (window.confirm(_langPackage.login.infoLogout.confirmInfo)) {
		try {
			webcl.ExitSys();
		} catch (exc) {
			_common.alertDebug(exc);
		}
		_safeToLogout = true;
		window.close();
	}
}

/**
 * 跳转到登陆界面
 */
function Login_gotoLoginInterface() {
	this.getInfo();
	var checked = "";
	var arrStatus = new Array("B", "C", "D");
	var arrStatusName = new Array(_langPackage.login.opStatusLabel.B, _langPackage.login.opStatusLabel.C, _langPackage.login.opStatusLabel.D);
	if (this.savePassword) checked = "checked=\"checked\"";
	var strOptions = "";
	for (var i = 0; i < arrStatus.length; i++) {
		strOptions += "<option value=\"" + arrStatus[i] + "\"";
		if (this.defaultStatus == arrStatus[i]) strOptions += " selected";
		strOptions += ">" + arrStatusName[i] + "</option>"
	}
	var data = {
		images_path	: _setting.imagesPath,
		company		: this.company,
		account		: this.account,
		passwd		: this.passwd,
		strOptions	: strOptions,
		checked		: checked
		};
	var str = TrimPath.processDOMTemplate("login_tpl", data);
	document.getElementById("mainDiv").innerHTML = str;	
	if (document.loginform.companyid.value == "") document.loginform.companyid.focus();
	else if (document.loginform.username.value == "") document.loginform.username.focus();
	else document.loginform.userpswd.focus();
	var data = {
		images_path	: _setting.imagesPath,
		wait_info	: _langPackage.login.infoConnect.waitTxt
		};
	var str = TrimPath.processDOMTemplate("main_wait_tpl", data);
	document.getElementById("loginWaitDiv").innerHTML = str;
}

/**
 * 跳转到等待界面
 */
function Login_gotoWaitInterface() {
	document.getElementById("loginDiv").style.display = "none";
	document.getElementById("loginWaitDiv").style.display = "";
}

/**
 * 将登陆信息保存到Cookie中，以便下次登陆使用
 */
function Login_saveInfo() {	
	Cookie_set("HJWEBCL_COMPANY", this.company, 365*24*60*60);
	Cookie_set("HJWEBCL_ACCOUNT", this.account, 365*24*60*60);
	Cookie_set("HJWEBCL_DEFAULTSTATUS", this.defaultStatus, 365*24*60*60);
	if (this.savePassword == 0) {
		Cookie_set("HJWEBCL_PASSWD", "", 1);
		Cookie_set("HJWEBCL_SAVEPASSWD", "0", 365*24*60*60);
	} else {
		Cookie_set("HJWEBCL_PASSWD", this.passwd, 365*24*60*60);
		Cookie_set("HJWEBCL_SAVEPASSWD", "1", 365*24*60*60);
	}
}

/**
 * 从Cookie中获取登陆信息
 */
function Login_getInfo() {
	var company = Cookie_get("HJWEBCL_COMPANY");
	var account = Cookie_get("HJWEBCL_ACCOUNT");
	var passwd = Cookie_get("HJWEBCL_PASSWD");
	var status = Cookie_get("HJWEBCL_DEFAULTSTATUS");
	var save = Cookie_get("HJWEBCL_SAVEPASSWD");
	if (company != null) this.company = company;
	if (account != null) this.account = account;
	if (status != null) this.defaultStatus = status;
	if (save != null) this.savePassword = parseInt(save);
	else {
		save = 0;
		this.savePassword = 0;
	}
	if (save == 1) {
		if (passwd != null) this.passwd = passwd;
	} else {
		this.passwd = "";
	}
}

/**
 * 提示系统处于断线状态
 * @param String status 当前与服务器的连接状态
 */
function Login_connectStatus(status) {
	var connectDiv = document.getElementById("connectDiv");
	switch (parseInt(status)) {
		case 1:
			connectDiv.innerHTML = '<img src="' + _setting.imagesPath + 'fettle_out.gif" height="14px" width="14" alt="' + _langPackage.login.infoConnect.statusDisconnect + '">';
			window.status = _langPackage.login.infoConnect.statusDisconnect;
			break;
		case 2:
		case 3:
			connectDiv.innerHTML = '<img src="' + _setting.imagesPath + 'fettle_ing.gif" height="14px" width="14" alt="' + _langPackage.login.infoConnect.statusReconnect + '">';
			window.status = _langPackage.login.infoConnect.statusReconnect;
			break;
		default:			
			connectDiv.innerHTML = '<img src="' + _setting.imagesPath + 'fettle_on.gif" height="14px" width="14" alt="' + _langPackage.login.infoConnect.statusConnect + '">';
			window.status = _langPackage.login.infoConnect.statusConnect;
	}
}

/**
 * 断线重新连接
 */
function Login_relogin() {
	if (!_login.reloginFlag) return;
	this.alertFlag = false;
	this.login("", "", this.company, this.account, this.passwd);
}

/**
 * 重新登陆完成
 */
function Login_reloginOk() {
	_monitor.deleteAllVisitors();
	this.changeMyStatus();
}

var _personSession = new PersonSession();
var _login = new Login();
//-->