﻿$(document).ready(function ($) {

    //$('.gvCustomQuery').fixedHeaderTable({ width: '300', height: '250', rows: 5, footer: true, autoShow: true });
    //$('.mohan').fixedHeaderTable({ width: '200', height: '250', rows: 5, footer: true, autoShow: true });


    $("#custom-query").hide();
    $("#notes").hide();
    $("#dvUtils").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();
            $("a#aShowAll").empty();
            $("a#aShowAll").append($(".csHideAll").val());
        }
    }

    var arrayEconomyIds = [];
    var arrayTopicIds = [];
    var arrayCuts = [];
    var arrayIndicators = [];





    $(".toggleReportData").click(function () {
        $("a#custom-query").toggle();
        $("div#reportCollapse").show();
        $(".cqDesc").show();
        $(".cqResultsDesc").hide();   
    });

    $(".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);
    }

    $(".ChooseAllCuts").click(function () {
        var checkedStatus = $(this).is(":checked");
        CheckAllCuts(checkedStatus);
    });

    function CheckAllCuts(checkedStatus) {
        $("ul#PreviewDataCuts").empty();
        $(".Cutcheckbox").each(function () {
            $(this).attr("checked", checkedStatus);
            Preview_Cut(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);
    });

    /*$("#economies-MostRecent").click(function () {
    var mostRecent = $(this).is(":checked");
    $("ul#PreviewEconomies").empty();

    var qryStr = "?ajax=1&mostRecent=" + mostRecent;

    $.get($(this).attr("rel") + qryStr, function (data) {

    $("div#economyLst").hide();
    $("div#economyLst").html(data);
    $("div#economyLst").fadeIn(1000);

    $(".economycheckbox").unbind();
    $(".economycheckbox").bind('click', economycheckbox_Click);

    });

    });*/

    function economycheckbox_Click() {
        Preview_Economy(this);
        $(".remove").unbind('click');
        $(".remove").bind('click', Remove_Click);
    }

    $(".economycheckbox").click(economycheckbox_Click);

    $(".economycheckbox").change(function () {

    });

    $(".Topiccheckbox").click(function () {
        Preview_Topics(this);
        $(".remove").unbind('click');
        $(".remove").bind('click', Remove_Click);
    });

    $(".Cutcheckbox").click(function () {
        Preview_Cut(this);
        $(".remove").unbind('click');
        $(".remove").bind('click', Remove_Click);
    });

    $(".remove").click(Remove_Click);

    $("#chkSENOB").click(function (event) {

        $("#dvLoad").addClass("loading");

        var ischkSENOB = $(this).is(":checked");

        if (ischkSENOB) {
            $(".seNOB").show();
            $(".abbrNOB").show();
        }
        else {
            $(".seNOB").hide();
            $(".abbrNOB").hide();
        }

        $("#dvLoad").removeClass("loading");

    });

    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_Cut(chk) {
        var checkedStatus = $(chk).is(":checked");
        if (checkedStatus) {
            $("ul#PreviewDataCuts").append('<li id=lbl' + $(chk).attr("id") + '>' + $(chk).attr("cutName") + '<a href="#DataCuts" class="remove" id=a' + $(chk).attr("id") + '><img src="/App_Themes/css/images/remove.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/remove.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/remove.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);
        CheckAllCuts(true);
        //        $("#cbEconomy" + $(".HdEconomyIds").val()).attr("checked", true);
        //        Preview_Economy($("#cbEconomy" + $(".HdEconomyIds").val()));

        var ecoIds = $(".HdEconomyIds").val().split(',');

        if (ecoIds.length > 0) {
            $.each(ecoIds, function (index, value) {
                $("#cbEconomy" + value).attr("checked", true);
                Preview_Economy($("#cbEconomy" + value));
            });
        }

        $(".cqDesc").hide();
        $(".cqResultsDesc").show();        
        
        GetCustomQueryReport();
    }

    $('.getCustomQryReport').click(function (event) {

        $("#reportCollapse").hide();
        $("#custom-query").show();
        $("#custom-query").addClass(" loading");
        $(".cqDesc").hide();
        $(".cqResultsDesc").show();   
        GetCustomQueryReport();
        event.stopPropagation();
        event.preventDefault();
        $("#custom-query").removeClass("loading");
    });

    function GetCustomQueryReport() {
        arrayEconomyIds = [];
        arrayTopicIds = [];
        arrayCuts = [];

        var Economy = false;
        var Topic = false;
        var Cut = false;

        $("#dvLoad").addClass("loading");

        $('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");

                    if (!Topic) {
                        if (arrayTopicIds[i].match('^(0|[1-9][0-9]*)$')) {
                            Topic = true;
                        }
                    }
                    break;
                case 'Cu':
                    arrayCuts[i] = $(this).attr('id').toString().substr(5); // $(this).attr("chklawtypeid");
                    if (!Cut) {
                        if (arrayCuts[i].match('^(0|[1-9][0-9]*)$')) {
                            Cut = true;
                        }
                    }
                    break;
            }

        });

        if (!Economy) {
            alert($(".HdAlertSelectEconomy").val());
            return;
        }

        if (!Topic) {
            alert($(".HdAlertSelectTopic").val());
            return;
        }

        $("#custom-query").show();
        $("#dvUtils").show();
        $("#reportCollapse").hide();
        $("#notes").show();


        $(".HdEcoDataIds").val(arrayEconomyIds);
        $(".HdTopicIds").val(arrayTopicIds);
        $(".HdCuts").val(arrayCuts);

        $(".HdIndIds").val("");

        var mostRecent = $("#economies-MostRecent").is(":checked");
        var ischkSENOB = $("#chkSENOB").is(":checked");

        var qryStr = "?ajax=1&getResult=1" + "&topicIds=" + arrayTopicIds + "&EconomyIds=" + arrayEconomyIds + "&cutsIds=" + arrayCuts + "&mostRecent=" + mostRecent;

        $.get(qryStr, function (data) {

            $(".CQResult").html(data);
            if (ischkSENOB) {
                $(".seNOB").show();
                $(".abbrNOB").show();
            }
            else {
                $(".seNOB").hide();
                $(".abbrNOB").hide();
            }

            var y = $(data).filter(function () { return $(this).is("input") });
            y.each(function () {
                document.title = $(this).val();
            });


            //FreezeRowsCols();

            //$('gvCustomQuery').fixedHeaderTable('destroy');
            //alert("test");
            //$('#tableBlock > table.myTable').fixedHeaderTable({ height: '600', altClass: 'odd', footer: hasFooter, cloneHeadToFoot: hasClone, fixedColumn: hasFixedColumn, themeClass: 'fancyTable' });
            //$('.gvCustomQuery').fixedHeaderTable({ height: '300', footer: false, cloneHeadToFoot: false, fixedColumn: true });
            //$('.gvCustomQuery').fixedHeaderTable({ width: '300', height: '250', rows: 5, footer: true, autoShow: true });
            //$('.gvCustomQuery').fixedHeaderTable({ cloneHeadToFoot: true, autoShow: false });
            //$('.gvCustomQuery').fixedHeaderTable('show', 100);

            attachClickEvent();

            //            FixGridViewHeaderInsideTHEAD($(".gvCustomQuery"));
            //            // register the jquery floater func..
            //            jQuery('.gvCustomQuery').floatHeader({
            //                fadeIn: 250,
            //                fadeOut: 250
            //            });

            //$('.gvCustomQuery').fixedHeaderTable({ width: '500', height: '250', rows: 5, footer: true, autoShow: true });

            $("#dvLoad").removeClass("loading");

        });
    }

    function FreezeRowsCols() {

        var oTable = $('.gvCustomQuery').dataTable({
            "sScrollY": "400px",
            "sScrollX": "100%",
            "sScrollXInner": "150%",
            "bScrollCollapse": true,
            "bPaginate": false
        });
        new FixedColumns(oTable, {
            "iLeftColumns": 3,
            "iLeftWidth": 300
        });
    }

    $(".excelText").click(ExcelExport);

    function ExcelExport(event) {
        event.stopPropagation();
        event.preventDefault();

        var ischkSENOB = $("#chkSENOB").is(":checked");

        var HdEcoDataIds = $(".HdEcoDataIds").val();
        var HdTopicIds = $(".HdTopicIds").val();
        var HdCuts = $(".HdCuts").val();
        var HdIndicators = $(".HdIndIds").val();
        var HdIndCols = $(".HdIndCols").val();

        var mostRecent = $("#economies-MostRecent").is(":checked");

        var qryStr = "/Handlers/CustomQuery.ashx?ajax=1&&excel=1" + "&EconomyIds=" + HdEcoDataIds + "&topicIds=" + HdTopicIds + "&cutsIds=" + HdCuts;
        qryStr = qryStr + "&HdIndicators=" + HdIndicators + "&HdIndCols=" + HdIndCols + "&ischkSENOB=" + ischkSENOB + "&mostRecent=" + mostRecent;


        window.location = qryStr;

        return false;
    }

    $(".SortCol").click(SortCol_Click);

    function SortCol_Click(event) {

        event.stopPropagation();
        event.preventDefault();

        var colId = ($(this).attr('colId').toString().substr(5));
        var colIndex = $(this).attr("colIndex");
        var tpId = "." + $(this).attr("tpId");
        var SortColName = $(this).attr("SortColName");
        var SortOrder = $(this).attr("SortOrder");


        var HdEcoDataIds = $(".HdEcoDataIds").val();
        var HdTopicIds = $(".HdTopicIds").val();
        var HdCuts = $(".HdCuts").val();
        var HdIndicators = $(".HdIndIds").val();
        var HdIndCols = $(".HdIndCols").val();

        $("#dvLoad").addClass("loading");



        $("#custom-query").show();
        $("#dvUtils").show();
        $("#reportCollapse").hide();

        var mostRecent = $("#economies-MostRecent").is(":checked");
        var ischkSENOB = $("#chkSENOB").is(":checked");

        var qryStr = $(this).attr("href") + "?ajax=1&getResult=1" + "&topicIds=" + HdTopicIds + "&EconomyIds=" + HdEcoDataIds + "&cutsIds=" + HdCuts + "&mostRecent=" + mostRecent;
        qryStr = qryStr + "&HdIndicators" + HdIndicators + "&sortColumn=" + colIndex + "&SortColName=" + SortColName + "&SortOrder=" + SortOrder + "&HdIndCols=" + HdIndCols;

        $.get(qryStr, function (data) {

            $(".CQResult").html(data);
            if (ischkSENOB) {
                $(".seNOB").show();
                $(".abbrNOB").show();
            }
            else {
                $(".seNOB").hide();
                $(".abbrNOB").hide();
            }
            var y = $(data).filter(function () { return $(this).is("input") });
            y.each(function () {
                document.title = $(this).val();
            });


            attachClickEvent();

            //FreezeRowsCols();

            $("#dvLoad").removeClass("loading");



            window.location.hash = "#" + "dvCQResult";

        });

    }

    $(".removeCol").click(RemoveCol_Click);

    function RemoveCol_Click(event) {

        event.stopPropagation();
        event.preventDefault();

        var colId = ($(this).attr('colId').toString().substr(5));
        var colIndex = "." + $(this).attr("colIndex");
        var tpId = "." + $(this).attr("tpId");
        var HdIndicators = $(".HdIndIds").val();
        if (HdIndicators == "")
            HdIndicators = colId;
        else
            HdIndicators += "," + colId;


        $(".HdIndIds").val(HdIndicators);

        var inds = HdIndicators.split(',');

        var HdIndCols = $(".HdIndCols").val();
        if (HdIndCols == "")
            HdIndCols = colIndex.substr(7);
        else
            HdIndCols += "," + colIndex.substr(7);


        $(".HdIndCols").val(HdIndCols);


        var v = $(tpId).attr("colSpan");
        $(tpId).attr("colSpan", v - 1);

        //alert("colId=>" + colId + ";colIndex=>" + colIndex + ";tpId=>" + tpId + ";v=>" + v);
        if (v <= 1) {
            $(tpId).attr("colHidden", 1);
            $(tpId).hide();
        }
        else
            $(tpId).attr("colHidden", 0);

        $(colIndex).hide();

        var calculatedWidth = getWidthForSubCutstable();
        //alert(calculatedWidth);
        $(".gvCustomQuery tr.open .breakdown").css({ width: calculatedWidth + "px" }).css("margin-right", "-" + calculatedWidth + "px");

        $('#btnUndoRemoveCol').removeAttr('disabled');

    }

    $("#btnUndoRemoveCol").click(function (event) {

        var HdIndicators = $(".HdIndIds").val();
        var inds = HdIndicators.split(',');
        var colId = ".colId" + inds[inds.length - 1];
        var colIndex = "." + $(colId).attr("colIndex");
        var tpId = "." + $(colId).attr("tpId");
        var v = $(tpId).attr("colSpan");
        $(tpId).attr("colSpan", v + 1);
        var colHidden = $(tpId).attr("colHidden");

        //alert("colId=>" + colId + ";colIndex=>" + colIndex + ";tpId=>" + tpId + ";v=>" + v + ";colHidden=>" + colHidden);
        if (colHidden == 1) {
            $(tpId).attr("colSpan", v);
            $(tpId).show();
            $(tpId).attr("colHidden", 0);            
        }


        
        $(colIndex).show();

        var oldInds = HdIndicators.split(inds[inds.length - 1]);
        var newInds = oldInds[0].substring(0, oldInds[0].length - 1);
        $(".HdIndIds").val(newInds);
        if (newInds == "")
            $('#btnUndoRemoveCol').attr('disabled', 'disabled');

        var HdIndCols = $(".HdIndCols").val();
        var indColIds = HdIndCols.split(',');
        var oldIndColIds = HdIndCols.split(indColIds[indColIds.length - 1]);
        var newIndColIds = oldIndColIds[0].substring(0, oldIndColIds[0].length - 1);
        $(".HdIndCols").val(newIndColIds);

        var calculatedWidth = getWidthForSubCutstable();
        //alert(calculatedWidth);
        $(".gvCustomQuery tr.open .breakdown").css({ width: calculatedWidth + "px" }).css("margin-right", "-" + calculatedWidth + "px");

    });

    $(".expand-toggle").click(subCutsExpander_click());

    //$(".subCutsCollapsedShow").click(subCutsExpander_click());

    function subCutsExpander_click(event) {

        if (event == undefined)
            return;

        event.preventDefault();
        event.stopPropagation();

        var ischkSENOB = $("#chkSENOB").is(":checked");

        var subCutsExpandedShow = "subCutsExpandedShow";
        var clsName = $(this)[0].className;
        var ecoId = $(this).attr("ecoId");

        var clsEcoId = "#eco" + ecoId;

        if ($($($(clsEcoId).parent("td"))[0]).find(".gvSubCuts").length == 1) {

            //alert("-" + $($($(clsEcoId).parent().parent("tr"))[0])[0].className + "-");
            if ($($($(clsEcoId).parent().parent("tr"))[0])[0].className == 'closed') {
                //if ($($($(clsEcoId).parent().parent("tr"))[0]).find(".closed").length == 1) {
                $($($(clsEcoId).parent().parent("tr"))[0]).addClass("open").removeClass('closed');

                var marginTop = 0;
                if (ischkSENOB)
                    marginTop = 35

                var calculatedWidth = getWidthForSubCutstable();
                $($($($(clsEcoId).parent("td"))[0]).find(".breakdown")[0]).css({ width: calculatedWidth + "px" }).css("margin-right", "-" + calculatedWidth + "px").css("margin-top", marginTop + "px");
            }
            else {
                $($($(clsEcoId).parent().parent("tr"))[0]).addClass("closed").removeClass('open');
            }

            //            if ($($($(clsEcoId).parent("td"))[0]).find(".subCutsCollapsedShow").length == 1) {

            //                //$($($(clsEcoId).parent("td"))[0]).find(".gvSubCuts").show();
            //                //$($($($(clsEcoId).parent("td"))[0]).find(".closed")[0]).addClass("open");
            //                //$($($($(clsEcoId).parent("td"))[0]).find(".closed")[0]).removeClass("closed");

            //                $($($(clsEcoId).parent().parent("tr"))[0]).addClass("open").removeClass('closed');

            //                $($($($(clsEcoId).parent("td"))[0]).find(".subCutsCollapsedShow")[0]).addClass("subCutsExpandedShow");

            //                $($($($(clsEcoId).parent("td"))[0]).find(".subCutsCollapsedShow")[0]).removeClass("subCutsCollapsedShow");



            //            }
            //            else if ($($($(clsEcoId).parent("td"))[0]).find(".subCutsExpandedShow").length == 1) {

            //                //$($($(clsEcoId).parent("td"))[0]).find(".gvSubCuts").hide();
            //                //$($($($(clsEcoId).parent("td"))[0]).find(".open")[0]).addClass("closed");
            //                //$($($($(clsEcoId).parent("td"))[0]).find(".open")[0]).removeClass("open");

            //                $($($(clsEcoId).parent().parent("tr"))[0]).addClass("closed").removeClass('open');


            //                //alert("subCutsExpandedShow = " + $($($(clsEcoId).parent("td"))[0]).find(".subCutsExpandedShow").length);

            //                $($($($(clsEcoId).parent("td"))[0]).find(".subCutsExpandedShow")[0]).addClass("subCutsCollapsedShow");

            //                $($($($(clsEcoId).parent("td"))[0]).find(".subCutsExpandedShow")[0]).removeClass("subCutsExpandedShow");

            //            }





            //            $(this).removeClass(clsName);
            //            var clsName1 = $.trim(clsName);
            //            if (clsName1 == subCutsExpandedShow) {
            //                $(clsEcoId).children().find(".gvSubCuts").hide();
            //                $(this).addClass("subCutsCollapsedShow");

            //            }
            //            else {
            //                $(clsEcoId).children().find(".gvSubCuts").show();
            //                $(this).addClass("subCutsExpandedShow");
            //            }


            return;
        }

        var HdEcoDataIds = $(".HdEcoDataIds").val();
        var HdTopicIds = $(".HdTopicIds").val();
        var HdCuts = $(".HdCuts").val();
        var HdIndicators = $(".HdIndIds").val();
        var HdIndCols = $(".HdIndCols").val();

        //var mohan = $($($($(clsEcoId).parent().parent("tr"))[0]).children().find(".breakdown")[0]).html();
        //alert ($($($($(clsEcoId).parent().parent("tr"))[0]).children().find(".breakdown")[0]).html());
        $("#dvLoad").addClass("loading");

        var qryStr = "/Handlers/CustomQuery.ashx?ajax=1&getResult=1" + "&EconomyIds=" + ecoId + "&topicIds=" + HdTopicIds + "&cutsIds=" + HdCuts;
        qryStr = qryStr + "&HdIndicators=" + HdIndicators + "&HdIndCols=" + HdIndCols;


        $.get(qryStr, function (data) {
            //$($($($(clsEcoId).parent().parent("tr"))[0]).children().find(".breakdown")[0]).html(data);

            $($($($(clsEcoId).parent("td"))[0]).find(".breakdown")[0]).html(data);

            var marginTop = 0;

            if (ischkSENOB) {
                $(".seNOB").show();
                $(".abbrNOB").show();
                marginTop = 35
            }
            else {
                $(".seNOB").hide();
                $(".abbrNOB").hide();
            }

            //$($($($(clsEcoId).parent("td"))[0]).find(".breakdown")[0]).addClass("open");


            //$($($(clsEcoId).parent("td"))[0]).find(".gvSubCuts")
            $($($($($($(clsEcoId).parent("td"))[0]).find(".gvSubCuts")[0]).parent("div"))[0]).attr("class", "dvSubCuts clearfix");
            //$($($($($($(clsEcoId).parent().parent("tr"))[0]).children().find(".gvSubCuts")[0]).parent("div"))[0]).attr("class", "dvSubCuts clearfix");


            //var colCount = $(".gvCustomQuery").attr("colCount");
            var calculatedWidth = getWidthForSubCutstable();
            //alert(calculatedWidth);

            //$($($($(clsEcoId).parent("td"))[0]).find(".breakdown")[0]).css({ width: calculatedWidth + "px", margin: "0 -" + calculatedWidth + "px 0 10px" });

            $($($($(clsEcoId).parent("td"))[0]).find(".breakdown")[0]).css({ width: calculatedWidth + "px" }).css("margin-right", "-" + calculatedWidth + "px").css("margin-top", marginTop + "px");

            //$($($($(clsEcoId).parent("td"))[0]).find(".subCutsCollapsedShow")[0]).addClass("subCutsExpandedShow");

            //$($($($(clsEcoId).parent("td"))[0]).find(".subCutsCollapsedShow")[0]).removeClass("subCutsCollapsedShow");

            $($($(clsEcoId).parent().parent("tr"))[0]).addClass("open").removeClass('closed');



            /*

            $($($(clsEcoId).parent().parent("tr"))[0]).children().find(".subCutsCollapsedShow")[0].removeClass(".subCutsCollapsedShow");
            alert($($($(clsEcoId).parent().parent("tr"))[0]).children().find(".subCutsCollapsedShow")[0].className);
            

            $($($(clsEcoId).parent().parent("tr"))[0]).children().find(".subCutsCollapsedShow")[0].addClass("subCutsExpandedShow");
            */
            attachClickEvent();

        });



    }

    $(".expand-collapse").click(subCutsAllExpander_click());

    $(".expanded-all").click(subCutsAllExpander_click());

    function subCutsAllExpander_click(event) {

        if (event == undefined)
            return;

        event.preventDefault();
        event.stopPropagation();

        var ischkSENOB = $("#chkSENOB").is(":checked");

        var subCutsExpandAllShow = "expanded-all";
        var clsName = $(this)[0].className;
        var clsName1 = $.trim(clsName);

        if (clsName1 == subCutsExpandAllShow) {
            //            $(".subCutsExpandedShow").each(function () {
            //                var clsName2 = $(this)[0].className;
            //                $(this).removeClass(clsName2);
            //                $(this).addClass(newclass);
            //            });

            //            $(".gvSubCuts").hide();

            //alert('collapsing all');

            $(".gvCustomQuery tr.open").addClass("closed").removeClass('open');
            //$($($(clsEcoId).parent().parent("tr"))[0]).addClass("closed").removeClass('open');


            $(this).addClass("expand-collapse").removeClass('expanded-all');
        }
        else {


            var HdIndicators = $(".HdIndIds").val();

            var HdEcoDataIds = $(".HdEcoDataIds").val();
            var HdTopicIds = $(".HdTopicIds").val();
            var HdCuts = $(".HdCuts").val();
            var HdIndicators = $(".HdIndIds").val();
            var HdIndCols = $(".HdIndCols").val();


            $("#dvLoad").addClass("loading");

            var qryStr = "/Handlers/CustomQuery.ashx?ajax=1&getResult=1&topicIds=" + HdTopicIds + "&cutsIds=" + HdCuts + "&HdIndicators=" + HdIndicators + "&HdIndCols=" + HdIndCols;

            $(".gvCustomQuery tr a.expand-toggle").each(function () {
                var ecoId = $(this).attr("ecoId");
                var clsEcoId = "#eco" + ecoId;


                if ($($($(clsEcoId).parent("td"))[0]).find(".gvSubCuts").length == 1) {
                    if ($($($(clsEcoId).parent().parent("tr"))[0])[0].className == 'closed') {
                        $($($(clsEcoId).parent().parent("tr"))[0]).addClass("open").removeClass('closed');

                        var marginTop = 0;
                        if (ischkSENOB)
                            marginTop = 35

                        var calculatedWidth = getWidthForSubCutstable();
                        $($($($(clsEcoId).parent("td"))[0]).find(".breakdown")[0]).css({ width: calculatedWidth + "px" }).css("margin-right", "-" + calculatedWidth + "px").css("margin-top", marginTop + "px");
                    }
                }
                else {


                    var qrystr1 = "";
                    qrystr1 = qryStr + "&EconomyIds=" + ecoId;
                    //alert(qrystr1);
                    $.get(qrystr1, function (data) {

                        //                        $($($($(clsEcoId).parent("td"))[0]).find(".breakdown")[0]).html(data);

                        //                        $($($($($($(clsEcoId).parent("td"))[0]).find(".gvSubCuts")[0]).parent("div"))[0]).attr("class", "dvSubCuts clearfix");

                        //                        $($($($(clsEcoId).parent("td"))[0]).find(".subCutsCollapsedShow")[0]).addClass("subCutsExpandedShow");

                        //                        $($($($(clsEcoId).parent("td"))[0]).find(".subCutsCollapsedShow")[0]).removeClass("subCutsCollapsedShow");

                        //                        $($($(clsEcoId).parent("td"))[0]).find(".gvSubCuts").show();


                        $($($($(clsEcoId).parent("td"))[0]).find(".breakdown")[0]).html(data);

                        var marginTop = 0;

                        if (ischkSENOB) {
                            $(".seNOB").show();
                            $(".abbrNOB").show();
                            marginTop = 35
                        }
                        else {
                            $(".seNOB").hide();
                            $(".abbrNOB").hide();
                        }

                        $($($($($($(clsEcoId).parent("td"))[0]).find(".gvSubCuts")[0]).parent("div"))[0]).attr("class", "dvSubCuts clearfix");

                        $($($(clsEcoId).parent().parent("tr"))[0]).addClass("open").removeClass('closed');

                        var calculatedWidth = getWidthForSubCutstable();
                        $($($($(clsEcoId).parent("td"))[0]).find(".breakdown")[0]).css({ width: calculatedWidth + "px" }).css("margin-right", "-" + calculatedWidth + "px").css("margin-top", marginTop + "px");

                        $(".expand-toggle").unbind('click');
                        $(".expand-toggle").bind('click', subCutsExpander_click);
                    });
                }
            });

            $(this).addClass("expanded-all").removeClass('expand-collapse');
            attachClickEvent();

        }

    }


    function getWidthForSubCutstable() {
        var colCount = $(".gvCustomQuery").attr("colCount");
        var HdIndicators = $(".HdIndIds").val();
        var calculatedWidth;
        //alert(HdIndicators.length);
        if (HdIndicators.length > 0) {
            var HdIndArr = $(".HdIndIds").val().split(',');
            calculatedWidth = (colCount - HdIndArr.length) * 180;
        }
        else {
            calculatedWidth = (colCount) * 180;
        }
        return calculatedWidth + 70;
    }

    function attachClickEvent() {

        $(".removeCol").unbind();
        $(".removeCol").bind('click', RemoveCol_Click);

        $(".SortCol").unbind();
        $(".SortCol").bind('click', SortCol_Click);

        $(".expand-toggle").unbind('click');
        $(".expand-toggle").bind('click', subCutsExpander_click);

        $(".expand-collapse").unbind('click');
        $(".expand-collapse").bind('click', subCutsAllExpander_click);

        $(".expanded-all").unbind('click');
        $(".expanded-all").bind('click', subCutsAllExpander_click);

        //        $(".subCutsExpandAllShow").unbind('click');
        //        $(".subCutsExpandAllShow").bind('click', subCutsAllExpander_click);

        //        $(".subCutsCollapseAllShow").unbind('click');
        //        $(".subCutsCollapseAllShow").bind('click', subCutsAllExpander_click);

        //        $(".subCutsExpandedShow").unbind('click');
        //        $(".subCutsExpandedShow").bind('click', subCutsExpander_click);

        //        $(".subCutsCollapsedShow").unbind('click');
        //        $(".subCutsCollapsedShow").bind('click', subCutsExpander_click);
    }


});


