window.onload = initialise;

function initialise()
{
  relExternal();
  externalLinks();
}

function externalLinks()
{
  if (!document.getElementsByTagName) return;
  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")
    {
      var onclicktext = "window.open('"+anchor.getAttribute("href")+"'); return false"; 
      anchor.setAttribute("onclick", onclicktext);
    }
  }
}


function relExternal()
{
  if(!document.getElementsByTagName) return;
  var links = document.getElementsByTagName("a");
  var domain = window.location.hostname;
  for (var i = 0; i < links.length; i++)
  {
    var link = links[i];
    var href = link.getAttribute("href");
    var linkhost = getHost(href);
    if(linkhost != domain && href != "#")
    {
      link.setAttribute("rel", "external");
    }
  }
}

function getHost(href)
{
  var parts = href.split("/");
  return parts[2];

}
