$(document).ready(function(){
  
  $('.airportPopupLink').show();

  $("#airportModal").dialog({
    bgiframe: true,
    autoOpen: false,
    height: 350,
    width: 620,
    resizable: false,
    modal: true
  });
  var modalLoading = $("#airportModal").html();
  $('#search_form .airportPopupLink, .airportPopup .popupLink').live('click', function(e){
    $("#airportModal").html(modalLoading);
    $("#airportModal").dialog('open');
    $("#airportModal").load(e.target.pathname);
    return false;
  });
  
  $('#search_form').submit(function()
  {
  	if ($('#from_code').val() == '') {
  		alert('Please enter departure city or airport code.');
  		$('#from_code').focus();
  		return false;
  	}
  	if ($('#to_code').val() == '') {
  		alert('Please enter destination city or airport code.');
  		$('#to_code').focus();
  		return false;
  	}	
  	if ($('#from_code').val() == $('#to_code').val() || extractIATACode($('#from_code').val()) == extractIATACode($('#to_code').val())) {
  		alert('Destination city or airport code must be different than the departure city or airport code.');
  		$('#to_code').select();
  		return false;
    }
    if (!departureDateValid('depart_date')) {
      alert("Invalid departure date. We cannot accept a departure date in the past or\nmore than 330 days in the future. Dates must be in the form of mm/dd/yy.");
      $('#depart_date').focus();
      return false;
    }
    if (!returnDateValid('depart_date', 'return_date')) {
      alert('Invalid return date.');
      $('#return_date').focus();
      return false;
    }
  	if (!dateDayDifference('depart_date', 'return_date')) {
  		alert("Packages cannot be booked for longer than 26 days. Please select an earlier return date.");
  		$('#return_date').focus();
  		return false;
  	}
    return true;
  });

});