$(document).ready(function () {
  $('#email_mini_btn').click(function(e){
    e.preventDefault();
    $('#email_mini').submit();
  });
  
  $("#email,#email_mini").each(function () {
    $(this).find("input[type='text']").focus(function () {
      if ($(this).val()=='Email Address' || $(this).val()=='ZIP Code' || $(this).val()=='Full Name') {
        $(this).val('');
        $(this).css('color','#000000');
      }
    });
    $(this).find("input[name='Name']").blur(function () {
      if ($(this).val().length==0) {
        $(this).css('color','#7d7d7d');
        $(this).val('Full Name');
      }
    });
    $(this).find("input[name='Email Address']").blur(function () {
      if ($(this).val().length==0) {
        $(this).css('color','#7d7d7d');
        $(this).val('Email Address');
      }
    });
    $(this).find("input[name='Zip']").blur(function () {
      if ($(this).val().length==0) {
        $(this).css('color','#7d7d7d');
        $(this).val('ZIP Code');
      }
    });
		
    // Validation
    $(this).submit(function () {
      var err = '';
      // check for name
      if ($(this).find("input[name='Name']").length > 0) {
        err = checkName($(this).find("input[name='Name']").val());
        if (err == '') {
        /* continue on */
        } else {
          alert(err);
          return false;
        }
      }
            
      // check email
      err = checkEmail($(this).find("input[name='Email Address']").val());
      if (err == '') {
        // check zip code
        err = checkZip($(this).find("input[name='Zip']").val());
        if (err == '') {
          return true;
        } else {
          alert(err);
          return false;
        }
      } else {
        alert(err);
        return false;
      }
    });
  });
});

function checkName (strng) {
  var error="";
  strng=$.trim(strng);
  if (strng == "" || strng=="Full Name") {
    error = "You didn't enter your Full Name.\n";
  }
  return error;
}

function checkEmail (strng) {
  var error="";
  if (strng == "") {
    error = "You didn't enter an Email Address.\n";
  } else {
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) {
      error = "Please enter a valid Email Address.\n";
    } else {
      //test email for illegal characters
      var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
      if (strng.match(illegalChars)) {
        error = "The Email Address contains illegal characters.\n";
      }
    }
  }
  return error;
}

function checkZip (strng) {
  var error="";
  if (strng == "" || strng == "Enter Your Zip Code") {
    error = "Please enter a Zip Code.\n";
  } else {
    var emailFilter=/([0-9]*)/;	    
    //test zip for illegal characters
    var illegalChars= /^([0-9]*)$/
    if (!strng.match(illegalChars)) {
      error = "Please enter a numeric Zip Code.\n";
    }
  }
  return error;
}
