// OPEN A PAGE IN A NEW WINDOW
// Create the new window
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {		
		var newWindow = window.open(this.getAttribute('href'), 'popup');
		if (newWindow) {
			// For IE 8
			try {
		        if (newWindow.focus()) {
			        newWindow.focus();
			    }
		    }
		    catch(err) {		        
		        return false;		        
		    }	
		return false;
		}
	return true;
	}
}

// OPEN A PAGE IN A POP-UP WINDOW
// Create the new window
function openInPopUpWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {		
		var newWindow = window.open(this.getAttribute('href'), 'popup', 'width=355, height=270, status=no, scrollbars=no, resizable=yes');
		if (newWindow) {
			// For IE 8
			try {
		        if (newWindow.focus()) {
			        newWindow.focus();
			    }
		    }
		    catch(err) {		        
		        return false;		        
		    }	
		return false;
		}
	return true;
	}
}

// CALL THIS FUNCTION TO INITIATE FUNCTION THAT OPENS CERTAIN LINKS IN NEW WINDOWS
function getNewWindowLinks() {
	if (document.getElementById && document.createElement && document.appendChild) {		
		var link;
		var links = document.getElementsByTagName('a');
		for (var i = 0; i < links.length; i++) {
			link = links[i];			
			if (/\bnon\-html\b/.test(link.className)) {				
				link.onclick = openInNewWindow;
			}			
			else if (/\boff\-site\b/.test(link.className)) {				
				link.onclick = openInNewWindow;
			}	
			else if (/\bpopup\b/.test(link.className)) {				
				link.onclick = openInPopUpWindow;
			}	
			// Remove hyperlinks from sub-navigation headers (Added by GML)
			else if (/\bsubnav\-header\b/.test(link.className)) {
				link.href = '#fix';
			}		
		}	
	}
}

// LOOK FOR CLOAKED LINKS AND MAKE THEM CLICKABLE
// Hopefully this will help hide e-mail addresses from spam spiders
function createMailtoLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {			
		var email; // E-mail address
		var mailto; // The mailto hyperlink
		var cloak; // The mailto span element
		var spans = document.getElementsByTagName('span'); // Array of span elements
		for (var i = 0; i < spans.length; i++) {
			cloak = spans[i];
			// Find all span elements with a class name of "cloak"
			if (/\bcloak\b/.test(cloak.className)) {				
				email = cloak.innerHTML;
				cloak.innerHTML = "";									
				mailto = document.createElement('a');															
				mailto.href = 'mailto:' + email;
				mailto.innerHTML = email;
				cloak.appendChild(mailto);			
			}
		}	
	}
}

// SET FOCUS ON PAGES WITH USER FORMS
// Look for inputs tags with the class name of "first"
function goToFirstInput() {		
	var objInput;		
	var aryInput = document.getElementsByTagName('input');
	for (var i = 0; i < aryInput.length; i++) {
		objInput = aryInput[i];
		// Find all inputs with a class name of "first"
		if (/\bfirst\b/.test(objInput.className)) {				
			objInput.focus();							
		}				
	}	
}

// WRITE E-MAIL ADDRESS TO HIDE IT FROM SPAM SPIDERS
function writeAddress(name,url) {
	document.write('<a href="mailto:' + name + '@' + url + '">' + name + '@' + url + '</a>');
}

// SHOW "EMAIL THIS PAGE" BUTTON
// Button requires JavaScript to function so is only shown if JavaScript is available
function showEmailButton() {	
	document.getElementById('button-email').style.display = 'block';
}	

// SHOW/HIDE "EMAIL THIS PAGE" BOX 
function sendPage(showhide) {
	if (document.getElementById) {
		var theStyle;
		switch (showhide) {
			case 'show':
				theStyle = 'block';
				break;
			default:
				theStyle = 'none';
		}
		document.getElementById("email-this-page").style.display = theStyle;		
	}
}			

// ON-LOAD EVENTS 
// Requires jQuery
$(document).ready(function() {
	getNewWindowLinks();
	showEmailButton();
	goToFirstInput();
	createMailtoLinks();
});