﻿$(document).ready(function ($) {
    $("#showRepData").hide();
    $("#xlReport").hide();
    $("div.economygrp").hide();
    $("div#economyLst").toggle();
    showEconomyList = function (index) {
        $("#ulActive li").removeClass("active");

        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();
        }
    }

    var arrayEconomyIds = [];
    var arrayTopicIds = [];
    var arrayYears = [];
    var Economy = false;
    var Year = false;


    $('.getCustomQryReport').click(function (event) {
        GetCustomQueryReport();
        event.stopPropagation();
        event.preventDefault();
    });

    function GetCustomQueryReport() {
        arrayEconomyIds = [];
        arrayTopicIds = [];
        arrayYears = [];

        $('input:checkbox:checked').each(function (i) {
            switch ($(this).attr('id').toString().substr(2, 2)) {
                case 'Ec':
                    arrayEconomyIds[i] = $(this).attr('id').toString().substr(9, $(this).attr('id').toString().length);
                    if (!Economy) {
                        if (arrayEconomyIds[i].match('^(0|[1-9][0-9]*)$')) {
                            Economy = true;
                        }
                    }
                    break;

                case 'To':
                    arrayTopicIds[i] = $(this).attr('id').toString().substr(7); // $(this).attr("chklawtypeid");
                    break;
                case 'Ye':
                    arrayYears[i] = $(this).attr('id').toString().substr(6); // $(this).attr("chklawtypeid");
                    if (!Year) {
                        if (arrayYears[i].match('^(0|[1-9][0-9]*)$')) {
                            Year = true;
                        }
                    }
                    break;
            }

        });

        if (!Economy || !Year) {
            alert("Please select atleast one Economy and one Year");
            return;
        }
        $("#showRepData").show();
        $("#xlReport").show();
        $("#reportCollapse").hide();

        $.get("/Custom-Query?topicIds=" + arrayTopicIds + "&EconomyIds=" + arrayEconomyIds + "&Years=" + arrayYears + "&ajax=1", function (data) {
            $(".CQResult").html(data);
            /*var y = $(data).filter(function () { return $(this).is("input") });
            y.each(function () {
                document.title = $(this).val();
            });*/
        });
    }


    $(".excel").click(ExcelExport);

    function ExcelExport(event) {
        event.stopPropagation();
        event.preventDefault();
        window.location = "/Handlers/CustomQuery.ashx?topicIds=" + arrayTopicIds + "&EconomyIds=" + arrayEconomyIds + "&Years=" + arrayYears + "&excel=true";
        return false;
    }

    $(".toggleReportData").click(function () {
        $("a#showRepData").toggle();
        $("div#reportCollapse").show();
    });

    $(".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);
                Preview_Economy(this);
            }
        });
        Preview_regionIncomeGroup(this);
        $(".remove").unbind('click');
        $(".remove").bind('click', Remove_Click);
    });

    $(".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);
                    Preview_Economy(this);
                }
            });
        }
        else {
            $(".economycheckbox").each(function () {
                if ($(this).attr("regionId") == incomeId) {
                    $(this).attr("checked", checkedStatus);
                    Preview_Economy(this);
                }
            });
        }
        Preview_regIncomeGroup(this);
        $(".remove").unbind('click');
        $(".remove").bind('click', Remove_Click);
    });

    $(".ChooseAllTopics").click(function () {
        var checkedStatus = $(this).is(":checked");
        CheckAllTopics(checkedStatus);
    });

    function CheckAllTopics(checkedStatus) {
        $("ul#PreviewTopics").empty();
        $(".Topiccheckbox").each(function () {
            $(this).attr("checked", checkedStatus);
            Preview_Topics(this);
        });
        $(".remove").unbind('click');
        $(".remove").bind('click', Remove_Click);
    }

    $(".ChooseAllYears").click(function () {
        var checkedStatus = $(this).is(":checked");
        CheckAllYears(checkedStatus);
    });

    function CheckAllYears(checkedStatus) {
        $("ul#PreviewDataYears").empty();
        $(".Yearcheckbox").each(function () {
            $(this).attr("checked", checkedStatus);
            Preview_Year(this);

        });
        $(".remove").unbind('click');
        $(".remove").bind('click', Remove_Click);
    }

    $(".ChooseAllEconomies").click(function () {
        var checkedStatus = $(this).is(":checked");
        $("ul#PreviewEconomies").empty();
        $(".economycheckbox").each(function () {
            $(this).attr("checked", checkedStatus);
            Preview_Economy(this);
        });
        $(".remove").unbind('click');
        $(".remove").bind('click', Remove_Click);
    });

    $(".economycheckbox").click(function () {
        Preview_Economy(this);
        $(".remove").unbind('click');
        $(".remove").bind('click', Remove_Click);
    });

    $(".economycheckbox").change(function () {

    });

    $(".Topiccheckbox").click(function () {
        Preview_Topics(this);
        $(".remove").unbind('click');
        $(".remove").bind('click', Remove_Click);
    });

    $(".Yearcheckbox").click(function () {
        Preview_Year(this);
        $(".remove").unbind('click');
        $(".remove").bind('click', Remove_Click);
    });

    $(".remove").click(Remove_Click);

    function Remove_Click() {
        var temp = ($(this).attr('id').toString().substr(1));
        $("li#lbl" + temp).remove();
        var check = document.getElementById(temp);
        check.checked = false;
    }

    function Preview_Year(chk) {
        var checkedStatus = $(chk).is(":checked");
        if (checkedStatus) {
            $("ul#PreviewDataYears").append('<li id=lbl' + $(chk).attr("id") + '>' + $(chk).attr("yearName") + '<a href="#DataYears" class="remove" id=a' + $(chk).attr("id") + '><img src="/App_Themes/css/images/buttons/remove-item-13x13.png" alt="remove item" width="13" height="13" /></a></li>');

        }
        else {
            $("li#lbl" + $(chk).attr("id")).remove();
        }
    }

    function Preview_Economy(chk) {
        var checkedStatus = $(chk).is(":checked");
        if (checkedStatus) {
            $("ul#PreviewEconomies").append('<li id=lbl' + $(chk).attr("id") + '>' + $(chk).attr("economyName") + '<a href="#Economies" class="remove" id=a' + $(chk).attr("id") + '><img src="/App_Themes/css/images/buttons/remove-item-13x13.png" alt="remove item" width="13" height="13" /></a></li>');
        }
        else {
            $("li#lbl" + $(chk).attr("id")).remove();
        }
    }

    function Preview_Topics(chk) {
        var checkedStatus = $(chk).is(":checked");
        if (checkedStatus) {
            $("ul#PreviewTopics").append('<li id=lbl' + $(chk).attr("id") + '>' + $(chk).attr("topicName") + '<a href="#Topics" class="remove" id=a' + $(chk).attr("id") + '><img src="/App_Themes/css/images/buttons/remove-item-13x13.png" alt="remove item" width="13" height="13" /></a></li>');
        }
        else {
            $("li#lbl" + $(chk).attr("id")).remove();
        }
    }

    function Preview_regionIncomeGroup(chk) {
        var checkedStatus = $(chk).is(":checked");
        if (checkedStatus) {
            $("p#regionIncomeGroup").append('<span class="mark" id=sp' + $(chk).attr("id") + '> ' + $(chk).attr("chkRegionname") + ' ,</span>');
        }
        else {
            $("span#sp" + $(chk).attr("id")).remove();
        }
    }

    function Preview_regIncomeGroup(chk) {
        var checkedStatus = $(chk).is(":checked");
        if (checkedStatus) {
            $("p#regionIncomeGroup").append('<span class="mark" id=sp' + $(chk).attr("id") + '> ' + $(chk).attr("chkIncomename") + ' ,</span>');
        }
        else {
            $("span#sp" + $(chk).attr("id")).remove();
        }
    }

    if ($(".HdEconomyIds").val() != "") {
        CheckAllTopics(true);
        CheckAllYears(true);
        $("#cbEconomy" + $(".HdEconomyIds").val()).attr("checked", true);
        GetCustomQueryReport();
    }

});


