/**
 * 
 * Author:    Aaron Drake <adrake@wustl.edu>
 * Copyright: 2006 Washington University in St. Louis
 * License:   
 */



var numOfCheckedRows = 0;



/**
 * Toggle the background color of a table row based on the status of its corresponding checkbox.
 * param string tableId  ID of the table containing the checkbox to toggle
 * param string checkboxId  ID of the checkbox to toggle
 * return void
 */
function toggleTableCheckbox(tableId, checkboxId)
{
	checkbox = document.getElementById(checkboxId);
	if (checkbox.checked == true) {
		checkbox.parentNode.parentNode.style['backgroundColor'] = '#e5decc';
		numOfCheckedRows++;
	}
	else {
		checkbox.parentNode.parentNode.style['backgroundColor'] = '';
		numOfCheckedRows--;
	}

	// check or uncheck the 'check all' checkbox as appropriate
	var table = document.getElementById(tableId);
	if (numOfCheckedRows == (table.getElementsByTagName('input').length - 1)) {
		table.getElementsByTagName('input')[0].checked = true;
	}
	else {
		table.getElementsByTagName('input')[0].checked = false;
	}
}



/**
 * Check/Uncheck all table rows & set their background colors.
 * param string tableId  ID of the table containing the checkboxes
 * param string checkAllId  ID of the checkbox for checking/unchecking all other checkboxes
 * return void
 */
function toggleAllTableCheckboxes(tableId, checkAllId)
{
	var childNodes = document.getElementById(tableId).getElementsByTagName('input');

	if (document.getElementById(checkAllId).checked == true) {
		for (var i = 1; i < childNodes.length; i++) {
			childNodes.item(i).checked = true;
			childNodes.item(i).parentNode.parentNode.style['backgroundColor'] = '#e5decc';
		}
		numOfCheckedRows = childNodes.length - 1;
	}
	else {
		for (var i = 1; i < childNodes.length; i++) {
			childNodes.item(i).checked = false;
			childNodes.item(i).parentNode.parentNode.style['backgroundColor'] = '';
		}
		numOfCheckedRows = 0;
	}
}



/**
 * Toggle the 'check all' color of a table row based on the status of its corresponding checkbox.
 * param string elementId  ID of the HTML element containing the checkbox to toggle
 * param string checkboxId  ID of the checkbox to toggle
 * return void
 */
function toggleCheckbox(elementId, checkboxId)
{
	checkbox = document.getElementById(checkboxId);
	if (checkbox.checked == true) {
		numOfCheckedRows++;
	}
	else {
		numOfCheckedRows--;
	}

	// check or uncheck the 'check all' checkbox as appropriate
	var element = document.getElementById(elementId);
	if (numOfCheckedRows == (element.getElementsByTagName('input').length - 1)) {
		element.getElementsByTagName('input')[0].checked = true;
	}
	else {
		element.getElementsByTagName('input')[0].checked = false;
	}
}



/**
 * Check/Uncheck all checkboxes in an HTML element.
 * param string elementId  ID of the HTML element containing the checkboxes
 * param string checkAllId  ID of the checkbox for checking/unchecking all other checkboxes
 * return void
 */
function toggleAllCheckboxes(elementId, checkAllId)
{
	var childNodes = document.getElementById(elementId).getElementsByTagName('input');

	if (document.getElementById(checkAllId).checked == true) {
		for (var i = 1; i < childNodes.length; i++) {
			childNodes.item(i).checked = true;
		}
		numOfCheckedRows = childNodes.length - 1;
	}
	else {
		for (var i = 1; i < childNodes.length; i++) {
			childNodes.item(i).checked = false;
		}
		numOfCheckedRows = 0;
	}
}