/*
 * $Id: ?.java 775 2006-08-31 16:37:42Z vitor.carreira $
 *
 * (C) 2006 - MetaVerse Solutions
 * Copyright 2006 MetaVerse Solutions, Inc. All rights reserved.
 * METAVERSE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Function that selects all or none checkboxes on a table with a given column.
 */ 
function selectAll( /*String*/tableId, /*int*/columnIndex ) {
	var select = false;
	var table = document.getElementById( tableId );
	if ( !table ) {
		return;
	}
	var row;
	var cell;
	var input;
	
	var checks = new Array();
	
	for ( var i=1; i < table.rows.length; i++ ) {
		cell = table.rows[i].cells[ columnIndex ];
		input = cell.getElementsByTagName('input')[0];
		if ( input.type == "checkbox" ) {
			if ( !input.checked ) {
				select = true;
				checks.push( input );
			}
		}
	}
	
	if ( select ) {
		// go for selection
		for ( var i=0; i < checks.length; i++ ) {
			checks[i].checked = true;
		}
	} else {
		//all are selected, unselect
		//skip header start on row = 1
		for ( var i=1; i < table.rows.length; i++ ) {
			cell = table.rows[i].cells[ columnIndex ];
			input = cell.getElementsByTagName('input')[0];
			if ( input.type == "checkbox" ) {
				input.checked = false;
			}
		}			
	}
}

/**
 * Function that tells if table has any checkbox selected in a given column.
 */ 
function hasSelection( /*String*/tableId, /*int*/columnIndex ) {
	var table = document.getElementById( tableId );
	if ( !table ) {
		return;
	}
	var cell;
	var input;
	for ( var i=1; i < table.rows.length; i++ ) {
		cell = table.rows[i].cells[ columnIndex ];
		input = cell.getElementsByTagName('input')[0];
		if ( input.type == "checkbox" ) {
			if ( input.checked ) {
				return true;
			}
		}
	}
	return false;
}
