$(document).ready(function() {
	// validate signup form on keyup and submit
	var validator = $("#newsletterform").validate({
		rules: {
			subscribe_email: {
				required: true,
				email: true,
				remote: {
				url: "http://makeithappen.flyingstartonline.com/public/home/check_subscribe_email",
				type: "post"
				}	
				
			},
			subscribe_postcode: {
				required: true,
				minlength: 5
			}
			
		},
		
		messages: {
				subscribe_postcode: {
				required: "Provide a post code"
				}
				
			},
		
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function() {
			$.post("http://makeithappen.flyingstartonline.com/public/home/subscribe", $("#newsletterform").serialize());
			 $('#thankyou').fadeOut('slow');   
			 $('#newsletterdone').fadeIn('slow');
		},
		

		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});
	
	// propose username by combining first- and lastname
	$("#username").focus(function() {
		var firstname = $("#firstname").val();
		var lastname = $("#lastname").val();
		if(firstname && lastname && !this.value) {
			this.value = firstname + "." + lastname;
		}
	});

});