// pexcard.js
// Author: PRL


window.addEventListener ? window.addEventListener("load", initPage, false) : window.attachEvent("onload", initPage);

var EMAIL_INSTR = "Enter email address";


function initPage()
{
  var emailField = $("INPUT#emailfield");
  if (emailField.attr("value") == "" || emailField.attr("value") == undefined)
  {
    emailField.bind("focus", inputFocus);
    emailField.addClass("instrmode");
    emailField.attr("value", EMAIL_INSTR);
  }

  $("#emailform").ajaxForm({
    target: "#msg",
    beforeSubmit: fieldCheck,
    success: hideForm
  })
}


function inputFocus()
{
  var emailField = $("INPUT#emailfield");
  if (emailField.hasClass("instrmode"))
  {
     emailField.removeClass("instrmode");
     emailField.attr("value", "");
  }
}


function fieldCheck(fieldData, jForm, jOpts)
{
  var emailField = $("INPUT#emailfield");
  var emailValue = emailField.attr("value");

  // clear the field if it just had the instructions in it
  if (emailValue == EMAIL_INSTR)
  {
    emailValue = "";
    emailField.attr("value", "");
  }
  if (emailValue == undefined || emailValue == "")
  {
    alert("You have not yet entered an email address");
    return false;
  }
  return true;
}


function hideForm()
{
  $("DIV#emailoptin").css("display", "none");
  $("DIV#msgbox").css("display", "block");
}


function showForm()
{
  $("DIV#msgbox").css("display", "none");
  $("DIV#emailoptin").css("display", "block");
}


// Obfuscate mailto links

function  email(user, domain)
{
   var addr = user + '@' + domain;
   document.write('<a href="mailto:'+addr+'">');
   document.write(addr);
   document.write('</a>');
}
