function addLoadEvent(func) {
  var oldonload = window.onload;
  if( typeof window.onload != 'function' ) {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/* The following function makes
  rel='external' links open in new windows and
  links starting with http:// linking through 
  /redirect.php */
  
function externalLinks() {
  if( !document.getElementsByTagName ) return;
  var anchors = document.getElementsByTagName("a");
  for( var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    string = anchor.getAttribute("href")
    if(string) {
      if( string.match(/^https?:\/{2}/) && ! string.match(/^http:\/{2}(www.scoutbase.org|www.sbuk.org.uk)/) ) {
        string = "/redirect.php/"+string;
        anchor.setAttribute('href',string);
      }
      if( anchor.getAttribute("rel") == "external" ) {
        anchor.target = "_blank";
        var container = null;
/* If the anchor has just one child and it is a text child - then 
   the anchor itself is the containing element */
        if( anchor.childNodes.length == 1 && anchor.firstChild.nodeType == 3 ) {
          container = anchor;
/* If it has at least one (check all nodes are text or text only containing "EM"
   blocks - and use the last one as a container (if the last one is text - we
   need to convert it into a span */
        } else if( anchor.childNodes.length > 0 ) {
          FLAG = 1;
          for( var L=0;L<anchor.childNodes.length;L++) { 
            var T;
            T = anchor.childNodes[L];
            if(!(
              T.nodeType == 3 || 
              (
                T.nodeType == 1 &&
                (T.nodeName == 'EM' || T.nodeName == 'I' || T.nodeName == 'ABBR') &&
                T.childNodes.length == 1 &&
                T.firstChild.nodeType == 3
              )
            )) { 
              FLAG = 0;
            }
          }
          if(FLAG==1) {
            var lastnode = anchor.childNodes[anchor.childNodes.length-1];
            if( lastnode.nodeType == 3 ) {
              container = document.createElement('span');
              container.appendChild( document.createTextNode( lastnode.nodeValue ) );
              anchor.replaceChild(container,lastnode);
            } else {
              container = lastnode;
            }
          } 
        } 
/* add the image in such away that it stays with the last word.

  Try and break the last block up into words... If there is only
  one word in the block - push the image at the end of the block
  and make the block no-wrap. IF there is more than one word -
  grab the last word and put it in a span - and attach image to
  this span.
*/
        if( container ) { 
          var T = container.firstChild.nodeValue.replace(/\s+/,' ');
          T = T.replace(/^ /,'');
          T = T.replace(/ $/,'');
          var im = document.createElement('img');
          im.className = 'external_img';
          var capt = 'page';
          if( anchor.parentNode.nodeName == 'H3' || anchor.parentNode.nodeName == 'H2' ) {
            im.src = '/n/nw-w.gif'
          } else if( string.match(/\.(gif|png|jpeg|jpg)$/) ) {
            im.src = '/n/img.gif'
            capt = 'image';
          } else if( string.match(/\.pdf$/)) {
            im.src = '/n/pdf.gif'
            capt = 'file'
          } else {
            im.src = '/n/nw.gif'
          }
          im.title     = '[this '+capt+' opens in a new window]'
          im.alt       = '[this '+capt+' opens in a new window]'
          var P = T.lastIndexOf(' ');
          if( P > -1 ) {
            container.removeChild(container.firstChild);
            container.appendChild(document.createTextNode( T.substring(0,P) ))
            sp = document.createElement('span');
            sp.className = 'external';
            sp.appendChild(document.createTextNode( T.substring(P,T.length) ))
            sp.appendChild(im); 
            container.appendChild(sp);
          } else {
            container.className = 'external';
            container.appendChild(im);
          }
        }
      }
    }
  }
}

addLoadEvent( externalLinks );
// _uacct = "UA-1030811-1";
// addLoadEvent( urchinTracker );

