$(function() {
	// Hide any error messages
	$('.error').hide();
//	$('input.text-input').css({backgroundColor:"#FFFFFF"});
//	$('input.text-input').focus(function(){
//		$(this).css({backgroundColor:"#FFDDAA"});
//	});
//	$('input.text-input').blur(function(){
//		$(this).css({backgroundColor:"#FFFFFF"});
//	});

	// Check for the send button being pressed.
	$(".sendButton").click(function() {
		// Hide any error messages
		$('.error').hide();

		// Get the email addressed entered.
		var email = $("input#emailField").val();

		// Set the regular expression to validate the email address and test it.
        var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
		var emailtest = reEmail.test(email);
		//alert ("emailtest = " + emailtest);

		// If the email address is empty, the default help text or otherwise
		// invalid, show the error message and select the email box for edit.
		if (email == "" || email == "Enter your email" || !emailtest) {
			$("label#email_error").show();
			$("input#emailField").focus();
			return false;
		}

		// Get the name entered.
		var name = $("input#nameField").val();

		// Get the length of the name string.
		var namelength = name.length;

		// If the name is empty, the default help text or less than 2 characters
		// long, show the error message and select the name box for edit.
		if (name == "" || name == "Enter your name" || namelength < 2) {
			$("label#name_error").show();
			$("input#nameField").focus();
			return false;
		}

		// Get the message entered.
		var message = $("textarea#messageField").val();

		// Get the length of the name string.
		var messagelength = message.length;

		// If the name is empty, the default help text or less than 2 characters
		// long, show the error message and select the name box for edit.
		if (message == "" || message == "Enter your message" || messagelength < 2) {
			$("label#message_error").show();
			$("textarea#messageField").focus();
			return false;
		}

		// Construct the overall data string to be sent.
		var dataString = 'name='+ name + '&email=' + email + '&message=' + message;
		//alert (dataString);return false;

		// Do the ajax process to send the message now that we're happpy.
		$.ajax({
			type: "POST",
			url: "http://www.tanglefootcloggers.co.uk/wp-content/themes/tanglefoot/bin/process.php",
			data: dataString,
			success: function() {
			//alert ("Success: " + dataString);
			$("label#message_sent").fadeIn("slow").fadeTo(1500, 1).fadeTo("slow", 0);
			$("input#emailField").val("Enter your email");
			$("input#nameField").val("Enter your name");
			$("textarea#messageField").val("Enter your message");
			},
			error: function() {
			alert ("Fail: " + dataString);
			$("label#message_notsent").fadeIn("slow");
			}
		});
		return false;
	});
});
//runOnLoad(function(){
//  $("input#name").select().focus();
//});
