/*
Javascript for Production London by electricSnow.co.uk

Please feel free to borrow, steal, abduct, and/or torture the code 
contained in this file. Though you need not give credit to
electricsnow.co.uk, a comment in your source code would help other 
developers.

Stay hungry, Stay foolish.

For more info vist http://jquery.com - New Wave Javascript

*/
// Check fields on form
function validate() { 
	// Check Fields
	var fnameValue = $('input[@name=fname]').fieldValue();
	var snameValue = $('input[@name=sname]').fieldValue();
	var phone1Value = $('input[@name=phone1]').fieldValue();
	var emailValue = $('input[@name=email]').fieldValue();
	var ageValue = $('input[@name=age]').fieldValue();
	var sexValue = $('input[@name=sex]').fieldValue();
	var spamValue = $('input[@name=spam]').fieldValue(); 
	var hearValue = $('input[@name=hear]').fieldValue(); 
	if (!fnameValue[0]) { 
		$('input[@name=fname]').css({ background: "#f07f2a" });
		alert('We need to know your first name');
		return false; 
	}
	if (!snameValue[0]) { 
		$('input[@name=sname]').css({ background: "#f07f2a" });
		alert('We need to know your surname');
		return false; 
	}
	if (!emailValue[0]) {
		$('input[@name=email]').css({ background: "#f07f2a" });
		alert('We need to know your email address');
		return false; 
	}
	if (!phone1Value[0]) { 
		$('input[@name=phone1]').css({ background: "#f07f2a" });
		alert('We may need to call you');
		return false; 
	}
	if (!ageValue[0]) {
		$('input[@name=age]').css({ background: "#f07f2a" });
		alert('We need to know your age');
		return false; 
	}
	if (!sexValue[0]) {
		alert('Are you male or female?');
		return false; 
	}
	if (!spamValue[0]) {
		$('input[@name=spam]').css({ background: "#f07f2a" });
		alert('Please answer the anti-spam question.');
		return false; 
	}
	if (!hearValue[0]) {
		$('input[@name=hear]').css({ background: "#f07f2a" });
		alert('Where did you hear about the study?');
		return false; 
	}

}
// Handle the form
function formResponse() {
    $('#contact').ajaxForm({ 
        beforeSubmit: validate,
        target: '#output2',
        success: function() { 
        	//alert("Your information has been securely sent");
        	//$('#submitButton').remove();
            return false;
        }
    }); 
};
// Show / hide of extras
function showExtra(){
	$("#showMore").toggle(function(){
	  $('#addressInfo').slideDown(400);
	  $(this).html("<span class='colorMe'>No thank you</span>").fadeIn(400);
	  $('#showMoreTxt').append('<input name="showMore" type="hidden" value="1" />')
	},function(){
	  $('#addressInfo').slideUp(400);
	  $(this).html("Yes please >").fadeIn(400);
	  $('input[@name=showMore]').remove();
	});
};
// Ready the DOM
$(document).ready(function() { 
	// Ajax Feedback
	$("#loading").ajaxStart(function(){
	  $(this).show();
	});
	$("#loading").ajaxStop(function(){
	  $(this).hide();
	});
	// Javascript Warning
	$('.JSwarning').remove();
	// Show / hide more info
	var yesORno = $("#showMore");	// Checkbox
	var extraInfo = $("#addressInfo")[yesORno.is(":checked") ? "show" : "hide"](); // The hidden fieldset
	yesORno.change(function() {
		extraInfo[this.checked ? "show" : "hide"](); // Go!!
	});
	// Form Protection
	$.get("app/token.php",function(txt){
	  $(".secure").append('<input type="hidden" name="ts" value="'+txt+'" />');
	});
	formResponse();
	showExtra();
});
