﻿function initQuestionnary () {
	
	// questionnary
	showQuestionnaryField = function () {
		// check current fieldset values
		if (!checkCurrentFieldset()) {
			return false;
		}
		// change current fieldset's class
		var current=false;
		$(".questionnary fieldset").each ( function () {
			if($(this).hasClass("current")) {
				$(this).removeClass("current");
				current = true;
			} else if (current) {
				$(this).addClass("current");
				current = false;
			}
		} );
		// show button if last fieldset
		if ($(".questionnary fieldset:last").hasClass("current")) {
			$(".questionnary form .next").hide();
			$(".questionnary form button").fadeIn("slow");
		} else {
			$(".questionnary form .next").hide();
			$(".questionnary form .next").fadeIn("slow");
		}
		// if necessary
		//if (setBodysidesHeights!=undefined) setBodysidesHeights();
	}
	// init fieldsets
	$(".questionnary form").addClass("js-form");
	$(".questionnary fieldset:first").addClass("current");
	$(".questionnary form .next").click( function () {
		showQuestionnaryField();
	} );
	$(".questionnary form").submit(function () {
		// submit form
		if (checkCurrentFieldset()) {
			var answers = $(".questionnary form").serialize();
			var ans_array = answers.split("=");
			for (i=1; i<ans_array.length; i++) ans_array[i] = ans_array[i].substr(0,1);
			// taggage carat 
			$(".questionnary .result").append("<!-- Carat --><img src=\"http://s0b.bluestreak.com/ix.e?bb&t=37223\" width=1 height=1 border=0>");
			//
			var s = "";
			if (ans_array[3]<2) $(".questionnary .result .obseques").hide();
			if (ans_array[2]==0) $(".questionnary .result .sante").hide();
			if (ans_array[1]==0) $(".questionnary .result .avenir").hide();
			if (ans_array[3]==2 && ans_array[1]==0 && ans_array[2]==0) $(".questionnary .result .gav").hide();
			//
			$(".questionnary form").hide();
			$(".questionnary .result").fadeIn("fast");
		}
		return false;
	} );
}
function checkCurrentFieldset () {
	var checked = false;
	$(".questionnary fieldset.current input").each( function () {
		if ($(this).attr("checked")) checked = true;
	} );
	if(!checked) {
		alert("Vous devez répondre à la question");
		return false;
	}
	return true;
}




$(function(){
	setTimeout(initQuestionnary, 0);
}); 





