// This function will capture all mouse clicks on the page
// if the click is on a link, and points to an external website, the user will be warned before proceding


function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
		if (oldonload) {
			oldonload();
		}
			func();
		}
	}
}
	
	
function interceptLinks(e) {
		var cont = true;
		
		// work out target dependent on browser quirks
		var targ;
		if (!e) var e = window.event;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode;
			
		// test if target is a link
		if (targ.toString().substr(0,7) == "http://"){
			var thisDomainName = document.location.toString().split("/")[2];
			thisDomainName = thisDomainName.split(":")[0];
			//alert(thisDomainName);
			var targetDomainName = targ.toString().split("/")[2];
			targetDomainName = targetDomainName.split(":")[0];
			//alert(targetDomainName);
			//continue if target domain equals current domain
			cont = thisDomainName == targetDomainName;
			
		}
		
		// warning message if external domain
		if (!cont){
			cont = confirm('You are now leaving the pbc-cork.ie website.\nLinks to other websites are provided as a convenience to visitors.\nPresentation Brothers College accepts no responsibility for the content of linked sites.\nDo you wish to proceed?');
		}
		
		//alert(targ);
		return cont;
	}
	
	// register function to listen for all mouse clicks
	document.onclick = interceptLinks;
	if (document.layers){
		document.captureEvents(Event.CLICK);
	}
	
	// make sure all external links have target="_BLANK"
	function setExternalLinks(){
		if(document.getElementsByTagName ){
			var thisDomainName = document.location.toString().split("/")[2];
			thisDomainName = thisDomainName.split(":")[0];
			var anchors = document.getElementsByTagName( "a" );
			
			var a = anchors[0];
			/*for (j in a){
				alert(j+":"+a[j]);	
			}*/
			
			
			for( var i = 0; i < anchors.length; i++ ) {
				var anc = anchors[i];
				if (anc.href.toString().substr(0,7) != "mailto:"){ // ignore mailto links
					var targetDomainName = anc.href.toString().split("/")[2];
					targetDomainName = targetDomainName.split(":")[0];
					if (targetDomainName != thisDomainName) {
						anc.target = "_blank";
					}
				}
			}
		}
	}

	addLoadEvent(setExternalLinks);
	
	
