function addLoadListener(fn){
  if (typeof window.addEventListener != 'undefined'){
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined'){
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined'){
    window.attachEvent('onload', fn);
  }
  else{
    var oldfn = window.onload;
    if (typeof window.onload != 'function'){
      window.onload = fn;
    }
    else{
      window.onload = function(){
        oldfn();
        fn();
      };
    }
  }
}

addLoadListener(searchbox);


/* SEARCH BOX BEHAVIORS  */
function searchbox(){
  var s = document.getElementById("searchterm");
  s.onfocus = function(){
    modifyText(s, "by search phrase");
  }
  s.onblur  = function(){
    modifyText(s, "by search phrase");
  }
}

function modifyText(field, text){
  if(field.value == text){
    updateField(field, '');
  }
  else if(field.value == ''){
    updateField(field, text);
  }
}

function updateField(field, text){
  field.value = text;
}


document.onclick = function(e){
  var target;
  target = e ? e.target : window.event.srcElement;
  while (target && !/^(a|body)$/i.test(target.nodeName)){
    target = target.parentNode;
  }
  if (target && target.getAttribute('rel')){
    switch(target.rel){
      case 'external':
      var external = window.open(target.href);
      break;
      case 'pop':
      var external = makePopup(target.href, 300, 500, 'scroll');     
      break;
    }
    return external.closed;
  }
  else return true;
}

function makePopup(url, width, height, overflow){
/*
  if (width > 640) { width = 640; }
  if (height > 480) { height = 480; }
*/
  if (overflow == '' || !/^(scroll|resize|both)$/.test(overflow)){
    overflow = 'both';
  }
  var win = window.open(url, '',
      'width=' + width + ',height=' + height
      + ',scrollbars=' + (/^(scroll|both)$/.test(overflow) ? 'yes' : 'no')
      + ',resizable=' + (/^(resize|both)$/.test(overflow) ? 'yes' : 'no')
      + ',status=no,toolbar=no,menubar=no,location=no'
  );
  return win;
}

