$(function () {
    //判断是否为Iframe
    if (self != top) top.location = self.location;

    //加载历史Cookie用户名
    if ($.cookie("cookieUserName") != null) $("#username").val($.cookie("cookieUserName"));

    //加载验证码
    function showverifyCode() {
        $("#verifycodeIMG").attr("src", "Ajax/VerifyCode.ashx");
    }

    $("#verifycodeIMG").click(function () {
        $(this).attr("src", "Ajax/VerifyCode.ashx?abc=" + Math.random() + "");
        $("#verifycode").val("");
        $("#verifycode").focus();
        return false;
    })

    $("#updateVerifyCode").click(function () {
        $("#verifycodeIMG").attr("src", "Ajax/VerifyCode.ashx?abc=" + Math.random() + "");
        $("#verifycode").val("");
        $("#verifycode").focus();
        return false;
    })

    showverifyCode();

    //判断验证码
    function VerifyCode() {
        var cookieverifycode = $.cookie("EoyooCorpVerify");
        if ($("#verifycode").val() == "") {
            $("#verifycode_msg").attr("class", "point ico-error R");
            $("#verifycode_msg").text("验证码不能为空");
            return false;
        }
        if ($("#verifycode").val().length < 4) {
            $("#verifycode_msg").attr("class", "point ico-error R");
            $("#verifycode_msg").text("长度限制4位");
            return false;
        }
        if ($("#verifycode").val().toLowerCase() != cookieverifycode.toLowerCase()) {
            $("#verifycode_msg").attr("class", "point ico-error R");
            $("#verifycode_msg").text("验证码输入错误");
            return false;
        }
        if ($("#verifycode_msg").hasClass("ico-error")) {
            $("#verifycode_msg").attr("class", "point ico-via Q");
            $("#verifycode_msg").text("验证通过");
            return true;
        }
    }

    $("#verifycode").blur(function () {
        if (!VerifyCode()) return VerifyCode();
    });

    //提交判断
    $("form").submit(function () {
        $("input[type='text'],input[type='password'],textarea").trigger("blur");
        $("select").trigger("change");

        ///新增方法填写在此///

        /////

        if ($("form span").hasClass("ico-error")) {
            $("input:submit").attr("disabled", false);
            $("input:submit").next("span").attr("class", "point ico-error R");
            $("input:submit").next("span").text("请按照提示正确操作，检查各项填写");
            setInterval(delSubmitBtnMsg, 3000);
            return false;
        } else {

            $.ajax({
                type: "POST",
                url: "Ajax/UserDispose.ashx",
                data: { com: 'managelogin', username: $("#username").val(), userpassword: $("#userpassword").val() },
                datatype: "json",
                beforeSend: function (data, textStatus) {
                    $("input:submit").next("span").attr("class", "loading F");
                    $("input:submit").next("span").html("验证中…");
                    $("input:submit").attr("disabled", true);
                },
                success: function (data, textStatus) {
                    if (data == "1") {
                        $.cookie("cookieUserName", $("#username").val(), 7);
                        $("input:submit").next("span").attr("class", "loading Q");
                        $("input:submit").next("span").html("验证正确,正在载入系统…");
                        setInterval(jumpUrl, 2000);
                        return false;
                    } else {
                        $("input:submit").next("span").attr("class", "loading R");
                        $("input:submit").next("span").text("用户名或密码错误!");
                        $("input:submit").attr("disabled", false);
                        $("#userpassword").val("");
                        setInterval(delSubmitBtnMsg, 3000);
                        return false;
                    }
                },
                error: function (e) { alert("登录失败,系统出现错误,请检查系统!"); }
            })
        }
        return false;
    });


    //登录成功跳转地址
    function jumpUrl() {
        location.href = "Main.aspx";
    }
})