function ascendDOM(e, target) {
  while (e.nodeName.toLowerCase() != target && 
      e.nodeName.toLowerCase() != 'html')
    e = e.parentNode;  
  return (e.nodeName.toLowerCase() == 'html') ? null : e;
}

function nextElement(obj) {
  var obj = obj.nextSibling;
  while (obj.nodeType != 1)
    obj = obj.nextSibling;
  return obj;
}

function initSecureMail()
{
  var mail_links = $A(document.getElementsByClassName("securemail"));
  mail_links.each(function(link) { new SecureMail(link) });
}

var SecureMail = Class.create();
SecureMail.prototype = {
  initialize: function(obj)
  {
    this.link = obj;
    this.address = this.link.firstChild.nodeValue.replace('||','@');
    this.link.firstChild.nodeValue = this.address;
    Event.observe(this.link, "click", this.redirect.bindAsEventListener(this), false);
  },
  redirect: function() { location.href = "mailto:"+this.address; }
}

var Cookie = Class.create();
Cookie.prototype = {
  initialize: function() {},
  create: function(name,value,days)
  {
    if (days)
    {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    } else {
      var expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
  },
  read: function(name)
  {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
  },
  erase: function(name)
  {
    createCookie(name,"",-1);
  }
}

ajaxSupport = Try.these(
  function() {return new ActiveXObject('Msxml2.XMLHTTP')},
  function() {return new ActiveXObject('Microsoft.XMLHTTP')},
  function() {return new XMLHttpRequest()}
) || false;

Event.observe(window, 'load', initSecureMail, false);
Event.observe(window, "unload", Event.unloadCache, false);
