$(document).ready(function(){

	//Configuracion de las fechas
	$("input.date-picker-alterno").datepicker({minDate:2,onClose:function(input){moverDia($(this).attr("id"));},numberOfMonths: 2});

	$(".datepicker-alterno").click(function(){
		var abuelo = $(this).parent().parent();
		$("li#" + abuelo.attr("id") + " input.date-picker-alterno").focus();
	});

		function moverDia(id){
			var startDate = $("#"+id).datepicker('getDate');
			if (startDate != null) {
				startDate.setDate(startDate.getDate()+1);
				if(id=="txtFeIni_1" && $("#"+id).val()!="mm/dd/yyyy"){
					$("#txtFeFin_1").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_1").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_2" && $("#"+id).val()!="mm/dd/yyyy"){
					$("#txtFeFin_2").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_2").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_3" && $("#"+id).val()!="mm/dd/yyyy"){
					$("#txtFeFin_3").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_3").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_4" && $("#"+id).val()!="mm/dd/yyyy"){
					$("#txtFeFin_4").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_4").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_5" && $("#"+id).val()!="mm/dd/yyyy"){
					$("#txtFeFin_5").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_5").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_6" && $("#"+id).val()!="mm/dd/yyyy"){
					$("#txtFeFin_6").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_6").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_7" && $("#"+id).val()!="mm/dd/yyyy"){
					$("#txtFeFin_7").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_7").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_8" && $("#"+id).val()!="mm/dd/yyyy"){
					$("#txtFeFin_8").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_8").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_12" && $("#"+id).val()!="mm/dd/yyyy"){
					$("#txtFeFin_12").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_12").datepicker('setDate',startDate);
				}
			}
		}
	
	
	$("#menu_vehiculos ul li a").click(function(e){
		e.preventDefault();
		var valores = $(this).attr("name").split('_');
		var cboHotel = $("#cboHotel_1").val();
		var txtHotel = $("#txtHotel_1").val();
		
		$(".contenedor_descrip_vehiculo").hide();
		$(".contenedor_precio").hide();						
		
		$("#contenedor_descrip_vehiculo_"+valores[1]).show();
		
		$("#txtFolio").val(valores[1]);
		
		$("#menu_vehiculos ul li a").removeClass("selected");
		$("#menu_vehiculos ul li a").css("height","auto");
		$(this).addClass("selected");
		
		if(txtHotel!=""){
			obtenerTarifasTraslado();
		}					
		
	});
	
	
	$("#frmTransferAlterno").submit(function(){
		var id = $("#txtFolio").val();
		var txtAccion = $("#txtAccion").val();
		
	
		var txtTo = $("#txtHotel_"+id).val();
		var to = $("#cboHotel_"+id).val();
	
		if(txtTo==""){
			alert("Please select your Hotel");
			$("#txtHotel_"+id).focus();
			$("#cboHotel_"+id).val("select");
		}
	
		if(to=="select"){
			alert("Please select your Hotel");
			$("#txtHotel_"+id).focus();
			$("#cboHotel_"+id).val("select");
			return false;
		}		
		
		if(txtAccion==0)
			return false;
		
	});
	
	$(".reservarTrasladoRT").click(function(){
		$("#txtViaje_1").val("Round trip");
		$("#txtAccion").val(1);
		
		var tipo = $("#txtTipo").val();
		
		switch(tipo){
			case '2':$("#txtFolio").val(6);break;
			case '3':$("#txtFolio").val(7);break;
		}
		
		$("#frmTransferAlterno").submit();
	});
	
	$(".reservarTrasladoOW").click(function(){
		$("#txtViaje_1").val("One Way");
		$("#txtAccion").val(1);
		
		var tipo = $("#txtTipo").val();
		
		switch(tipo){
			case '2':$("#txtFolio").val(6);break;
			case '3':$("#txtFolio").val(7);break;
		}
		
		$("#frmTransferAlterno").submit();
	});
	
	

	$("#txtHotel_1").autocomplete("/includes/cargarSoloHoteles.php", {
		width: 260,
		selectFirst: false
	});

	$("#txtHotel_1").result(function(event, data, formatted) {
		if (data){
			$("#cboHotel_1").val(data[1]);
			obtenerTarifasTraslado();
		}
	});
	
	
	function obtenerTarifasTraslado(){
		var txtServicio = $("#txtServicio").val();
		var cboHotel = $("#cboHotel_1").val();
		var txtFolio = $("#txtFolio").val();
		var txtPax = $("#txtPax_1").val();
		
		$.post("/gets/obtenerTarifaTraslado.php",{cboHotel:cboHotel,txtServicio:txtServicio,txtFolio:txtFolio,txtPax:txtPax},function(data){
			sucess:
				if(data.error==0){
					$(".contenedor_precio").hide();
					$("#precio_rt_"+txtFolio).empty().text("$"+data.precio_rt+" "+data.moneda);
					$("#precio_ow_"+txtFolio).empty().text("$"+data.precio_ow+" "+data.moneda);
					$("#contenedor_precio_"+txtFolio).show();
				}
		},"json");		
	}
	

	$("#txtHotel_2").autocomplete("/includes/cargarSoloHoteles.php", {
		width: 260,
		selectFirst: false
	});

	$("#txtHotel_2").result(function(event, data, formatted) {
		if (data){
			$("#cboHotel_2").val(data[1]);
		}
	});

	$("#txtHotel_3").autocomplete("/includes/cargarSoloHoteles.php", {
		width: 260,
		selectFirst: false
	});

	$("#txtHotel_3").result(function(event, data, formatted) {
		if (data){
			$("#cboHotel_3").val(data[1]);
		}
	});
	
	$("#txtHotel_12").autocomplete("/includes/cargarSoloHoteles.php", {
		width: 260,
		selectFirst: false
	});

	$("#txtHotel_12").result(function(event, data, formatted) {
		if (data){
			$("#cboHotel_12").val(data[1]);
		}
	});

	$("#txtHotel_4").autocomplete("/includes/cargarSoloHoteles.php", {
		width: 260,
		selectFirst: false
	});

	$("#txtHotel_4").result(function(event, data, formatted) {
		if (data){
			$("#cboHotel_4").val(data[1]);
		}
	});

	$("#txtHotel_5").autocomplete("/includes/cargarSoloHoteles.php", {
		width: 260,
		selectFirst: false
	});

	$("#txtHotel_5").result(function(event, data, formatted) {
		if (data){
			$("#cboHotel_5").val(data[1]);
		}
	});

	$("#txtHotel_6").autocomplete("/includes/cargarHotelesShare.php", {
		width: 260,
		selectFirst: false
	});

	$("#txtHotel_6").result(function(event, data, formatted) {
		if (data){
			$("#cboHotel_6").val(data[1]);
		}
	});

	$("#txtHotel_7").autocomplete("/includes/cargarHotelesNonStop.php", {
		width: 260,
		selectFirst: false
	});

	$("#txtHotel_7").result(function(event, data, formatted) {
		if (data){
			$("#cboHotel_7").val(data[1]);
		}
	});

	$("#txtHotel_8").autocomplete("/includes/cargarHotelesShare.php", {
		width: 260,
		selectFirst: false
	});

	$("#txtHotel_8").result(function(event, data, formatted) {
		if (data){
			$("#cboHotel_8").val(data[1]);
		}
	});
	
	

	$("#txtHotel_1").keypress(function(e){
		if(e.keyCode==8){
			$("#cboHotel_1").val("select");
		}

	});

	$("#txtHotel_2").keypress(function(e){
		if(e.keyCode==8){
			$("#cboHotel_2").val("select");
		}

	});

	$("#txtHotel_3").keypress(function(e){
		if(e.keyCode==8){
			$("#cboHotel_3").val("select");
		}

	});
	
	$("#txtHotel_12").keypress(function(e){
		if(e.keyCode==8){
			$("#cboHotel_12").val("select");
		}

	});

	$("#txtHotel_4").keypress(function(e){
		if(e.keyCode==8){
			$("#cboHotel_4").val("select");
		}

	});

	$("#txtHotel_5").keypress(function(e){
		if(e.keyCode==8){
			$("#cboHotel_5").val("select");
		}

	});

	$("#txtHotel_6").keypress(function(e){
		if(e.keyCode==8){
			$("#cboHotel_6").val("select");
		}

	});

	$("#txtHotel_7").keypress(function(e){
		if(e.keyCode==8){
			$("#cboHotel_7").val("select");
		}

	});

	$("#txtHotel_8").keypress(function(e){
		if(e.keyCode==8){
			$("#cboHotel_8").val("select");
		}

	});

    //Para reservar desde el listado de autos
	$(".seleccionar_auto").click(function(){
		var vehiculo = $(this).val();
		$('.info_listado').find('.fila_vehiculo:visible').fadeOut(0);

		if($(this).attr("checked"))
			$("#fila_vehiculo_" + vehiculo).show();
	});

	$(".reservar_transfers").click(function(e){
		e.preventDefault();
		var vehiculo = $(this).attr("name");

		var valores = vehiculo.split("_");

		$("#txtFolio").val(valores[1]);
		$("#frmQuickAlternativo").submit();
	});

	//Para validar el formulario de traslados
	$("#frmQuickAlternativo").submit(function(){
		var id = $("#txtFolio").val();

		var txtTo = $("#txtHotel_"+id).val();
		var to = $("#cboHotel_"+id).val();
		var fe_1 = $("#txtFeIni_"+id).val();
		var fe_2 = $("#txtFeFin_"+id).val();

		if(txtTo==""){
			$("#cboHotel_"+id).val("select");
			to="select";
		}

		if(to=="select"){
			alert("Please select your Hotel");
			$("#txtHotel_"+id).focus();
			return false;
		}

		if(fe_1=="mm/dd/yyyy"||fe_1==""){
			alert("Please enter arrival");
        	$("#txtFeIni_"+id).focus();
			return false;
		}

		if(fe_2=="mm/dd/yyyy"||fe_1==""){
			alert("Please enter Departure");
			$("#txtFeFin_"+id).focus();
			return false;
		}


		return true;
	});
	
	
	//Con esto agrego al carrito el tour de los listados
	$(".instrucciones_transfers").click(function(e){
		e.preventDefault();
		//$("#contenedor_agency").toggle();
		$.post('/includes/frmInstruccionesTraslados.php',{}, function(data) {
	 		sucess:
	 			$.facebox(data);
	 	});		
	});
	
	

});
