var gridName;
var ControlPrefix; // Holds the Control prefix 
var chkSingleControl; // Holds the Control Sufix
var hdnIdHolder; // Holds the hidden field which contains the ID of the data key
var hdnSelectedIDList; // Holds the hidden field to which the selected IDs has to be there
var chkSelectAll // Holds the select all Check boxes.

// ---------------------------------------------------------------------------------------------------

function SelectAllModules(isChecked) {
    var iterator = 2;
    var nameOfTheItem;
    var elementID = document.getElementById("gvModuleOperationsList_ctl" + (iterator < 10 ? "0" + iterator : iterator)
            + "_chkModuleID");
    while (elementID != null) {
        if (iterator != 8) {
            elementID.checked = isChecked;
            if (!isChecked) {
                UnCheckOperations("gvModuleOperationsList_ctl" + (iterator < 10 ? "0" + iterator : iterator)
            + "_chkModuleID", isChecked);

            }
            AddRemoveSelectedItems("hdnSelectedModuleList", "gvModuleOperationsList_ctl" +
                (iterator < 10 ? "0" + iterator : iterator) + "_hdnModuleID", isChecked);
            SelectAllOperations("gvModuleOperationsList_ctl" + (iterator < 10 ? "0" + iterator : iterator)
            + "_gvOperationsList_ctl01_chkSelectAllOperations", isChecked);
            iterator++;
            elementID = document.getElementById("gvModuleOperationsList_ctl" +
                (iterator < 10 ? "0" + iterator : iterator) + "_chkModuleID");
        }
        else {
            elementID = null;
        }
    }
}

function SelectOrUnSelectHeader(checkBoxName, isChecked) {
    var iterator = 2;
    var checkedItems = 0;
    var nameOfTheItem = checkBoxName.toString();
    var elementID = document.getElementById("gvModuleOperationsList_ctl" +
            (iterator < 10 ? "0" + iterator : iterator) + "_chkModuleID");
    AddRemoveSelectedItems("hdnSelectedModuleList", checkBoxName.toString().substring
            (0, checkBoxName.toString().lastIndexOf("_")) + "_hdnModuleID",
            isChecked);
    while (elementID != null) {
        if (elementID.checked) checkedItems++;
        iterator++;
        elementID = document.getElementById("gvModuleOperationsList_ctl" +
                (iterator < 10 ? "0" + iterator : iterator) + "_chkModuleID");
    }
    document.getElementById('gvModuleOperationsList_ctl01_chkModules').checked = (iterator - 2 == checkedItems);
}

function SelectAllOperations(selectAllCheckBoxName, isChecked) {
    var prefix = selectAllCheckBoxName.toString().substring(0, selectAllCheckBoxName.toString().lastIndexOf("_") - 2);
    var nameOfItem = selectAllCheckBoxName.toString();
    var iterator = 2;
    var elementID = document.getElementById(prefix + (iterator < 10 ? "0" + iterator : iterator)
            + "_chkOperationCheck");
    while (elementID != null) {
        elementID.checked = isChecked;
        AddRemoveSelectedItems("hdnSelectedOperationList", prefix + (iterator < 10 ? "0" +
                iterator : iterator) + "_hdnOperationID", isChecked);
        iterator++;
        elementID = document.getElementById(prefix + (iterator < 10 ? "0" + iterator : iterator) +
                    "_chkOperationCheck");
    }
    document.getElementById(nameOfItem.substring(0, nameOfItem.indexOf("_",
                nameOfItem.indexOf("_") + 1)) + "_chkModuleID").checked = isChecked;
    AddRemoveSelectedItems("hdnSelectedModuleList", nameOfItem.substring(0, nameOfItem.indexOf("_",
                nameOfItem.indexOf("_") + 1)) + "_hdnModuleID", isChecked);
}

function UnCheckOperations(checkboxName, isChecked) {
    var nameOfTheItem = checkboxName.toString();   
    if (!isChecked) {
        if (document.getElementById(nameOfTheItem.substring(
            0, nameOfTheItem.lastIndexOf("_")) + "_gvOperationsList_ctl01_chkSelectAllOperations") != null) {
            document.getElementById(nameOfTheItem.substring(
            0, nameOfTheItem.lastIndexOf("_")) + "_gvOperationsList_ctl01_chkSelectAllOperations").checked = false;
            SelectAllOperations(nameOfTheItem.substring(
            0, nameOfTheItem.lastIndexOf("_")) + "_gvOperationsList_ctl01_chkSelectAllOperations", false);
        }
    }
    else {
        if (document.getElementById(nameOfTheItem.substring(
            0, nameOfTheItem.lastIndexOf("_")) + "_gvOperationsList_ctl01_chkSelectAllOperations") != null) {
            document.getElementById(nameOfTheItem.substring(
            0, nameOfTheItem.lastIndexOf("_")) + "_gvOperationsList_ctl01_chkSelectAllOperations").checked = true;
            SelectAllOperations(nameOfTheItem.substring(
            0, nameOfTheItem.lastIndexOf("_")) + "_gvOperationsList_ctl01_chkSelectAllOperations", true);
        }

    }
}

function SelectOrUnSelectOperationsHead(checkBoxName, isChecked) {
    var nameOfItem = checkBoxName.toString();
    AddRemoveSelectedItems("hdnSelectedOperationList", nameOfItem.substring(0,
             nameOfItem.lastIndexOf("_")) + "_hdnOperationID", isChecked);
    var prefix = nameOfItem.substring(0, nameOfItem.lastIndexOf("_") - 2);
    var iterator = 2;
    var checkedItems = 0;
    var elementID = document.getElementById(prefix + (iterator < 10 ? "0" + iterator : iterator) +
                    "_chkOperationCheck");
    while (elementID != null) {
        if (elementID.checked) checkedItems++;
        iterator++;
        elementID = document.getElementById(prefix + (iterator < 10 ? "0" + iterator : iterator) +
                    "_chkOperationCheck");
    }
    document.getElementById(prefix + "01" + "_chkSelectAllOperations").checked = (checkedItems == iterator - 2);

    document.getElementById(nameOfItem.substring(0, nameOfItem.indexOf("_",
                nameOfItem.indexOf("_") + 1)) + "_chkModuleID").checked = (checkedItems > 0);
    SelectOrUnSelectHeader(nameOfItem.substring(0, nameOfItem.indexOf("_",
                nameOfItem.indexOf("_") + 1)) + "_chkModuleID", isChecked);
}

function AddRemoveSelectedItems(holderField, valueField, isChecked) {
    var selectedValues = document.getElementById(holderField).value.split(",");
    var selectedValue = document.getElementById(valueField).value;
    var i = 2;
    if (Array.contains(selectedValues, selectedValue)) {
        if (!isChecked) {
            Array.remove(selectedValues, selectedValue);
        }
    }
    else if(isChecked){
        if (selectedValues[selectedValues.length - 1] == "") {
            Array.insert(selectedValues, selectedValues.length - 1, selectedValue);
        }
        else {
            Array.add(selectedValues, selectedValue);
        }
    }
    document.getElementById(holderField).value = selectedValues.join(",");
}

function ExpandOrCollpase(buttonName) {

    var isIE = CheckBrowser();
    var nameofButton = buttonName.toString();

    var OperationsElement = document.getElementById(nameofButton.substring(0, nameofButton.lastIndexOf("_")) + "_trOperationsList");

    if (OperationsElement != null) {

        if (document.getElementById("hdnId1") != null) {

            document.getElementById("hdnId1").value = "";

            document.getElementById("hdnId1").value = nameofButton;

        }

        var operationsClassName = isIE ? OperationsElement.getAttribute('className') : OperationsElement.getAttribute('class');

        if (operationsClassName == 'ControlShow') {


            OperationsElement.setAttribute(isIE ? 'className' : 'class', 'ControlHide');

            document.getElementById(buttonName).src = '../images/gvCollapsedButton.png';

        }

        else {

            OperationsElement.setAttribute(isIE ? 'className' : 'class', 'ControlShow');

            document.getElementById(buttonName).src = '../images/gvExpandedButton.png';

        }

    }

    else {

        var OperationsElement = document.getElementById(nameofButton.substring(0, nameofButton.lastIndexOf("_")) + "_trOperationsList1");

        if (OperationsElement != null) {

            if (document.getElementById("hdnId2") != null) {

                document.getElementById("hdnId2").value = "";

                document.getElementById("hdnId2").value = nameofButton;

            }

            var operationsClassName = OperationsElement.getAttribute(isIE ? 'className' : 'class');

            if (operationsClassName == 'ControlShow') {

                OperationsElement.setAttribute(isIE ? 'className' : 'class', 'ControlHide');

                document.getElementById(buttonName).src = '../images/gvCollapsedButton.png';

            }

            else {

                OperationsElement.setAttribute(isIE ? 'className' : 'class', 'ControlShow');

                document.getElementById(buttonName).src = '../images/gvExpandedButton.png';

            }

        }

        else {

            var OperationsElement = document.getElementById(nameofButton.substring(0, nameofButton.lastIndexOf("_")) + "_trOperationsList2");

            var operationsClassName = OperationsElement.getAttribute(isIE ? 'className' : 'class');

            if (operationsClassName == 'ControlShow') {

                OperationsElement.setAttribute(isIE ? 'className' : 'class', 'ControlHide');

                document.getElementById(buttonName).src = '../images/gvCollapsedButton.png';

            }

            else {

                OperationsElement.setAttribute(isIE ? 'className' : 'class', 'ControlShow');

                document.getElementById(buttonName).src = '../images/gvExpandedButton.png';

            }

        }

    }

}