// This function searches for a table header and returns its zero-based index in the surrounding table.
function getColumnIndex(table, tableHeader){
    columnIndex = 0;
	for(i=0; i<table.rows [0].cells.length; i++){
		if (table.rows [0].cells [i] == tableHeader){
			columnIndex = i;
		}
	}

    return columnIndex;
}

// This function provides the innerText attribute to Firefox/Mozilla. The innerText attribute is normally only available to Internet Explorer.
function emulateInnerText(htmlText){
    if (htmlText != null){
        return htmlText.replace(/<[^>]+>/g,"");
    }

    return htmlText;
}

// This function checks the content of all HTML column nodes with the same index as a given table header node, for a given pattern.
// When the pattern doesn't match, the associated row will be made invisible.
function doFilter(pattern, tableHeader){
	table = tableHeader.parentNode.parentNode.parentNode;
    columnIndex = getColumnIndex(table, tableHeader);

	numberOfRows = table.rows.length;
	for (i=1; i<numberOfRows; i++){
        if (table.rows [i].cells [columnIndex] != null){
            if ( emulateInnerText(table.rows [i].cells [columnIndex].innerHTML.toLowerCase()).indexOf(pattern.toLowerCase())!=-1){
                table.rows[i].style.display = '';
            }
            else{
                table.rows[i].style.display = 'none';
            }
        }
	}
}

// This function makes table columns invisible when 'show' is set to false, or lets them reappear when 'show' is set to true.
function doShowHide(tableHeader, show){
	table = tableHeader.parentNode.parentNode.parentNode;
    columnIndex = getColumnIndex(table, tableHeader);

    if (show){
        tableHeader.style.display = '';
    }
    else{
        tableHeader.style.display = 'none';
    }

	numberOfRows = table.rows.length;
	for (i=1; i<numberOfRows; i++){
        if (table.rows [i].cells [columnIndex] != null){
            if (show){
                table.rows [i].cells [columnIndex].style.display = '';
            }
            else{
                table.rows [i].cells [columnIndex].style.display = 'none';
            }
        }
	}
}

// This function is the JavaScript version of the Java String.endsWith() method.
function endsWith(pattern, string){
    return string.substr(string.length - pattern.length) == pattern;
}

// This function looks for all input elements on this page. It then checks if these input elements control the visibility of a table column.
// When it does, the specific column is shown or hidden.
function showHideAllColumns(){
	inputTags = document.getElementsByTagName('INPUT');
	for ( var i=0; i<inputTags.length; i++ ){
	    if ( endsWith('ShowHideCheckbox',  inputTags.item(i).name) && inputTags.item(i).value != 'true'){
	        columnId = inputTags.item(i).name.substr(0, inputTags.item(i).name.length - 'ShowHideCheckbox'.length);
	        doShowHide( document.getElementById( columnId ), false );
	    }
	}
}