/* externalLinks.js
(requires behavior.js to be loaded already)
*/

/* Behavior rules */
var externalLinkRules = {

  /* Behavior rule for the <A> element */
  'a' : function(element) {

    /* List of strings that start an internal link.  Note that relative
       links (that do not start with http) do not have to be listed here.
    */z
    var internalLinkRegexpArray =
    [
     new RegExp('^https?://[^/]*(cityofalamosa\.org|alamosarec\.org|alamosalibrary\.org)', 'i')
     ];

    /* Get the HREF for this link and convert to lower case */
    var url = element.href.toLowerCase();

    /* Skip if a relative URL (doesn't start with http) */
    if (url.substr(0, 4) != 'http') { return; }

    /* Loop through all the internal link regular expressions */
    for (var i=0; i<internalLinkRegexpArray.length; i++) {
      
      /* Check for a match with this regular expression */
      if (url.match(internalLinkRegexpArray[i])) {

	/* Got a match, so it's an internal link and we're done */
	return;

      }
    }
    
    /* If we get here it's an external link */
    /* Add an onclick event to the link */
    element.onclick = function(){

      return confirm('You are now leaving an official City of Alamosa website.\n\nWhen you exit this site, you are no longer protected by our privacy policy.\n\nThe City of Alamosa does not endorse nor is responsible for the content provided on linked sites. ');

    }

  }

};

Behaviour.register(externalLinkRules);

