// Title: tigra tables
// Description: See the demo at url
// URL: http://www.softcomplex.com/products/tigra_tables/
// Version: 1.0
// Date: 03-04-2002 (mm-dd-yyyy)
// Author: Denis Gritcyuk <denis@softcomplex.com>
// Notes: Permission given to use this script in any kind of applications if
//    header lines are left unchanged. Feel free to contact the author
//    for feature requests and/or donations

// Modified By:
// Name:	Alex Chau
// On:		03-04-2003
// Description:	1.) removed the onClick row color change effect
//		2.) Added new effect so that a new color (selected_color) can be assign on selected row
// Usage:	From html page, 
//			1.) Set table ID such as <table ID="demo1_table">
//			2.) Set the selected row ID if any such as <tr ID='selectedrow'>
//			3.) After table tag, add JavaScript:
//				tigra_tables(str_tableid, num_header_offset, num_footer_offset,
//					str_odd_color, str_even_color, str_mover_color, 
//					str_selected_color, str_selectedID);
//				e.g: tigra_tables('demo1_table', 0, 0, '#ffffff',
//					'#ccccff', '#ffccff', '#ffff80', 'selectedrow');

function tigra_tables (
		str_tableid, // table id (req.)
		num_header_offset, // how many rows to skip before applying effects at the begining (opt.)
		num_footer_offset, // how many rows to skip at the bottom of the table (opt.)
		str_odd_color, // background color for odd rows (opt.)
		str_even_color, // background color for even rows (opt.)
		str_mover_color, // background color for rows with mouse over (opt.)
		str_selected_color, // background color for selected rows (opt.)
		str_selectedID //ID for the selected rows (opt.)
	) {

	 // skip non DOM browsers
	if (typeof(document.all) != 'object') return;

	// validate required parameters
	if (!str_tableid) return alert ("No table(s) ID specified in parameters");
	var obj_tables = (document.all ? document.all[str_tableid] : document.getElementById(str_tableid));
	if (!obj_tables)
		return;
		//return alert ("Can't find table(s) with specified ID (" + str_tableid + ")");

	// set defaults for optional parameters
	var col_config = [];
	col_config.header_offset = (num_header_offset ? num_header_offset : 0);
	col_config.footer_offset = (num_footer_offset ? num_footer_offset : 0);
	col_config.odd_color = (str_odd_color ? str_odd_color : '#ffffff');
	col_config.even_color = (str_even_color ? str_even_color : '#dbeaf5');
	col_config.mover_color = (str_mover_color ? str_mover_color : '#6699cc');
	col_config.selected_color = (str_selected_color ? str_selected_color : '#ffff80');
	
	// init multiple tables with same ID
	if (obj_tables.length)
		for (var i = 0; i < obj_tables.length; i++)
			tt_init_table(obj_tables[i], col_config, str_selectedID);
	// init single table
	else
		tt_init_table(obj_tables, col_config, str_selectedID);
}

function tt_init_table (obj_table, col_config, str_selectedID) {
	//Check if any row(s) is selcted, set the selected row color to this particular rows
	var obj_selectedRow = (document.all ? document.all[str_selectedID] : document.getElementById(str_selectedID));
	if(str_selectedID!=null && obj_selectedRow!=null) {
		// handle multiple selected rows with the same ID
		if(obj_selectedRow.length)
			for (var i = 0; i < obj_selectedRow.length; i++)
				obj_selectedRow[i].style.backgroundColor = col_config.selected_color;
		else
		// handle single selected row
			obj_selectedRow.style.backgroundColor = col_config.selected_color;
	}
	
	var col_lconfig = [],
		col_trs = obj_table.rows;
	for (var i = col_config.header_offset; i < col_trs.length - col_config.footer_offset; i++) {
		//set the selected flag
		col_trs[i].selectedFlag = "NO";
		if(str_selectedID!=null && col_trs[i].style.backgroundColor == col_config.selected_color)
			col_trs[i].selectedFlag = "YES";
		col_trs[i].config = col_config;
		col_trs[i].lconfig = col_lconfig;
		col_trs[i].set_color = tt_set_color;
		col_trs[i].onmouseover = tt_mover; 
		col_trs[i].onmouseout = tt_mout;
		col_trs[i].order = (i - col_config.header_offset) % 2;
		col_trs[i].onmouseout();
	}

}
function tt_set_color(str_color) {
	this.style.backgroundColor = str_color;
}

// event handlers
function tt_mover () {
	this.set_color(this.config.mover_color);
}
function tt_mout () {
	if(this.selectedFlag == "YES")
		this.set_color(this.config.selected_color);
	else
		this.set_color(this.order ? this.config.odd_color : this.config.even_color);
}

