// Author: Mark Fierling (mark[at]wmakers[dot]com)
// Date: Aug 23, 2004
// License: free to use as long as you leave all comments in tact
// Compatibility: only tested on IE6
// future: Make Mozilla compatible
// this script requires the placeTFInputFields function to be called after the table to be sorted is rendered
// ie. < script type="text/javascript">if (document.getElementById("filterDataTable")) placeTFInputFields("filterDataTable");< /script>

// places filter input fields into a new row at the end of the table header
function placeTFInputFields(tableId) {
	var obj_table = document.getElementById(tableId);
	var rows = obj_table.tHead.rows;
	// add a new cell
	var newRow = obj_table.tHead.appendChild(rows[(rows.length-1)].cloneNode(true));
	var newRowCells = newRow.cells;
	// add a filter input text field into each cell
	for (var i=0,n=newRowCells.length;i<n;i++) newRowCells[i].innerHTML = '<input type="Text" style="width:100%" onkeyup="TFcolor(this);updateTFParams(\'' + tableId + '\',' + i + ',this.value);">';
	//alert(newRow.cells.length);
	//alert(rows.length);
}

// colors filter input fields, depending on valid input
function TFcolor(Input) {
	Input.style.background = (Input.value == "") ? "#ffffff" : "#a8ffae";
}

/* Table Filter functionality description
	1) all filter information is stored inside two arrays (arr_filter=[0,1,4] and arr_filter_strings=["foo","7","hello"])
	2) the arrays get populated and updated by information found inside the first row's cells TF_active ("0" or "1") and TF_string (ie. "foo") attributes.
	3) The function updateTFParams is called to change any parameters.
	4) the function doTF does the filtering by first showing all rows and then looping over the filter arrays, hiding rows when they contain a match.
*/
// define filter arrays
arr_filter = new Array();
arr_filter_strings = new Array();
// updates Table Filter parameters
function updateTFParams(tableId,column,str) {
	var obj_table = document.getElementById(tableId);
	var rows = obj_table.rows;
	var firstRow = rows.item(0);
	var firstRowCells = firstRow.cells;
	var Active = (str=="")?"0":"1";
	//alert(Active);
	var paramsChanged = false;
	// initialize Filter Parameters if not already initialized 
	if (!firstRow.getAttribute("TF_initialized")) {
		initTFParams(firstRow);
		// Read Table Filter Parameters and store them in arrays (arr_filter & arr_filter_strings)
		updateTFArrays(firstRowCells);
	}
	var cell = firstRowCells.item(column);
	if (cell.getAttribute("TF_string")!=str) {
		cell.setAttribute("TF_string",str);
		paramsChanged = true;
	}
	if (cell.getAttribute("TF_active")!=Active) {
		cell.setAttribute("TF_active",Active);
		paramsChanged = true;
	}
	// execute Table Filter, if any parameters have changed
	if (paramsChanged) doTF(tableId);
}
// reads Table Filter parameters and stores them in two arrays
function updateTFArrays(firstRowCells) {
	// read parameters and build filter array
	// example:	arr_filter			=	[0,1,4]					- column 1,2 and 5 need to be filtered by (have filter parameters)
	//			arr_filter_strings	=	["foo","7","hello"]		- strings to be filtered for in column 1,2 and 5 respectively
	var counter = 0;
	// clear arrays
	arr_filter = [];
	arr_filter_strings = [];
	// populate arrays
	for (var i=0,n=firstRowCells.length;i<n;i++) {
		var cell = firstRowCells.item(i);
		// if parameter valid, add it to the filter array
		if (cell.getAttribute("TF_active") == "1" && cell.getAttribute("TF_string") != "") {
			arr_filter[counter] = i;
			arr_filter_strings[counter] = cell.getAttribute("TF_string");
			counter++;
		}
	}
}
// initializes the Table filter parameters
function initTFParams(firstRow) {
	var firstRowCells = firstRow.cells;
	//initialize table filter parameters (located in the cells of the row)
	for (var i=0,n=firstRowCells.length;i<n;i++) {
		cell = firstRowCells.item(i);
		if (!cell.getAttribute("TF_string")) cell.setAttribute("TF_string","");
		if (cell.getAttribute("TF_string")=="") cell.setAttribute("TF_active","0");
		else cell.setAttribute("TF_active","1");
	}
	firstRow.setAttribute("TF_initialized","1");
}
// Show all rows
function TFShowAllRows(tableId) {
	var obj_table = document.getElementById(tableId);
	var rows = obj_table.rows;
	// loop through all rows and make them visible
	for (var i=0,n=rows.length;i<n;i++) {
		var row = rows.item(i);
		row.style.display = "";
	}
}
// execute the Table Filter
function doTF(tableId,column,text) {
	var obj_table = document.getElementById(tableId);
	var rows = obj_table.rows;
	var firstRow = rows.item(0);
	var firstRowCells = firstRow.cells;
	rows = obj_table.tBodies[0].rows;
	
	// initialize Table Filter Parameters, if not already initialized
	if (!firstRow.getAttribute("TF_initialized")) initTFParams(firstRow);

	// Read Table Filter Parameters and store them in arrays (arr_filter & arr_filter_strings)
	updateTFArrays(firstRowCells);
	
	// show all rows
	TFShowAllRows(tableId);

	// if the filter is empty (no parameters set), exit
	if (!arr_filter.length) return;

	// loop through the filter array
	for (var i=0,n=arr_filter.length;i<n;i++) {
		// set searchString to current column search parameter
		var textRE = new RegExp(arr_filter_strings[i],"i");
		//alert("i = "+i+"\nn = "+n+"\ni<n = "+(i<n)+"\narr_filter[i] = "+arr_filter[i]+"\nsearching for :'"+textRE+"'");
		// loop through all rows
		for (var j=0,m=rows.length;j<m;j++) {
			var row = rows.item(j);
			// search cell only if the current row isn't already hidden
			if (row.style.display != "none") {
				var cell = row.cells[arr_filter[i]];
				var cellText = cell.innerText;
				row.style.display = ((textRE.test(cellText)) ? "" : "none");
			}
		}
	}
}

