/*
 * 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
		//	}
		}
	}
}

/*
 * Pops up a route description window
 */
function fnPlanRoute(strPostalCode) {
	window.open('http://www.viamichelin.com/viamichelin/gbr/dyn/controller/ItiWGPerformPage?isAvoidFrontiers=false&isFavoriseAutoroute=false&isAvoidPeage=false&isAvoidVignette=false&isAvoidLNR=false&strTypeStep=0&strVehicle=0&reinit=1&strStartCP=' + strPostalCode + '&strStartCity=&strStartCityCountry=285&strDestAddress=Julianasingel+7&strDestCP=5802AS&strDestCity=Venray&strDestCityCountry=285');
}

/*
 * Popup function
 */
var generalPopup;
function fnPopup(page, width, height, options) {
	var top = (screen.height-height)/2;
	var left = (screen.width-width)/2;
	return window.open(page, '', 'top='+top+',left='+left+',width='+width+',height='+height+','+options);
}

function fnAlbumPopup(link, width, height) {
	generalPopup = fnPopup(link, width + 100, height + 125, 'toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=yes');
	return false;
}