$(document).ready(function(){ 
  
$(".numbersOnly").keypress(function(e) {
  // if the letter is not digit or decimal point then do nothing
  if( e.which!=8 && e.which!=0 && e.which!=46 && (e.which<48 || e.which>57)) {
    return false;  
  } 
}); /* end keypress */

/* onChange event handlers here, written in the jQuery notation */

$("#days").change(function() {
  // update trip durations on site
  var v = $(this).val() ;  $("#tripduration").val(v) ; 
  $("#tripduration2").val(v) ; $("#tripduration3").val(v) ;
  // recalculate values that rely on this value in the calculations
  calculateCarRental() ;
  calculateHotelCost() ;
  calculateCampgroundCost() ;
  calculateRestaurantCost() ;
  calculateGroceryCost();
  calculateTotals() ;
});

$("#people").change(function() {
  // update number of people on site
  var v = $(this).val() ;  $("#totalpeople").val(v) ;
  $("#totalpeople2").val(v) ;  $("#totalpeople3").val(v) ;
  // recalculate values that rely on this value in the calculations
  calculateFlightTotals() ;
  calculateHotelCost() ;
  calculateCampgroundCost() ;
  calculateRestaurantCost() ;
  calculateGroceryCost();
  calculateTotals() ;
});
$("#pplitre").change(function() {
  // recalculate values that rely on this value in the calculations
  calculateCarRentalFuel() ;
  calculateCarFuelCost() ;
  calculateRVFuelCost() ;
  calculateTotals() ;
});

$("#lkmcar").change(function() {
  // recalculate values that rely on this value in the calculations
  calculateCarFuelCost() ;
  $("#mpgcar").val(parseFloat( (60 / ($(this).val() / 4.55)) ).toFixed(2));
  calculateTotals() ;
});

$("#lkmtow").change(function() {
  // recalculate values that rely on this value in the calculations
  calculateRVFuelCost() ;
  $("#mpgtow").val(parseFloat( (60 / ($(this).val() / 4.55)) ).toFixed(2));
  calculateTotals() ;
});

$("#distancekm").change(function() {
  // recalculate values that rely on this value in the calculations
  calculateCarFuelCost() ;
  calculateRVFuelCost() ;
  // update milage rating
  $("#distancemiles").val(parseFloat($(this).val() / 1.609).toFixed(2)) ;
  calculateTotals() ;
});

$("#carprice").change(function() {
  // recalculate values that rely on this value in the calculations
  calculateCarRental() ;
  calculateTotals() ;
});

$("#distancerentalkm").change(function() {
  // recalculate values that rely on this value in the calculations
  calculateCarRentalFuel() ;
  $("#distancerentalmiles").val(parseFloat($(this).val() / 1.609).toFixed(2)) ;
  calculateTotals() ;
});

$("#personalmeals").change(function() {
  // recalculate values that rely on this value in the calculations
  calculateRestaurantCost() ;
  calculateTotals() ;
});

$("#personalgroceries").change(function() {
  // recalculate values that rely on this value in the calculations
  calculateGroceryCost();
  calculateTotals() ;
});

$("#personalairfares").change(function() {
  // recalculate values that rely on this value in the calculations
  calculateFlightTotals() ;
  calculateTotals() ;
});

$("#motelrate").change(function() {
  // recalculate values that rely on this value in the calculations
  calculateHotelCost() ;
  calculateTotals() ;
});

$("#campground").change(function() {
  // recalculate values that rely on this value in the calculations
  calculateCampgroundCost() ;
  calculateTotals() ;
});

/* calculates the flight totals */
function calculateFlightTotals() {
  var total = parseFloat($("#people").val() * $("#personalairfares").val()) ;
  $("#totalflights").val(total.toFixed(2)) ;
}

/* calculate the car rental charge */
function calculateCarRental() {
  var total = parseFloat($("#days").val() * $("#carprice").val()) ;
  $("#totalcar").val(total.toFixed(2)) ;
} 

/* calculate car rental fuel */
function calculateCarRentalFuel() {
  var total = parseFloat($("#distancerentalkm").val() * $("#lkmcar").val()
     * $("#pplitre").val()) / 100 ;
  $("#totalrentalfuel").val(total.toFixed(2)) ;
} 

/* calculate the car fuel cost on a trip */
function calculateCarFuelCost() {
  var total = parseFloat($("#distancekm").val() * $("#pplitre").val() 
     * $("#lkmcar").val()) / 100 ;
  $("#totalfuel").val(total.toFixed(2)) ;
} 

/* calculate the RV fuel cost */
function calculateRVFuelCost() {
  var total = parseFloat($("#distancekm").val() * $("#pplitre").val() 
     * $("#lkmtow").val()) / 100 ;
  $("#totalfuelrv").val(total.toFixed(2)) ;
} 

/* calculate hotel cost - flights and trip */
function calculateHotelCost() {
  var total = parseFloat($("#days").val() * $("#motelrate").val()) ;
  total = total.toFixed(2) ;
  $("#totalhotel").val(total) ;
  $("#totalmotel").val(total) ;
} 

/* calculate cost of staying at campgrounds in RV */
function calculateCampgroundCost() {
  var total = parseFloat($("#days").val() * $("#campground").val()) ;
  $("#totalcampgrounds").val(total.toFixed(2)) ;
} 

/* calculate cost of eating restaurants (flight and trip) */
function calculateRestaurantCost() {
  var total = parseFloat($("#days").val() * $("#people").val() 
     * $("#personalmeals").val()) ;
  $("#totalmeals").val(total.toFixed(2)) ;
  $("#totalmeals2").val(total.toFixed(2)) ;
} 

/* calculate grocery cost (RV) */
function calculateGroceryCost() {
  var total = parseFloat($("#days").val() * $("#people").val() 
     * $("#personalgroceries").val()) ;
  $("#totalgroceries").val(total.toFixed(2)) ;
} 

/* summarize the totals for the site */
function calculateTotals() {
  var flights = $("#totalflights").val() ;
  var cars = $("#totalcar").val();
  var rentalFuel = $("#totalrentalfuel").val();
  var motels = $("#totalmotel").val();
  var meals = $("#totalmeals").val();
  var fuel = $("#totalfuel").val();
  var hotel = $("#totalhotel").val();
  var meals2 = $("#totalmeals2").val();
  var rvfuel = $("#totalfuelrv").val();
  var campgrounds = $("#totalcampgrounds").val();
  var groceries = $("#totalgroceries").val();
  /* summarize totals */
  var thf = parseFloat(flights) + parseFloat(cars) 
     + parseFloat(rentalFuel) + parseFloat(motels) + parseFloat(meals) ;
  var th = parseFloat(fuel) + parseFloat(hotel) + parseFloat(meals2) ;
  var tr = parseFloat(rvfuel) + parseFloat(campgrounds) 
     + parseFloat(groceries) ;

  /* display formatted values and savings */
  $("#triphotelflight").val(parseFloat(thf).toFixed(2));
  $("#triphotel").val(parseFloat(th).toFixed(2));
  $("#triprv").val(parseFloat(tr).toFixed(2));
  $("#savings").val(parseFloat(thf - tr).toFixed(2)) ;
  $("#savings2").val(parseFloat(th - tr).toFixed(2)) ;
}

// run a calculation on the fields to load numbers
calculateFlightTotals() ;
calculateCarRental() ;
calculateCarRentalFuel() ;
calculateCarFuelCost() ;
calculateRVFuelCost() ;
calculateHotelCost() ;
calculateCampgroundCost() ;
calculateRestaurantCost() ;
calculateGroceryCost() ;
calculateTotals() ;
}); /* end document.ready */