﻿
// detect all anchors in the current page that have the rel attribute set to 'external'
// and set the anchors target property to '_blank'; this allows us to not use 'target' attributes
// on anchor tags which is no longer allowed in strict xhtml;
// also, all PDFs open in a new window
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		var href = anchor.getAttribute("href");
		if(href) {
			var openInNewWindow = (anchor.getAttribute("rel") == "external");
			if(!openInNewWindow) {
				var lastPeriod = href.lastIndexOf('.');
				if(lastPeriod > 0 && href.substring(lastPeriod).toLowerCase() == '.pdf')
					openInNewWindow = true;
			}
			if(openInNewWindow)
				anchor.target = "_blank";
				
			//Handle obfuscated email links -- see server-side MailTo class
			if(anchor.className == 'obfuscated_mailto_link') {
				var realEmail = deobfuscate(href.substr('mailto:'.length).replace('@nospam.com',''));
				anchor.href = 'mailto:' + realEmail;
				anchor.innerHTML = realEmail;
			}
		}
	}
}

//Used for obfuscated email links
function deobfuscate(s) {
	var pieces = s.split('_');
	var s2 = '';
	for(var j=0;j<pieces.length;j++) {
		s2 += String.fromCharCode(parseInt(pieces[j],0) + j);
	}
	return s2;
}