/* 
** Author: Jamie Jackson
** Date: 12/01/05
** Comments: These scripts will rewrite all external links on a page. They will
** rewrite the hrefs to point to a disclaimer page.
*/

disclaimerPage = "exitdisclaimer.cfm";
// change to "true" to switch on debug output
debugMode = false;

/* Find a case-insensitive string within a substring (returns boolean) */
function findNoCase(substr, str) {
  var found = (str.toUpperCase().indexOf(substr.toUpperCase()) != -1);
  return found;
}

/* The following function will take every link on the page, and modify it to
** use a prompt (if appropriate)
*/
function disclaimExternalLinks(urlExclusions, dir) {
  // optional directory parameter
	if ( dir == null ) {
    dir = '';
  }
  // will populate this with the original link properties (later)
  var origLinks = new Array(1);
  // loop over all links
  for (var i=0; i < document.links.length; i++) {
    // reset some vars to their defaults
    var matchFound = false;
    var link = document.links[i];
    // put old link properties into an array.
    origLinks[i] = new Array();
    origLinks[i]["href"] = link.href;
    origLinks[i]["innerHTML"] = link.innerHTML;     
    origLinks[i]["protocol"] = link.protocol;
    if (debugMode==true) {
      origLinks[i]["outerHTML"] = link.outerHTML;
      origLinks[i]["innerText"] = link.innerText;
      origLinks[i]["outerText"] = link.outerText; 
      origLinks[i]["host"] = link.host;
    }
    // loop over all exclusions
    for (var j=0; j < urlExclusions.length; j++) {
      if ( findNoCase(urlExclusions[j], link.host) ) {
        // exclusion pattern matched, set matchFound to true, and break loop
        matchFound = true;
        break;
      }
    }
    // if matchFound is false, tweak the link's attributes
    if ( !matchFound && (origLinks[i].protocol != "mailto:" && origLinks[i].protocol != "javascript:") ) {      
      // rewrite the link and provide this page (referrer) &target (destination)
      link.href = dir +disclaimerPage +"?target=" +escape(origLinks[i]["href"]) +"&referrer=" +escape(location.href);
      // IE botches the original innerHTML when href is rewritten. this helps
      // by rewriting the botched innerHTML from the original
      link.innerHTML = origLinks[i]["innerHTML"];
    }
  }
      
  /* need dpDump by "Depressed Press" imported to run this
  Object.dpDump(origLinks[77], "Original Link Properties", false, 1);
  Object.dpDump(document.links[77], "New Link Properties", false, 1);
  Object.dpDump(origLinks[77].protocol != "mailto:", "origLinks[77].protocol != 'mailto:'", false, 1);
  */

      // debug output
  if (debugMode==true) {
    for (var d=0; d < document.links.length; d++) {
      if ( origLinks[d].href != document.links[d].href ) {
        document.write("<span style='color: blue; font-size: xx-small;'>Before (link #" +d +"): Href:" + origLinks[d].href + "</span><br />");
        document.write("<span style='color: red; font-size: xx-small;'>_After (link #" +d +"): Href:" + document.links[d].href + "</span><hr>");
      }
    }
  }
}


/* URL Exclusion Array
** URLs that have this substring should not be disclaimed
** added toofus.org - lcl
*/
urlExclusions = new Array(
  "healthymarriageinfo.org",
  "healthymarriageinfo2.org",
  ".mil",
  ".gov",
  "mailto:",
  "caliber.com",
  "va01librarydev:8080",
  "excludeMeFromDisclaimer",
  "va01webwin07dev",
  "va01webwin07stg",
  "localhost",
  "local",
  "twoofus.org"
);