// isDate funcion function isDate(dateStr) { var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; var matchArray = dateStr.match(datePat); // is the format ok? //var $cancel; if (matchArray == null) { //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy."); return false; } month = matchArray[3]; // p@rse date into variables day = matchArray[1]; year = matchArray[5]; if (month < 1 || month > 12) { // check month range //alert("Month must be between 1 and 12."); return false; } if (day < 1 || day > 31) { //alert("Day must be between 1 and 31."); return false; } if ((month==4 || month==6 || month==9 || month==11) && day==31) { //alert("Month "+month+" doesn`t have 31 days!") return false; } if (month == 2) { // check for february 29th var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); if (day > 29 || (day==29 && !isleap)) { alert("February " + year + " doesn`t have " + day + " days!"); return false; } } return true; // date is valid } /***** FORMAT CURRENCY *****/ function formatCurrency(num) { num = isNaN(num) || num === '' || num === null ? 0.00 : num; return parseFloat(num).toFixed(2); } /***** FORM UN-GREYING *****/ $(function() { // Get the JSON data $.getJSON('js/jsonData.php', function(json) { // Put data into array for use later // Un-grey fields $('#pickupOption').change(function() { if ($('#pickupOption').is(':checked')) { $('#pickupAddress').attr('disabled',false).animate({opacity:1},750); $('#pickupAddressLabel').animate({opacity:1},750); } else { $('#pickupAddress').attr('disabled',true).animate({opacity:0.3},750); $('#pickupAddressLabel').animate({opacity:0.3},750); }; }); $('#easycover-discount').change(function() { if ($('#easycover-discount').is(':checked')) { $('#easycover-policy-number').attr('disabled',false).animate({opacity:1},750); } else { $('#easycover-policy-number').attr('disabled',true).animate({opacity:0.3},750); }; }); $('#agreement').change(function() { if ($('#agreement').is(':checked')) { $('#submit').attr('disabled',false).animate({opacity:1},750); } else { $('#submit').attr('disabled',true).animate({opacity:0.3},750); } }); // If airport pickup is selected $('#pickupLocation').change(function(){ if ($('#pickupLocation').val()=='alicanteAirport' | $('#pickupLocation').val()=='murciaAirport' ) { $('#flightNumber').attr('disabled',false).animate({opacity:1},750); $('#flightNumberLabel').animate({opacity:1},750); } else { $('#flightNumber').attr('disabled',true).animate({opacity:0.3},750); $('#flightNumberLabel').animate({opacity:0.3},750); }; }); // Car type activation $('#cars li label,#cars li input').click(function(){ $('#cars li label').removeClass('active'); $('#cars li h3').removeClass('strong'); $(this).addClass('active'); $(this).siblings('label').addClass('active'); $(this).siblings('h3').addClass('strong'); }) /****** THE CALCULATION SCRIPT ******/ // Initialize // Configurable variables: // var $carModelRate = ; var $childSeatRate = ''; // € Per day var $gpsRate = 2; // € Per day var $discountRate = 10; // Percent var $outOfHoursRate = 6; var $todaysRate = 0; var $easyrideRate = 25; // Set EasyRide price $('#easyride_help_price').html($easyrideRate); // var $start_offer = ''; // var $end_offer = ''; // var $name_offer = ''; // var $car_offer = ''; // var $price_offer = ; // var $highPriceDay = $highPrice/7; // var $lowPriceDay = $lowPrice/7; var $witchingHour = 2; var $basicPrice = 0; // Set price to zero printPrice($basicPrice); /***** CALCULATE BASIC PRICE BASED ON DATES SELECTED *****/ // First check if dates are valid $('#pick-up-date, #drop-off-date').change(function(){ $datecheck = $(this).val(); // alert (isDate($datecheck)); if (!isDate($datecheck)){ alert ('Please enter a valid date!'); $(this).val(''); } }); // Change departure date if it is blank and arrival date is selected $('#pick-up-date').change(function(){ $arrival = $('#pick-up-date').val(); }); // Calculate date difference, rate and send calculations $('#pick-up-date, #drop-off-date, #modelOption, .modelType').change(function(){ calculatePrice(json); }); $('.modelType').on('ifChanged', function (event) { calculatePrice(json); }); // If GPS is selected $('#gps').change(function(){ if ($('#gps').is(':checked')) { $gpsPrice = $gpsRate*$dateDif; //alert ($gpsPrice); printPrice(); } else { $gpsPrice = 0; printPrice(); }; }); // 'Tis the Witchin' Hour $('#departureTime1,#arrivalTime1').change(function(){ if (($('#departureTime1').val()- $('#arrivalTime1').val())>=$witchingHour && $lessThanWeek==false) { alert ('If the vehicle is dropped with more than a '+$witchingHour+' hour difference from the pickup time, you will be charged an extra day.'); $veryLate = true; printPrice(); } else { $veryLate = false; printPrice(); }; }); // Out of hours $('#arrivalTime1').change(function(){ if ($('#arrivalTime1').val()<7 && $('#arrivalTime1').val()>0){ $outOfHoursIn = true; printPrice(); alert('There is a surcharge of '+$outOfHoursRate+'€ for picking up the vehicle before 07:00h'); } else if ($('#arrivalTime1').val()==23) { $outOfHoursIn = true; printPrice(); alert('There is a surcharge of '+$outOfHoursRate+'€ for picking up the vehicle after 23:00h'); } else { $outOfHoursIn = false; printPrice(); } }); $('#departureTime1').change(function(){ if ($('#departureTime1').val()==23){ $outOfHoursOut = true; printPrice(); alert('There is a surcharge of '+$outOfHoursRate+'€ for picking up or dropping off the vehicle after 23:00h'); } else if ($('#departureTime1').val()<7 && $('#departureTime1').val()>0) { $outOfHoursOut = true; printPrice(); alert('There is a surcharge of '+$outOfHoursRate+'€ for picking up or dropping off the vehicle before 07:00h'); } else { $outOfHoursOut = false; printPrice(); } }); // Child seats $('#childSeats').change(function(){ $childSelect = $('#childSeats'); $seatsNumber = $childSelect.val(); $childSeatPrice = ($dateDif)*$childSeatRate*($seatsNumber-1); //alert($childSeatPrice); if ($seatsNumber<2) { $childSeatPrice = 0; } printPrice(); }); // Easyride $('input[name=easyride]').change(function(){ $easyride = $('input[name=easyride]:checked').val(); switch($easyride) { case 'no': $easyridePrice = 0; break; case 'pickup': $easyridePrice = $easyrideRate; break; case 'dropoff': $easyridePrice = $easyrideRate; break; case 'both': $easyridePrice = $easyrideRate*2; break; } printPrice(); }); // EasyCover discount $('#easycover-discount').on('ifChanged', function(){ if ($('#easycover-discount').is(':checked')) { $discountIs = true; calculatePrice(json); $('#easycoverId').focus(); } else { $discountIs = false; calculatePrice(json); }; }); /****** ALERTS ******/ $('#quoteForm').submit(function(){ // Pickup & drop off details //alert (isDate($('#pick-up-date').val())); $cancel = false; if ($('#pick-up-date').val()==''){ alert('Please enter a pickup date.'); $cancel=true; } if ($('#pickupLocation').val()=='notSpecified'){ alert('Please enter a pickup location.'); $cancel=true; } if ($('#drop-off-date').val()==''){ alert('Please enter a drop off date.'); $cancel=true; } if ($('#departureLocation').val()=='notSpecified'){ alert('Please enter a drop off location.'); $cancel=true; } // Contact details if ($('#name').val()==''){ alert('Please enter your name.'); $cancel=true; } if ($('#tel').val()==''){ alert('Please enter your telephone number.'); $cancel=true; } if ($('#email').val()==''){ alert('Please enter your email address.'); $cancel=true; } // Just in case // if ($('#quoted_price').val()<0 && $cancel==false){ alert('There seems to be a problem with the form. Please revise your details.'); $cancel=true; } if ($cancel==true) { return false; } }); // End JSON }); }); function calculatePrice(json){ var $startSeasonDate = '2018/07/01'; var $endSeasonDate = '2018/09/15'; var $startEasterDate = '2018/03/23'; var $endEasterDate = '2018/04/14'; var $latest_date_bookable = '2019/01/31'; // Latest day bookable var $offer = []; var $pricestandardLow = 0; var $pricestandardHigh = 0; var $pricehatchbackLow = 0; var $pricehatchbackHigh = 0; var $price9_seaterLow = 0; var $price9_seaterHigh = 0; var $priceestateLow = 0; var $priceestateHigh = 0; var $price7_seaterLow = 0; var $price7_seaterHigh = 0; var $priceautomaticLow = 0; var $priceautomaticHigh = 0; $arrival = $('#pick-up-date').val(); $departure = $('#drop-off-date').val(); var $day = 1000*60*60*24; $today = new Date(); var $year = $today.getFullYear(); $startSeason = new Date($startSeasonDate); $endSeason = new Date($endSeasonDate); $startEaster = new Date($startEasterDate); $endEaster = new Date($endEasterDate); // Format selected dates for calculations $arrivalArray = $arrival.split('/'); $departureArray = $departure.split('/'); $latestArray = $latest_date_bookable.split('/'); $arrival = new Date($arrivalArray[2],$arrivalArray[1]-1,$arrivalArray[0]); $departure = new Date($departureArray[2],$departureArray[1]-1,$departureArray[0]); $latest = new Date($latestArray[0],$latestArray[1]-1,$latestArray[2]); // alert($latest); $latestShow = $latestArray[2]+'/'+($latestArray[1])+'/'+$latestArray[0]; // Cap bookings if ($arrival>$latest || $departure>$latest) { alert('Sorry, we are not accepting bookings after '+$latestShow); $('#pick-up-date').val(''); $('#drop-off-date').val(''); return false; } // Cap bookings for 2014 *** Obsolete! *** /* if ($arrivalArray[2]>2013 || $departureArray[2]>2013){ alert('Sorry, but we are not accepting bookings for 2014 at this moment.'); $('#pick-up-date').val(''); $('#drop-off-date').val(''); }; */ //alert ($startSeasonDate + ' and ' + $endSeasonDate); // Get model type $modelType = $('input.modelType:checked').val(); $modelType = $modelType; // Calculate price depending on model type switch ($modelType) { case 'car_standard': $lowPriceFinal = $pricestandardLow; $highPriceFinal = $pricestandardHigh; break; case 'car_hatchback': $lowPriceFinal = $pricehatchbackLow; $highPriceFinal = $pricehatchbackHigh; break; case 'car_9_seater': $lowPriceFinal = $price9_seaterLow; $highPriceFinal = $price9_seaterHigh; break; case 'car_estate': $lowPriceFinal = $priceestateLow; $highPriceFinal = $priceestateHigh; break; case 'car_7_seater': $lowPriceFinal = $price7_seaterLow; $highPriceFinal = $price7_seaterHigh; break; case 'car_automatic': $lowPriceFinal = $priceautomaticLow; $highPriceFinal = $priceautomaticHigh; break; } // No budget cars for certain months! // *** Obsolete! *** $badMonths = ['04','05','06','07','08','09','10','12']; if ($modelType=='budget' && ($.inArray(($arrivalArray[1]),$badMonths)!=-1 || $.inArray(($departureArray[1]),$badMonths)!=-1)) { alert ("Sorry, our budget cars are fully booked for your travel dates"); $('#standardLabel').trigger('click'); $('#standardRadio').trigger('click'); } // Get model list if select model is checked if ($('#modelOption').is(':checked')) { $.get('car_models.php',{ id: $modelType}, function(data) { $('#model').html(data); }); }; // Calculate the date difference $dateDif = (($departure-$arrival)/($day)); $lessThanWeek = ($dateDif<7)?true:false; // Is it less than a week? // Alert if date difference is negative or a date is in the past if ($dateDif<0) { alert('Drop off date must be after pickup date.'); $('#drop-off-date').val(''); return false; }; // Alert if date is in the path if ( $arrival<$today-$day) { alert('You can\'t book a past date.'); $('#pick-up-date').val(''); return false; }; if ( $departure<$today) { alert('You can\'t book a past date.'); $('#drop-off-date').val(''); return false; }; // Must be at least 7 days!! $dateDif = ($dateDif<7)?7:$dateDif; // Get price based on date difference, checking rate for each day var $dateCal = $arrival.valueOf(); $basicPrice = 0; for ($n=1;$n<$dateDif+1;$n++) { $dateFormatted = new Date($dateCal); $highPriceDay = $highPriceFinal/7; $lowPriceDay = $lowPriceFinal/7; // Run through offers to see if today is special offer $todaysRate = (($dateCal>=$startSeason && $dateCal<=$endSeason) || ($dateCal>=$startEaster && $dateCal<=$endEaster))?$highPriceDay:$lowPriceDay; $.each(json, function(key,item) { // Dates $offerStartDate = new Date(item.desde); $offerEndDate = new Date(item.hasta); $offerName = item.nombre; // Prices $offer['c_standard'] = item.price_standard; $offer['c_hatchback'] = item.price_hatchback; $offer['c_estate'] = item.price_estate; $offer['c_7_seater'] = item.price_7_seater; $offer['c_9_seater'] = item.price_9_seater; // Check if special offer if ($dateCal>=$offerStartDate && $dateCal<=$offerEndDate && $offer['c_'+$modelType] && $offer['c_'+$modelType]!='0') { $todaysRate = $offer['c_'+$modelType]/7; // alert ($offer['c_'+$modelType]); } else { $todaysRate = $todaysRate; } }); // Add it up -(if 31st October, no charge - unexplained bug!) *** Obsolete? *** if ($dateFormatted!='Mon Oct 31 2014 23:00:00 GMT+0100 (Romance Standard Time)') { $basicPrice += $todaysRate; } $dateCal += $day; } printPrice($basicPrice); } /***** CALCULATE FINAL PRICE FUNCTION *****/ // Calculate, Format & Output function function printPrice($basicPrice) { var $rate = 0; var $childSeatPrice = 0; var $gpsPrice = 0; var $easyridePrice = 0; var $carModelPrice = 0; var $discountAmount = 0; var $discountIs = false; var $veryLate = false; var $lessThanWeek = false; var $outOfHoursIn = false; var $outOfHoursOut = false; var $outOfHoursRate = 6; // Calculate price var $price; // Check if current selected model is normal, premium, executive or budget $model = $('#model').val(); if ($('#modelOption').is(':checked')) { $carModelPrice = $carModelRate; } else { $carModelPrice = 0; }; // $price = (($basicPrice)+$childSeatPrice+$gpsPrice+$carModelPrice); // Tis late? if ($veryLate==true && $todaysRate) { $price += $todaysRate; }; // Out of hours? if ($outOfHoursIn) { $price += $outOfHoursRate; } if ($outOfHoursOut) { $price += $outOfHoursRate; } // Is there a discount? $discountAmount = ($discountIs==true)?($price*$discountRate/100):0; // Make sure there is minimum rate. $price = ($price<$rate && $price!=0)?$rate:$price; // Add easyride price $price += $easyridePrice; $price = $price-$discountAmount; // Output price $('#finalPrice').text(formatCurrency($price)); $('#quoted_price').val(formatCurrency($price)); }