$(document).ready(function() {
	

	// Hide Loading and Output Elements
	$("#loader").hide();
	$("#filter").hide();	
	
	// Remove Wordpress paragraph and line-break tags from the form
	$('#quick-form br').remove();
	
		
	// Setup Form Submit Options
	var options = {
		beforeSubmit: function() {
		$('#loader').fadeIn(50);
		},
		dataType: 'json',
		url: 'http://www.purposive.com/form2mail.php',
		success: processJson
		}
	
	
	// Form Submit Function
	function processJson(data) {
		var status = data.status;
		$('#loader').fadeOut(300);		
				
		// If Fields are Empty
		if(status == 1) {$('#output').hide().empty().fadeIn(150).append("<p>You must fill in all required fields.</p>");}
		
		// If Name is Wrong
		if(status == 2) {$('#output').hide().empty().fadeIn(150).append("<p>Your name must be between 3 and 30 characters.</p>");}								
		
		// If Spam Bot
		if(status == 3) {$('#output').hide().empty().fadeIn(150).append("<p>Leave the spam field blank please.</p>");}
		
		// If Fields are Empty
		if(status == 4) {$('#output').hide().empty().fadeIn(150).append("<p>Your email address was not valid.</p>");}
		
		// If Probems
		if(status == 6) {$('#output').hide().empty().fadeIn(150).append("<p>There was a problem sending the form.</p>");}
		
		// If Message Sent
		if(status == 5) {
						
			$('#quick-form #output').hide(50);								
			$('#quick-form form').slideUp(600);	
			$('#quick-form #thankyou').hide().append("<h6>Thank You.</h6><p>Your message has been sent.</p>").slideDown(600);														
			$('#loader').fadeOut(500);
			return false;						
		
		}// End of Status 5
						
	} 
	// End of processJson
	

	$('#quick-form form').ajaxForm(options);


}); // End jQuery


// Ajax Mailing List Box

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);
	}
	$(function()
	{
		$("#subForm input:submit").click(function() {	
			
			// First, disable the form from submitting
			$('form#subForm').submit(function() { return false; });
			
			// Grab form action
			var formAction = $("form#subForm").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
			var id = "hugm";
			var emailId = id + "-" + id;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				alert("Please enter a valid email address");
				return;
			}
			
			// Serialize form values to be submitted with POST
			var str = $("form#subForm").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
			$.ajax({
				url: "http://www.purposive.com/lib/proxy.php",
				type: "POST",
				data: serialized,
				success: function(data){
					// Server-side validation
					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.');
					}
					else
					{
						$("#newsletter").slideUp(function(){
							$("#newsletter h2").text("Confirmation");
							$("#newsletter p").text("Success! You have been added to Purposive's mailing list.");
							$("#newsletter form").hide();
							$("#newsletter").slideDown();
						});
					}
				}
			});
		});
	});