﻿// Common.js
// =========
// Common javascript functions usable by all pages.



// addLoadEvent(): By Simon Wilson. Adds the given function as a page load 
// event.

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}



// addEvent(): By Scott Andrew. Attaches the given function as an event to the
// given element. 

function addEvent(elm, evType, fn, useCapture) {
	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;
	}
}



// getElementsByClass(): By Dustin Diaz. Returns all the elements under 'node'
// of the tag type 'tag' whose class matches that given in 'searchClass'.
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}



// getEventTarget(): By Luke Rissacher. Returns the DOM element that's the 
// target of the most recent event. 'e' is the event passed to the event 
// handler, if any, in the Netscape-style event model.
function getEventTarget(e) {
	if (!e) var e = window.event;
	var target = (window.event) ? e.srcElement : e.target;
	return target;
}