function EmailForm(form_id){
  this.form = $(form_id);
  this.input = this.form.find('input[type="text"]');
  this.button = this.form.find('div.submit input');
  var _this = this;
  this.form.submit(function(ev){
    _this.submitHandler();
    return false;
  })
}

EmailForm.prototype.submitHandler = function(){
  $('.notice, .error').remove();
  this.button.val('Submitting').attr('disabled','disabled').css('opacity',0.7);
  var data = this.form.serialize();
  var _this = this;
  $.ajax({
    type:"POST",
    url:this.form.attr('action')+'.js',
    data: data,
    dataType: 'json',
    success: function(response){
      _this.button.val('Submit').removeAttr('disabled').css('opacity',1);
      if(response.success){
        var message = $('<div class="notice">Thanks! We&#x27;ll keep you in the loop.</div>');
        _this.input.val('');
        _this.form.before(message);
        setTimeout("$('.notice').fadeOut('slow')",1500);
      } else {
        var message = '';
        for(var i=0; i<response.length; i++){
          message = '<li>'+response[i][0].substring(0,1).toUpperCase() + response[i][0].substring(1)+' '+response[i][1]
        }
        message = '<div class="error"><ul>'+message+'</ul></div>'
        _this.form.before(message);
      }
    }
  })
}

$(function(){
  if($('#new-email-form').length > 0){
    new EmailForm('#new-email-form');
  }
})