function addLoadListener(fn) {
	if (typeof window.addEventListener != 'undefined') {
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined') {
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined') {
		window.attachEvent('onload', fn);
	}
	else {
		return false;
	}
	return true;
}

function attachEventListener(target, eventType, functionRef, capture){
    if (typeof target.addEventListener != "undefined") {
        target.addEventListener(eventType, functionRef, capture);
    }
    else if (typeof target.attachEvent != "undefined") {
        target.attachEvent("on" + eventType, functionRef);
    }
    else {
        return false;
    }
    return true;
}

// Get elements by any attribute.
function getElementsByAttribute(attribute,attributeValue) {
	var elementArray = new Array();
	var matchedArray = new Array();
	if (document.all) {
		elementArray = document.all;
	}
	else {
		elementArray = document.getElementsByTagName("*");
	}	
	for (var i=0; i<elementArray.length; i++) {
		if (attribute == "class") {
			var pattern = new RegExp("(^| )" + attributeValue + "( |$)");
			
			if (pattern.test(elementArray[i].className)) {
				matchedArray[matchedArray.length] = elementArray[i];
			}
		}
		else if (attribute == "for") {
			if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for")) {
				if (elementArray[i].htmlFor == attributeValue) {
					matchedArray[matchedArray.length] = elementArray[i];
				}
			}
		}
		else if (elementArray[i].getAttribute(attribute) = attributeValue) {
			matchedArray[matchedArray.length] = elementArray[i];
		}
	}
}

function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} 
	else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

// Nice function that helps you add alternating classes to any number of child elements.
function striper(parentElementTag, parentElementClass, childElementTag, styleClasses)
{
	var i=0,currentParent,currentChild;
	// capability and sanity check
	if ((document.getElementsByTagName)&&(parentElementTag)&&(childElementTag)&&(styleClasses)) {
		// turn the comma separate list of classes into an array
		var styles = styleClasses.split(',');
		// get an array of all parent tags
		var parentItems = document.getElementsByTagName(parentElementTag);
		// loop through all parent elements
		while (currentParent = parentItems[i++]) {
			// if parentElementClass was null, or if the current parent's class matches the specified class
			if ((parentElementClass == null)||(currentParent.className == parentElementClass)) {
				var j=0,k=0;
				// get all child elements in the current parent element
				var childItems = currentParent.getElementsByTagName(childElementTag);
				// loop through all child elements
				while (currentChild = childItems[j++]) {
					// based on the current element and the number of styles in the array, work out which class to apply
					k = (j+(styles.length-1)) % styles.length;
					// add the class to the child element - if any other classes were already present, they're kept intact
					currentChild.className = currentChild.className+" "+styles[k];
				}
			}
		}
	}
}

// Really handy function. Adds any class you want to any element you want.
function addClass(target, classValue) {
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	if (!pattern.test(target.className)) {
		if (target.className == "") {
			target.className = classValue;
		}
		else {
			target.className += " " + classValue;
		}
	}
	return true;
}

// Finds all the elements of a certain class.
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;
}

// Creates popup windows.
function popUp(url,width,height,overflow) {
	if (width > 640) { width = 640;}
	if (height > 480) {height = 480;}
	
	if (overflow == '' || !/^(scroll|resize|both)$/.test(overflow))
	{
		overflow = 'both';
	}
	
	var win = window.open(url, '','width=' + width + ',height=' + height + ',scrollbars=' + (/^(scroll|both)$/.test(overflow) ? 'yes' : 'no') + ',status=yes,toolbar=no,menubar=no,location=no');
	return win;
}

// Opens new links in a completely new window, by adding 'rel="external"' attribute to your <a>.
document.onclick = function(e) {
	var target = e ? e.target : window.event.srcElement;
	
	while (target && !/^(a|body)$/i.test(target.nodeName)) {
		target = target.parentNode;
	}
	
	if (target && target.getAttribute('rel') && target.rel == 'external') {
		var external = window.open(target.href);
		return external.closed;
	}
	
}

