var shuttleFare = {
  webInterface: {
    /* store selected direction */
    to_from:function(v){
      $('#to_from_value').attr("value",v)
      $('#to_from_display').text(v+" airport")
      $('#location_select').show('slow')
    },
    /* store selected direction */
    landmark:function(v){
      if(v == 'hotel') {
        $('#loc_resi').hide('fast')
        $('#loc_hotel').show('slow')
      }

      else {
        $('#loc_hotel').hide('fast')
        $('#loc_resi').show('slow')
    }
        $('#landmark_value').attr("value",v)
    },
    /* change return field status when selecting roundtrip/oneway */
    toggleReturn:function(bOn) {
      if (bOn) {
        $('#search_return_day').removeAttr("disabled");
      } else {
        $('#search_return_day').attr("disabled", true);
      }
    },
    showSearchOpts: function(){
      $('#search_opts').show('slow')
    },
    ajaxLogin: function() {
      $.post("/customers", $('#ajax_login').serialize(), function(data) {
        $("#login_area_form").replaceWith(data["html"])
        if (data["header"] != undefined )
          $(".member-signin").html(data["header"])
        $("#card_name_f").attr("value", data["user"].first)
        $("#card_name_l").attr("value", data["user"].last)

      }, "json")
    },
    showLogin: function(e) {
      //show login box on checkout page
      e.preventDefault()
      
      $("#login_area").show('slow')
      $('#ajax_login [name="customer[login]"]').blur(function(e) {
        if (e.target.value.length>0) {
          $('#reservation-form [name="customer[login]"]').attr("value", e.target.value)
        } 
      })
      $('#ajax_login [name="customer[login]"]').blur(function(e) {
        if (e.target.value.length>0) {
          $('.new_customer').hide('fast')
        } else {
          $('.new_customer').show('fast')

        }
      })
      $(".checkout-login").hide('slow')
    }
  }
}

function setText(id,txt) {
  document.getElementById(id).innerHTML = txt;
}
function clearText(thefield){
  if (thefield.defaultValue==thefield.value)
    thefield.value = ""
}

function CloseFacebox() {
  $(document).trigger('close.facebox');
}

function setRadio(radio) {
  $(radio).attr("checked","checked");
}

// ==================================================
// Function: updatePickupDropoff()
// Purpose: multiple search forms, pull values from
// other forms before submitting
// ==================================================
function updatePickupDropoff() {
  modify = document.getElementById('modify_form');
  pickup = document.getElementById('pickup_dropoff');
	
  pickup.search_flight_day.value = modify.search_flight_day.value;
  pickup.search_flight_time.value = modify.search_flight_time.value;
  pickup.search_return_day.value = modify.search_return_day.value;
  pickup.search_return_time.value = modify.search_return_time.value;
  pickup.search_domestic.value = $("#modify #search_domestic_true").is(':checked')
  pickup.search_adults.value = modify.search_adults.value;
  pickup.search_childs.value = modify.search_childs.value;
	
	
  $('#button_search').hide();
  $('#button_processing').show();

  return true;
}

// ==================================================
// Function: clearAllErrors()
// Purpose: clears the div border error on fields
// ==================================================
var errorColor = "red";
var errorBorder = "2px solid red";
var landmark_label = "Hotel/Landmark and City, State";
//var address_label  = "Street Address";
var zip_label      = "Zip Code";

function clearAllErrors(form) {
  $(form + ' #airportName').css('border',"1px solid #aaa");
  $(form + ' #airportName').css('padding',"4px");
	
  $(form + ' #search_landmark').css('border',"1px solid #aaa");
  $(form + ' #search_landmark').css('padding',"4px");
	
  //$(form + ' #search_address').css('border',"1px solid #aaa");
  //$(form + ' #search_address').css('padding',"4px");
	
  $(form + ' #search_zipcode').css('border',"1px solid #aaa");
  $(form + ' #search_zipcode').css('padding',"4px");
	
  if ($(form + ' #search_landmark').val() == landmark_label) {
    $(form + ' #search_landmark').val('');
  }
  //if ($(form + ' #search_address').val() == address_label) { $(form + ' #search_address').val(''); }
  if ($(form + ' #search_zipcode').val() == zip_label) {
    $(form + ' #search_zipcode').val('');
  }
}
function setLabels(form) {
  if ($(form + ' #search_landmark').val() == '' ) {
    $(form + ' #search_landmark').val(landmark_label);
  }
  //if ($(form + ' #search_address').val() == '' ) { $(form + ' #search_address').val(address_label); }
  if ($(form + ' #search_zipcode').val() == '' ) {
    $(form + ' #search_zipcode').val(zip_label);
  }
}
// ==================================================
// Function: validateZip()
// Purpose: validates search fields
// ==================================================
function validateZip(form,errorField) {
	
  form = "#" + form;
	
  // Clear default labels before validation
  clearAllErrors(form);
	
  // Airport must be selected
  if ( $(form + ' #search_airport_id').val() == '0' || $(form + ' #search_airport_id').val() == '' ) {
    $(form + ' #airportName').css('border',errorBorder);
    setLabels(form);
    alert('Please Select Your Airport');
    return false;
  }
	
  // Home/Office is checked Zipcode
  if ( $(form + ' #search_zipcode').val() == '' ) {
    $(form + ' #search_zipcode').css('border',errorBorder);
    setLabels(form);
    alert('Please enter 5 digit zip code');
    return false;
  }
  if ($(form + ' #search_zipcode').val().match(/^\d{5}$/) == null) {
    $(form + ' #search_zipcode').css('border',errorBorder);
    setLabels(form);
    alert('Zip code must be 5 digits');
    return false;
  }

  $(form + ' #search_search_type').val('Zip');
	
  $('#button_search_zip').hide();
  $('#button_search_landmark').hide();
  $('#button_processing_zip').show();

  return true;
}
function validateLandmark(form,errorField) {
	
  form = "#" + form;
	
  // Clear default labels before validation
  clearAllErrors(form);
	
  // Airport must be selected
  if ( $(form + ' #search_airport_id').val() == '0' || $(form + ' #search_airport_id').val() == '' ) {
    $(form + ' #airportName').css('border',errorBorder);
    setLabels(form);
    alert('Please Select Your Airport');
    return false;
  }
	
  if ( $(form + ' #search_landmark').val() == '' ) {
    $(form + ' #search_landmark').val(landmark_label);
    $(form + ' #search_landmark').css('border',errorBorder);
    setLabels(form);
    alert('Please enter Hotel/Landmark');
    return false;
  }
	
  $(form + ' #search_search_type').val('Landmark');
	
  $('#button_search_zip').hide();
  $('#button_search_landmark').hide();
  $('#button_processing_landmark').show();

  return true;
}

function validateSearch(form,errorField) {
	
  form = "#" + form;
	
  // Clear default labels before validation
  clearAllErrors(form);
	
  // Airport must be selected
  if ( $(form + ' #search_airport_id').val() == '0' ) {
    $(form + ' #airportName').css('border',errorBorder);
    setLabels(form);
    alert('Please Select Your Airport');
    return false;
  }
	
  // Home/Office is checked
  //if ( $(form + ' #search_search_type_zip').is(':checked')) {
  if ( $(form + ' #search_search_type').val() == 'Zip') {
    // Address
    //if ( $(form + ' #search_address').val() == '' ) {
    //	 $(form + ' #search_address').css('border',errorBorder);
    //	 setLabels(form);
    //	 alert('Please enter street address');
    //   return false;
    //}
    //if ($(form + ' #search_address').val().match(/^(\d)/) == null) {
    //	$(form + ' #search_address').css('border',errorBorder);
    //	setLabels(form);
    //	alert('Street number missing in address');
    //	return false;
    //}
    //if ($(form + ' #search_address').val().match(/\s\w/) == null) {
    //	$(form + ' #search_address').css('border',errorBorder);
    //	setLabels(form);
    //	alert('Street name missing in address');
    //	return false;
    //}
		
    // Zipcode
    if ( $(form + ' #search_zipcode').val() == '' ) {
      $(form + ' #search_zipcode').css('border',errorBorder);
      setLabels(form);
      alert('Please enter 5 digit zip code');
      return false;
    }
    if ($(form + ' #search_zipcode').val().match(/^\d{5}$/) == null) {
      $(form + ' #search_zipcode').css('border',errorBorder);
      setLabels(form);
      alert('Zip code must be 5 digits');
      return false;
    }
  }
  // Landmark is checked
  else {
    if ( $(form + ' #search_landmark').val() == '' ) {
      $(form + ' #search_landmark').val(landmark_label);
      $(form + ' #search_landmark').css('border',errorBorder);
      setLabels(form);
      alert('Please enter Hotel/Landmark');
      return false;
    }
  }
	
  // Validate Flight Date is less than Return Date
  if ( $(form + ' #search_oneway_false').is(':checked')) {
    var flight_day   = new Date($(form + ' #flight_day').val());
    var return_day = new Date($(form + ' #return_day').val());
	
    flight_day = flight_day.getTime();
    return_day = return_day.getTime();
	
    if (flight_day > return_day) {
      alert('Please select a valid Return Date');
      return false;
    }
    if(flight_day == return_day) {
      flight_time   = $(form + ' #search_flight_time').val();
      return_time = $(form + ' #search_return_time').val();
		
      a = parseInt(flight_time.split(":")[0]);
      d = parseInt(return_time.split(":")[0]);
		
      if ( a > d ) {
        alert('Please select a valid Return Date');
        return false;
      }
      if ( a == d ) {
        // hour is equal, check minutes
        a = parseInt(flight_time.split(":")[1]);
        d = parseInt(return_time.split(":")[1]);
        if ( a >= d ) {
          alert('Please select a valid Return Date');
          return false;
        }
      }
    }
  }
	
  $('#button_search').hide();
  $('#button_processing').show();

  return true;
}
// ==================================================
// Function: validateModifySearch()
// Purpose: validates search fields
// ==================================================
function validateModifySearch(form,errorField) {
	
  form = "#" + form;
	
  // Validate Flight Date is less than Return Date
  if ( $(form + ' #search_oneway').val() == 'false') {
    var flight_day   = new Date($(form + ' #search_flight_day').val());
    var return_day = new Date($(form + ' #search_return_day').val());
	
    flight_day = flight_day.getTime();
    return_day = return_day.getTime();
	
    if (flight_day > return_day) {
      alert('Please select a valid Return Date');
      return false;
    }
    if(flight_day == return_day) {
      flight_time   = $(form + ' #search_flight_time').val();
      return_time = $(form + ' #search_return_time').val();
		
      a = parseInt(flight_time.split(":")[0]);
      d = parseInt(return_time.split(":")[0]);
		
      if ( a > d ) {
        alert('Please select a valid Return Date');
        return false;
      }
      if ( a == d ) {
        // hour is equal, check minutes
        a = parseInt(flight_time.split(":")[1]);
        d = parseInt(return_time.split(":")[1]);
        if ( a >= d ) {
          alert('Please select a valid Return Date');
          return false;
        }
      }
    }
  }
	
  $('#update_on').show();

  return true;
}


// ==================================================
// Function: validateReservation()
// Purpose: verifies input parameters for reservation
// ==================================================
function validateReservation() {
  if ( $('#reservation_loc_address').val() == '') {
    setText('errors_reserve','Please enter an address');
    return false;
  }
  if ($('#reservation_loc_address').val().match(/^(\d)/) == null) {
    setText('errors_reserve','Street number missing in address');
    return false;
  }
  if ($('reservation_loc_address').val().match(/\s\w/) == null) {
    setText('errors_reserve','Street address missing');
    return false;
  }
  setText('errors_reserve','');
  return true;
}

function unescapePlus( str ) {
  return unescape( str ).replace( /\+/g, ' ' );
}

function bookmarkPage(){
  if (window.sidebar && window.sidebar.addPanel) {
    // Mozilla Firefox Bookmark
    window.sidebar.addPanel(document.title, 'http://www.shuttlefare.com',"");
  } else if(window.external) {
    // IE Favorite
    window.external.AddFavorite('http://www.shuttlefare.com', document.title);
  } else if(window.opera && window.print) {
    // Opera Hotlist
    return true;
  }
}

function backHistory() {
  history.go(-1);
}
  