LinkProcessor = 
{
	checkForLeaving : true,
	LeavingMessage : "<strong>YOU ARE LEAVING THE DEPARTMENT OF ENERGY HANFORD WEBSITE.</strong><br><br>The \"Continue\" link below will take you out of the DOE Hanford website.<br><br>Links which take you out of this site are not under the control of DOE Hanford, and we are not responsible for the contents or any further links from such site. DOE Hanford is providing these links to you only as a convenience.<br><br>Do you wish to leave this site?<br />	<input type='button' value='cancel' onclick='LinkProcessor.navCancel()'> <input type='button' value='continue' onclick='LinkProcessor.navContinue()'>",
	useInternalSystem : false,
	messagePopup : undefined,
	linkAddress : undefined,
	levelTopDomain: 2,
	exitPage: "http://www.hanford.gov/exit/index.cfm?dest=",
	baseHref: undefined,
	contentClass: "secondaryPageContent",
	styledAnchorClassName: "contentLink",
	alwaysNewWindow: [],
	validHosts: [],
	init: function()
	{
		var bases = document.getElementsByTagName("BASE");
		if ( bases.length > 0)
			this.baseHref = bases[0].href;
		this.scanLinks(document);	
		this.scanAnchor();		
	},
	scanAnchor: function()
	{
		var cnt = "";
		var i = -1;
		var secondaryPage = $$("." + this.contentClass);
		for ( var j = 0; j < secondaryPage.length; j++)
		{
		
			cnt = secondaryPage[j].getElementsByTagName("a")
			for (i = 0; i < cnt.length; i++)
			{
				if ( cnt[i].className == "")
					cnt[i].className = this.styledAnchorClassName;
			}
		
		}
	},
	jumpToAnchor: function(sAnchorName)
	{
		window.location.hash = sAnchorName;
	},
	scanLinks : function(parElm)
	{
		var anchors = parElm.getElementsByTagName("A");
		for ( i = 0; i < anchors.length; i++)
		{
			if (anchors[i].href && !this.isJS(anchors[i].href) && !this.isMailTo(anchors[i].href) ) {
			
				if (anchors[i].getAttribute("adjustedForExt") != "adjusted") {
					anchors[i].setAttribute("adjustedForExt", "adjusted")
					if ( ! anchors[i].onclick )
						Event.observe(anchors[i], "click", this.anchorClicked.bindAsEventListener(this));
				}
			}
		
							
		}
		
		anchors = parElm.getElementsByTagName("AREA");
		for ( i = 0; i < anchors.length; i++)
		{
			if (anchors[i].href && !this.isJS(anchors[i].href) && !this.isMailTo(anchors[i].href) ) {
			
				if (anchors[i].getAttribute("adjustedForExt") != "adjusted") {
					anchors[i].setAttribute("adjustedForExt", "adjusted")
					Event.observe(anchors[i], "click", this.anchorClicked.bindAsEventListener(this));
				}
			}
		
							
		}
		
		
		
		
	},
	
	anchorClicked : function(event)
	{
		var element = Event.findElement(event, "a");
		if ( ! element )
		{
			element = Event.findElement(event, "area")
			if ( !element)
				return true;
		}
		if (!this.followLink (element.href, element.target) )
		{
			Event.stop(event);
			return false;
		}
		return true;
		
	},
	
	isJS : function(sURL)
	{
		var tmpStr = this.parseUri(sURL);
		if ( tmpStr["protocol"] == "javascript")
			return true;
		return false;
	},

	isMailTo: function(sURL)
	{
		var tmpStr = this.parseUri(sURL);

		if ( tmpStr["user"] == "mailto" || tmpStr["protocol"] == "mailto")
		{		
			return true;
		}

		return false;
	},
	navContinue: function()
	{
		if ( this.linkAddress != undefined)
			window.location = this.linkAddress;
	},
	navCancel: function()
	{
		if ( this.messagePopup != undefined)
			this.messagePopup.destroy();
		this.messagePopup = undefined;	
	},

	parentDomain : function(sDomainName)	
	{
		var domParts = sDomainName.split(".");
		if ( domParts.length >= 2)
		{
			return domParts[domParts.length -2] + "." +domParts[domParts.length -1];  
		}
		else
			return sDomainName;
	},
	isLinkAnchor: function(sAddress)
	{
		var tmpStr = this.parseUri(sAddress);

		if ( sAddress.substr(0, this.baseHref.length) == this.baseHref &&  sAddress.substr(this.baseHref.length, 1) == "#")
			return true;
		return false;	
	},
	
	isLinkExternal: function(sAddress)
	{		
		var tmpStr = this.parseUri(sAddress);
		if ( tmpStr["protocol"] != "" && tmpStr["protocol"] != "javascript" && tmpStr["user"] != "mailto" && tmpStr["protocol"] != "file")
		{
			
			var outHost = tmpStr["host"];
			if ( tmpStr["user"].indexOf('/') > 1)
				outHost = tmpStr["user"].split('/')[0];
			var currentHost = window.location.host;
		
			if ( this.validHosts.length > 0)
			{
				for ( var i = 0; i < this.validHosts.length; i++)
				{
					if ( this.parentDomain(outHost) == this.validHosts[i] )
					{
						return false;
					}	
				}
			} else {
				if ( this.parentDomain(outHost) == this.parentDomain(currentHost) )
				{
					return false;
				}		
			}
			return true;
		}	
		return false;
	},
	isLinkPopup: function(sAddress)
	{
		if ( this.alwaysNewWindow.length == 0 )
			return false;
		
		var tmpStr = this.parseUri(sAddress);
		if ( tmpStr["protocol"] != "" && tmpStr["protocol"] != "javascript")
		{
			var outHost = tmpStr["host"];
			for ( var i =0; i < this.alwaysNewWindow.length; i++)
			{
				if ( this.alwaysNewWindow[i] == outHost)
					return true;
			}
		}
		return false;
	},
	followLink: function(sAddress, sTarget)
	{
		if ( this.isLinkExternal(sAddress) && this.checkForLeaving )
		{
			if ( this.useInternalSystem )
			{
				this.linkAddress = sAddress;	
				this.messagePopup = new UIMessages(this.LeavingMessage, { fullScreen: true});
				return false;

			} else {
			
				window.location = (this.exitPage + escape(sAddress));
				return false;	
			}
			
		} else {
			if ( this.isLinkAnchor(sAddress))
			{
				var tmpStr = this.parseUri(sAddress);
				window.location.hash =  tmpStr.anchor;
				return false;
			} else if ( this.isLinkPopup(sAddress)) {
				window.open(sAddress, '_blank')
				return false;
			}
			else
				if ( sTarget )
				{
					window.open(sAddress, sTarget)
					return false;
				} else {
					window.location = sAddress;
					return false;
				}
		}
		return true;		
	},
	
	
	parseUri: function (str) {
	var	o   = this.parseUriOptions,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
},


	parseUriOptions : {
		strictMode: false,
		key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
		q: {
			name: "queryKey",
			parser: /(?:^|&)([^&=]*)=?([^&]*)/g
		},
		parser: {
			strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
			loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
		}
	}
	
};

Event.observe(window, "load", LinkProcessor.init.bindAsEventListener(LinkProcessor))

