function ajaxLocations(group) {
		
		$(group).attr('disabled', 'disabled');
				
		$(group[0]).bind({
			  change: function() {
			  	
				distrito_id = $(this).val();
							  
				jsonLocations('concelhos;distrito='+distrito_id,group[1])
				
				//liga o select
				$(group[1]).removeAttr('disabled');
				
				//muda o texto por defeito do select no pai span para o valor escolhido
				$(this).prev('span.input').text( $('option:selected', this).text() );

			  }
		});
		
		$(group[1]).bind({
			  change: function() {
			  
			  	concelho_id = $(this).val();
			  
				jsonLocations('freguesias;distrito='+distrito_id+';concelho='+concelho_id,group[2])
				
				//liga o select
				$(group[2]).removeAttr('disabled');
				
				//muda o texto por defeito do select no pai span para o valor escolhido
				$(this).prev('span.input').text( $('option:selected', this).text() );

			  }
		});
		
		$(group[2]).bind({
			  change: function() {
				  
				//muda o texto por defeito do select no pai span para o valor escolhido
				$(this).prev('span.input').text( $('option:selected', this).text() );

			  }
		});

		
		function jsonLocations(location,dest) {
		
			if ( location ){} else{
				location = 'distritos';
			}
			
			$.ajax({
			   type: "GET",
			   url: "ajax_localidades.php",
			   data: "tipo="+location,
			   dataType: "json",
			   success: function(data){
			   
					$(dest).empty();
					
					$(dest).append("<option>todos</option>");
			   
				 	$.each(data, function(i,item){
						
						$(dest).append("<option value="+ item.id +">"+item.nome+"</option>");
				 	});
				 
				 	$(dest).prev('span.input').text('');
			   }
			 });
			
		};
		
		$(group[0]).removeAttr('disabled');

		jsonLocations(null,group[0]);
		
}; 
