﻿///Finds the index of the header column containing the specified text
function getColumnIndex(table, text){
var colIndex;
var headers = table.getElementsByTagName("TH");
for(i = 0; i < headers.length; i++){     
     if(headers[i].innerHTML.toUpperCase().indexOf(text.toUpperCase()) > -1){ 
        colIndex = i;
        break;
     } 
}
return colIndex;
}
   
///Applies a cssClass to all cells in the specified table column
function applyCssClassToColumn(table, colNum, cssClassName){
    var currentCell;
    //Iterate through each cell in the table
    for (rowIndex = 0; rowIndex < table.rows.length; rowIndex++)
    {
        for(colIndex = 0; colIndex < table.rows[rowIndex].cells.length; colIndex++){
            currentCell = table.rows[rowIndex].cells[colIndex];
            //If we're in the specified row, apply the highlight css class, 
            //otherwise clear the css class (This allows it to be call repeatedly)
            if(colIndex == colNum){
                currentCell.className = cssClassName;
            }
            else{
                currentCell.className = '';
            }
        }
        
    }
}

///Finds the index of the table header column containing the specified text, and
///applies a cssClass to all cells in the specified column
function getAndHighlightColumn(tableName, text, cssClassName){
if(document.getElementById){
    var table = document.getElementById(tableName);
    if(table){
        var colIndex = getColumnIndex(table, text); 
        if(colIndex){
            applyCssClassToColumn(table, colIndex, cssClassName);   
        }
    }
}
}
