// based on http://www.sitepoint.com/article/standards-compliant-world
// modified to set link targets selectively for popup windows

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")) {
      if ((anchor.getAttribute("rel") == "external") || 
          (anchor.getAttribute("rel") == "popup")) {
        anchor.target = "_blank";
      }
    }
  }
}
window.onload = externalLinks;

// Hide email address from prying spambots.
// Use text = 'DEFAULT' to for text equal to email address.
// Use subject = 'NONE' to not pre-set the Subject: line.

function emLink(text, subject) {
  var foo = "Z2HDRZG3HTZAZVMJS";
  var bar = unescape("%29%5D%241%263%28%5D%08752%3Ex.%25%3E");
  var baz = foo.length;
  var gleep = "";
  for (var erp = 0; erp < baz; erp++) {
    gleep += String.fromCharCode(foo.charCodeAt(erp)^bar.charCodeAt(erp));
  }

  if (text == "DEFAULT") {
    text = gleep;
  }

  if (subject == "NONE") {
    subject = "";
  } else {
    subject = "?subject=" + subject;
  }

  document.write('<a class=\"contact-us\" href=\"mailto:' + gleep
		+ subject + '\">' + text + '</a>');
}

// "nice" popup window group (only opens one, links open in original window)
// rphair Tue Oct 26 19:49:53 GMTST 2004

var popup = null;

function closePopup() {
  if (popup != null) {
    if (!popup.closed) popup.close();
  }
}

function openPopup(anchor, width, height) {
  closePopup();
  popup = window.open(anchor.href, 'popup', 'width=' + width +
    ',height=' + height + ',resizable=yes,scrollbars=yes' +
    ',location=no,status=no,menubar=no,toolbar=no');
  popup.focus();
  return false;
}

// Check for a valid email address
// regexp: http://javascript.internet.com/forms/email-validation---basic.html
// changed maximum TLD length to 6 (for .info and .museum)
// rphair Wed Oct 18 18:51:15 GMTDT 2006

function validEmail(form) {
  var entry = form.email.value;

  entry = entry.replace(/^ +/, "");
  entry = entry.replace(/ +$/, "");

  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/.test(entry) || entry == "") {
    return true;
  }
  alert("Invalid email: \"" + entry + "\": please retry");
  form.email.focus();
  return false;
}
