/*
 * Due to some of the "legal" changes in the handling of external links to the corporate website, a set of
 * controls needed to be added to warn users when exiting to 3rd party websites and/or content.
 * See: Jira CRP-172 sub task CRP-169   MJF
 * Code constructed by Derek Winfield and executed by Michael Farnham 05/03/2007
 * Code alteration by Derek Winfield and Michael Farnham to include the various possible domain names that are pointed
 * to the MonotypeImaging.com domain. The "window.location.hostname" will read the incomming url and use that as the
 * comparison to also compare to. MJF 09/05/2007
*/
var monotypeExternalLinks = function () {
	var internalDomain = "monotypeimaging.com";
	var confirmationMessage = "You are leaving Monotype Imaging Holdings Inc.'s\nWeb site. You may return to the Monotype Web site\nby closing the window to the third-party Web site\nthat you have opened\n\nDo you wish to continue?";
	function markExternalLinks() {
		var pattern = new RegExp("^\\w+:\\/\\/(\\w+\\.)?(test)?" + internalDomain + "\\/");
		var pattern2 = new RegExp("^\\w+:\\/\\/" + window.location.hostname + "\\/");
		var links = document.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++) {
			if (!pattern.test(links[i].href) && !pattern2.test(links[i].href) && links[i].href.substring(0, 7) != "mailto:" && links[i].href.substring(0, 11) != "javascript:") {
				links[i].setAttribute('target','_blank');
				addListener(links[i], "click", confirmExit);
			}
		}
	}

	function confirmExit(e) {
		if (!confirm(confirmationMessage)) {
			if (e.preventDefault) {
				e.preventDefault();
			} else {
				e.returnValue = false;
			}
			return false;
		} else {
			return true;
		}
	}
	var addListener = function() {
		if ( window.addEventListener ) {
			return function(el, type, fn) {
				el.addEventListener(type, fn, false);
			};
		} else if ( window.attachEvent ) {
			return function(el, type, fn) {
				var f = function() {
					fn.call(el, window.event);
				};
				el.attachEvent('on'+type, f);
			};
		} else {
			return function(el, type, fn) {
				element['on'+type] = fn;
			}
		}
	}();

	addListener(window, "load", markExternalLinks, false);
}();
