/*
 * Function used to get elements which have a certain classname. The classname does not have to be
 * the only classname an object has.
 */
document.getElementsByClassName = function (needle) {
	var s = [document.documentElement || document.body], i = 0, r = [], l = 0, e;
	var re = new RegExp('(^|\\s)' + needle + '(\\s|$)');

	do {
		e = s[i];

		while (e) {
			if (e.nodeType == 1) {
				if (e.className && re.test(e.className)) r[l++] = e;

				s[i++] = e.firstChild;
			}

			e = e.nextSibling;
		}
	}
	while (i--);

	return r;
}

/*
 * Function used to get elements which have a certain classname. The classname does not have to be
 * the only classname an object has.
 */
document.getElementsWithExternalRel = function () {
	var s = [document.documentElement || document.body], i = 0, r = [], l = 0, e;
	var re = new RegExp('^external$'); // '(^|\\s)external(\\s|$)'

	do {
		e = s[i];

		while (e) {
			/* Element nodes only */
			if (e.nodeType == 1) {
				if (e.rel && re.test(e.rel)) r[l++] = e;

				s[i++] = e.firstChild;
			}

			e = e.nextSibling;
		}
	}

	while (i--);

	return r;
}

/*
 * Open a certain URL in a new window. If a popup blocker prevents this the link is opened in the
 * current window.
 */
function fnDOM1Open( event ) {
	if ( window.event ) {
		var shiftKey = window.event.shiftKey;
	} else {
		var shiftKey = event.shiftKey;
	}

	if ( !shiftKey ) {
		var strURL = this.href;
		var oNewWindow = window.open( strURL );

		if ( !oNewWindow ) {
			window.location.href = strURL;
		}

		return false;
	} else {
		return true;
	}
}

/*
 * Assign an onclick event handler to all links having the classname "newWindow"
 */
function fnAssignPopupLinks() {
	var aAnchorList	 = document.getElementsWithExternalRel();
	i		 = aAnchorList.length;

	while (i--) {
		oAnchor	 = aAnchorList[i];

		//mOpen	 = new Function('return fnOpen(\''+oAnchor.href+'\');');

		if ( oAnchor.href ) {
		// At the moment only using the DOM level 1 approach as it works for all browsers.
		//	if ( document.addEventListener ) {
		//		oAnchor.addEventListener('click', fnDOM2Open, true); // DOM level 2
		//	} else if ( document.attachEvent ) {
		//		oAnchor.attachEvent('onclick', mOpen); // IE5+
		//	} else {
				oAnchor.onclick = fnDOM1Open; // DOM level 1
		//	}
		}
	}
}

