$(function () {

    $("#Password").keydown(function (e) {
        if (e.which == 13) {
            $('#btnLogin').click();
            return false;
        }
    });

    if ($("#LoginId")) {
        $("#Password").focus();
    }
    else {
        $("#LoginId").focus();
    }

    $("#btnLogin").click(function () {
        ClearErrorMsg();

        if (!$("#LoginId").val()) {
            $("#LoginId").addClass("redBorder");
            return SetErrorMsg("请输入登录账号");
        }

        if (!$("#Password").val()) {
            $("#Password").addClass("redBorder");
            return SetErrorMsg("请输入登录密码");
        }

         $.blockUI({ message: '<h5>请稍候...</h5>' });

        $.ajax({
            url: "/Account/Login",
            type: 'POST',
            cache: false,
            data: { LoginId: $("#LoginId").val(), Password: $("#Password").val(), AutoLogin: $("#AutoLogin").prop("checked") },
            success: function (result) {
                $.unblockUI();
                if (result.code == 0) {
                    location.href = "/MySpace"
                }
                else if (result.code == 1) {
                    SetErrorMsg('帐号或密码错误');
                }
                else if (result.code == 2) {
                    location.href = "/Overview/CompanyList"
                }
                else if (result.code > 90) {
                    SetErrorMsg(result.message);
                }
            },
            error: function (e) {
                $.unblockUI();
                SetErrorMsg('系统错误，请联系管理员');
            }
        });

    });

});