$(function()
	{
		$("#newsletter input:submit").click(function() {	
			
			// First, disable the form from submitting
			$('form#newsletter').submit(function() { return false; });
			
			// Grab form action
			formAction = $("form#newsletter").attr("action");
			
			// 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
			id = "duftu";
			emailId = id + "-" + id;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				$(".newsletter-email").animate({ color: "red" }, 100);
  				$(".newsletter-email").animate({ color: "#ccc" }, 3000);
				return;
			}
			
			// Serialize form values to be submitted with POST
			var str = $("form#newsletter").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "http://sixelevenbicycleco.com/proxy.php",
				type: "POST",
				data: final,
				success: function(data){
					//Check to make sure that the email was accepted
					if (data.search(/invalid/i) != -1) {
						//alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
						$(".newsletter-email").animate({ color: "red" }, 100);
	  				$(".newsletter-email").animate({ color: "#ccc" }, 3000);
					}
					else
					{
						$("#form").hide(); // If successfully submitted hides the form
						$("#thanks").fadeIn("slow");  // Shows "Thanks for subscribing" div
					}
				}
			});
		});
	});
	function checkEmail(email)
	{	
		var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	}
	
	

$(document).ready(function() { 
//Contact form
    var contactOptions = {
	    beforeSubmit:  contactShowRequest, 
        success:       contactShowResponse  // post-submit callback 
    }; 
 
    $('#contact_form').ajaxForm(contactOptions); 
    
    //order form
    var orderOptions = {
	    beforeSubmit:  orderShowRequest, 
        success:       orderShowResponse  // post-submit callback 
    }; 
 
    $('#order_form').ajaxForm(orderOptions); 
}); 


//Contact Form
function contactShowRequest(formData, jqForm, options)  { 
    $("#contact_submit").hide(); // If successfully submitted hides the form
	$("#contact-wait").show(); // If successfully submitted hides the form
}

function contactShowResponse(responseText, statusText, xhr, $form)  { 
    $("#contact_form").hide(); // If successfully submitted hides the form
    $(".contact-wait").hide();  // Shows "Thanks for subscribing" div
	$("#contact-thanks").fadeIn("slow");  // Shows "Thanks for subscribing" div
}

//Order Form
function orderShowRequest(formData, jqForm, options)  { 
    $("#order_submit").hide(); // If successfully submitted hides the form
	$("#order-wait").show(); // If successfully submitted hides the form
}

function orderShowResponse(responseText, statusText, xhr, $form)  { 
    $("#order_form").hide(); // If successfully submitted hides the form
    $("#contact-wait").hide();  // Shows "Thanks for subscribing" div
	$("#order-thanks").fadeIn("slow");  // Shows "Thanks for subscribing" div
}