﻿$(document).ready(function ($) {
    $("#showRepData").hide();
    $("div#economyLst").hide();
    $("div.economygrp").hide();

    showEconomyList = function (index) {
        if (index == "all") {
            $("div#economyLst").toggle();
            if ($("#aShowAll").html() == $(".csHideAll").val()) {
                $("a#aShowAll").empty();
                $("a#aShowAll").append($(".csShowAll").val());
            }
            else {
                $("a#aShowAll").empty();
                $("a#aShowAll").append($(".csHideAll").val());
            }
            $("div.economygrp").show();
        }
        else {
            $("div#economyLst").show();
            $("div.economygrp").hide();
//            $("#li" + index).addClass('active');
            $("div#selectEconomy" + index).show();
        }
    }

    // $(function () {


    $('.reportbutton').click(function () {
        var myarrayEconomies = [];
        var myarrayRegions = [];
        var myarrayIncomes = [];
        var myarrayLawTypes = [];
        $('input:checkbox:checked').each(function (i) {
            switch ($(this).attr('id').toString().substr(2, 2)) {
                case 'Ec':
                    myarrayEconomies[i] = $(this).attr('id').toString().substr(9, $(this).attr('id').toString().length);
                    break;

                case 'La':
                    myarrayLawTypes[i] = $(this).attr("chklawtypeid");
                    break;
            }
            $("[id$='showRepData']").show();
            $("[id$='dvChooseEconomy']").hide();
        });

        

        $.get("/Law-library?LawTypes=" + myarrayLawTypes + "&EconomyIds=" + myarrayEconomies + "&ajax=1", function (data) {
            $(".LLResult").html(data);
            hideAllSubNavDiv();
            var y = $(data).filter(function () { return $(this).is("input") });
            y.each(function () {
                document.title = $(this).val();
            });
            $(".sub-nav a").unbind('click');
            $(".sub-nav a").bind('click', subNavClicked);

        });
    });

    function subNavClicked() {
        var div = $(this).attr("class");
        if (div == "")
            return;
        $(this).parents(".sub-nav").find("a").each(function () {
            $(this).parent().removeClass("active");
        });
        $(this).parent().addClass("active");
        hideAllSubNavDiv();
        $("#" + div).show();
    }

    function hideAllSubNavDiv() {
        $(".tabContent").each(function () {
            if (!$("." + $(this).attr("id")).parent().is('.active'))
                $(this).hide();
        });
    }

    $(".toggleReportData").click(function () {
        $("[id$='showRepData']").toggle();
        //$("[id$='LawLibraryMain']").show();
        $("[id$='dvChooseEconomy']").show();


        //        $("div#economyLst").show();
        //        $("div.economygrp").show();
    });

    $("[id$='hreffrag2']").click(function () {
        $("[id$='fragment-2']").show();
        $("[id$='fragment-1']").hide();
        $("[id$='frag-2']").addClass("active");
        $("[id$='frag-1']").removeClass("active");
    });

    $("[id$='hreffrag1']").click(function () {
        $("[id$='fragment-1']").show();
        $("[id$='fragment-2']").hide();
        $("[id$='frag-1']").addClass("active");
        $("[id$='frag-2']").removeClass("active");
    });

    $(".regioncheckbox").click(function () {
        var regionId = $(this).attr("chkRegionID");
        var checkedStatus = $(this).is(":checked");
        $(".economycheckbox").each(function () {
            if ($(this).attr("regionId") == regionId) {
                $(this).attr("checked", checkedStatus);
            }
        });
    });

    $(".incomecheckbox").click(function () {
        var incomeId = $(this).attr("chkIncomeID");
        var checkedStatus = $(this).is(":checked");
        if (incomeId != 5) {
            $(".economycheckbox").each(function () {
                if ($(this).attr("incomeGroupID") == incomeId) {
                    $(this).attr("checked", checkedStatus);
                }
            });
        }
        else {
            $(".economycheckbox").each(function () {
                if ($(this).attr("regionId") == incomeId) {
                    $(this).attr("checked", checkedStatus);
                }
            });
        }
    });

    $(".ChooseAllLaws").click(function () {
        var checkedStatus = $(this).is(":checked");
        $(".LawTypecheckbox").each(function () {
            $(this).attr("checked", checkedStatus);
        });
    });

    $(".ChooseAllYears").click(function () {
        var checkedStatus = $(this).is(":checked");
        $("ul#PreviewDataYears").empty();
        $(".Yearcheckbox").each(function () {
            $(this).attr("checked", checkedStatus);
        });
    });

    $(".ChooseAllEconomies").click(function () {
        var checkedStatus = $(this).is(":checked");
        $("ul#PreviewEconomies").empty();
        $(".economycheckbox").each(function () {
            $(this).attr("checked", checkedStatus);
        });
    });
});
