$(document).ready(function() {

   $( "#mensaje" ).dialog({
     modal: true,
     autoOpen: false,
     buttons: {
              Ok: function() {
                  $( this ).dialog( "close" );
              }
     }
   });

   $('#enviar').click(function() {
     if ($('#originCountry').val()=="" || $('#destinationCountry').val()=="" || $('#originAirport').val()=="" || $('#destinationAiport').val()=="") {
       $( "#mensaje" ).dialog( "open" );
       $( "#origen" ).val("");
       $( "#destino" ).val("");
       return false;
     }
     total = parseInt($('#adults').val()) + parseInt($('#children').val()) + parseInt($('#infants').val());
     $("#totalTravellers").val(total);
     $("#buscador_vuelos").submit();
   });
   $( "#origen" ).autocomplete({
      source: function(request, response) {
        $.ajax({
          url: "/req.php?q=" + $("#origen").val(),
          dataType: "xml",
          minLength: 3,
          html: true,
          success: function( xmlResponse ) {
            var data = $( "airport", xmlResponse ).map(function() {
              return {
                value: '<img src="/common/imagenes/banderas/'+$( "idCountry", this ).text()+'.gif" /> ' + $( "name", this ).text() + ' ('+$( "idAirport", this ).text()+')',
                id: $( "idAirport", this ).text(),
                txt: $( "name", this ).text() + ' ('+$( "idAirport", this ).text()+')',
                pais: $( "idCountry", this ).text()
              };
            });
          response(data);
          }
        })
      },
      select: function( event, ui ) {
        $('#originAirport').val(ui.item.id);
        $('#originCountry').val(ui.item.pais);
        $('#origen').val(ui.item.txt);
        return false;
      }
    });

    $( "#destino" ).autocomplete({
      source: function(request, response) {
        $.ajax({
          url: "/req.php?q=" + $("#destino").val(),
          dataType: "xml",
          minLength: 3,
          html: true,
          success: function( xmlResponse ) {
            var data = $( "airport", xmlResponse ).map(function() {
              return {
                value: '<img src="/common/imagenes/banderas/'+$( "idCountry", this ).text()+'.gif" /> ' + $( "name", this ).text() + ' ('+$( "idAirport", this ).text()+')',
                id: $( "idAirport", this ).text(),
                txt: $( "name", this ).text() + ' ('+$( "idAirport", this ).text()+')',
                pais: $( "idCountry", this ).text()
              };
            });
          response(data);
          }
        })
      },
      select: function( event, ui ) {
        $('#destinationAirport').val(ui.item.id);
        $('#destinationCountry').val(ui.item.pais);
        $('#destino').val(ui.item.txt);
        return false;
      }
    });

  $("#fecha_salida").datepicker({
     dateFormat: 'dd/mm/yy',
     minDate: 0,
     numberOfMonths: 2,
     firstDay: 1,
     onSelect: function( selectedDate ) {
        instance = $(this).data("datepicker");

        date = $.datepicker.parseDate(
          instance.settings.dateFormat ||
          $.datepicker._defaults.dateFormat,
          selectedDate, instance.settings );
        date.setDate(date.getDate()+1);

        $("#fecha_vuelta").datepicker("option", "minDate", date);
      }
  });

  $("#fecha_vuelta").datepicker({
     dateFormat: 'dd/mm/yy',
     minDate: 0,
     numberOfMonths: 2,
     firstDay: 1,
     onSelect: function(selectedDate) {
        instance = $(this).data("datepicker");

        date = $.datepicker.parseDate(
          instance.settings.dateFormat ||
          $.datepicker._defaults.dateFormat,
          selectedDate, instance.settings );
        date.setDate(date.getDate()-1);

        $("#fecha_salida").datepicker("option", "maxDate", date);
      }
  });

});

