// QuickSearch script for tables
// written by Mark Schenk, 2006
// 
// script based on http://virtuelvis.com/scripts/filterlist2.js
// rewritten to include IE and to add extras

if (window.addEventListener) {
	window.addEventListener("load",initSearch,false) }
else if (window.attachEvent) {
	window.attachEvent("onload", initSearch); }

function initSearch() {
	// basic object detection
	if(!document.getElementById || !document.getElementsByTagName) { return; }

	// check if QuickSearch table AND search area is present 
	if (!document.getElementById('qstable')||!document.getElementById('qs')) { return; }

	// give id of the table that has QuickSearch
	// is global variable on purpose
	searchTable = document.getElementById('qstable');

	document.getElementById('qs').style.display = 'block';
	document.getElementById('qsfield').onkeyup = testEvent;
}

function quickSearch(tInput){
	searchText = new RegExp(tInput.value,"i");
	var inRows = searchTable.getElementsByTagName('tbody')[0].getElementsByTagName('tr');

	// set number of searchable columns
	var cols;
	var colsdef  = 2;
	var colsinput = document.getElementById('searchcols');

	if (colsinput) {
		if (colsinput.type =='checkbox') {
			colsinput.checked == true ? cols = colsinput.value : cols = colsdef;
		} else {
			cols = colsinput.value;
		}
		
	}

	// start looping through all rows
	for (var i = 0; cRow = inRows[i]; i++){
	inCells = cRow.getElementsByTagName('td');
	var gevonden = false; 
  
	for (var j=0; j<cols; j++) { // only first two columns
		cCell = inCells[j];
		var t = cCell.innerText?cCell.innerText:getTextContent(cCell);
		if ((tInput.value.length == 0) || (t.search(searchText) != -1)){ gevonden=true; } 
	}

	// next line does not work in IE
	gevonden == true?cRow.className = 'show':cRow.className = 'noshow';
	}

}

function getTextContent(node) {
	if (node.nodeType == 3) {
	return node.nodeValue;
	} // text node
	if (node.nodeType == 1) { // element node
	var text = [];
	for (var chld = node.firstChild;chld;chld=chld.nextSibling) {
		text.push(getTextContent(chld));
	}
	return text.join("");
	} return ""; // some other node, won't contain text nodes.
}

function testEvent(e){
	if (!e) var e = window.event;
	quickSearch(this);
}

function clearQS() {
	qsfield = document.getElementById('qsfield'); 
	qsfield.value = '';
	quickSearch(qsfield);
}

function redoQS(){
	qsfield = document.getElementById('qsfield'); 
	quickSearch(qsfield);
}