/*
  Wuit.com
  Copyright (C) Fred Wu
*/

$(document).ready(function() {
  $(document).pngFix();
  
  $("input#user_email").val("Enter your email");
  $("input#user_email").click(function() {
    if ($(this).val() == "Enter your email") {
      $(this).val("")
    }
    $(this).css({color: "#8c8a43"});
  }).keypress(function(e) {
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
      $("a#btn-notify").trigger("click");
      e.preventDefault();
    }
  });
  
  $("a#btn-notify").click(function(e){
    $("div.notify-msg").hide();
    if (isEmail($("input#user_email").val())) {
      $.post($("form#frm-notify").attr("action"), {
        email: $("input#user_email").val()
      }, function(emailRecorded) {
        if (emailRecorded == "True") {
          $("div#notify-success").fadeIn();
        } else if (emailRecorded == "Exists") {
          $("div#notify-warning").fadeIn();
        } else {
          $("div#notify-error").fadeIn();
        }
      });
      return false;
    } else {
      $("div#notify-failed").fadeIn();
    }
    e.preventDefault();
  });
});

function isEmail(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);
}