﻿//grid checkboxes
function ChangeCheckBoxState(id, checkState)
{
    var cb = document.getElementById(id);
    if (cb != null)
       cb.checked = checkState;
}

function ChangeAllCheckBoxStates(checkState)
{
    // Toggles through all of the checkboxes defined in the CheckBoxIDs array
    // and updates their value to the checkState input parameter
    if (CheckBoxIDs != null)
    {
        for (var i = 0; i < CheckBoxIDs.length; i++) {
            ChangeCheckBoxState(CheckBoxIDs[i], checkState);
            var str = CheckBoxIDs[i];
            var ex = str.match('HeaderLevelCheckBox');

            if (ex == null) {
                selectrow(document.getElementById(str));
            }
        }
    }
}

function ChangeHeaderAsNeeded(id) {
    if (id != null) {
        var value = id.id.replace("_RowLevelCheckBox", "");

        if (id.checked) {
            document.getElementById(value).className = "row select_raw";
        } else {
            document.getElementById(value).className = "row";
        }

        // Whenever a checkbox in the GridView is toggled, we need to
        // check the Header checkbox if ALL of the GridView checkboxes are
        // checked, and uncheck it otherwise
        if (CheckBoxIDs != null) {
            // check to see if all other checkboxes are checked
            for (var i = 1; i < CheckBoxIDs.length; i++) {
                var cb = document.getElementById(CheckBoxIDs[i]);
                if (!cb.checked) {
                    // Whoops, there is an unchecked checkbox, make sure
                    // that the header checkbox is unchecked
                    ChangeCheckBoxState(CheckBoxIDs[0], false);
                    return;
                }
            }

            // If we reach here, ALL GridView checkboxes are checked
            ChangeCheckBoxState(CheckBoxIDs[0], true);
        } 
    }
}
//select row
function selectrow(id) {
    if (id != null) {
        var value = id.id.replace("_RowLevelCheckBox", "");
        if (id.checked) {
            document.getElementById(value).className = "row select_raw";
        } else {
            document.getElementById(value).className = "row";
        } 
    }
}

//delete grid items
function confirmMsg(frm) {
    var chk = false;
    // loop through all elements
    for (i = 0; i < frm.length; i++) {
        // Look for our checkboxes only
        if (frm.elements[i].name.indexOf("RowLevelCheckBox") != -1) {
            // If any are checked then confirm alert, otherwise nothing happens
            if (frm.elements[i].checked) {
                document.getElementById('ctl00_MainContent_ToDeleteGridIndex').value += frm.elements[i].id.replace("_RowLevelCheckBox",
                    "").replace("ctl00_MainContent_PostingsGrid_Row", "") + ",";
                chk = true;
            }
        }
    }
    if (chk == true)
        return confirm('Are you sure you want to delete your posting(s)?');
    else {
        alert('Please check posting before delete it!')
        return false;
    }
}

//check selected grid items
function checkSelected(frm) {
    var count = 0;

    // loop through all elements
    for (i = 0; i < frm.length; i++) {
        // Look for our checkboxes only
        if (frm.elements[i].name.indexOf("RowLevelCheckBox") != -1) {
            // If any are checked then confirm alert, otherwise nothing happens
            if (frm.elements[i].checked) count++;
        }
    }
    if (count == 1) return true;
    else if (count > 1) {
        alert('Please check only one item!')
        return false;
    }
    else {
        alert('Please check at least one item!')
        return false;
    }
}

function checkListSelection(list, rbt) {
    if (document.getElementById(list)) {
        if (document.getElementById(list).selectedIndex == 0) {
            alert("Select your country please.");
            rbt.checked = false;
            return false;
        }
    }
    setTimeout('__doPostBack(\'ctl00$MainContent$CountrySelectedRButton\',\'\')', 0)
    return true;
}

function checkAjaxListSelection(list, rbt) {
    if (document.getElementById(list)) {
        if (document.getElementById(list).selectedIndex == 0) {
            alert("Select your country please.");
            rbt.checked = false;
            return false;
        }
    }
    setTimeout('__doPostBack(\'ctl00$CountrySelectedRButton\',\'\')', 0)
    return true;
}

