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

	/*Predefined claas name and colors*/
	var className = "highlightTable";
	var rowHighLight = "#e6e6e6";
	var pale = "#fbefcf";
	var dark = "#f9e3b6";
	var grey = "#f2f3f5";
	var colHighLight = "";
	//var rowHighLight = "#F0EDFF";
	//var colHighLight = "#F8F5F6";
	var defaultReset = "#ffffff";
	var colHighLightReset = "";
	
	var defaultResets = new Array();
	/*Predefined claas name and colors*/

	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)];
			if( table.className.search("grey") != -1 )
				defaultResets[i] = grey;
			else
				defaultResets[i] = defaultReset;
			var row_count = table.rows.length;
			for ( var j=0; j<row_count; j++ ) {
				var row = table.rows[j];
				
				if (row.className == "no-highlighting")
					{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 ( var k=0; k<row.cells.length; k++ ) {
					var 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 = "";
							if ( cell.getAttribute("colspan") == null )
								colspaner = 1;
							else
								colspaner = cell.getAttribute("colspan");

							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+"_";
		else
			table = '';

		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+"_";
		else
			table = '';

		temp_row = document.getElementById(table+"row_"+row_id+"_");
		var classname;
		var classes = new Array();
		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 )
				{
					if( temp_row.cells[z].className.search("pale") != -1 )
						rowHighLightReset = pale;
					else if( temp_row.cells[z].className.search("dark") != -1 )
						rowHighLightReset = dark;
					else
						rowHighLightReset = defaultResets[table_id - 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;
		}

	}
	
	function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
	function toHex(N) {
	 if (N==null) return "00";
	 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
	 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
	 return "0123456789ABCDEF".charAt((N-N%16)/16)
		  + "0123456789ABCDEF".charAt(N%16);
	}
	
	function trim(str, chars) {
		return ltrim(rtrim(str, chars), chars);
	}
	
	function ltrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	}
	
	function rtrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}