cookieparts = document.cookie.split("; ")
mail = "";

for (i = 0; i < cookieparts.length; ++i)
  if (cookieparts[i].substr(0,11) == "bugaddress=")
    mail = cookieparts[i].slice(11)

// date - number: milliseconds
// date + number: string

function set()
{
  document.getElementById("address").innerHTML = mail || "Address not set";
  buglist = "http://bugzilla.mozilla.org/buglist.cgi?";
  da = new Date((new Date()) - 1000*60*60*24*7);

  if (mail)
  {
    // set/renew cookie.
    document.cookie = "bugaddress=" + mail + "; expires=" + (new Date(new Date() - (-1000*60*60*24*100))).toGMTString();

    // set links
    document.getElementById("reported").href = buglist + "email1=" + mail + "&emailtype1=exact&emailreporter1=1&order=bugs.bug_id%20desc";
    document.getElementById("own").href = buglist + "email1=" + mail + "&emailtype1=exact&emailassigned_to1=1&resolution=---&order=bugs.bug_id"
    document.getElementById("changed").href = buglist + "field0-0-0=longdesc&type0-0-0=changedby&value0-0-0=" + mail + "&field0-1-0=longdesc&type0-1-0=changedafter&value0-1-0=" + da.getFullYear() + "/" + (da.getMonth() + 1) + "/" + da.getDate();

    // set comment-by field in form
    if (!document.commentsearch["value0-0-0"].value)
      document.commentsearch["value0-0-0"].value = mail;
  }

}
set();
document.getElementById("unhide").style.display = "";

function change(link)
{
  temp = prompt("Bugzilla e-mail address:", mail);
  if (temp == null) return; // cancel
  mail = temp;
  set();
  if (!mail)
    // remove cookie
    document.cookie = "bugaddress=" + "; expires=" + (new Date(new Date() - 1000*60*60*24*100)).toGMTString();

  if (mail && link)
    location = link.href;
}

function ensure()
{
  if (!mail)
    change();
  return !!mail;
}
