/*
 * @Author: Eric Stevens
 * @Date: 08-May-2006
 * @Purpose: Repeatedly polls the page while it's loading, looking for new links
 * 			 and redirecting external links through a disclaimer page.
 * @Template: disclaimers.js
 * @Notes: This script polls the page several times a second, looking for
 *         newly loaded links, and disclaiming them per corporate policy as 
 *         appropriate.  Once the page is completely loaded, the script will 
 *         stop the repeated polling.
 *         
 *         This script should be included some time after the <body> tag, but as
 *         early as possible to facilitate the feature that stops polling the 
 *         page for new links several times a second.
 */

var ed_continueExternalDisclaimers = true;
var ed_setBodyOnload = false;
var ed_pollTime = 250; //milliseconds
//Parts breakdown
// 0 = full match
// 1 = protocol
// 2 = hostname
// 3 = filename
// 4 = arguments
// 5 = named anchor
//                 protocol     host      file      args     anchor
var ed_regexp = /^(?:([a-z]+:\/\/)([^\/]+))?([^?]*)\??([^#]*)#?(.*)/i;
var ed_urlParts = ed_regexp.exec(window.location.href);
var ed_sourceDomain = ed_urlParts[2];

//Override the body onload
var ed_oldOnLoad = window.onload;
window.onload = function(event){
	if (ed_oldOnLoad) ed_oldOnLoad(event);
	ed_finishExternalDisclaimers();
}

function ed_doExternalDisclaimers(){
	var a;
	var msg = "";
	var allA = document.getElementsByTagName("a");
	//loop over all <a> elements
	
	for (var x = 0; x < allA.length; x++){
		a = allA[x];
		if (a.getAttribute('ed_processed') == null){
			ed_addDisclaimersToAnchor(a,ed_sourceDomain);
			a.setAttribute('ed_processed',true);
		}
	}
	if (ed_continueExternalDisclaimers){
		setTimeout(ed_doExternalDisclaimers, ed_pollTime);
	}
}

function ed_finishExternalDisclaimers(){
	ed_continueExternalDisclaimers = false;
}

function ed_addDisclaimersToAnchor(a,sourceDomain){
	var href = a.getAttribute("href");
	if (!href) return;
	var parts = ed_regexp.exec(href);
	if (parts[1] > '' && parts[2] != sourceDomain) {
		//Provide the _blank target if there is not already a target set.
		if (a.getAttribute('target') == null) a.setAttribute('target','_blank');
		if (a.target == null) a.target = "_blank";
		
		var innerHTML = a.innerHTML;
		//change the href to point at the disclaimer page
		var newLink = 'http://careers.sanofipasteur.us/disclaimer/'
			+'?dest='+escape(href)
			+'&src='+escape(window.location.href.substr(0,200))
			+'&target='+escape(a.getAttribute('target'));
		a.setAttribute('href',newLink);
		a.href = newLink;
		
		//Because there is some confusion with this script being hosted in a common
		//location, we always want to open the new window w/o an address bar.
		//we therefore need an onClick event, and this onClick needs to preserve
		//existing onClick events.  Because we don't know what the existing onclick
		//does, it may do things that we can't account for here (such as open a window
		//of its own).  We should therefore only do something if there wasn't already
		//an onclick.
		var onClick = a.onclick;
		if (typeof(onClick) == 'undefined') onClick = a.getAttribute('onclick');
		if (typeof(onClick) == 'undefined' || onClick == null) {
			a.onclick = function(){
				window.open(this.href,"","toolbar=yes,location=no,address=no,menubar=yes,resizable=yes,status=yes,scrollbars=yes,resize=yes");
				return false;
			};
		}
		
		//a kludge to prevent IE from munging the inner HTML in a few rare 
		//circumstances when the href is changed.
		if (innerHTML) a.innerHTML = innerHTML;
		
		//Keep the statusbar text consistent to let the user know where they're 
		//ultimately going.  But keep any existing onMouseOver and onMouseOut 
		//events
		var oldMouseOver = a.onmouseover;
		var oldMouseOut = a.onmouseout;
		a.onmouseover = function(event){
			window.status = href;
			if (oldMouseOver) oldMouseOver(event);
			return true;
		}
		a.onmouseout = function(event){
			window.status = '';
			if (oldMouseOut) oldMouseOut(event);
			return true;
		}
	}
}

setTimeout(ed_doExternalDisclaimers, ed_pollTime);


