var tbl;        //root of the JavaScript object
var tbl_dom;    //pointer to the HTML table
var txt;
var MyTimer;          //timer

addEventSimple(window, "load", page_load);

function page_load() {
	removeEventSimple(window, "load", page_load);
	tbl = new table();
	tbl_dom = document.getElementById("tbl");
	txt = document.getElementById("txtSearch");
	load_data();
	tbl.render();
	addEventSimple(txt, "keyup", search); 
}

function table() {
	this.header;
	this.rows = new Array();
	this.last;
	this.add = function() {
		this.last = new row();
		if (this.header) {
			this.rows[this.rows.length] = this.last;
		} else {
			this.header = this.last;
		}
	}
	this.render = function() {
		var thead = document.createElement("thead");
		var h = this.header.render();
		h.className = "header";
		thead.appendChild(h);
		tbl_dom.appendChild(thead);
		var tbody = document.createElement("tbody");
		for (var i=0; i<this.rows.length; i++) {
			var r = this.rows[i].render();
			if (i % 2 != 0) r.className = "alt";
			tbody.appendChild(r);
		}
		tbl_dom.appendChild(tbody);
	}
	this.search = function(value) {
		for (var i=0; i<this.rows.length; i++) {
			this.rows[i].search(value);
		}
	}
}

function row() {
	this.data = new Array();
	this.row_dom;
	this.add = function(value) {
		this.data[this.data.length] = value;
	}
	this.render = function() {
		this.row_dom = document.createElement("tr");
		for (var i=0; i<this.data.length; i++) {
			var c = document.createElement("td");
			c.innerHTML = this.data[i];
			this.row_dom.appendChild(c);
		}
		return this.row_dom;
	}
	this.search = function(value) {
		if (value.length == 0) {
			this.row_dom.style.display = "";
			return;
		}
		//alert(value);
		var val = new Array()
		for (var i=0; i<value.length; i++) {
			if (value[i] != "") { val[val.length] = value[i]; }
		}
		for (var i=0; i<this.data.length; i++) {
			for (var j=0; j<val.length; j++) {
			    //if (this.data[i].search(new RegExp(val[j]), "i") != -1) {
				if (this.data[i].toLowerCase().indexOf(val[j].toLowerCase()) != -1) {
					val[j] = "";
				}
			}
		}
		for (var j=0; j<val.length; j++) {
		    if (val[j] != "") {
		        this.row_dom.style.display = "none";
		        return;
		    }
		}
		this.row_dom.style.display = "";
	}
	return this;
}

function search() {
	if (!MyTimer) {
	    MyTimer = window.setTimeout(function() { search2() }, 1000);
	}
}

function search2() {
    tbl.search(txt.value.split(" "));
    window.clearTimeout(MyTimer);
    MyTimer = null;
}

//The following two functions are from quirksmode.org, the best JavaScript event site ever
//http://www.quirksmode.org/js/eventSimple.html
function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

function removeEventSimple(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}