
var objtxtUserId;
var objtxtPassword;
var objtxtValid;
var objSelectOnline;
var m_SelectOnlineValue = "1";
var m_UserList = [];//账户密码信息

$(function () {
    document.onkeydown = function (e) {
        var ev = document.all ? window.event : e;
        if (ev.keyCode == 13) {
            SetSubmit();
        }
    }
    $("#loadingForm").hide();
    objtxtUserId = $("#txtUserId");
    objtxtUserId.focus();
    objtxtPassword = $("#txtPassword");
    objtxtValid = $("#txtValid");
    objSelectOnline = $("#selectOnline");

	//获取企业信息
    var systemNews = $.parseJSON(loginObj.getdebuginfo());
    $("#loginLogo").attr("src", "../img/logo" + systemNews.companyid + ".png");
    loginObj.getuserinfo();

    if (systemNews.companyid.length > 0) {
        $("#btnReg").hide();
        $("#btnMem").hide();
    }
});

//赋值用户名密码
function AsyncGetUserInfo(jsonResult) {
    m_UserList = jsonResult.list;
    if (m_UserList.length > 0) {
        objtxtUserId.val(m_UserList[m_UserList.length - 1].usercode);
        objtxtPassword.val(m_UserList[m_UserList.length - 1].password);
        $("#cbxRemember").attr("checked", "checked");
        $("#cbxBootAutomaticRun").attr("checked", m_UserList[m_UserList.length - 1].automaticrun);

        if (m_UserList[m_UserList.length - 1].bislogin == true) {
            //$("#cbxBootLogin").attr("checked", jsonResult.bislogin)
            $("#cbxRemember").attr("checked", "checked");
            $("#cbxRemember").attr("disabled", "false");
        }
        //else {
            //if (m_UserList[m_UserList.length - 1].password.length > 0) {
            //    //选中记住我
               
        //    //}
        //}
        ComErgodicUser();//遍历存储的用户名
    }
	//自动登录（开启自动登录 且 当前程序进程数 < 2  且 保存了用户名和密码）
    //if (jsonResult.bislogin == true && jsonResult.ncurrentcount < 2 && jsonResult.mobile.length > 0 && jsonResult.password.length > 0) {
    //	SetSubmit();
    //}
}

//
function SetSelectOnline(value) {
    m_SelectOnlineValue = value;
}

//提交
function SetSubmit() {
   
	//记住密码
    var bIsChecked = false;
    if ($("#cbxRemember").prop("checked") == true) {
        bIsChecked = true;
    }
	//自动启动
    var bIsRun = false;
    if ($("#cbxBootAutomaticRun").prop("checked") == true) {
    	bIsRun = true;
    }
	//自动登录
    var bIsLogin = false;
    //if ($("#cbxBootLogin").prop("checked") == true) {
    //	bIsLogin = true;
    //}
    if (objtxtUserId.val().length == 0) {
        objtxtUserId.focus();
        ShowError("myAlert", "请您输入用户名！");

        return;
    }
    if (objtxtPassword.val().length == 0) {
        objtxtPassword.focus();
        ShowError("myAlert", "请您输入密码！");
        return;
    }

    $("#loadingForm").show();
    $("#logingForm").hide();
    
    var strJson = $.toJSON({
        mobile: objtxtUserId.val(), password: objtxtPassword.val(), online: m_SelectOnlineValue,
        remember: bIsChecked, bisautomaticrun: bIsRun, bIsLogin: bIsLogin, list: m_UserList
    });

    loginObj.userlogin(strJson);
}

//登录结果
function AsyncUserLogin(jsonResult) {
    if (!jsonResult.success) {
        if (jsonResult.error == "para error!") {
            ShowError("myAlert", "未知错误！");
        } else {
            ShowError("myAlert", jsonResult.error);
        }
        $("#loadingForm").hide();
        $("#logingForm").show();
    } else {
        loginObj.canceldialog();
    }
}
//开机自动启动
function BootAutomaticRun() {

	//自动启动
	var bIsRun = false;
	if ($("#cbxBootAutomaticRun").prop("checked") == true) {
		bIsRun = true;
	}
	var strJson = $.toJSON({
		bisautomaticrun: bIsRun
	});
	loginObj.bootautomaticrun(strJson);
}

function AsyncBootAutomaticRunInfo(jsonResult) {
	if (!jsonResult.success) {
		ShowError("myAlert", "设置自动启动失败！");
		$("#cbxBootAutomaticRun").prop("checked", false);
	}

}
//选择自动登录按钮，自动选中记住我并且禁用
function BootLoginRemberMe() {
	if ($("#cbxBootLogin").is(":checked") == true) {
		$("#cbxRemember").attr("checked", "checked");
		$("#cbxRemember").attr("disabled", "false");
	} else {
		$("#cbxRemember").removeAttr("disabled");
	}
}

//选择存在的账户
function SelectUserCode(index) {

    objtxtUserId.val(m_UserList[index].usercode);
    objtxtPassword.val(m_UserList[index].password);

    $("#cbxBootAutomaticRun").attr("checked", m_UserList[index].automaticrun);

    if (m_UserList[index].bislogin == true) {
        //$("#cbxBootLogin").attr("checked", jsonResult.bislogin)
        $("#cbxRemember").attr("checked", "checked");
        $("#cbxRemember").attr("disabled", "false");
    } else {
        if (m_UserList[index].password.length > 0) {
            //选中记住我
            $("#cbxRemember").attr("checked", "checked");
        }
    }
}
//删除账户缓存的某个账户
function DelUser(index) {
    event.stopPropagation();
    m_UserList.splice(index, 1);
    ComErgodicUser();//遍历存储的用户名
}

//公共遍历方法 遍历存储的用户
function ComErgodicUser() {
    var count = "";
    for (var i = m_UserList.length - 1; i >= 0 ; i--) {
        count += "<li onclick=\"SelectUserCode(" + i + ");\"><a href=\"#\">" + m_UserList[i].usercode + "<i onclick=\"DelUser(" + i + ")\" class=\"fa fa-close pull-right\" aria-hidden=\"true\"></i></a></li>";
    }
    $("#UserMobile").html(count);
}