//This JS is for table highlighting.  Just add "highlightTable" as a table class, and this will do the rest!

//Predefined class name and colors
var className = 'highlightTable';
var rowHighLight = '#f2f2f2';
var colHighLight = '#EBEAE6';
// different for reports section
if (typeof isReport != 'undefined') {
	rowHighLight = '#EFEFEF';
	colHighLight = '#DCF4FB';
}
var rowHighLightReset = '#ffffff';
var colHighLightReset = '#ffffff';
//for zuBox only
function highlightZuBoxTables(tables) {
	var table_length = tables.length;
	for (var i=0; i<table_length; i++) {
		highZuBoxWork(tables[i], i+99, table_length);
	}
}
function highZuBoxWork(table, tnum, table_length) {
	var row_count = table.rows.length;
	for (var j=0; j<row_count; j++) {
		var row = table.rows[j];
		if (row.className.indexOf("no-highlighting") > 0) {
			continue
		}
		if (row.id == "no-highlighting-new-table") {
			i++;
			table_length = table_length + 1;
			continue
		}
		i = tnum;
		row.id = "tb_"+(i+1)+"_row_"+(j+1)+"_";
		var col_count = row.cells.length;
		for (var k=0; k<row.cells.length; k++) {
			var cell = 	row.cells[k];
			if (cell.getAttribute("colspan") != null) {
				col_count = col_count + (cell.getAttribute("colspan") - 1);
			}
		}
		var col_offset = 0;
		for (k=0; k<row.cells.length; k++) {
			cell = row.cells[k];
			if (cell.getAttribute("scope") == 2) {
				cell.id = "tb_"+(i+1)+"_row_"+(j+1)+"_col_"+((k+1)+col_offset)+"_ tb_"+(i+1)+"_row_"+(j+1)+"_col_"+(((k+1)+col_offset)+1)+"_";
				cell.onmouseover = new Function("highlightMore('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"','2');return false");
				cell.onmouseout = new Function("highlightMoreReset('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"','2');return false");
			}
			else {
				if (cell.getAttribute("colspan") != null) {
					var ids = '';
					colspaner = cell.getAttribute("colspan");
					if (cell.getAttribute("colspan") == null) {
						colspaner = 1;
					}
					for (z=0; z<colspaner; z++) {
						ids = ids + " tb_"+(i+1)+"_row_"+(j+1)+"_col_"+(((k+1)+col_offset)+z)+"_";
					}
					cell.id = ids;
					cell.onmouseover = new Function("highlightMore('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"','"+colspaner+"');return false");
					cell.onmouseout = new Function("highlightMoreReset('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"','"+colspaner+"');return false");
				}
				else {
					cell.id = "tb_"+(i+1)+"_row_"+(j+1)+"_col_"+((k+1)+col_offset)+"_";
					cell.onmouseover = new Function("highlight('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"');return false");
					cell.onmouseout = new Function("highlightReset('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"');return false");
				}
			}
			if ( cell.getAttribute("colspan") != null ) {
				col_offset = col_offset + (cell.getAttribute("colspan") - 1);
			}
		}
	}
}
function highlightTables() {
	if (!document.getElementsByTagName)
		return;
	var tables = getElementsByClass(className, document, "table")
	var table_length = tables.length;
	for (var i=0; i<table_length; i++) {
		var table = tables[i-(table_length-tables.length)];
		var row_count = table.rows.length;
		for (var j=0; j<row_count; j++) {
			var row = table.rows[j];
			if (row.className.indexOf("no-highlighting") >= 0) {
				continue
			}
			if (row.id == "no-highlighting-new-table") {
				i++;
				table_length = table_length + 1;
				continue
			}
			row.id = "tb_"+(i+1)+"_row_"+(j+1)+"_";
			var col_count = row.cells.length;
			for (var k=0; k<row.cells.length; k++) {
				var cell = 	row.cells[k];
				if (cell.getAttribute("colspan") != null) {
					col_count = col_count + (cell.getAttribute("colspan") - 1);
				}
			}
			var col_offset = 0;
			for (k=0; k<row.cells.length; k++) {
				cell = row.cells[k];
				if (cell.getAttribute("scope") == 2) {
					cell.id = "tb_"+(i+1)+"_row_"+(j+1)+"_col_"+((k+1)+col_offset)+"_ tb_"+(i+1)+"_row_"+(j+1)+"_col_"+(((k+1)+col_offset)+1)+"_";
					cell.onmouseover = new Function("highlightMore('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"','2');return false");
					cell.onmouseout = new Function("highlightMoreReset('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"','2');return false");
				}
				else {
					if (cell.getAttribute("colspan") != null) {
						var ids = '';
						colspaner = cell.getAttribute("colspan");
						if (cell.getAttribute("colspan") == null) {
							colspaner = 1;
						}
						for (z=0; z<colspaner; z++) {
							ids = ids + " tb_"+(i+1)+"_row_"+(j+1)+"_col_"+(((k+1)+col_offset)+z)+"_";
						}
						cell.id = ids;
						cell.onmouseover = new Function("highlightMore('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"','"+colspaner+"');return false");
						cell.onmouseout = new Function("highlightMoreReset('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"','"+colspaner+"');return false");
					}
					else {
						cell.id = "tb_"+(i+1)+"_row_"+(j+1)+"_col_"+((k+1)+col_offset)+"_";
						cell.onmouseover = new Function("highlight('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"');return false");
						cell.onmouseout = new Function("highlightReset('"+(j+1)+"','"+((k+1)+col_offset)+"','"+(i+1)+"','"+(row_count)+"','"+(col_count)+"');return false");
					}
				}
				if (cell.getAttribute("colspan") != null) {
					col_offset = col_offset + (cell.getAttribute("colspan") - 1);
				}
			}
		}
	}
}
function highlightMore(row_id, column_id, table_id, number_of_rows, number_of_columns, cols, rows) {
	for (x=0; x<parseInt(cols); x++) {
		c_id = parseInt(column_id) + x;
		highlight(row_id, c_id, table_id, number_of_rows, number_of_columns);
	}
}
function highlightMoreReset(row_id, column_id, table_id, number_of_rows, number_of_columns, cols, rows) {
	for (x=0; x<parseInt(cols); x++) {
		c_id = parseInt(column_id) + x;
		highlightReset(row_id, c_id, table_id, number_of_rows, number_of_columns);
	}
}
function highlight(row_id, column_id, table_id, number_of_rows, number_of_columns) {
	var table = '';
	if (table_id) {
		table = "tb_"+table_id+"_";
	}
	temp_row = document.getElementById(table+"row_"+row_id+"_");
	for (i=1; i<=number_of_columns; i++) {
		for (z=0; z<temp_row.cells.length; z++) {
			if (temp_row.cells[z].id.search(table+"row_"+row_id+"_col_"+i+"_") != -1) {
				temp_row.cells[z].style.backgroundColor = rowHighLight;
			}
		}
	}
	for (i=1; i<=number_of_rows; i++) {
		if (i != row_id) {
			temp_row = document.getElementById(table+"row_"+i+"_");
			if (temp_row != null) {
				for (z=0; z<temp_row.cells.length; z++) {
					if (temp_row.cells[z].id.search(table+"row_"+i+"_col_"+column_id+"_") != -1) {
						temp_row.cells[z].style.backgroundColor = colHighLight;
					}
				}
			}
		}
	}
}
function highlightReset(row_id, column_id, table_id, number_of_rows, number_of_columns) {
	var table = '';
	if (table_id) {
		table = "tb_"+table_id+"_";
	}
	temp_row = document.getElementById(table+"row_"+row_id+"_");
	for (i=1; i<=number_of_columns; i++) {
		for (z=0; z<temp_row.cells.length; z++) {
			if (temp_row.cells[z].id.search(table+"row_"+row_id+"_col_"+i+"_") != -1) {
				temp_row.cells[z].style.backgroundColor = rowHighLightReset;
			}
		}
	}
	for (i=1; i<=number_of_rows; i++) {
		if (i != row_id) {
			temp_row = document.getElementById(table+"row_"+i+"_");
			if (temp_row != null) {
				for (z=0; z<temp_row.cells.length; z++) {
					if (temp_row.cells[z].id.search(table+"row_"+i+"_col_"+column_id+"_") != -1) {
						temp_row.cells[z].style.backgroundColor = colHighLightReset;
					}
				}
			}
		}
	}
}
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i=0, j=0; i<elsLen; i++) {
		if (pattern.test(els[i].className)) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
function setRowHighlightColor(color) {
	rowHighLight = color;
}
function setColHighlightColor(color) {
	colHighLight = color;
}
function highlightCell(tableNumber, rowNumber, columnNumber, type) {
	cell = document.getElementById("tb_" + tableNumber + "_row_" + rowNumber + "_col_" + columnNumber + "_");
	if (cell) {
		if (type == "column") {
			colour = colHighLight;
		}
		else if (type == "reset_row") {
			colour = rowHighLightReset;
		}
		else if (type == "reset_column") {
			colour = colHighLightReset;
		}
		else {
			colour = rowHighLight;
		}
		cell.style.backgroundColor = colour;
	}
}
highlightTables();
