
// When the DOM is ready...
$(function(){
	
	// Hide stuff with the JavaScript. If JS is disabled, the form will still be useable.
	// NOTE:
	// Sometimes using the .hide(); function isn't as ideal as it uses display: none; 
	// which has problems with some screen readers. Applying a CSS class to kick it off the
	// screen is usually prefered, but since we will be UNhiding these as well, this works.
	$(".name_wrap").hide();
	$("#website_url_wrap").hide();
	$("#website_setup_wrap").hide();
	$("#information_wrap").hide();
	$("#where_seen_wrap").hide();
	$("#recaptcha_wrap").hide();
	$("#vercode_wrap").hide();
	//error and message hide till end
	$("#error").hide();
	$("#message").hide();	
	// Reset form elements back to default values	
	$("#submit_button").attr("disabled",true);
	$("#contact").val('Please Choose');
	$("#website_type").val('Please Choose');
	$("#where_seen").val('Please Choose');
	$("#step_2 input[type=radio]").each(function(){
		this.checked = false;
	});
	
	// disable visible bits of steps 2 and 3 till needed
	$("#website_type").attr("disabled",true);
	$("#doyouhavewebsite_toggle_on").attr("disabled",true);
	$("#doyouhavewebsite_toggle_off").attr("disabled",true);
	
	
	
	// Fade out steps 2 and 3 until ready
	$("#step_2").css({ opacity: 0.3 });
	$("#step_2 legend").css({
				opacity: 0.3 // For dumb Internet Explorer
			});
	$("#step_3").css({ opacity: 0.3 });
	$("#step_3 legend").css({
				opacity: 0.3 // For dumb Internet Explorer
			});
	
	$.stepTwoComplete_one = "not complete";
	$.stepTwoComplete_two = "not complete"; 
		
	// When a dropdown selection is made
	$("#contact").change(function() {

		$(".name_wrap").slideUp().find("input").removeClass("active_name_field");
		
		switch ($("#contact option:selected").text()) {
			case 'yes':
				$("#name_wrap").slideDown().find("input").addClass("active_name_field");
				$("#business_wrap").slideDown().find("input").addClass("active_name_field");				
				$("#telephone_wrap").slideDown().find("input").addClass("active_name_field");
				$("#email_wrap").slideDown().find("input").addClass("active_name_field");
				$("input#yourname").focus();
				break;
			case 'no':
				$("#callus_wrap").slideDown();
				break;
			}
	});
	
	$(".name_input").blur(function(){
	
		var all_complete = true;
				
		$(".active_name_field").each(function(){
			if ($(this).val() == '' ) {
				all_complete = false;
			};
		});
		
		if (all_complete) {
			$("#step_1")
			.animate({
				paddingBottom: "80px"
			})
			.css({
				"background-image": "url(images/check3.png)",
				"background-position": "bottom center",
				"background-repeat": "no-repeat"
			});
			$("#step_2").css({
				opacity: 1.0
			});
			$("#step_2 legend").css({
				opacity: 1.0 // For dumb Internet Explorer
			});
			// enable first part of step 2
			$("#doyouhavewebsite_toggle_on").attr("disabled",false);
			$("#doyouhavewebsite_toggle_off").attr("disabled",false);
		};
	});
	
	function stepTwoTest() {
		if (($.stepTwoComplete_one == "complete") && ($.stepTwoComplete_two == "complete")) {
			$("#step_2")
			.animate({
				paddingBottom: "80px"
			})
			.css({
				"background-image": "url(images/check3.png)",
				"background-position": "bottom center",
				"background-repeat": "no-repeat"
			});
			$("#step_3").css({
				opacity: 1.0
			});
			$("#step_3 legend").css({
				opacity: 1.0 // For dumb Internet Explorer
			});
			$("#website_type").attr("disabled",false);
		}
	};
	
	$("#step_2 input[name=doyouhaveawebsite_toggle_group]").click(function(){
		$.stepTwoComplete_one = "complete"; 
		if ($("#doyouhavewebsite_toggle_on:checked").val() == 'on') {
			$("#website_url_wrap").slideDown().find("input").addClass("active_url_field");
			$("#information_wrap").slideDown();
			$("#website_setup_wrap").slideUp();
			$("input#website_url").focus();
		}
		else if($("#doyouhavewebsite_toggle_off:checked").val() == 'on') {
			$("#website_setup_wrap").slideDown();
			$("#information_wrap").slideDown();
			$("#website_url_wrap").slideUp();
		} else {
			$("#website_url_wrap").slideUp();
			$("#website_setup_wrap").slideUp();
		};
		stepTwoTest();
	});
	
	$("#step_2 input[name=website_setup_toggle_group]").click(function(){
		$.stepTwoComplete_two = "complete"; 
		stepTwoTest();
	});
	
	// step 2 all done if website details input
	$(".url_input").blur(function(){
	
		var all_complete = true;
				
		$(".active_url_field").each(function(){
			if ($(this).val() == '' ) {
				all_complete = false;
			};
		});
		
		if (all_complete) {
			$.stepTwoComplete_two = "complete"; 
			stepTwoTest();			
		};
	});					
	
	// When a dropdown selection is made enable 'where_seen_wrap'
	$("#website_type").change(function() {
		
		$("#where_seen_wrap").slideDown().find("input").addClass("active_name_field");
		
	});
	
	//show the reCaptcha image
	function showRecaptcha(element, submitButton, themeName) {
  		Recaptcha.create("6LfwhAUAAAAAAJW0K9K2kpVYd0eFwbVca9JUe-Sy", element, {
        theme: themeName,
        tabindex: 0,
        callback: Recaptcha.focus_response_field
		  });
		}
	
	// When a dropdown selection is made enable submit button IF all else done as well
	$("#where_seen").change(function() {
				$("#recaptcha_wrap").slideDown();
				$("#vercode_wrap").slideDown();
				$("#submit_button").attr("disabled",false);
				$("input#vercode").focus();
	});

$(document).ready(function(){
$("form").submit(function(){

var str = $("form").serialize();

   $.ajax({
   type: "POST",
   url: "mailer.php",
   data: str,
   success: function(msg){
    
		$("#recaptcha_wrap").ajaxComplete(function(event, request, settings){

		if(msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
	{
		$("#captchasubmit").hide();
		$("#error").hide();
		$("#message").slideDown();
	//	result = '<div class="notification_ok">Your message has been sent. Thank you!</div>';
	}
	else
	{
		$("#error").slideDown();
		$("input#vercode").focus();
	}
	
	//$(this).html(result);
	
	});
	
	}
	
	 });
	
	return false;
	
	});

});

//end of file
	
});

