/**
* +--------------------------------------------------------------------+
* | This file is Copyright (c) Squiz Pty Ltd       |
* | ACN 084 670 600                                                    |
* +--------------------------------------------------------------------+
* | IMPORTANT: Your use of this Software is subject to the terms of    |
* | the Licence provided in the file licence.txt. If you cannot find   |
* | this file please contact Squiz (www.squiz.net) so we may provide   |
* | you a copy.                                                        |
* +--------------------------------------------------------------------+
*
************************************************************************
*
* Modified on: 16 October 2007
* Modified by: DCITA - Joel Starkey / Tim Riley
* Change log:
* 16 October 2007
* 	- To check the link against multiple base URL's
* 	- To add email addresses (mailto:) as an exception
*
*/

var base_url = new Array('netalert.gov.au','www.netalert.gov.au','text.netalert.gov.au','netalert.gov.au');
var warning_text = 'You are now leaving the NetAlert website. NetAlert is not responsible for the quality, validity or accuracy of any information or for any content found on the sites which are accessible from its website.  The site you are going to may not share the Privacy and Copyright policies of the NetAlert website.';
var exceptions = new Array('no-exceptions'); //if there are separate websites using a sub-folder, ie www.dcita.gov.au/deaflympics


//Scan all hyperlinks  <a> in current webpage.
function ScanLinks() {
	var rewrite = true; 
	var links = document.getElementsByTagName('a');
	for ( var i = 0; i < links.length; i++ ) {
		//Looping the links found in the page
		if (ShouldRewriteLink(links[i])) {
			RewriteLink(links[i]);
		}//End if
	}//End for
}//End function ScanLinks()


/* Check to see if the hyperlink should be re-written based on the following conditions:
 *		- The link does not appear in the Exceptions array
 *		- The link is not an email address
 *		- The link does not match a base URL
*/
function ShouldRewriteLink(link) {
	if (IsException(link)) {
		//If there is an exception, don't rewrite
		return true;
	}//End if
	else if (AreThereEmailAddresses(link)){
		//If the link is an email address 
		return false;
	}
	else {
		if(link.href) {
			for ( var j = 0; j < base_url.length; j++ ) {
				//Loop through the base URL's
				if(link.href.indexOf(base_url[j]) != -1) {
					//If the current link matched a base URL return false (don't rewrite)
					return false;
				}//End if
			}//End for
		}//End if
	}//End else
	return true;
}//End function ShouldRewriteLink()


//Check to see if the current hyperlink matches a URL in the exceptions array
function IsException(link) {
	if (link.href ) {
		for ( var i = 0; i < exceptions.length; i ++ ) {
			//Loop through exceptions array, return true if found
			if ( link.href.indexOf(exceptions[i]) != -1 ) {
				return true;
			}//End if
		}//End for
	}//End if
	return false;
}//End IsException()

//Check to see if the current hyperlink matches an email address (mailto:)
function AreThereEmailAddresses(link) {
if ( link.href ) {
		//Is the link an email address, return true if found
		if (/^mailto:/.test(link.href)) {
			return true;
		}//End if
	}//End if
	return false;
}//End AreThereEmailAddresses()


/* The hyperlink is rewritten so a JavaScript alert box appears if there are:
 *		- no exceptions
 *		- no match to the base URL's
*/
function RewriteLink(link) {
	var old_href = link.href;
	link.onclick = function() {
		alert(warning_text);
		location.href = old_href;
	}//End onclick
	link.onkeyup = function(e) {
		var KeyID = (window.event) ? event.keyCode : e.keyCode;
		if ( KeyID == 13 ) {
			if (confirm(warning_text) ) {
				location.href = old_href;
			}// End if
		}// End if
	}//End onkeyup
}//End function RewriteLink ()


function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }// End if
}


addEvent(window,'load',ScanLinks);
