/**************************************************/
/** QUESTIONNAIRE Q0-ecran1 ***********************/
/**************************************************/

function q0_ecran1(){
	$("#charte").change(function(){								
		$("#blk_submit").animate({opacity:'toggle'}, 250 );
	});
	
	$('#btn_pop').click(function(event){
		event.preventDefault();
		$('#pop').fadeIn(250, function(){
			$('#pop-ctt').fadeIn(250);
		})						 
	})
	
	$('#overlay').click(function(){
		$('#pop-ctt').fadeOut(250, function(){
			$('#pop').fadeOut(250);
		})						 
	})
}

/**************************************************/
/** QUESTIONNAIRE Q1-ecran2 ***********************/
/**************************************************/

function q1_ecran2(){
	$('#q1_2_0 input').change(function(){
		if ($(this).is("#pro")){
			$('fieldset:not(#q1_2_0), #blk_submit').hide();
			$("#q1_2_1").fadeIn(250);
		} else if ($(this).is("#form")){	
			$('fieldset:not(#q1_2_0), #blk_submit').hide();
			$("#q1_2_5").fadeIn(250);
		} else {
			$('fieldset:not(#q1_2_0)').hide();
			$("#blk_submit").fadeIn(250);
		}
	});
	
	$('#q1_2_1 input').change(function(){
		if ($(this).is("#ouvrir")){
			$('fieldset:not(#q1_2_0, #q1_2_1), #blk_submit').hide();
			$("#q1_2_2").fadeIn(250);
		} else if ($(this).is("#rejoindre")){	
			$('fieldset:not(#q1_2_0, #q1_2_1), #blk_submit').hide();
			$("#q1_2_3").fadeIn(250);
		} else if ($(this).is("#joindre")){
			$('fieldset:not(#q1_2_0, #q1_2_1), #blk_submit').hide();
			$("#q1_2_4").fadeIn(250);
		} else if($(this).is("#workshop-join-circuit")){
			$('fieldset:not(#q1_2_0, #q1_2_1), #blk_submit').hide();
			$("#q1_2_7").fadeIn(250);		
		}
		
	});
        
	$("#q1_2_2 input").change(function(){
		if ( $(this).is(":checked") ){
			$("fieldset:not(#q1_2_0, #q1_2_1, #q1_2_2)").hide();
			$("#blk_submit").fadeIn(250);
		} else {
			$("#blk_submit").hide();
		}
	});

	$("#drop-atelier").change(function(){
		$("fieldset:not(#q1_2_0, #q1_2_1, #q1_2_2, #q1_2_3)").hide();
		$("#blk_submit").fadeIn(250);
	});

	$("#drop-manif").change(function(){
		$("fieldset:not(#q1_2_0, #q1_2_1, #q1_2_4)").hide();
		$("#blk_submit").fadeIn(250);
	});

	$('#q1_2_5 input').change(function(){
		if ($(this).is("#jo")){
			$('fieldset:not(#q1_2_0, #q1_2_5), #blk_submit').hide();
			$("#q1_2_6").fadeIn(250);
		} else if($(this).is("#workshop-join-circuit")){
			$('fieldset:not(#q1_2_0, #q1_2_5), #blk_submit').hide();
			$("#q1_2_7").fadeIn(250)
		} else if($(this).is("#center-join-circuit")){
			$('fieldset:not(#q1_2_0, #q1_2_5), #blk_submit').hide();
			$("#q1_2_8").fadeIn(250)
		} else {
			$('fieldset:not(#q1_2_0, #q1_2_5)').hide();
			$("#blk_submit").fadeIn(250);
		}
	});
	
	$("#drop-manif2").change(function(){
		$("fieldset:not(#q1_2_0, #q1_2_5, #q1_2_6)").hide();
		$("#blk_submit").fadeIn(250);
	});
	
	$("#drop-circuit").change(function(){
		//$("fieldset:not(#q1_2_0, #q1_2_5, #q1_2_6)").hide();
		$("#blk_submit").fadeIn(250);
	});	
	
	$("#drop-circuit2").change(function(){
		//$("fieldset:not(#q1_2_0, #q1_2_5, #q1_2_6)").hide();
		$("#blk_submit").fadeIn(250);
	});	
	
	var cloneManif = $('#drop-manif').clone();
	var cloneAtelier = $('#drop-atelier').clone();
	var cloneCentre = $('#drop-manif2').clone();
	$('#page .droplist').change(function(){
		$(this).next().find('option').eq(0).attr('selected', true);
		if ($(this).parent().parent().parent().attr('id')=='q1_2_4'){
			var clone = cloneManif;
		} else if($(this).parent().parent().parent().attr('id')=='q1_2_3'){
			var clone = cloneAtelier;
		} else {
			var clone = cloneCentre;
		}
		$.each($(this).find('option'), function(){
			if ($(this).is(":selected")){
				$(this).parent().next().find('option[region_id]').remove();
				$(this).parent().next().append(clone.clone().find('option[region_id="'+$(this).val()+'"]'));
			}
		})
		
	});
	
}


function initControlRegistration()
{
        jQuery.validator.addMethod("islength", function(value, element) { 
            return this.optional(element) || (value.length == 9 || value.length == 14); 
        });

	$("#registration_form").validate({
		rules: {
			"registration[siren_siret]" : {
					number:true,
					islength:[9,14]
			}
		},
		messages : {
			"registration[siren_siret]" : {
				number: "Erreur format Siren/Siret",
				islength: "Doit être de 9 ou de 14 caractères"
			}
                        
		},
		success: function(label) {
			label.parent().append('<span class="valid-thick"></span>');
		},
		errorElement: "span",
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
			element.parent().find('.valid-thick').each(function(){
				$(this).remove();
			});
		}
	});
	
	
}
function q2_ecran4(){
	$(".hide").hide();
	$.each($("#q2_4_1 input:checkbox"), function(){
		if ($(this).is(":checked")){
			$(this).parent().find(' .hide').show();
		}
	});
	$("#q2_4_1 input:checkbox, #price_4").change(function(){
		if ($(this).is(":checked")){
			$(this).parent().find('.hide').fadeIn(250);
			$(this).parent().find('.hide input').focus();
		}else {
			$(this).parent().find('.hide').hide();
		}
	});
	$.each($("#registration_awards_list_5"), function(){
		if ($(this).is(":checked")){
			$(this).parent().parent().next().show();
		}
	});
	$("#registration_awards_list_5").change(function(){
		if ($(this).is(":checked")){
			$(this).parent().parent().next().fadeIn(500);
			$(this).parent().parent().next().find('input').focus();
		}else {
			$(this).parent().parent().next().hide();
		}
	});
	
	/* METIERS */
	var cloneSecteur = $('#drop-secteur-0').clone();
	var cloneMetier = $('#drop-metier-0').clone();
	
	var nMetier = 0;
	$.each($('#selection_metier1, #selection_metier2'), function(){
		if($(this).find('select:has("option[class]:selected")').length==0){
			$(this).hide();	
		}
	})
	$('#btn-add').click(function(event){
		if(nMetier<2){
			nMetier++;
			$('#selection_metier'+nMetier).fadeIn(250, function(){
				if(nMetier==2){
					$('#btn-add').fadeOut(500);	
				}
			});
		}
	})

	$.each($('#drop-metier-0, #drop-metier-1, #drop-metier-2'), function(){
		$(this).find('option[class]').not(':selected').not('.secteur_'+$(this).prev().find('option:selected').val()).remove();
	})
	var selectd = $('#drop-secteur-0, #drop-secteur-1, #drop-secteur-2').find('option:selected').val();

	$.each($('#drop-secteur-0, #drop-secteur-1, #drop-secteur-2'), function(){
		for (var i=0; i<selectd.length; i++){
			$(this).find('option[value="'+selectd[i]+'"]').not(':selected').attr('disabled', true);
		}
	})
	
	$('#drop-secteur-0, #drop-secteur-1, #drop-secteur-2').change(function(){
		$(this).next().find('option').eq(0).attr('selected', true);
		$.each($(this).find('option'), function(){
			if ($(this).is(":selected")){
				$(this).parent().next().find('option[class]').remove();
				$(this).parent().next().append(cloneMetier.clone().find('option[class="secteur_'+$(this).val()+'"]'))
			}
		})
	});
        
        initControlRegistration();
}


function q2_ecran4_form(){
	var blocs = $(".sel_combo");
	for (var i=1; i<blocs.length; i++){
		//$(".sel_combo").eq(i).hide();
	}
	
	/* METIERS */
	var cloneSecteur = $('#drop-secteur-0').clone();
	var cloneMetier = $('#drop-metier-0').clone();
	
	var nMetier = 0;
	$.each($('.sel_combo'), function(){
		if($(this).find('select:has("option[class]:selected")').length==0 && $(this).index()>0){
			$(this).hide();	
		}
		
	})
	$('#btn-add').click(function(event){
		//if(nMetier<2){
			nMetier++;
			blocs.eq(nMetier).fadeIn(250, function(){
				if(nMetier==9){
					$('#btn-add').fadeOut(500);	
				}
			});
		//}
	})

	$.each($('#drop-metier-0, #drop-metier-1, #drop-metier-2'), function(){
		$(this).find('option[class]').not(':selected').not('.secteur_'+$(this).prev().find('option:selected').val()).remove();
	})
//	var selectd = $('#drop-secteur-0, #drop-secteur-1, #drop-secteur-2').find('option:selected').val();
//
//	$.each($('#drop-secteur-0, #drop-secteur-1, #drop-secteur-2'), function(){
//		for (var i=0; i<selectd.length; i++){
//			$(this).find('option[value="'+selectd[i]+'"]').not(':selected').attr('disabled', true);
//		}
//	})
	
	$('#drop-secteur-0, #drop-secteur-1, #drop-secteur-2').change(function(){
		$(this).next().find('option').eq(0).attr('selected', true);
		$.each($(this).find('option'), function(){
			if ($(this).is(":selected")){
				$(this).parent().next().find('option[class]').remove();
				$(this).parent().next().append(cloneMetier.clone().find('option[class="secteur_'+$(this).val()+'"]'))
			}
		})
	});
}




function q2_ecran5(){
	var lngt = $("#q2_5_0 input:checkbox").length-1;
	
	$("#q2_5_0 .hide").hide();
	$.each($("#q2_5_0 input:checkbox"), function(){
		if ($(this).is(":checked")){
			$(this).parent().find('.hide').show();
		}
	})
	$.each($("#event_info_has_theme_1"), function(){
		if ($(this).is(":checked")){
			$(this).parent().parent().parent().find('.hide').fadeIn(250);
		}
	})

	$('#thema input').change(function(){
		if ($(this).is(":checked") && $(this).val()==1){
			$(this).parent().parent().parent().find('.hide').fadeIn(250);
		}else {
			$(this).parent().parent().parent().find('.hide').hide();
		}
	})
	
	$("#q2_5_0 input:checkbox").change(function(){
		if ($(this).is(":checked")){
			$(this).parent().find('.hide').fadeIn(250);
			$(this).parent().find('.hide input').focus();
			if ($(this).parent().index()==lngt && $(this).attr('id') == 'zero'){
				for(var i=0; i<lngt; i++){
					$("#q2_5_0 input:checkbox").eq(i).attr('checked', false);
					$("#q2_5_0 input:checkbox").eq(i).parent().find('.hide').hide();
				}
			} else if ($("#q2_5_0 input:checkbox").eq(lngt).attr('id') == 'zero'){
				$("#q2_5_0 input:checkbox").eq(lngt).attr('checked', false);
			}
		} else {
			$(this).parent().find('.hide').hide();
		}
		
	});
}


function q2_ecran6(){
	$(".hide, #q2_6_4").hide();
	$.each($("#q2_6_1 input:checkbox"), function(){
		if ($(this).is(":checked")){
			$(this).parent().next().show();
		}
	})
	$.each($('#registration_entry_type_By_appointment, #groupe_rdv, #scolaire_rdv'), function(){
		if ($(this).is(":checked")){
			$('#q2_6_4').show();
		}
	})
	$.each($('#groupe_ok, #scolaires_ok'), function(){
		if ($(this).is(":checked")){
			$(this).parent().parent().find('.hide').show();
		}
	})
	$('#groupe_ok, #scolaires_ok').change(function(){
		if (!$(this).is(":checked")){
			$(this).parent().next().find('input').removeAttr('checked').val('');
		}
	})
	
	$("#q2_6_1 input:checkbox").change(function(){
		if ($(this).is(":checked") && $(this).is("#tous")){
			$("#q2_6_1 input:checkbox").not($(this)).removeAttr('checked');
			$("#q2_6_1 input:checkbox").not($(this)).parent().next().hide();
		} else if ($(this).is(":checked") && $(this).not("#tous")){
			$("#tous").removeAttr('checked');
			$("#tous").parent().next().hide();	
		}
	});
	
	$("#q2_6_1 input:checkbox, #q2_6_2 p input:checkbox, #q2_6_3 p input:checkbox").change(function(){
		if ($(this).is(":checked")){
			$(this).parent().next().fadeIn(250);
		}else {
			$(this).parent().next().hide();
		}
	});
	
	$("#registration_entry_type_By_appointment, #groupe_rdv, #scolaire_rdv").change(function(){
		if ($(this).is(":checked")){
			$('#q2_6_4').fadeIn(250);
		} else if (!$("#registration_entry_type_By_appointment, #groupe_rdv, #scolaire_rdv").is(":checked")) {
			$('#q2_6_4').hide();
		}
	});
	
	$("#registration_group_free_max, #registration_school_free_max").change(function(){
		if ($(this).parent().prev().prev().not(":checked")){
			$(this).parent().prev().prev().attr('checked', true);
		}
		$(this).parent().parent().next().find('input').eq(1).val(parseFloat($(this).val())+1)
	});
}

function q2_ecran7(){
	$('#q2_7_1, #q2_7_3_1, #q2_7_4_1, #q2_7_3_2, #q2_7_4_2, #transp1, #registration_transportation_2_type, #transp2').hide();
	
	var initValues = new Array();
	for (var i=0; i<$("#q2_7_1 input").length; i++){
		initValues.push( $('#q2_7_1 input').eq(i).val());
	}
	
	$.each($("#q2_7_0 input"), function(){
		if ($(this).is(":checked")){
			if($(this).val() == 0)	{
				$('#q2_7_1 span.text').text("Veuillez renseignez l'adresse d'accueil du public");
				$('#q2_7_1 input').val('');
			}
			$('#q2_7_1').show();
		}
	})
	
	$('#q2_7_0 input').change(function(){
		if($(this).val() == 1)	{
			$('#q2_7_1 span.text').text("Adresse d'accueil du public (à compléter ou modifier si nécessaire)");

			for (var i=0; i<$("#q2_7_1 input").length; i++){
				$("#q2_7_1 input").eq(i).val(initValues[i]);
			}
		} else {
			$('#q2_7_1 span.text').text("Veuillez renseigner l'adresse d'accueil du public");
			$('#q2_7_1 input').val('');
		}
		$('#q2_7_1').fadeIn(250);
	})
	

	$.each($('#registration_transportation_1_type option'), function(){
		if ($(this).is(":selected") && $(this).val() != ''){
			if($(this).text() == 'Route'){
				$('#q2_7_4_1').show();
			} else if($(this).text() == 'Autre') {
				$('#transp1').show();
			} else {
				$('#q2_7_3_1').show();
			}
		}
	})

	$('#registration_transportation_1_type').change(function(){
		if ($(this).find('option:selected').text() == 'Route'){
			$('#q2_7_4_1').fadeIn(250);
			$('#q2_7_3_1, #transp1').hide();
		} else if($(this).find('option:selected').text() == 'Autre') {
			$('#transp1').fadeIn(250);
			$('#q2_7_4_1, #q2_7_3_1').hide();
		} else {
			$('#q2_7_3_1').fadeIn(250);
			$('#q2_7_4_1, #transp1').hide();
		}
	});
	
	
	$.each($('#q2_7_2_2 .radio_list input'), function(){
		if ($(this).is(":checked") && $(this).val() == 1){
			$('#registration_transportation_2_type').show();
		}
	})
	$('#q2_7_2_2 .radio_list input').change(function(){
		if($(this).val() == 1)	{
			$('#registration_transportation_2_type').fadeIn(250);
		} else {
			$('#registration_transportation_2_type, #q2_7_3_2, #q2_7_4_2, #transp2').hide();
		}
	})
	
	$.each($('#registration_transportation_2_type option'), function(){
		if ($(this).is(":selected") && $(this).val() != ''){
			if($(this).text() == 'Route'){
				$('#q2_7_4_2').show();
			} else if($(this).text() == 'Autre') {
				$('#transp2').show();
			} else {
				$('#q2_7_3_2').show();
			}
		}
	})
	$('#registration_transportation_2_type').change(function(){
		if ($(this).find('option:selected').text() == 'Route'){
			$('#q2_7_4_2').fadeIn(250);
			$('#q2_7_3_2, #transp2').hide();
		} else if($(this).find('option:selected').text() == 'Autre') {
			$('#transp2').fadeIn(250);
			$('#q2_7_4_2, #q2_7_3_2').hide();
		} else {
			$('#q2_7_3_2').fadeIn(250);
			$('#q2_7_4_2, #transp2').hide();
		}
	});
}

function maxLength(){
	$('textarea[maxlength]').keyup(function(){ 
		var limit = parseInt($(this).attr('maxlength'));
		var text = $(this).val(); 
		var chars = text.length;
		if(chars > limit){
			var new_text = text.substr(0, limit);
			$(this).val(new_text);  
		}  
	}); 
}

function infobulle(){
	$('.info').mouseenter(function(){
		$(this).next().fadeIn(250);
	}).mouseleave(function(){
		$(this).next().fadeOut(250);
	});
}

$("#plaquette-registration-form .supprimer_plaquette").live("click",function(event){
    event.preventDefault();
    $("#registration_plaquette_action").val('delete');
    $("#plaquette-registration-form").submit();
});

$(document).ready(function(){
  if($('#accordion_logs_registration').length > 0){
    $(function(){
      $('#accordion_logs_registration').accordion({
          collapsible: true,
          active: false,
          autoHeight: false
      });
    });
  }

  if($('#pictures #sortable').length > 0){
    $(function(){
      $( "#pictures #sortable" ).sortable({
        opacity: 0.7,
        revert: true,
        update : function (event, ui) { 
          $('#sortable input.position').each(function(i) {           
            $(this).val( (i+1) );
          });
        }

      });

    });
  }
  
  if($('#list_hosted_registrations').length > 0){
    $(function(){
      $( "#list_hosted_registrations" ).sortable({
        opacity: 0.7,
        revert: true,
        update : function (event, ui) { 
          $('#list_hosted_registrations').each(function(i) {           
            $(this).val( (i+1) );
          });
        }

      });

    });
  }
  
  if($('#circuit_tri_content').length > 0){
    $('#circuit_tri_content').height($('#circuit_tri_content').height());
    $(function() {
      $( "#circuit_tri_content" ).sortable({
        placeholder: "ui-state-highlight",
        opacity: 0.7,
        revert: true
      });
      $( "#circuit_tri_content" ).disableSelection();
    });
  }
});
