jQuery(document).ready(function(){

$('#advanced').hide();

$('#show_adv').click(function() {
$('#advanced').toggle();
});

   var msg="";
    $("form").submit(function() {
      if ($("#departure_port").val() == "") {
        msg="Wybierz lub wpisz port wylotu!\n";
      }
      
      if ($("#arrival_port").val() == "") {
        msg=msg+"Wybierz lub wpisz port docelowy!\n";
      }
if ($("#dep_date").val() == "") {
        msg=msg+"Podaj datę wylotu!\n";
      }

if ($("#arr_date").val() == "") {
        msg=msg+"Podaj datę powrotu!\n";
      }


if (msg!="") {
alert(msg);
msg="";
return false;      
      }
      
      return true;
    });



$('#oneway').click(function() {

    $('#arr_date').attr("disabled", true);
    $('#arr_date').hide();
       }); 

  $('#return').click(function() {
$('#arr_date').removeAttr("disabled");
$('#arr_date').show();
       }); 


  $('#copy_flights').click(function() {

var city=$('#arrival_port').val();


$('#city').val(city.substr(0,strpos(city, '[', 0)-1));


$('#checkin').val($('#dep_date').val());
$('#checkout').val($('#arr_date').val());
       }); 



$('#departure_port').click(function() {
$('#departure_port').val("");
});

$('#arrival_port').click(function() {
$('#arrival_port').val("");
});

$('#arrival_port_2').click(function() {
$('#arrival_port_2').val("");
});

$("span.apt").live("click", function(){
  $("#departure_port").val($(this).text() );
  $('#dep_port').dialog("close");
$("#adt_count").show();
$("#child_count").show();
$("#inf_count").show();
$("#flex_date").show();
$("#specific_airline1").show();

});


$("span.apt2").live("click", function(){
  $("#arrival_port").val($(this).text() );
  $('#arr_port').dialog("close");
$("#modal").hide();
$("#adt_count").show();
$("#child_count").show();
$("#inf_count").show();
$("#flex_date").show();
$("#specific_airline1").show();

});

$("span.apt3").live("click", function(){
  $("#arrival_port_2").val($(this).text() );
  $('#arr_port_2').dialog("close");
$("#modal").hide();
$("#adt_count").show();
$("#child_count").show();
$("#inf_count").show();
$("#flex_date").show();
$("#specific_airline1").show();


});



$("#departure_port").autocomplete(dep_apt_table);
$("#arrival_port").autocomplete(dep_apt_table);

$("#departure_port2").autocomplete(dep_apt_table);
$("#arrival_port2").autocomplete(dep_apt_table);

$("#departure_port3").autocomplete(dep_apt_table);
$("#arrival_port3").autocomplete(dep_apt_table);

$("#departure_port4").autocomplete(dep_apt_table);
$("#arrival_port4").autocomplete(dep_apt_table);

$("#departure_port5").autocomplete(dep_apt_table);
$("#arrival_port5").autocomplete(dep_apt_table);

$("#departure_port6").autocomplete(dep_apt_table);
$("#arrival_port6").autocomplete(dep_apt_table);


$("#arrival_port_2").autocomplete(dep_apt_table);

$("#multi_dep_1").autocomplete(dep_apt_table);
$("#multi_dep_2").autocomplete(dep_apt_table);
$("#multi_dep_3").autocomplete(dep_apt_table);

$("#multi_arr_1").autocomplete(dep_apt_table);
$("#multi_arr_2").autocomplete(dep_apt_table);
$("#multi_arr_3").autocomplete(dep_apt_table);

$(function() {
		$("#dep_date").datepicker({dateFormat: 'yy-mm-dd' ,
    maxDate:'+12m',minDate:'0d',
     rangeSelect: false, numberOfMonths: 2, 
    showOn: 'focus',
    showAnim: 'slideDown',speed:'fast',
    onClose: function(date) { 
    if ($('#arr_date').val()<date) $('#arr_date').val(date);
    var data=date.split("-"); 
    $('#arr_date').datepicker('option', 'minDate', new Date(data[0],data[1]-1,data[2]));
    }
    });
	});



$(function() {
		$("#dep_date2").datepicker({dateFormat: 'yy-mm-dd' ,
    maxDate:'+12m',minDate:'0d',
     rangeSelect: false, numberOfMonths: 2, 
    showOn: 'focus',
    showAnim: 'slideDown',speed:'fast'
    });
	});

$(function() {
		$("#dep_date3").datepicker({dateFormat: 'yy-mm-dd' ,
    maxDate:'+12m',minDate:'0d',
     rangeSelect: false, numberOfMonths: 2, 
    showOn: 'focus',
    showAnim: 'slideDown',speed:'fast'
    });
	});

$(function() {
		$("#dep_date4").datepicker({dateFormat: 'yy-mm-dd' ,
    maxDate:'+12m',minDate:'0d',
     rangeSelect: false, numberOfMonths: 2, 
    showOn: 'focus',
    showAnim: 'slideDown',speed:'fast'
    });
	});

$(function() {
		$("#dep_date5").datepicker({dateFormat: 'yy-mm-dd' ,
    maxDate:'+12m',minDate:'0d',
     rangeSelect: false, numberOfMonths: 2, 
    showOn: 'focus',
    showAnim: 'slideDown',speed:'fast'
    });
	});

$(function() {
		$("#dep_date6").datepicker({dateFormat: 'yy-mm-dd' ,
    maxDate:'+12m',minDate:'0d',
     rangeSelect: false, numberOfMonths: 2, 
    showOn: 'focus',
    showAnim: 'slideDown',speed:'fast'
    });
	});


$(function() {
		$("#checkin").datepicker({dateFormat: 'yy-mm-dd' ,
    maxDate:'+12m',minDate:'0d',
     rangeSelect: false, numberOfMonths: 2, 
    showOn: 'focus',
    showAnim: 'slideDown',speed:'fast',
    onClose: function(date) { 
    if ($('#checkout').val()<date) $('#checkout').val(date);
    var data=date.split("-"); 
    $('#h_arr_date').datepicker('option', 'minDate', new Date(data[0],data[1]-1,data[2]));
    }
    });
	});


$(function() {
		$("#d_birth_chd1").datepicker({dateFormat: 'yy-mm-dd' ,
    maxDate:'0d',minDate:'-144m',
     rangeSelect: false, numberOfMonths: 2, 
    showOn: 'focus',
    showAnim: 'slideDown',speed:'fast',
    onClose: function(date) { 
    }
    });
	});


$(function() {
		$("#checkout").datepicker({dateFormat: 'yy-mm-dd' ,
    maxDate:'+12m',minDate:'0d',rangeSelect: false, numberOfMonths: 2, 
    showOn: 'focus',
    showAnim:'slideDown',speed:'fast'});
});



$(function() {
		$("#arr_date").datepicker({dateFormat: 'yy-mm-dd' ,
    maxDate:'+12m',minDate:'0d',rangeSelect: false, numberOfMonths: 2, 
    showOn: 'focus',
    showAnim:'slideDown',speed:'fast'});
});



function strpos( haystack, needle, offset){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
 
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}


$('#dep_port').dialog({
autoOpen: false,
width: 500,height:450,
close: function() { 
$("#modal").hide();
$("#adt_count").show();
$("#child_count").show();
$("#inf_count").show();
$("#flex_date").show();
$("#specific_airline1").show();

} 
});



$('#arr_port').dialog({
autoOpen: false,
width: 500,height:450 
,close: function() { $("#modal").hide(); 
$("#adt_count").show();
$("#child_count").show();
$("#inf_count").show();
$("#flex_date").show();
$("#specific_airline1").show();

}
});

$('#arr_port_2').dialog({
autoOpen: false,
width: 500,height:450 
,close: function() { $("#modal").hide(); 
$("#adt_count").show();
$("#child_count").show();
$("#inf_count").show();
$("#flex_date").show();
$("#specific_airline1").show();

}
});


    $("#dep_combo").change(function () {
          var str = "";
          $("#dep_combo option:selected").each(function () {
                str += $(this).val();
              });

$.ajax({
   type: "GET",
   url: "m_apt_list1.php",
   data: "country="+str+"&language=<?echo($language);?>",
    error: function(){
        alert('Blad polaczenia z baza danych');
    },success: function(msg){
$("#dep_apt_list").html(msg);}
});
        });;


    $("#arr_combo").change(function () {
          var str = "";
          $("#arr_combo option:selected").each(function () {
                str += $(this).val();
              });

$.ajax({
   type: "GET",
   url: "m_apt_list2.php",
   data: "country="+str+"&language=<?echo($language);?>",
    error: function(){
        alert('Blad polaczenia z baza danych');
    },success: function(msg){
$("#arr_apt_list").html(msg);}
});
        });


    $("#arr_combo_2").change(function () {
          var str = "";
          $("#arr_combo_2 option:selected").each(function () {
                str += $(this).val();
              });

$.ajax({
   type: "GET",
   url: "m_apt_list3.php",
   data: "country="+str+"&language=<?echo($language);?>",
    error: function(){
        alert('Blad polaczenia z baza danych');
    },success: function(msg){
$("#arr_apt_list_2").html(msg);}
});
        });


$('#last_search_win').dialog({
autoOpen: false,
width: 500,height:450,
buttons: {
"Ok": function() {
 $(this).dialog("close"); 
 $("#modal").hide();
 $("#adt_count").show();
$("#child_count").show();
$("#inf_count").show();
 $("#flex_date").show();
$("#specific_airline1").show();

 }
},

close: function() { 
$("#modal").hide();
$("#adt_count").show();
$("#child_count").show();
$("#inf_count").show();
$("#flex_date").show();
$("#specific_airline1").show();

} 
});

$('#last_search').click(function() {
$("#modal").show();
$("#adt_count").hide();
$("#child_count").hide();
$("#inf_count").hide();
$("#flex_date").hide();
$("#specific_airline1").hide();
$('#last_search_win').dialog('open');
});



$('#dep_list').click(function() {
$("#modal").show();
$("#adt_count").hide();
$("#child_count").hide();
$("#inf_count").hide();
$("#flex_date").hide();
$("#specific_airline1").hide();

$('#dep_port').dialog('open');
});


$('#arr_list').click(function() {
$("#modal").show();
$("#adt_count").hide();
$("#child_count").hide();
$("#inf_count").hide();
$("#flex_date").hide();
$("#specific_airline1").hide();

$('#arr_port').dialog('open');
});

$('#arr_list_2').click(function() {
$("#modal").show();
$("#adt_count").hide();
$("#child_count").hide();
$("#inf_count").hide();
$("#flex_date").hide();
$("#specific_airline1").hide();

$('#arr_port_2').dialog('open');
});


});

