// JavaScript for Product and Testing pages

var useragent = navigator.userAgent.toLowerCase();

function uaMatch(userstr) {		// User Agent search function
	if(useragent.match(userstr.toLowerCase())) {
		return true;
	}
	else {
		return false;
	}
}

function createStyleLink(style) {
	var head = document.getElementsByTagName("head")[0];
	var cssnode = document.createElement("link");
	cssnode.type = "text/css";
	cssnode.rel = "stylesheet";
	cssnode.media = "screen";
	cssnode.href = style;
	head.appendChild(cssnode);
}

if(uaMatch("MSIE")) {
	createStyleLink("/css/products/ie.css");
}
else if(uaMatch("Firefox/3") && uaMatch("Windows")) {
	createStyleLink("/css/products/firefox3.css");
}
else if(uaMatch("Firefox/3") && uaMatch("Macintosh")) {
	createStyleLink("/css/products/firefox3-nix.css");
}
else if(uaMatch("Firefox/2")) {
	createStyleLink("/css/products/firefox2.css");
}
if(uaMatch("MSIE [6].[0-9]")) {
	createStyleLink("/css/products/ie6.css");
}

// Opens links with rel="external" in a new window, opens PDFs in a new window, and inserts Readr icon in PDF links
function externalLink() {
	if(document.getElementsByTagName) {
		var links = document.getElementsByTagName("a");		// Puts all anchor elements on the page in an array
		for(var i = 0; i < links.length; i++) {		// Cycles through all elements in the array
			var anchor = links[i];		// Sets a local variable equal to the current anchor
			var ext = anchor.getAttribute("href").substring(anchor.getAttribute("href").length - 4, anchor.getAttribute("href").length).toLowerCase();
			if(anchor.getAttribute("href") && anchor.getAttribute("rel") === "external" || ext === ".pdf") {	// If the anchor is a link and rel="external", or it links to a PDF
				anchor.target = "_blank";	// Sets the target to _blank
			}
			if(ext === ".pdf")	{	// If the anchor links to a PDF, set the class to PDF
				anchor.className = "pdf";		// PDF class displays the Adobe Reader icon next to the link
			}
		}
	}
}

onload = function () {
	externalLink();
};