$(document).ready(function(){

	$("#submit").click(function(){
	

	
		$(".error").hide();
		
		var hasError = false;
		var emailWrong = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var emailVal = $("#email").val();
		if(emailVal == '') {
			$("#email").after('<span class="error"></span>');
			hasError = true;
		} else if(!emailReg.test(emailVal)) {
			$("#email").after('<span class="error"></span>');
			hasError = true;
			emailWrong = true;
		}

		var nameVal = $("#name").val();
		if(nameVal == '') {
			$("#name").after('<span class="error"></span>');
			hasError = true;
		}
		
		
		var companyVal = $('#company').val();
		var phoneVal = $('#phone').val();
		var commentsVal = $('#comments').val();
	
		if ($('#subscribe').is(':checked')) {
			var subVal = "I would like to subscribe to updates";
		} else {
			var subVal = "Please do not send me updates";
		}
		
		var respVal = $('#reservep').val();
		
		if ($('#reserve').is(':checked')) {
			var resVal = "Please reserve "+respVal+" places for me at the launch event";
		} else {
			var resVal = "Don't worry about reserving a place at the launch event for me";
		}
		
		if (hasError == true) {
			alert("Please fill out all the fields marked with a cross.");
		} else if (emailWrong == true) {
			alert("Please make sure you have entered a correct email address.");
		}
		
		if(hasError == false) {
			$(this).hide();
			$("#submit").append('<img src="img/loader.gif" alt="Loading" id="loading" />');
			
			$.post("sendemailajax.php",
			   { name: nameVal, email: emailVal, company: companyVal, phone: phoneVal, comments: commentsVal, subscribe: subVal, reserve: resVal },
			   	function(data){
					$("#submit").slideUp("normal", function() {
						$("#submit").before('<img src="img/ok.png" class="sent" /><span class="sent">Thank you!</span>');
					});
				}
			);
		
		}

		return false;

	});
	
});