function addEvent(elm, evType, fn, useCapture) {
// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
// By Scott Andrew
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

function ascendDOM(e, target) {
// climb up the tree to the supplied tag.
	while (e.nodeName.toLowerCase() != target && e.nodeName.toLowerCase() != 'html')
		e = e.parentNode;
	return (e.nodeName.toLowerCase() == 'html') ? null : e;
}

function hl_on(e) {
// turn on highlighting
	var el;
	if (window.event && window.event.srcElement)
		el = window.event.srcElement;
	if (e && e.target)
		el = e.target;
	if (!el) return;
	var parent_row = ascendDOM(el, 'tr');
	if (parent_row == null) return;
	parent_row.className = parent_row.className.replace(/hi/, 'lo');
}

function hl_off(e) {
// turn off highlighting
	var el;
	if (window.event && window.event.srcElement)
		el = window.event.srcElement;
	if (e && e.target)
		el = e.target;
	if (!el) return;
	var parent_row = ascendDOM(el, 'tr');
	if (parent_row == null) return;
	parent_row.className = parent_row.className.replace(/lo/, 'hi');
}

function showtype(e) {
// show new section of table
	var el;
	if (window.event && window.event.srcElement)
		el = window.event.srcElement;
	if (e && e.target)
		el = e.target;
	if (!el) return;
	var typestatus = [0, 0, 0, 0];
	if (el.firstChild.nodeValue == "Goalkeepers" || el.firstChild.nodeValue == "All Players") typestatus[0] = 1;
	if (el.firstChild.nodeValue == "Defenders" || el.firstChild.nodeValue == "All Players") typestatus[1] = 1;
	if (el.firstChild.nodeValue == "Midfielders" || el.firstChild.nodeValue == "All Players") typestatus[2] = 1;
	if (el.firstChild.nodeValue == "Strikers" || el.firstChild.nodeValue == "All Players") typestatus[3] = 1;
	el = document.getElementById("tab");
	for (var i = 0; i < el.rows.length; i++) {
		if (el.rows[i].cells[2].firstChild.nodeValue == ".Gk") rowtype = 0;
		if (el.rows[i].cells[2].firstChild.nodeValue == "Def") rowtype = 1;
		if (el.rows[i].cells[2].firstChild.nodeValue == "Mid") rowtype = 2;
		if (el.rows[i].cells[2].firstChild.nodeValue == "Str") rowtype = 3;
		if (typestatus[rowtype] == 1) el.rows[i].style.display = "";
		if (typestatus[rowtype] == 0) el.rows[i].style.display = "none";
	}
}

function showclub(e) {
// show new section of table
	var el;
	if (window.event && window.event.srcElement)
		el = window.event.srcElement;
	if (e && e.target)
		el = e.target;
	if (!el) return;
	var club = el.firstChild.nodeValue;
	if (club == "Ex Players") club = "Out";
	el = document.getElementById("tab");
	for (var i = 0; i < el.rows.length; i++) {
		if (el.rows[i].cells[1].firstChild.nodeValue == club) {
			el.rows[i].style.display = ""
		} else {
			el.rows[i].style.display = "none"
		}
	}
}

function addListeners() {
	if (!document.getElementsByTagName) return;
	var all_cells = document.getElementsByTagName('tr');
	for (var i = 0; i < all_cells.length; i++) {
		addEvent(all_cells[i], 'mouseover', hl_on, false);
		addEvent(all_cells[i], 'mouseout', hl_off, false);
	}
	var all_links = document.getElementsByTagName('a');
	for (var i = 0; i < all_links.length; i++) {
		if ((" " + all_links[i].className + " ").indexOf("playtype") != -1) {
			addEvent(all_links[i], 'click', showtype, false);
		}
		if ((" " + all_links[i].className + " ").indexOf("clubtype") != -1) {
			addEvent(all_links[i], 'click', showclub, false);
		}
	}
}

addEvent(window, 'load', addListeners, false);