$(document).ready(function() {

    var $0 = $("#RolePermissions_cbAll"); $0.attr("Privilege", "0");

    $0 = $("#RolePermissions_cbSummary"); $0.attr("Privilege", "10");

    // 店铺管理
    $0 = $("#RolePermissions_cbShop"); $0.attr("Privilege", "1");
    $0 = $("#RolePermissions_cbSiteContent"); $0.attr("Privilege", "11");
    $0 = $("#RolePermissions_cbDateTimeSettings"); $0.attr("Privilege", "12");
    $0 = $("#RolePermissions_cbOnlineService"); $0.attr("Privilege", "13");
    $0 = $("#RolePermissions_cbTQOnlineServerSet"); $0.attr("Privilege", "14");
    $0 = $("#RolePermissions_cbVotes"); $0.attr("Privilege", "15");
    $0 = $("#RolePermissions_cbAdvPositions"); $0.attr("Privilege", "16");
    $0 = $("#RolePermissions_cbFriendlyLinks"); $0.attr("Privilege", "17");
    $0 = $("#RolePermissions_cbManageThemes"); $0.attr("Privilege", "18");
    $0 = $("#RolePermissions_cbManageHotKeywords"); $0.attr("Privilege", "19");
    // 商品管理
    $0 = $("#RolePermissions_cbProductCatalog"); $0.attr("Privilege", "2");
    $0 = $("#RolePermissions_cbProductTypes"); $0.attr("Privilege", "21");
    $0 = $("#RolePermissions_cbProductTypesView"); $0.attr("Privilege", "21_1");
    $0 = $("#RolePermissions_cbProductTypesAdd"); $0.attr("Privilege", "21_2");
    $0 = $("#RolePermissions_cbProductTypesEdit"); $0.attr("Privilege", "21_3");
    $0 = $("#RolePermissions_cbProductTypesDelete"); $0.attr("Privilege", "21_4");
    $0 = $("#RolePermissions_cbManageCategories"); $0.attr("Privilege", "22");
    $0 = $("#RolePermissions_cbManageCategoriesView"); $0.attr("Privilege", "22_1");
    $0 = $("#RolePermissions_cbManageCategoriesAdd"); $0.attr("Privilege", "22_2");
    $0 = $("#RolePermissions_cbManageCategoriesEdit"); $0.attr("Privilege", "22_3");
    $0 = $("#RolePermissions_cbManageCategoriesDelete"); $0.attr("Privilege", "22_4");
    $0 = $("#RolePermissions_cbSetCategoriesThemes"); $0.attr("Privilege", "23");
    $0 = $("#RolePermissions_cbPriceChange"); $0.attr("Privilege", "24");
    $0 = $("#RolePermissions_cbProductUnclassified"); $0.attr("Privilege", "25");
    $0 = $("#RolePermissions_cbManageProducts"); $0.attr("Privilege", "26");
    $0 = $("#RolePermissions_cbManageProductsView"); $0.attr("Privilege", "26_1");
    $0 = $("#RolePermissions_cbManageProductsAdd"); $0.attr("Privilege", "26_2");
    $0 = $("#RolePermissions_cbManageProductsEdit"); $0.attr("Privilege", "26_3");
    $0 = $("#RolePermissions_cbManageProductsDelete"); $0.attr("Privilege", "26_4");
    $0 = $("#RolePermissions_cbManageProductsUp"); $0.attr("Privilege", "26_5");
    $0 = $("#RolePermissions_cbManageProductsDown"); $0.attr("Privilege", "26_6");
    $0 = $("#RolePermissions_cbProductImport"); $0.attr("Privilege", "26_7");
    $0 = $("#RolePermissions_cbProductExport"); $0.attr("Privilege", "26_8");
    $0 = $("#RolePermissions_cbProductBatchUpload"); $0.attr("Privilege", "27");
    $0 = $("#RolePermissions_cbBrandCategories"); $0.attr("Privilege", "28");
    $0 = $("#RolePermissions_cbSuppliers"); $0.attr("Privilege", "29");
    // 客户管理    
    $0 = $("#RolePermissions_cbManageUsers"); $0.attr("Privilege", "3");
    $0 = $("#RolePermissions_cbAgentialRanks"); $0.attr("Privilege", "31");
    $0 = $("#RolePermissions_cbAgentialRanksView"); $0.attr("Privilege", "31_1");
    $0 = $("#RolePermissions_cbAgentialRanksAdd"); $0.attr("Privilege", "31_2");
    $0 = $("#RolePermissions_cbAgentialRanksEdit"); $0.attr("Privilege", "31_3");
    $0 = $("#RolePermissions_cbAgentialRanksDelete"); $0.attr("Privilege", "31_4");
    $0 = $("#RolePermissions_cbManageAgents"); $0.attr("Privilege", "32");
    $0 = $("#RolePermissions_cbManageAgentsView"); $0.attr("Privilege", "32_1");
    $0 = $("#RolePermissions_cbManageAgentsAdd"); $0.attr("Privilege", "32_2");
    $0 = $("#RolePermissions_cbManageAgentsEdit"); $0.attr("Privilege", "32_3");
    $0 = $("#RolePermissions_cbManageAgentsDelete"); $0.attr("Privilege", "32_4");
    $0 = $("#RolePermissions_cbSalesSettings"); $0.attr("Privilege", "33");
    $0 = $("#RolePermissions_cbMemberRanks"); $0.attr("Privilege", "34");
    $0 = $("#RolePermissions_cbMemberRanksView"); $0.attr("Privilege", "34_1");
    $0 = $("#RolePermissions_cbMemberRanksAdd"); $0.attr("Privilege", "34_2");
    $0 = $("#RolePermissions_cbMemberRanksEdit"); $0.attr("Privilege", "34_3");
    $0 = $("#RolePermissions_cbMemberRanksDelete"); $0.attr("Privilege", "34_4");
    $0 = $("#RolePermissions_cbManageMembers"); $0.attr("Privilege", "35");
    $0 = $("#RolePermissions_cbManageMembersView"); $0.attr("Privilege", "35_1");
    $0 = $("#RolePermissions_cbManageMembersAdd"); $0.attr("Privilege", "35_2");
    $0 = $("#RolePermissions_cbManageMembersEdit"); $0.attr("Privilege", "35_3");
    $0 = $("#RolePermissions_cbManageMembersDelete"); $0.attr("Privilege", "35_4");
    $0 = $("#RolePermissions_cbManageMembersUp"); $0.attr("Privilege", "35_5");
    $0 = $("#RolePermissions_cbBalanceDetails"); $0.attr("Privilege", "36");
    $0 = $("#RolePermissions_cbBalanceDrawRequest"); $0.attr("Privilege", "37");
    $0 = $("#RolePermissions_cbBalanceFreezeDetails"); $0.attr("Privilege", "38");
    $0 = $("#RolePermissions_cbAccountSummary"); $0.attr("Privilege", "39");
    $0 = $("#RolePermissions_cbReCharge"); $0.attr("Privilege", "310");
    $0 = $("#RolePermissions_cbDeductRequestDetails"); $0.attr("Privilege", "311");
    $0 = $("#RolePermissions_cbOpenIdServices"); $0.attr("Privilege", "312");
    // 定单管理
    $0 = $("#RolePermissions_cbSales"); $0.attr("Privilege", "4");
    $0 = $("#RolePermissions_cbManageOrder"); $0.attr("Privilege", "41");
    $0 = $("#RolePermissions_cbManageOrderView"); $0.attr("Privilege", "41_1");
    $0 = $("#RolePermissions_cbManageOrderDelete"); $0.attr("Privilege", "41_2");
    $0 = $("#RolePermissions_cbManageOrderEdit"); $0.attr("Privilege", "41_3");
    $0 = $("#RolePermissions_cbManageOrderConfirm"); $0.attr("Privilege", "41_4");
    $0 = $("#RolePermissions_cbManageOrderSendingGoods"); $0.attr("Privilege", "41_5");
    $0 = $("#RolePermissions_cbManageOrderSendedGoods"); $0.attr("Privilege", "41_6");
    $0 = $("#RolePermissions_cbManageOrderFinished"); $0.attr("Privilege", "41_7");
    $0 = $("#RolePermissions_cbManageOrderRefund"); $0.attr("Privilege", "41_8");
    $0 = $("#RolePermissions_cbManageOrderRemark"); $0.attr("Privilege", "41_9");
    $0 = $("#RolePermissions_cbManageOrderWords"); $0.attr("Privilege", "41_10");
    $0 = $("#RolePermissions_cbOrderLookupLists"); $0.attr("Privilege", "42");
    $0 = $("#RolePermissions_cbCurrencys"); $0.attr("Privilege", "43");
    $0 = $("#RolePermissions_cbAddPaymentMode"); $0.attr("Privilege", "44");
    $0 = $("#RolePermissions_cbPaymentModes"); $0.attr("Privilege", "45");
    $0 = $("#RolePermissions_cbCreateShippingMode"); $0.attr("Privilege", "46");
    $0 = $("#RolePermissions_cbShippingModes"); $0.attr("Privilege", "47");
    $0 = $("#RolePermissions_cbRegions"); $0.attr("Privilege", "48");
    $0 = $("#RolePermissions_cbAreas"); $0.attr("Privilege", "49");
    // 网店促销              
    $0 = $("#RolePermissions_cbPromote"); $0.attr("Privilege", "5");
    $0 = $("#RolePermissions_cbAddGift"); $0.attr("Privilege", "51");
    $0 = $("#RolePermissions_cbGifts"); $0.attr("Privilege", "52");
    $0 = $("#RolePermissions_cbPromoteAdd"); $0.attr("Privilege", "53");
    $0 = $("#RolePermissions_cbPromoteSales"); $0.attr("Privilege", "54");
    $0 = $("#RolePermissions_cbAddCoupon"); $0.attr("Privilege", "55");
    $0 = $("#RolePermissions_cbCoupons"); $0.attr("Privilege", "56");
    //  内容管理
    $0 = $("#RolePermissions_cbContent"); $0.attr("Privilege", "6");
    $0 = $("#RolePermissions_cbAddAffiche"); $0.attr("Privilege", "61");
    $0 = $("#RolePermissions_cbAfficheList"); $0.attr("Privilege", "62");
    $0 = $("#RolePermissions_cbHelpCategories"); $0.attr("Privilege", "63");
    $0 = $("#RolePermissions_cbAddHelp"); $0.attr("Privilege", "64");
    $0 = $("#RolePermissions_cbHelpList"); $0.attr("Privilege", "65");
    $0 = $("#RolePermissions_cbArticleCategories"); $0.attr("Privilege", "66");
    $0 = $("#RolePermissions_cbAddArticle"); $0.attr("Privilege", "67");
    $0 = $("#RolePermissions_cbArticleList"); $0.attr("Privilege", "68");

    // 客户反馈
    $0 = $("#RolePermissions_cbFeedBack"); $0.attr("Privilege", "7");
    $0 = $("#RolePermissions_cbProductConsultationsManage"); $0.attr("Privilege", "71");
    $0 = $("#RolePermissions_cbProductReviewsManage"); $0.attr("Privilege", "72");
    $0 = $("#RolePermissions_cbReceivedMessages"); $0.attr("Privilege", "73");
    $0 = $("#RolePermissions_cbSendedMessages"); $0.attr("Privilege", "74");
    $0 = $("#RolePermissions_cbSendMessage"); $0.attr("Privilege", "75");
    $0 = $("#RolePermissions_cbManageLeaveComments"); $0.attr("Privilege", "76");
    // 网商工具             
    $0 = $("#RolePermissions_cbWebTool"); $0.attr("Privilege", "8");
    $0 = $("#RolePermissions_cbEmailSettings"); $0.attr("Privilege", "81");
    $0 = $("#RolePermissions_cbEmailTemplets"); $0.attr("Privilege", "82");
    $0 = $("#RolePermissions_cbEmailToUser"); $0.attr("Privilege", "83");
    $0 = $("#RolePermissions_cbCnzzStatisticsSet"); $0.attr("Privilege", "84");
    $0 = $("#RolePermissions_cbCnzzStatistics"); $0.attr("Privilege", "85");
    $0 = $("#RolePermissions_cbIntegrationForum"); $0.attr("Privilege", "86");
    // 报表统计
    $0 = $("#RolePermissions_cbReport"); $0.attr("Privilege", "9");
    $0 = $("#RolePermissions_cbSaleTotalStatistics"); $0.attr("Privilege", "91");
    $0 = $("#RolePermissions_cbUserOrderStatistics"); $0.attr("Privilege", "92");
    $0 = $("#RolePermissions_cbSaleList"); $0.attr("Privilege", "93");
    $0 = $("#RolePermissions_cbSaleTargetAnalyse"); $0.attr("Privilege", "94");
    $0 = $("#RolePermissions_cbBalanceDetailsStatistics"); $0.attr("Privilege", "95");
    $0 = $("#RolePermissions_cbBalanceDrawRequestStatistics"); $0.attr("Privilege", "96");
    $0 = $("#RolePermissions_cbMemberRanking"); $0.attr("Privilege", "97");
    $0 = $("#RolePermissions_cbUserStatistics"); $0.attr("Privilege", "98");
    $0 = $("#RolePermissions_cbProductSaleRanking"); $0.attr("Privilege", "99");
    $0 = $("#RolePermissions_cbProductSaleStatistics"); $0.attr("Privilege", "910");

    // 加载后的全选操作 ------------------------------------------------------------------------------------------------------------------------------------

    showOneLayerOnLoad();

    function showOneLayerOnLoad() {

        var flag = true;

        for (var i = 1; i <= 10; i++) {
            $_Control1 = $("input[type='checkbox'][Privilege='" + i + "']");
            var result1 = showTwoLayerOnLoad(i);

            // 如果当前一级下没有下级则判断自己，如果没有选择设置为 false            
            if (result1 == "no" && !$_Control1.attr("checked"))
                flag = false;
            else if (result1)
                $_Control1.attr("checked", true);
            else
                flag = false;
        }
        if (flag)
            $("input[type='checkbox'][Privilege='0']").attr("checked", true);
    };

    function showTwoLayerOnLoad(one) {

        var flag2 = true;
        for (var j = 1; j <= 11; j++) {

            $_Control2 = $("input[type='checkbox'][Privilege='" + one + j + "']");

            // 如果当前一级下没有下级则返回 no ,告诉上级无下级
            if ($_Control2.attr("id") == undefined && j == 1) {
                flag2 = "no";
                return flag2;
            }
            // 如果已经循环到尽头则返回结果
            else if ($_Control2.attr("id") == undefined) {
                return flag2;
            }
            // 如果有下级且没到尽头继续操作
            else if ($_Control2.attr("id") != undefined) {

                // 判断当前的二级下的三级情况
                var result2 = showTheeLayerOnLoad(one, j);


                // 如果当前二级下没有三级则判断自己,如果没选择设置为 false
                if (result2 == "no" && !$_Control2.attr("checked"))
                    flag2 = false;
                else if (result2)
                    $_Control2.attr("checked", true);
                else
                    flag2 = false;
            }
        }

        return flag2;
    };

    function showTheeLayerOnLoad(one, two) {

        var flag3 = true;
        for (var k = 1; k <= 10; k++) {

            $_Control3 = $("input[type='checkbox'][Privilege$='" + one + two + "_" + k + "']");

            // 如果当前二级下没有下级则返回 no ,告诉上级无下级
            if ($_Control3.attr("id") == undefined && k == 1)
                return "no";
            // 如果已经循环到尽头则返回结果
            else if ($_Control3.attr("id") == undefined)
                return flag3;
            // 如果有下级且没到尽头继续操作
            else if ($_Control3.attr("id") != undefined && !$_Control3.attr("checked"))
                flag3 = false;
        }

        return flag3;
    };

    // 单击触发事件 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    $("input[type='checkbox']").bind('click', function() {

        var value = this.checked;
        // 全选
        if ($(this).attr("Privilege") == "0")
            $("input[type='checkbox']").attr("checked", value);
        // 一层的选择
        else if ($(this).attr("Privilege") > 0 && $(this).attr("Privilege") <= 10) {
            // 没有被选择时
            if (!value)
                $("input[type='checkbox'][Privilege='0']").attr("checked", false);
            // 选择时
            else {
                if (IsOneLayerAllChecked())
                    $("input[type='checkbox'][Privilege='0']").attr("checked", true);
            }
            $("input[type='checkbox'][Privilege^='" + $(this).attr("Privilege") + "'][Privilege!='10']").attr("checked", value);
            //showTwoLayer(this.Privilege,value);
        }
        // 二层的选择
        else if ($(this).attr("Privilege").length = 2 && $(this).attr("Privilege") > 10) {
            // 没有被选择时
            if (!value) {
                $("input[type='checkbox'][Privilege='0']").attr("checked", false);
                $("input[type='checkbox'][Privilege='" + $(this).attr("Privilege").substring(0, 1) + "']").attr("checked", false);
            }
            // 选择时
            else {
                if (IsTwoLayerAllCheckedOfOne($(this).attr("Privilege").substring(0, 1)))
                    $("input[type='checkbox'][Privilege='" + $(this).attr("Privilege").substring(0, 1) + "']").attr("checked", true);
                if (IsOneLayerAllChecked())
                    $("input[type='checkbox'][Privilege='0']").attr("checked", true);
            }
            showTheeLayer2($(this).attr("Privilege"), value);
        }
        // 三层的选择
        else {
            // 没有被选择时
            if (!value) {
                $("input[type='checkbox'][Privilege='0']").attr("checked", false);
                $("input[type='checkbox'][Privilege='" + $(this).attr("Privilege").substring(0, 1) + "']").attr("checked", false);
                $("input[type='checkbox'][Privilege='" + $(this).attr("Privilege").substring(0, $(this).attr("Privilege").indexOf("_")) + "']").attr("checked", false);
            }
            // 选择时
            else {
                if (IsThreeLayerAllCheckedOfTwo($(this).attr("Privilege").substring(0, $(this).attr("Privilege").indexOf("_"))))
                    $("input[type='checkbox'][Privilege='" + $(this).attr("Privilege").substring(0, $(this).attr("Privilege").indexOf("_")) + "']").attr("checked", true);
                if (IsTwoLayerAllCheckedOfOne($(this).attr("Privilege").substring(0, 1)))
                    $("input[type='checkbox'][Privilege='" + $(this).attr("Privilege").substring(0, 1) + "']").attr("checked", true);
                if (IsOneLayerAllChecked())
                    $("input[type='checkbox'][Privilege='0']").attr("checked", true);
            }
        }
    })

    // 选择后判断父类是否应该被选择-----------------------------------------------------------------------------------------------------------------------------------------

    // 判断一层是否都选中了
    var IsOneLayerAllChecked = function() {
        for (var i = 1; i <= 10; i++) {
            if (!$("input[type='checkbox'][Privilege='" + i + "']").attr("checked")) {
                return false;
            }
        }
        return true;
    }

    // 判断某一层下的二层是否都选中了
    var IsTwoLayerAllCheckedOfOne = function(one) {
        for (var i = 1; i <= 11; i++) {
            $_Control2 = $("input[type='checkbox'][Privilege='" + one + i + "']");
            if ($_Control2.attr("id") == undefined) {
                break;
            }

            if (!$_Control2.attr("checked")) {
                return false;
            }
        }
        return true;
    }

    // 判断某二层下的三层是否都选中了
    var IsThreeLayerAllCheckedOfTwo = function(two) {
        for (var i = 1; i <= 10; i++) {
            $_Control3 = $("input[type='checkbox'][Privilege='" + two + "_" + i + "']");

            if ($_Control3.attr("id") == undefined) {
                break;
            }

            if (!$_Control3.attr("checked")) {
                return false;
            }
        }
        return true;
    }

    // 全选反选操作-----------------------------------------------------------------------------------------------------------------------------------------------------------------

    //    var showOneLayer = function(one,value) { 
    //    
    //        for(var i=1;i<=10;i++)
    //        {
    //            
    //            $("input[type='checkbox'][Privilege='"+i+"']).attr("checked",value)"); 
    //                      
    //            showTwoLayer(i,value);
    //        } 
    //    };

    //    var showTwoLayer = function(one,value) { 
    //    
    //        for(var j=1;j<=13;j++)
    //        {
    //            
    //            $_Control1=$("input[type='checkbox'][Privilege='"+one+j+"']"); 
    //                      
    //            if ($_Control1.attr("id")==undefined)
    //            {
    //                break;
    //            }
    //            $_Control1.attr("checked",value);
    //            
    //            showTheeLayer(one,j ,value);
    //        } 
    //    };

    var showTheeLayer = function(one, two, value) {

        for (var k = 1; k <= 10; k++) {

            $_Control2 = $("input[type='checkbox'][Privilege$='" + one + two + "_" + k + "']");

            if ($_Control2.attr("id") == undefined) {
                break;
            }
            $_Control2.attr("checked", value);

        }
    };

    var showTheeLayer2 = function(two, value) {

        for (var k = 1; k <= 10; k++) {

            $_Control2 = $("input[type='checkbox'][Privilege$='" + two + "_" + k + "']");

            if ($_Control2.attr("id") == undefined) {
                break;
            }
            $_Control2.attr("checked", value);

        }
    };

}
);
  