﻿// common operation for chkOne and chkAll rows
// table head contains chkAll or chkAllRows, table rows contain chkOne or chkOneRow

function checkAndHighlightAllChkOne(checkBox) { // select checkboxes and highlight the row, or vice verse
    clearMessage();

    var tableBody = getCheckBoxsParentTableBody(checkBox);
    var inputs = tableBody.getElementsByTagName('INPUT');

    // process all CheckBoxes naming *chkOne*
    // not just one/current table, can be hierarchical if this checkBox is in higher level
    for (var i = 0; i < inputs.length; i++)
        if (inputs[i].type == 'checkbox' && inputs[i].id.indexOf('chkOne') >= 0) {
            // check/uncheck first before hightligh
            inputs[i].checked = checkBox.checked;
            
            // highlight/unhighlight current row
            highlightRow(inputs[i]);
        }
}

function checkAllChkOneInContainer(container, checked) {//container could be Table or TBody
    // only select/deselect checkboxes, not highlight rows
    var inputs = container.getElementsByTagName('INPUT');

    // check/uncheck all chkOne boxes (naming with "chkOne") inside the container
    for (var i = 0; i < inputs.length; i++)
        if (inputs[i].type == 'checkbox' && inputs[i].id.indexOf('chkOne') >= 0)
            inputs[i].checked = checked;
}

function checkOneRow(checkBox) {
    checkOneRow(checkBox, false);
}

function checkOneRow(checkBox, hasTableFooter) {
    clearMessage();
    
    highlightRow(checkBox);
    
    var tableBody = getCheckBoxsParentTableBody(checkBox);
    var tableHeader = tableBody.firstChild;
    var chkAll = tableHeader.getElementsByTagName('INPUT')[0];

    var totalCheckBox = tableBody.childNodes.length - 1;
    if (hasTableFooter)
        totalCheckBox--;
        
    var totalBoxChecked = countCheckedBoxesInTableBody(tableBody, totalCheckBox);
    
    if (totalBoxChecked == totalCheckBox)
        chkAll.checked = true;
    else
        chkAll.checked = false;
}

function countCheckedBoxesInContainer(container) {
    // any container, not limit to table body
    // could be hierarchical
    var inputs = container.getElementsByTagName('INPUT');
    var totalChecked = 0;
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == 'checkbox' && 
            // only chkOne*, igonore chkAll*
            inputs[i].id.indexOf('chkOne') >= 0 &&
            inputs[i].checked)
        {
            totalChecked++;
        }
    }
    return totalChecked;
}

function countCheckedBoxesInTableBody(tableBody) {
    // count checked boxes in current table, not hierarchical
    var totalChecked = 0;

    // ignore 1st row TH: i starts from 1 instead of 0
    for (var i = 1; i < tableBody.childNodes.length; i++) { 
        // only inspect the 1st checkbox of current row
        // could have other checkboxes in column values
        var chkOne = tableBody.childNodes[i].getElementsByTagName('INPUT')[0];

        // skip table footer, who does not have a checkbox
        if (chkOne && chkOne.checked)   
            totalChecked++;
    }
    return totalChecked;
}

function getCheckBoxsParentTableBody(checkBox) {
    return getTableRowOfCheckBox(checkBox).parentElement;
}

function getTableRowOfCheckBox(checkBox) {
    var tableRow;

    // check if checkbox has attribute
    if (checkBox.parentElement.tagName == 'SPAN')       // has attribute
        // CheckBox->Span->TD->TR
        tableRow = checkBox.parentElement.parentElement.parentElement;
    else
        // CheckBox->TD->TR
        tableRow = checkBox.parentElement.parentElement;

    return tableRow;
}

function hasAnyBoxChecked(containerId) {    // any container, flat and hierarchical
    var container = document.getElementById(getClientId(containerId));
    return hasAnyBoxCheckedInContainer(container);
}

function hasAnyBoxCheckedInContainer(container) {
    // any container, not limit to table/TBody
    // could be hierarchical
    var inputs = container.getElementsByTagName('INPUT');

    for (var i = 0; i < inputs.length; i++)
        if (inputs[i].type == 'checkbox' && 
            // chkAll* or chkOne* only
            (inputs[i].id.indexOf('chkAll') >= 0 || inputs[i].id.indexOf('chkOne') >= 0) &&
            inputs[i].checked
            )
            return true;

    return false;
}

function hasAnyRowSelected(container) {     //table body only, not hierarchical
    // any container, not limit to table/TBody
    // could be hierarchical
    var inputs = container.getElementsByTagName('INPUT');

    for (var i = 0; i < inputs.length; i++)
        if (inputs[i].type == 'checkbox' && 
            // chkOne* only
            inputs[i].id.indexOf('chkOne') >= 0 &&
            inputs[i].checked
            )
            return true;

    return false;
}

function _hasAnyRowSelected(tableBody) {     //table body only, not hierarchical
    // check if any checkbox checked in current TBody

    // start from table head: if the checkbox there is checked, then all checkboxes are checked
    for (var i = 0; i < tableBody.childNodes.length; i++) { 
        // only inspect the 1st checkbox of current row
        // could have other checkboxes in column values
        var chkOne = tableBody.childNodes[i].getElementsByTagName('INPUT')[0];

        // skip table footer, which does not have a checkbox
        if (chkOne && chkOne.checked)   
            return true;
    }
    
    return false;
}

function highlightRow(checkBox) {
    //highlight/unhighlight a row the checkbox resides in
    var tableRow = getTableRowOfCheckBox(checkBox);
    var currentBColor = tableRow.style.backgroundColor;

    // do not change backgroud color of exisitng/previously selected item
    // only change newly selected item 
    if (currentBColor != '#d8f0ff')
        tableRow.style.backgroundColor = checkBox.checked ? 'LightCyan' : 'White';
}

