// Script written by Sky Apperley (skyapperley.co.uk) December 2011

$(document).ready(function() {

 // =========== VOODOO  =========== 
	
	var allInputs = $('input'), textArea = $('textarea'), messageBox = $('#message'), nameInput = $('#name'), emailInput = $('#email');
	
	$('textarea').css('color', '#7C7C7C');
	$('#name, #email').focus(function() {
		if ($(this).val() == $(this).attr("title")) { $(this).removeAttr("value").css('color', '#000').removeClass('redinput');}
		$(this).removeClass('redinput');
	});
	textArea.focus(function() {
		if ($(this).val() == $(this).attr("title")) { $(this).removeAttr("value").css('color', '#000').removeClass('redinput').val('');}
		$(this).removeClass('redinput');
	});
	allInputs.css('color', '#7C7C7C').blur(function() {
		$(this).removeClass('redinput');
		$('.formoutput').fadeOut('slow');
	});
	textArea.blur(function() {
		$(this).removeClass('redinput');
		$('.formoutput').fadeOut('slow');
	});
	
	var displayError = function(){
		$('.formoutput').hide().html("<p>Oops, please fill out the fields marked by red.</p>").fadeIn('slow');
	}

	$('.submit').click(function() { 
		// Simple validation before sending
		var validated = true;
		if (nameInput.val() == "" || nameInput.val() == "name" ) {
			nameInput.addClass('redinput').css('color', '#fff');
			validated = false;
			displayError();
		};
		if (emailInput.val() == "" || emailInput.val() == "email" ) {
			emailInput.addClass('redinput').css('color', '#fff');
			validated = false;
			displayError();
		};
		if (messageBox.val() == "" || messageBox.val() == "message" ) {
			messageBox.addClass('redinput').css('color', '#fff');
			validated = false;
			displayError();
		};
		// code for captcha for when I get time
/*		if ($('#security_code').val() == "" || $('#security_code').val() == "Enter Security Code*" ) {
			$('#security_code').addClass('redinput');
			validated = false;
			displayError()
		};*/
		if (validated) {
			$.post('php/post_to_sky.php', {
				'send': 'OK',
				'name': nameInput.val(), 
				'email': emailInput.val(), 
				'message': messageBox.val(), 
				'security_code': $('#security_code').val() 
			}, function(data) { 
				//$('.formoutput').hide().html(data).fadeIn('slow'); // for testing only
				if (data) {
					$('#name, #email, #message, .submit').hide('slow');
					$('.formoutput').hide().html("<h2>Thank you for your message. I'll contact you shortly.</h2>").fadeIn('slow');
					//$('#security_code').val("");
				} else {
					$('.formoutput').hide().html("<p>Oops, there seems to be a server error with sending your email.</p>").fadeIn('slow');
				};
			});
		};
		return false;
	});
	$('.formoutput').hide();

}); // end document.ready

