var defaultText="Email Address";

function isValidEmailAddress(emailAddress) {
  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  return pattern.test(emailAddress);
}

// Process form send requests
function sendForm() {
  // If the email field is empty or the default hint, drop out
  if (
    (document.getElementById('user_email').value=="")
    ||
    (document.getElementById('user_email').value==defaultText)
    ||
    (!isValidEmailAddress(document.getElementById('user_email').value))
  ) {
    alert("Please enter a valid email address.");
    return false;
  }

  // Send the request via AJAX
  $.ajax({
    type: "POST",
    data: $('#user_email').serialize(),
    success: function() {
      $('#interaction_request').hide();
      $('#interaction_response').fadeIn();
    }
  });
}

// Set up the email hint
if (document.getElementById('user_email')) {
  document.getElementById('user_email').onfocus=function() {
    if (this.value==defaultText) {
      this.value="";
    }
  }
  
  document.getElementById('user_email').onblur=function() {
    if (this.value=="") {
      this.value=defaultText;
    }
  }
}

// Set up the request_form submit binding
if (document.getElementById('request_form')) {
  document.getElementById('request_form').onsubmit=function() {
    sendForm();
    return false;
  }
}
