/* Functions to fixup URLs of the form
   "/mailto/?u=USER&amp;d=DOMAIN"
   turning them into
   "mailto:USER@DOMAIN"

   Usage: Give the body element the attribute onload="mailto_fixup()".

   Note that before this script runs, the browser will already have converted
   &amp; to &, and it will have made relative URLs absolute.
   So the script will see "http://www.math.ntnu.no/mailto/?u=USER&d=DOMAIN".
   Unfortunately, that means our domain must be hard coded in the script.
   (It is in document.URL, but before relying on that we should also check
    for a base element inside the header.  Urgh.)

   For added aid in obfuscating email addresses, periods may be replaced
   by commas.  This script will reinstate the periods.
*/

function mailto_fixURL (anchor) {
  var url = anchor.href;
  if (url.search(/^http:\/\/www.math.ntnu.no\/mailto\/\?/)>=0) {
    url = url.replace(/^http:\/\/www.math.ntnu.no\/mailto\/\?/,"mailto:");
    url = url.replace(/u=(.+)&d=(.+)/,"$1@$2");
    url = url.replace(/d=(.+)&u=(.+)/,"$2@$1");
    url = url.replace(/,/g,".");
    anchor.href = url;
  }
}

function mailto_fixup () {
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
    mailto_fixURL(anchors[i]); }}

