
// Comportements JS propres à Accessiform

$(function(){
	
	// Activation du champ "autre" - bouton radio et checkbox
	var activerAutre = function( selecteur )
	{
		// $.each nécessaire pour les radio
		$.each( $(selecteur), function( index, value ){
			if( $(this).attr('checked') )
			{
				$('input[name=' + $(this).attr('id') + '_autre]').attr( 'disabled', false );
				$('#' + $(this).attr('id') + '_autre_conteneur').show();
			}
			else
			{
				$('input[name=' + $(this).attr('id') + '_autre]').attr( 'disabled', true );
				$('#' + $(this).attr('id') + '_autre_conteneur').hide();
			}
		});
	};
	$('input[type=radio][name^=accessiform_]').click( function(){
		activerAutre( 'input[type=radio][name^=accessiform_]' );
	});
	activerAutre( 'input[type=radio][name^=accessiform_]' );
	$('input[type=checkbox][name^=accessiform_]').click( function(){
		activerAutre( 'input[type=checkbox][name^=accessiform_]' );
	});
	activerAutre( 'input[type=checkbox][name^=accessiform_]' );
	
	
	// Activation des blocs conditionnels - bouton radio et checkbox
	var afficheBlocConditionnelRadio = function( selecteur )
	{
		$.each( $(selecteur), function( index, value ){
			if( typeof( $(this).attr('rel') ) != 'undefined' && $(this).attr('rel') != '' )
			{
				$('#afblocconditionnel_' + $(this).attr('relField') + '_' + $(this).attr('rel')).hide();
			}
		});
		var showBlocks = '';
		$.each( $(selecteur), function( index, value ){
			if( typeof( $(this).attr('rel') ) != 'undefined' && $(this).attr('rel') != '' && $(this).attr('checked') )
			{
				if( showBlocks != '' )
					showBlocks = showBlocks + ', ';
				showBlocks = showBlocks + '#afblocconditionnel_' + $(this).attr('relField') + '_' + $(this).attr('rel');
			}
		});
		$(showBlocks).show();
	};
	$('input[type=radio][name^=accessiform_]').click( function(){
		afficheBlocConditionnelRadio( 'input[type=radio][name^=accessiform_]' );
	});
	afficheBlocConditionnelRadio( 'input[type=radio][name^=accessiform_]' );
	$('input[type=checkbox][name^=accessiform_]').click( function(){
		afficheBlocConditionnelRadio( 'input[type=checkbox][name^=accessiform_]' );
	});
	afficheBlocConditionnelRadio( 'input[type=checkbox][name^=accessiform_]' );
	
});
