function addEvent(elm, evType, fn, useCapture) {
  // cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
  // By Scott Andrew
  if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent) {
    var r = elm.attachEvent('on' + evType, fn);
    return r;
  } else {
    elm['on' + evType] = fn;
    return false;
  }
}
function makeFocusFuncForId(id) {
  return function() {
    var elm = document.getElementById(id);
    if (elm) elm.focus();
  }
}
function filter_tags(phrase) {
  var tagsContainer = document.getElementById("tags");
  var links = tagsContainer.getElementsByTagName("a");
  for (var x = 0, len = links.length; x < len; x++) {
    var link = links[x];
    if (link.className == "tag") {
      var tagName = link.childNodes.item(0).data.toLowerCase();
      if (tagName.search(phrase) == -1) {
        link.parentNode.style.display='none';
      } else {
        link.parentNode.style.display='';
      }
    }
  }
}
function watchForFilterChange() {
  var inputField = document.getElementById('filter_input');
  if (inputField) {
    inputFieldValue = inputField.value.toLowerCase();
  }
  // define a recursive function that filters if a change has occurred
  // and calls itself somewhat later.
  function checkAndWait() {
    inputField = document.getElementById('filter_input');
    if (inputField) {
      var v = inputField.value.toLowerCase();
      if (v != inputFieldValue) {
        inputFieldValue = v;
        filter_tags(inputFieldValue);
      }
    }
    setTimeout(checkAndWait,100);
  }
  checkAndWait();
}
function pause(ms) {
  var exitTime = new Date().getTime() + ms;
  while(true) {
    if(new Date().getTime() > exitTime) return;
  }
}
// not currently using this, as it is subjectively faster (on pages with tons 
// of options) to poll regularly and update if necessary, rather
// than on every key up event. E.g., when a user rapidly types a four-letter
// word, we don't need to update after the second and third key presses if
// we've just completed the first filter and already have the next three 
// letters).
function tag_search_key_up() {
  var inputField = document.getElementById('filter_input');
  if (inputField) {
    filter_tags(inputField.value.toLowerCase());
  }
}
