$(function() {

/*********************************************************************************************** 
	PASO 1 
***********************************************************************************************/
	
	//CALENDARIO EN CAMPOS DE FECHA
	$.datepick.setDefaults({showOn: 'both', buttonImageOnly: true, 
    buttonImage: 'http://www.busescruzdelsur.cl/VEL/js/img/calendar-icon.png', buttonText: 'Calendario',
	yearRange: '2008:2018'	});
	
	$('#vp_fecha_ida').datepick(); 
	$('#vp_fecha_vuelta').datepick(); 
	
	
	//OCULTAR/MOSTRAR CAMPOS DE DATOS DE FECHA DE VUELTA
	$('#vp_soloida').click(function(){
		$('#FilaDatosVuelta').hide();	
	});
	$('#vp_idayvuelta').click(function(){
		$('#FilaDatosVuelta').show();	
	});


/*********************************************************************************************** 
	PASO 6.1 
***********************************************************************************************/

	//FECHAS DE NACIMIENTO EN DATOS DE PASAJEROS
	
	$("input[id^=vp_fecha_nacimiento_conjunto_]").each(function(){
		
		var id =  $(this).attr("id");
		var n = id.substring( id.lastIndexOf('_') + 1 );

		$(this).datepick({ 
			yearRange: '1910:2008',
			beforeShow: function(){ readLinked(n) }, 
			onSelect: function(){ updateLinked(id, n) }, 
			showOn: "both", 
			buttonImageOnly: true 
		}); 
		$("#vp_fecha_nacimiento_mes_" + n + ", #vp_fecha_nacimiento_ano_" + n).change(function(){ checkLinkedDays(n) });
	});
	
	// Prepare to show a date picker linked to three select controls 
	function readLinked(n) { 
		$("#vp_fecha_nacimiento_conjunto_"+ n).val($("#vp_fecha_nacimiento_dia_"+ n).val() + "/" + 
			$("#vp_fecha_nacimiento_mes_"+ n).val()  + "/" + $("#vp_fecha_nacimiento_ano_"+ n).val()); 
		return {}; 
	} 
	 
	// Update three select controls to match a date picker selection 
	function updateLinked(id, n) { 
		var date = $("#" + id).datepick("getDate");
		$("#vp_fecha_nacimiento_dia_"+ n).val(date.getDate()); 
		$("#vp_fecha_nacimiento_mes_"+ n).val(date.getMonth() ); 
		$("#vp_fecha_nacimiento_ano_"+ n).val(date.getFullYear()); 
	} 
	 
	// Prevent selection of invalid dates through the select controls 
	function checkLinkedDays(n) { 
		var daysInMonth = 32 - new Date($("#vp_fecha_nacimiento_ano_"+ n).val(), 
			$("#vp_fecha_nacimiento_mes_"+ n).val() - 1, 32).getDate(); 
		$("#vp_fecha_nacimiento_dia_"+ n + " option").attr("disabled", ""); 
		$("#vp_fecha_nacimiento_dia_"+ n + " option:gt(" + (daysInMonth - 1) +")").attr("disabled", "disabled"); 
		if ($("#vp_fecha_nacimiento_dia_"+ n).val() > daysInMonth) { 
			$("#vp_fecha_nacimiento_dia_"+ n).val(daysInMonth); 
		} 
	} 

	
});