/*
	Grunley scripts
	Updated 3/5/2006	
*/


/* --- Popup window functions --- */

// Resizable and scrollable popup window
function popup(URL, width, height) {
	popWin = window.open(URL, "popWin", "width=" + width + ",height=" + height + ",resizable=1,scrollbars=1,location=0,toolbar=0");
	popWin.focus();
	return false;
}

// Fixed popup window
function popupFixed(URL, width, height) {
	popWin = window.open(URL, "popWin", "width=" + width + ",height=" + height + ",resizable=0,scrollbars=0,location=0,toolbar=0");
	popWin.focus();
	return false;
}


/* --- Display enhancements --- */


// Alternate data table row colors
function tablerows() {
	var evenodd, tables, rows, i, j, newCorner, firstcell, allrows, lastrow;

	tables = document.getElementsByTagName("table");

	for (i = 0; i < tables.length; i++)
		// If a "data" table
		if (tables[i].className.indexOf("data") >= 0) {
			rows = tables[i].getElementsByTagName("tr");
			evenodd = "even";

			// Traverse each row
			for (j = 0; j < rows.length; j++)

				// Ignore nested tables
				if (rows[j].parentNode.parentNode == tables[i] || rows[j].parentNode == tables[i]) {

					// Check for at least one data cell in row (skip header rows)
					if (rows[j].getElementsByTagName("td").length > 0) {
						evenodd = (evenodd == "data-even") ? "data-odd" : "data-even";

						// Insert/append new class name; Skip rows with predefined even/odd classes
						rows[j].className += (rows[j].className == null) ? evenodd : (rows[j].className == "data-odd" || rows[j].className == "data-even") ? "" : " " + evenodd;
					}
				}
		}
}


/* --- Generic functions --- */

// Add/remove class names
function addClass(oldClass, newClass) {
	// Check if class already exists
	if (oldClass.indexOf(newClass) >= 0) return oldClass;

	return (oldClass == "") ? newClass : oldClass + " " + newClass;
}
function removeClass(oldClass, removedClass) {
	var regEx = new RegExp("\s*" + removedClass, "i");
	return oldClass.replace(regEx, "");
}


// Add/remove events
function addEvent(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn](window.event); }
		obj.attachEvent("on"+type, obj[type+fn]);
	}
}
function removeEvent(obj, type, fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(type, fn, false);
	else if (obj.detachEvent) {
		obj.detachEvent("on"+type, obj[type+fn]);
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}



// Window load functions
if (document.getElementById) {
	addEvent(window, 'load', tablerows);
}
