
// 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.
	$("#step_2").hide();
	$("#step_3").hide();
	$("#step_4").hide();
	$(".submit #submitid").attr("disabled",true);
	$(".submit #submitid").css({ opacity: 0 });
	$("#.row.cgv").css({ opacity: 0 });
	$("#captcha").css({ opacity: 0 });
	
	
	
	
	/*AJOUT DE CLASS AU FORM FINAL*/
	$('fieldset#step_4 .row:nth-child(1)').addClass('odd');
	$('fieldset#step_4 .row:nth-child(2)').addClass('odd');
	$('fieldset#step_4 .row:nth-child(3)').addClass('odd');
	$('fieldset#step_4 .row:nth-child(4)').addClass('odd');
	$('fieldset#step_4 .row:nth-child(5)').addClass('civ');
	$('fieldset#step_4 .row:nth-child(6)').addClass('name');
	$('fieldset#step_4 .row:nth-child(7)').addClass('prenom');
	$('fieldset#step_4 .row:nth-child(8)').addClass('adresse');
	$('fieldset#step_4 .row:nth-child(9)').addClass('cp');
	$('fieldset#step_4 .row:nth-child(10)').addClass('ville');
	$('fieldset#step_4 .row:nth-child(11)').addClass('email');
	$('fieldset#step_4 .row:nth-child(12)').addClass('tel');
	$('fieldset#step_4 .row:nth-child(13)').addClass('cgv');
	
	/*$("#special_accommodations_wrap").hide();*/
	
	// Reset form elements back to default values
	/*$("#submit_button").attr("disabled",true);
	$("#num_attendees").val('Please Choose');*/
	$("#step_1 input[type=radio]").each(function(){
		this.checked = false;
	});
	$("#step_2 input[type=radio]").each(function(){
		this.checked = false;
	});
	$("#step_3 input[type=radio]").each(function(){
		this.checked = false;
	});
	
	
	// Fade out steps 2 and 3 until ready
	$("#bouton1").css({ opacity: 0.3 });
	$("#bouton2").css({ opacity: 0.3 });
	$("#bouton3").css({ opacity: 0.3 });
	//$("#step_3").css({ opacity: 0 });
	
	$.stepOneComplete = "not complete";
	$.stepTwoComplete = "not complete"; 
	$.stepThreeComplete = "not complete"; 
		
	
	
	$("#step_1 input[name=que_signifie_les_initiales_vtt_]").click(function(){
		$.stepOneComplete = "complete"; 
		stepOneTest();
		
	});
	
	
	
	
	$("#step_2 input[name=comment_est_le_maillot_du_meilleur_grimpeur_]").click(function(){
		$.stepTwoComplete = "complete"; 
		stepTwoTest();
		
	});
	
	$("#step_3 input[name=dans_quel_dpartement_sont_fabriques_les_brioches_ma_boulange_]").click(function(){
		$.stepThreeComplete = "complete"; 
		stepThreeTest();
		
	});
	
	
	
	
	//FONCTION DE TEST//
	
	/*QUESTION 1*/
	function stepOneTest() {
		if (($.stepOneComplete == "complete")){
			$("#bouton1")
			.css({
				opacity:1.0,
				"cursor":"pointer"
			});
			bouton();
			
		}
	};
	
	
	/*QUESTION 2*/
	function stepTwoTest() {
		if (($.stepTwoComplete == "complete")){
			$("#bouton2")
			.css({
				opacity:1.0,
				"cursor":"pointer"
			});
			bouton2();
			
		}
	};
	
	/*QUESTION 3*/
	function stepThreeTest() {
		if (($.stepThreeComplete == "complete")){
			$("#bouton3")
			.css({
				opacity:1.0,
				"cursor":"pointer"
			});
			bouton3();
			
		}
	};
	
	
	function bouton() {
		$("#bouton1").click(function(){
		$("#step_1").hide();
		$("#step_2").show();
		});
	};
	
	function bouton2() {
		$("#bouton2").click(function(){
		$("#step_2").hide();
		$("#step_3").show();
		});
	};
	
	function bouton3() {
		$("#bouton3").click(function(){
		$("#step_3").hide();
		$("#step_4").show();
		$("#center").css({
			"background":"white url(/images/maboulange/concours/bg-game2.png) no-repeat top left"
		});
		$(".arriere-plan").css({
			"background":"transparent url(/images/maboulange/concours/bg-question2.png) no-repeat 20px 145px"
		});
		$("#submitid").css({ opacity: 0.5 });
		});
		$("#.row.cgv").css({ opacity: 1.0 });
	$("#captcha").css({ opacity: 1.0 });
	};
	
	
	
	
	/*FORMULAIRE COMPLET*/
	/*CONTROLE DES CHAMPS OBLIGATOIRES*/
	
	
	
	/*CHAMP NOM*/
	$(".name").find("input").addClass("active_name_field");
	$(".active_name_field").blur(function(){
		var all_complete = true;	
		$(".active_name_field").each(function(){
			if ($(this).val() == '' ) {
				$(".active_name_field").addClass("error");
				all_complete = false;
		
			};
		});
		if (all_complete) {
			$(".active_name_field").removeClass("error");
			$.nomOK = "complete";
		};
	});
	
	
	/*CHAMP PRENOM*/
	$(".prenom").find("input").addClass("active_prenom_field");
	$(".active_prenom_field").blur(function(){
		var all_complete2 = true;		
		$(".active_prenom_field").each(function(){
			if ($(this).val() == '' ) {
				$(".active_prenom_field").addClass("error");
				all_complete2 = false;
			
			};
		});
		if (all_complete2) {
			$(".active_prenom_field").removeClass("error");
			$.prenomOK = "complete";
			
		};
	});
	
	
	/*CHAMP MAIL*/
	$(".email").find("input").addClass("active_email_field");
	$(".active_email_field").blur(function(){
		var all_complete3 = true;		
		$(".active_email_field").each(function(){
			if ($(this).val() == '' ) {
				$(".active_email_field").addClass("error");
				all_complete3 = false;
			}
			else{
				if(!$(".active_email_field").val().match(/^[a-z0-9\-_.]+@[a-z0-9\-_.]+\.[a-z]{2,3}$/i)){
						$(".active_email_field").addClass("error");
						all_complete3 = false;
					}
					else{
						all_complete3 = true;
					}
			
			
			}
					
		});
		if (all_complete3) {
			$(".active_email_field").removeClass("error");
			$.mailOK = "complete";
			
		};
	});
	
	
	
	
	/*CHAMP TELEPHONE
	$(".tel").find("input").addClass("active_tel_field");
 
	$(".active_tel_field").blur(function(){
		var all_complete4 = true;		
		$(".active_tel_field").each(function(){
			if ($(this).val() == '' ) {
				$(".active_tel_field").addClass("error");
				all_complete4 = false;
			}
			else{
				if(!$(".active_tel_field").val().match(/^[0-9]{10}$/i)){
						$(".active_tel_field").addClass("error");
						all_complete4 = false;
					}
					else{
						all_complete4 = true;
					}
			
			
			}
					
		});
		if (all_complete4) {
			$(".active_tel_field").removeClass("error");
			$.telOK = "complete";
			
		};
	});*/
	
	
	

	$("#jai_bien_pris_connaissance_des_conditions_de_participation_et_je_les_accepte").click(function(){
		if (this.checked && $.nomOK == 'complete' && $.prenomOK == 'complete' && $.mailOK == 'complete' /*&& $.telOK == 'complete'*/) {
				$(".submit #submitid").css({ 
					opacity: 1.0,
					"cursor":"pointer"
					});
					  
				$(".submit #submitid").attr("disabled",false);
				
			} else {
				$(".submit #submitid").css({ opacity: 0.5 });
				$(".submit #submitid").attr("disabled",true);
				
		}
	});
	
	
	
	
	/*SCRIPT MOBILE*/
	$("#bouton4").click(function(){
		$("#step_1").hide();
		$("#step_2").show();
		});
	
	$("#bouton5").click(function(){
		$("#step_2").hide();
		$("#step_3").show();
		});
		
		$("#bouton6").click(function(){
		$("#step_3").hide();
		$("#step_4").show();
		$("#center").css({
			"background":"white url(/images/maboulange/mobile/fond-smart.png) no-repeat top left"
		});
		});
	
});
