﻿// exitPage.js
//
// Christopher Torgalson for Andornot Consulting 2006
//
// How to use:
// 	1. Set configuration vars below (in almost any case, this should be all the configuration need by this script)
//	2. Call this file--assuming it is still named exitPage.js--from an html page with the following code (be sure to
//		adapt 'path/to/exitPage.js' to the locations of the files on your server!):
//	
//			<script type="text/javascript" src="path/to/exitPage.js"></script>

/*----------------------------------------------------------------------
	Configuration vars:
	NOTE: ALL VARS SHOULD HAVE A VALUE, EVEN IF IT'S AN EMPTY STRING!
----------------------------------------------------------------------*/

// What domain does this script reside on?
var homeDomain = 'grn-hiv-du.org';
// Should links contain 'target="_blank"' to open the link in a new window (true/false):
var externalTarget = true;
// Should the url of external links be displayed in the browser's status bar (true/false) 
// [note: some browsers do not allow scripts to change the status bar text!]:
var urlInStatus = false;
// What should the tooltip say when an external script is moused over? 
// [the url will be appended to the end of this text]:
var linkTitleMessage = 'Click here to go to this website:';
// Add this class to the internal links:
var localLinkClass = 'stay';
// Add this class to the external links:
var externalLinkClass = 'go';
// What should the popup dialogue box say?
// [the url will be appended to the end of this text]:
var jsDialogueMessage = 'You are about to leave this site for: ';
// How long is the longest url we're willing to display?
var charsInTamedUrl = 60;

/*----------------------------------------------------------------------
	End configuration vars
----------------------------------------------------------------------*/

// This function--which is executed on an onclick event on a link, does the follwing:
//	1. Pops up a dialogue box asking the user if s/he would like to leave the site,
//	2. If the user DOES wish to leave, sets the value of the href attribute back to the value of the 'rel' attribute
//	3. If the user DOES NOT wish to leave, returns false.
//
function leaveThisSite() {
	var tamedUrl = URLTamer(this.rel,charsInTamedUrl);
	var response = confirm(jsDialogueMessage + '\n' + tamedUrl);
	if(response) {
		this.href = this.rel;
		return true;
	} else {
		return false;
	}
}

function displayUrlInStatus() {
	window.status = this.rel;
	return true;
}

function removeUrlInStatus() {
	window.status = '';
	return true;
}

// This function--which is executed on page load--loops through all the links in the document.
// While it loops, it performs the following tasks:
// 	1. Check the href attribute of the current link for the current domain,
//	2. If the current domain IS NOT in the link,
//		a. Define a function call (to 'leaveThisSite') for the onclick event for that link,
//		b. Set the 'rel' attribute of the link to the value of the 'href' attribute,
//		c. Set the value of the 'href' attribute to '#',
//		d. Check prefs for and set the following attributes on the external links:
//			i. title
//			ii. class
//			iii. target
//		e. Check prefs for and set text in status bar 
//	3. If the current domain IS in the link, 
//		a. Change check prefs for and set the value of the 'class' attribute of the link,
//		b. [optional] define a function call for the onclick event for the link (not implemented)
//
function checkDomain() {
	var linkList = document.getElementsByTagName('a');
	for(var i=0;i<linkList.length;i++) {
		var currentHref = linkList[i].href;
		var currentDomainSearchResult = currentHref.search(homeDomain);
		var javascriptSearchResult = currentHref.search('javascript');
		var mailtoSearchResult = currentHref.search('mailto');
		
		if((currentDomainSearchResult == -1)  && (mailtoSearchResult == -1) && (javascriptSearchResult == -1)) {
			linkList[i].rel = linkList[i].href;
			linkList[i].href = '#';
			linkList[i].onclick = leaveThisSite;
			
			// Decide whether to show a link title:
			//
			if (linkTitleMessage) {
				linkList[i].title = linkTitleMessage + ' ' + linkList[i].rel;
			}
			
			// Decide whether to assign a new css class to the external links:
			//
			if (externalLinkClass) {
				linkList[i].className = externalLinkClass;
			}
			
			// Decide whether to show url in status bar:
			//
			if (urlInStatus) {
				linkList[i].onmouseover = displayUrlInStatus;
				linkList[i].onmouseout = removeUrlInStatus;
			}
			
			// Decide whether to open external links in a new window:
			//
			if (externalTarget) {
				linkList[i].target = "_blank";
			}
			
		} else if (localLinkClass) {
			linkList[i].className = localLinkClass;
		}
	}
}

// Here, we just define a function call on page load. If there are other .js functions called on page load,
// just add them all into a single function that gets called on page load, or add the function to an 'onload'
// attribute in the page's body element.
//
window.onload = checkDomain;

/*
Shortens a single or multiple URL strings to maximum number of characters.
Peter Tyrrell, Andornot Consulting, May 2003

Slightly adapted for use with exitPage.js (above) plus minor reformatting by Christopher Torgalson. March 2006
*/

function URLTamer(url,maxlen) {
  var mid;
  var takeaway;
  var strfirsthalf;
  var strbefore;
  var strafter;
  var strfinal = "";
  var ellipsis = "...";
  var splitchar = "|split|";
  var arrurl = new Array();
  var i;

  // split url string if splitchar is found
  arrurl = url.split(splitchar);
  
  // loop through the array, building hyperlink for each member
  for (i=0; i<arrurl.length; i++) {
  	if (arrurl[i].length > maxlen) {	
			mid = Math.round(arrurl[i].length / 2);
			takeaway = Math.round((arrurl[i].length - maxlen) / 2);
			takeaway += ellipsis.length / 2;
			takeaway = Math.round(takeaway);
				
			strbefore = arrurl[i].substring(0, mid);
			strbefore = strbefore.substring(0, strbefore.length + 1 - takeaway);
						
			strafter = arrurl[i].substring(mid, arrurl[i].length + 1);
			strafter = strafter.substring(takeaway, strafter.length + 1);
				
			// [BH] Since, for our purposes, we don't need the markup, but only the text, comment out the 
			// url-building portions, and return only the text:
			
			//strfinal += "<a target=\"_blank\" href=\"" + arrurl[i] + "\">"
			//		+ strbefore + ellipsis + strafter
			//		+ "</a><br />";
			strfinal = strbefore + ellipsis + strafter;
		} else {
			// [BH] Again, we don't need html, only text:
			
			//strfinal += "<a target=\"_blank\" href=\"" + arrurl[i] + "\">"
			//		+ arrurl[i]
			//		+ "</a><br />";
			strfinal = arrurl[i];
		}
  }
  return strfinal;
}