// JavaScript Document
/*
	Author: Gabrielle C. Espina
	Created: 2007-12-03 
	Updated: 2007-12-03
*/

var GabeUtil = {};

GabeUtil.confirmRedirect = function(location, message) {
	
	if(typeof message == 'undefined') {
		message = 'Continue?';	
	}
	
	if(confirm(message)) {
		window.location = location;
	}
}

GabeUtil.numericOnly = function(field) {
	
	var allowed = "0123456789";
	var len = field.value.length;
	
	var filter = function(e) {
		e = GabeUtil.getEvent(e);
	}
	
	GabeUtil.addEventListener(field, "keydown", filter, false);
	
}

GabeUtil.createTable = function(doc, cols, rows, className) {
	var table = doc.createElement("table");
	var tbody = doc.createElement("tbody");
	if(className) {
		table.className = className;	
	}
	table.border = "1px";
	for(var i = 0; i < rows; i++) {
		var newRow = doc.createElement("tr");
		for(var j = 0; j < cols; j++) {
			var newCell = doc.createElement("td");	
			newCell.innerHTML = "&nbsp;";
			newRow.appendChild(newCell);
		}
		tbody.appendChild(newRow);
	}
	table.appendChild(tbody);
	return table;
}

/*
	Gets elements by class name
*/
GabeUtil.getElements = function(aparent, tagName, className) {
	
	if(typeof tagName == "undefined") {
		// No tagName specified
		tagName = "*";
	}
	
	var temp = aparent.getElementsByTagName(tagName);
	var elements = [];
	
	for(ekey in temp) {
		if(typeof temp[ekey] == "object") {
			elements.push(temp[ekey]);
		}
	}
		
	if(typeof className != "undefined") {
		// Class name was specified
		for(ekey in elements) {
			if(elements[ekey].className != className) {
				elements.splice(ekey, 1);	
			}	
		}
	}
	
	return elements;
	
}

/*
	Determines if internet browser is IE.
*/
GabeUtil.isIE = function() {
	if(typeof window.addEventListener == "undefined") {
		return true;
	}	
	return false;
}

/*
	Adds an event listener to an element
*/
GabeUtil.addEventListener = function(aelement, action, afunction, bool) {
	if(typeof aelement.addEventListener == "function") {
		aelement.addEventListener(action, afunction, bool);	
	} else {
		aelement.attachEvent("on" + action, afunction);
	}
}

/*
	Removes an event listener from an element.
*/
GabeUtil.removeEventListener = function(aelement, action, afunction, bool) {
	if(typeof aelement.addEventListener == "function") {
		aelement.removeEventListener(action, afunction, bool);	
	} else {
		aelement.detachEvent("on" + action, afunction);
	}
}

/*
	Generalizes the event object.
*/
GabeUtil.getEvent = function(e) {
	if(window.event) {
		//alert("windows!");
		e = window.event;
		e.target = e.srcElement;
		e.preventDefault = function() {
			e.returnValue = false;
		}
	}
	return e;
}

GabeUtil.getScrollPos = function() {
	var pos = {};
	if(!GabeUtil.isIE()) {
		// Mozilla
		pos.x = window.pageXOffset;
		pos.y = window.pageYOffset;
	} else {
		if(typeof document.documentElement.scrollLeft != "undefined") {
			pos.x = document.documentElement.scrollLeft;
			pos.y = document.documentElement.scrollTop;
		} else {
			pos.x = document.body.scrollLeft;
			pos.y = document.body.scrollTop;
		}
	}
	return pos;
}

GabeUtil.getMousePos = function(e) {
	var pos = {};
	var wscroll = GabeUtil.getScrollPos();
	pos.x = wscroll.x + e.clientX;
	pos.y = wscroll.y + e.clientY;
	//alert(pos.x + " " + pos.y);
	return pos;
}

GabeUtil.getElementPosition = function(element) {
	var parent = element.offsetParent;
	var pos = {};
	pos.x = element.offsetLeft;
	pos.y = element.offsetTop;
	while(parent != null) {
		pos.x += parseInt(parent.offsetLeft, 10);
		pos.y += parseInt(parent.offsetTop, 10);	
		parent = parent.offsetParent;
	}
	//alert("epos " + pos.x + " " + pos.y);
	return pos;
}