$(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()!="dd/mm/yyyy"){
					$("#txtFeFin_1").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_1").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_2" && $("#"+id).val()!="dd/mm/yyyy"){
					$("#txtFeFin_2").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_2").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_3" && $("#"+id).val()!="dd/mm/yyyy"){
					$("#txtFeFin_3").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_3").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_4" && $("#"+id).val()!="dd/mm/yyyy"){
					$("#txtFeFin_4").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_4").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_5" && $("#"+id).val()!="dd/mm/yyyy"){
					$("#txtFeFin_5").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_5").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_6" && $("#"+id).val()!="dd/mm/yyyy"){
					$("#txtFeFin_6").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_6").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_7" && $("#"+id).val()!="dd/mm/yyyy"){
					$("#txtFeFin_7").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_7").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_8" && $("#"+id).val()!="dd/mm/yyyy"){
					$("#txtFeFin_8").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_8").datepicker('setDate',startDate);
				}
				if(id=="txtFeIni_12" && $("#"+id).val()!="dd/mm/yyyy"){
					$("#txtFeFin_12").datepicker('option', 'minDate',startDate);
					$("#txtFeFin_12").datepicker('setDate',startDate);
				}
			}
		}
		
		
	$.ajax({
		url: "/gets/cargarSoloHoteles.php",
		dataType: "xml",
		success: function( xmlResponse ) {
			var txtOrigenVuelo = $( "hotel", xmlResponse ).map(function() {
				return {
					value: $( "nombre", this ).text(),
					id: $( "keyword", this ).text()
				};
			}).get();
			$( "#txtHotel_1" ).autocomplete({
				source: txtOrigenVuelo,
				minLength: 1,
				select: function( event, ui ) {
				       $("#cboHotel_1").val(ui.item.id);
				}
			});
			$( "#txtHotel_2" ).autocomplete({
				source: txtOrigenVuelo,
				minLength: 1,
				select: function( event, ui ) {
				       $("#cboHotel_2").val(ui.item.id);
				}
			});
			$( "#txtHotel_3" ).autocomplete({
				source: txtOrigenVuelo,
				minLength: 1,
				select: function( event, ui ) {
				       $("#cboHotel_3").val(ui.item.id);
				}
			});
			$( "#txtHotel_12" ).autocomplete({
				source: txtOrigenVuelo,
				minLength: 1,
				select: function( event, ui ) {
				       $("#cboHotel_12").val(ui.item.id);
				}
			});
			$( "#txtHotel_4" ).autocomplete({
				source: txtOrigenVuelo,
				minLength: 1,
				select: function( event, ui ) {
				       $("#cboHotel_4").val(ui.item.id);
				}
			});
			$( "#txtHotel_5" ).autocomplete({
				source: txtOrigenVuelo,
				minLength: 1,
				select: function( event, ui ) {
				       $("#cboHotel_5").val(ui.item.id);
				}
			});
			$( "#txtHotel_8" ).autocomplete({
				source: txtOrigenVuelo,
				minLength: 1,
				select: function( event, ui ) {
				       $("#cboHotel_8").val(ui.item.id);
				}
			});
			
		}
	});
	
	
	$.ajax({
		url: "/gets/cargarHotelesShare.php",
		dataType: "xml",
		success: function( xmlResponse ) {
			var txtOrigenVuelo = $( "hotel", xmlResponse ).map(function() {
				return {
					value: $( "nombre", this ).text(),
					id: $( "keyword", this ).text()
				};
			}).get();
			$( "#txtHotel_6" ).autocomplete({
				source: txtOrigenVuelo,
				minLength: 1,
				select: function( event, ui ) {
				       $("#cboHotel_6").val(ui.item.id);
				}
			});
		}
	});
	
	$.ajax({
		url: "/gets/cargarHotelesNonStop.php",
		dataType: "xml",
		success: function( xmlResponse ) {
			var txtOrigenVuelo = $( "hotel", xmlResponse ).map(function() {
				return {
					value: $( "nombre", this ).text(),
					id: $( "keyword", this ).text()
				};
			}).get();
			$( "#txtHotel_7" ).autocomplete({
				source: txtOrigenVuelo,
				minLength: 1,
				select: function( event, ui ) {
				       $("#cboHotel_7").val(ui.item.id);
				}
			});
		}
	});

	

	

	$("#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("Por favor proporcione su hotel de llegada");
			$("#txtHotel_"+id).focus();
			return false;
		}

		if(fe_1=="dd/mm/yyyy"||fe_1==""){
			alert("Por favor proporcione la fecha de salida");
        	$("#txtFeIni_"+id).focus();
			return false;
		}

		if(fe_2=="dd/mm/yyyy"||fe_1==""){
			alert("Por favor proporcione la fecha de regreso");
			$("#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);
	 			$("div#facebox div.content").css("width","auto");
	 	});		
	});
	

});
