$(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 ($('#Legs2:checked').length > 0 && !returnDateValid('depart_date', 'return_date')) {
      alert('Invalid return date.');
      $('#return_date').focus();
      return false;
    }
    return true;
  });

  $('#Legs2').click(function(){
    $('#return_caption, #return_select, #return_date, .returntrip, #return_cell').css('visibility', 'visible');
  });
  $('#Legs1').click(function(){
    $('#return_caption, #return_select, #return_date, .returntrip, #return_cell').css('visibility', 'hidden');
  });

});
