function strExtLinks() {
  if (!document.getElementsByTagName) return;
  if (document.createElementNS) {
    var XHTMLNS = 'http://www.w3.org/1999/xhtml';

    var startTextNode = document.createTextNode('Open external links (marked with ');
    var endTextNode = document.createTextNode(') in new window');

    var extImg = document.createElementNS(XHTMLNS,'img');
    extImg.src = '/~robby/external.gif';
    extImg.title = 'Links with this graphic point to external sites';
    extImg.alt = 'External link graphic';

    var extLabel = document.createElementNS(XHTMLNS,'label');
    extLabel.setAttribute('for','xlinks');
    extLabel.appendChild(startTextNode);
    extLabel.appendChild(extImg);
    extLabel.appendChild(endTextNode);

    var textNode = document.createTextNode(' ');
    var extInput = document.createElementNS(XHTMLNS,'input');
    extInput.id = 'xlinks';
    extInput.type = 'checkbox';
    if (document.cookie.indexOf("newwin=true")!=-1) extInput.checked = 'checked';
    extInput.onclick = 'setExtLinks(this.checked);';

    var extLinks = document.getElementById('extLinks');
    extLinks.appendChild(extInput);
    extLinks.appendChild(textNode);
    extLinks.appendChild(extLabel);
  } else {
    strLink = '<input type="checkbox" id="xlinks"';
    if (document.cookie.indexOf("newwin=true")!=-1) strLink += ' checked="checked"';
    strLink += '  onclick="setExtLinks(this.checked);" /> <label for="xlinks">Open external links (marked with <img src="/~robby/external.gif" title="Links with this graphic point to external sites" alt="External link graphic" />) in new window</label>';
    document.write(strLink);
  }
  var extLinks = document.getElementById('extLinks');
  extLinks.style.marginBottom = '1.0em';
}

function setExtLinks(xlink) {
  if (!document.getElementsByTagName) return;
  var now=new Date();
  now.setTime(now.getTime()+(14*24*60*60*1000)); /* set expiration date to 14 days */
  document.cookie=xlink?"newwin=true; expires="+now.toGMTString()+";":"newwin=false;";
  externalLinks();
}

function externalLinks() {
  if (!document.getElementsByTagName) return;
  if (document.cookie.length > 0) {
    if (document.cookie.indexOf("newwin=true")!=-1) {
      newwin = true;
    } else {
      newwin = false;
    }
  }

  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
      anchor.target = newwin?"_blank":"_self";
      if (anchor.title != '') {
        anchor.title = 'External link: ' + anchor.title;
      } else {
        anchor.title = 'External link';
      }
      anchor.style.background = 'transparent url(/~robby/external.gif) no-repeat bottom right';
      anchor.style.paddingRight = '13px';
      anchor.style.whiteSpace = 'nowrap';
    }
  }
}
window.onload = externalLinks;