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

        ///新增方法填写在此///
        if ($("#userClassName_hide").val() != $("#userClassName_txt").val()) {
            if (!isUserClassNameExist($("#userClassName_txt").val())) {
                $("#userClassName_txt").next("span").attr("class", "point ico-error R");
                $("#userClassName_txt").next("span").html("身份名已经存在,请更换个试试");
                return false;
            }
        }
        /////

        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 {
            $("input:submit").attr("disabled", true);
            $("input:submit").next("span").attr("class", "Q");
            $("input:submit").next("span").html("<img src=\"Images/Loading.gif\" align=\"middle\" /> 正在保存…");
        }
    });

    //检查分类名是否存在
    function isUserClassNameExist(name) {
        var retValue = false
        $.ajax({
            type: "POST",
            url: "Ajax/UserDispose.ashx",
            data: { 'com': 'isuserclassexist', 'userclassname': name },
            datatype: "json",
            async: false,
            beforeSend: function (data, textStatus) { },
            success: function (data, textStatus) {
                if (data == "0") {
                    retValue = true;
                } else {
                    retValue = false;
                }
            },
            error: function (e) { alert("连接失败,请向Eoyoo官方技术人员反馈!"); }
        })
        return retValue;
    }

    //菜单伸缩
    $("a[rel='stretch']").click(function () {
        var $object = $(this);
        $("div[rootid='" + $object.attr("href") + "']").each(function () {
            if ($(this).is(":hidden") == false) {
                $(this).hide();
                $object.attr("class", "stretch_off");
            } else {
                $(this).show();
                $object.attr("class", "stretch_on");
            }
        });
        return false;
    });

    //一级点击全选
    $("input[rel='rootChk']").bind('click', function () {
        var $object = $(this);
        if ($object.attr("checked")) {
            $("div[rootid='" + $object.val() + "']").find("input").each(function () {
                $(this).attr("checked", true);
                $object.attr("checked", true);
            });
        } else {
            $("div[rootid='" + $object.val() + "']").find("input").each(function () {
                $(this).removeAttr("checked");
                $object.removeAttr("checked");
            });
        }
        insertValue();
    });

    //二级点击全选
    $("input[rel='subChk']").bind('click', function () {
        var $object = $(this);
        if ($object.attr("checked")) {
            $("div[titleid='" + $object.val() + "']").find("input").each(function () {
                $(this).attr("checked", true);
                $object.attr("checked", true);
            });
        } else {
            $("div[titleid='" + $object.val() + "']").find("input").each(function () {
                $(this).removeAttr("checked");
                $object.removeAttr("checked");
            });
        }
        insertValue();
    });
    //三级点击全选
    $("input[rel='itemChk']").bind('click', function () {
        var $object = $(this);
        if ($object.attr("checked")) {
            $("div[itemid='" + $object.val() + "']").find("input").each(function () {
                $(this).attr("checked", true);
                $object.attr("checked", true);
            });
        } else {
            $("div[itemid='" + $object.val() + "']").find("input").each(function () {
                $(this).removeAttr("checked");
                $object.removeAttr("checked");
            });
        }
        insertValue();
    });

    //批量全选
    $("#allPower").click(function () {
        var $object = $(this);
        if ($object.attr("checked")) {
            $("#powerInput input[type='checkbox']").each(function () {
                $(this).attr("checked", true);
                $object.attr("checked", true);
                $object.next("b").text("全部移除权限");
                $object.next("b").attr("class", "K");
            });
        } else {
            $("#powerInput input[type='checkbox']").each(function () {
                $(this).removeAttr("checked");
                $object.removeAttr("checked");
                $object.next("b").text("全部赋予权限");
                $object.next("b").attr("class", "");
            });
        }
        insertValue();
    })

    //单个勾选赋值
    $("input[type='checkbox']").bind('click', function () {
        insertValue();
    });

    //勾选隐藏控件赋值
    function insertValue() {
        $("#userClassRole_hide").val("");
        var valueID = "";
        $("#powerInput input[type='checkbox']").each(function () {
            if ($(this).attr("checked")) {
                valueID += $(this).val() + ",";
            }
        });
        $("#userClassRole_hide").val(valueID);
    }
})