 function checkEmail(emailVal) { 
    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return pattern.test(emailVal);
  }
  jQuery(function() {
	jQuery('form.subForm').submit(function() { 
	  // Grab form action
      var formAction = jQuery(this).attr("action");
      var formElem = this;
      // Hacking together id for email field
      // Replace the xxxxx below:
      // If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
      var id = "idkttj";
      var emailVal = jQuery(this).find(".ead").val();
      var subForm = jQuery(this).find(".whichsubform").val();
      
      // Validate email address with regex
      if (!checkEmail(emailVal)) {
        alert("Please enter a valid email address");
        return false;
      }
      
      // Serialize form values to be submitted with POST
      var str = jQuery(this).serialize();
      
      // Add form action to end of serialized data
      // CDATA is used to avoid validation errors
      //<![CDATA[
      var serialized = str + "&action=" + formAction;
      // ]]>
      
      // Submit the form via ajax
      jQuery.ajax({
        url: "/proxy.php",
        type: "POST",
        data: serialized,
        beforeSend: function() {
    	  jQuery(formElem).append("<div class='form_sending'>Please wait while we send your details...</div>"); // If successfully submitted hides the form
      	},
        success: function(data){
          // Server-side validation
          if (data.search(/invalid/i) != -1) {
        	jQuery(".form_sending").remove();
            alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
          }
          else
          {
        	jQuery(formElem).html("<div class='confirmation'>Thank you for subscribing. We have received your details.</div>"); // If successfully submitted hides the form
            // Fire off Google Analytics fake pageview
        	_gaq.push(['_trackPageview','/subscribed?e='+emailVal+'&f='+subForm]);
          }
        }
      });
      return false;
    });
  });
