var sections = ['part1', 'part2', 'part3', 'part4', 'part5', 'part6', 'part7', 'part8', 'part9', 'part10' ];

showSection = function(section) {
	for (var i = 0; i < sections.length; i++) {
		if (sections[i] == section) {
			$("#" + section).show();
		}
		else {
			$("#" + sections[i]).hide();
		}
	}
}

selectStar = function(section, isSelect) {
	if (isSelect) {
		document.getElementById('star_'+section).src = "images/quiz/stars/star_red_"+section+".gif";
	} else {
		document.getElementById('star_'+section).src = "images/quiz/stars/star_gray_"+section+".gif";	
	}
}

$(document).ready(
	function() {
		showSection('part1');
	}
);

$(document).ready(function(){

	$(".fieldError").hide();
	//jQuery.validator.messages.required = "";
	//set the defaults for all the forms
	jQuery.validator.setDefaults({ 
		debug: true,
		event: "change",
		errorElement: "span",
		errorClass: "fieldErr",
		focusInvalid: false,
		highlight: function(element, errorClass) {
			$(element).parents("li").addClass(errorClass);
		},
		unhighlight: function(element, errorClass) {
			$(element).parents("li").removeClass(errorClass);
		}
	});
	
	$("#part1").validate({
		errorContainer: $("#fieldError"),
		showErrors: function(errorMap, errorList) { customShowErrors(this, "#part1"); },
		rules: {			
			question1: { required: true }
		}
	});
	
	$("#part2").validate({
		errorContainer: $("#fieldError"),
		showErrors: function(errorMap, errorList) { customShowErrors(this, "#part2"); },
		rules: {
			question2: { required: true }
		}
	});
	
	$("#part3").validate({
		errorContainer: $("#fieldError"),
		showErrors: function(errorMap, errorList) { customShowErrors(this, "#part3"); },
		rules: {
			question3: { required: true }
		}
	});
	
	$("#part4").validate({
		errorContainer: $("#fieldError"),
		showErrors: function(errorMap, errorList) { customShowErrors(this, "#part4"); },
		rules: {
			question4: { required: true }
		}
	});
	
	$("#part5").validate({
		errorContainer: $("#fieldError"),
		showErrors: function(errorMap, errorList) { customShowErrors(this, "#part5"); },
		rules: {
			question5: { required: true }
		}
	});
	
	$("#part6").validate({
		errorContainer: $("#fieldError"),
		showErrors: function(errorMap, errorList) { customShowErrors(this, "#part6"); },
		rules: {
			question6: { required: true }
		}
	});
	
	$("#part7").validate({
		errorContainer: $("#fieldError"),
		showErrors: function(errorMap, errorList) { customShowErrors(this, "#part7"); },
		rules: {
			question7: { required: true }
		}
	});
	
	$("#part8").validate({
		errorContainer: $("#fieldError"),
		showErrors: function(errorMap, errorList) { customShowErrors(this, "#part8"); },
		rules: {
			question8: { required: true }
		}
	});
	
	$("#part9").validate({
		errorContainer: $("#fieldError"),
		showErrors: function(errorMap, errorList) { customShowErrors(this, "#part9"); },
		rules: {
			question9: { required: true }
		}
	});
	
	$("#part10").validate({
		//submitHandler: function(form) { reSubmitForm(form); },
		errorContainer: $("#fieldError"),
		showErrors: function(errorMap, errorList) { customShowErrors(this, "#part10"); },
		rules: {
			question10: { required: true }
		}
	});
	
	$("#gt2f1").click(function() {
		if($("#part1").valid()){
			showSection("part2");
			selectStar(2,true);
		}
		else {
			return false;
		}
	});
	$("#gt3f2").click(function() {
		if($("#part2").valid()){
			showSection("part3");
			selectStar(3,true);
		}
		else {
			return false;
		}
	});
	$("#gt4f3").click(function() {
		if($("#part3").valid()){
			showSection("part4");
			selectStar(4,true);
		}
		else {
			return false;
		}
	});
	$("#gt5f4").click(function() {
		if($("#part4").valid()){
			showSection("part5");
			selectStar(5,true);
		}
		else {
			return false;
		}
	});
	$("#gt6f5").click(function() {
		if($("#part5").valid()){
			showSection("part6");
			selectStar(6,true);
		}
		else {
			return false;
		}
	});
	$("#gt7f6").click(function() {
		if($("#part6").valid()){
			showSection("part7");
			selectStar(7,true);
		}
		else {
			return false;
		}
	});
	$("#gt8f7").click(function() {
		if($("#part7").valid()){
			showSection("part8");
			selectStar(8,true);
		}
		else {
			return false;
		}
	});
	$("#gt9f8").click(function() {
		if($("#part8").valid()){
			showSection("part9");
			selectStar(9,true);
		}
		else {
			return false;
		}
	});
	$("#gt10f9").click(function() {
		if($("#part9").valid()){
			showSection("part10");
			selectStar(10,true);
		}
		else {
			return false;
		}
	});
	$("#quiz_container #submit").click(function() {
		if($("#part10").valid()){
			reSubmitForm();
		}
		else {
			return false;
		}
	});
	
	$("#gt1f2").click(function() {		
		showSection("part1");
		selectStar(2,false);
	});
	$("#gt2f3").click(function() {		
		showSection("part2");
		selectStar(3,false);
	});
	$("#gt3f4").click(function() {		
		showSection("part3");
		selectStar(4,false);
	});
	$("#gt4f5").click(function() {		
		showSection("part4");
		selectStar(5,false);
	});
	$("#gt5f6").click(function() {		
		showSection("part5");
		selectStar(6,false);
	});
	$("#gt6f7").click(function() {		
		showSection("part6");
		selectStar(7,false);
	});
	$("#gt7f8").click(function() {		
		showSection("part7");
		selectStar(8,false);
	});
	$("#gt8f9").click(function() {		
		showSection("part8");
		selectStar(9,false);
	});
	$("#gt9f10").click(function() {		
		showSection("part9");
		selectStar(10,false);
	});
});

customShowErrors = function(cntxt, elId){
	if(cntxt.numberOfInvalids() > 0){
		/*var tmp = (elId != "#part10")? "proceeding to the next section." : "submitting the questionnaire.";*/
		$(elId + " .fieldError").html("Champs Requis");
		$(elId + " .fieldError").show();
		for(var i=0; cntxt.errorList[i]; i++){
			cntxt.settings.highlight && cntxt.settings.highlight.call( cntxt, cntxt.errorList[i].element, cntxt.settings.errorClass );
		}
		//showSection( $(cntxt.errorList[0].element).parents("form:has(div.prevNext)").attr("id") );
	}
	else {
		$(elId + " .fieldError").hide();
	}
}

reSubmitForm = function(){
	mapValues();
	$("#actualForm").submit();
}

mapValues = function(){
	$("input[name='q1']").val($(".question1").fieldValue());
	$("input[name='q2']").val($(".question2").fieldValue());
	$("input[name='q3']").val($(".question3").fieldValue());
	$("input[name='q4']").val($(".question4").fieldValue());
	$("input[name='q5']").val($(".question5").fieldValue());
	$("input[name='q6']").val($(".question6").fieldValue());
	$("input[name='q7']").val($(".question7").fieldValue());
	$("input[name='q8']").val($(".question8").fieldValue());
	$("input[name='q9']").val($(".question9").fieldValue());
	$("input[name='q10']").val($(".question10").fieldValue());
}