// --------------------------------------------------------------------------------
/*
   Program: a1villa_checkavailability.js
   
   Author:  James Whitfield
   Date:    23 July 2003
   
   Description:
   
   Checks availability against the bookings file.

   Amendments:

   01/11/03  JJW  Recognize villa.
   03/12/03  JJW  Reworking on booking references
   02/01/04  JJW  Recognize booking status.
   29/02/04  JJW  Increased number of nights available to 28 on drop-down.  Added
                  in 'flexible dates' option.
   06/07/05  JJW  Alternative dates and flexibility added
   18/07/05  JJW  Corrected day of arrival.
   06/10/05  JJW  Amended 'GetVillaName' to reflect passed value from form.
   12/10/05  JJW  Reworked logic for days 1-9.
   15/10/06  JJW  Only return nights when not 0 for FormatStayPeriod.
   07/06/09  JJW  Revamped for new 2009 website.
   13/10/09  JJW  Corrected for FireFox.
   19/10/09  JJW  Corrected issue with no earlier-later dates when provisional
                  booking clashes with dates requested.  Renamed variables.
   22/10/09  JJW  Corrected for Firefox.
   02/01/10  JJW  Ensure provisionally held message is displayed correctly.

*/
// --------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------------------------------------
// Determine availability of the specific dates.
// 0=Not available
// 1=Is Available
// 2=Provisional Held

function IsVillaAvailable(start_date, end_date, villa_code)
{

 var start_date_dms    = Date.parse(start_date);
 var end_date_dms      = Date.parse(end_date);
 var vm_arrival_date   = new Date();
 var vm_departure_date = new Date();
 var is_available      = 1; 

// Read through current bookings, working on the optimistic approach 
// that there is availability.  That is, until a booking exists that
// clashes with your request.  Compare against dates e.g. CEF20E10

 for (idx = 0; idx < bk.length; idx++) {
    if (bk[idx].charAt(5) == villa_code) {
        vm_booking_status = bk[idx].charAt(0);
        vm_arrival_year   = bk[idx].charCodeAt(1) - 64 +2000;  
        vm_arrival_month  = bk[idx].charCodeAt(2) - 65;

        if ( bk[idx].charAt(3) == "0" ) {
           vm_arrival_day  = parseInt(bk[idx].charAt(4)) }
         else { vm_arrival_day  = parseInt(bk[idx].substring(3,5)) };

        var vm_nights         = parseInt(bk[idx].substring(6,8));
        var vm_arrival_date   = new Date(vm_arrival_day + " " + months[vm_arrival_month] + " " + vm_arrival_year);
        var vm_arrival_dms    = Date.parse(vm_arrival_date);
        var vm_departure_dms  = vm_arrival_dms + (vm_nights * msecsInADay);
        var vm_departure_date = new Date(vm_departure_dms);

        if ((start_date_dms <= vm_departure_dms) && (end_date_dms >= vm_arrival_dms)) {
           is_available = (vm_booking_status == "P") ? 2 : 0;
           break;
        }; 
    }; // if
 }; //for
 return is_available
}

//------------------------------------------------------------------------------------------------------------------------


function ListVillaAvailability(intArrival, intDeparture, no_of_nights, flexibility_days)
{

   var entered_arrival_date   = new Date(intArrival);
   var entered_departure_date = new Date(intDeparture);

   var htmlList= "<table border='0' width='98%' class='bkg_border_gold' cellspacing='1' cellpadding='1'>" + 
                 "<tr height='10'><td colspan='3'></td></tr>";

   // --------------------------------------------------------------------------------
   // Check for availability for the requested dates or alternatively the days ahead
   // --------------------------------------------------------------------------------

   var villas_to_book= ["Elegant","Lakeside"];
   for ( villa_idx= 0; villa_idx < 2; villa_idx++ ) {

       var villa_to_check      = villas_to_book[villa_idx].charAt(0);
       var has_earlier_arrival = 0;
       var no_of_days_ahead    = 0;
       var provisional_booking_exists = "N";

       var earlier_arrival_date   = new Date(intArrival);
       var earlier_departure_date = new Date(intDeparture);
       var later_arrival_date     = new Date(intArrival);
       var later_departure_date   = new Date(intDeparture);

       do {  
 
         var has_availability = IsVillaAvailable(later_arrival_date, later_departure_date, villa_to_check);
         if ( has_availability == 2 ) {
            provisional_booking_exists= "Y";
            has_availability= 0;
          }; //provisional

         if ( has_availability == 0 ) {
            later_arrival_date.setDate(later_arrival_date.getDate() +1);
            later_departure_date.setDate(later_departure_date.getDate() +1);
            no_of_days_ahead++;
         };
       } while (( has_availability == 0 ) && ( no_of_days_ahead <= flexibility_days ));

       // --------------------------------------------------------------------------------
       // If the villa is not available, check for availability for earlier dates
       // --------------------------------------------------------------------------------

       var no_of_days_before = 0;
       var today_date        = new Date();

       if ( no_of_days_ahead != 0 ) {

          var no_of_days_before = 1;
          earlier_arrival_date.setDate(entered_arrival_date.getDate() -1);
          earlier_departure_date.setDate(entered_departure_date.getDate() -1);

          do {   
            has_earlier_arrival = IsVillaAvailable(earlier_arrival_date, earlier_departure_date, villa_to_check);
            if ( has_earlier_arrival == 0 ) {
               earlier_arrival_date.setDate(earlier_arrival_date.getDate() -1);
               earlier_departure_date.setDate(earlier_departure_date.getDate() -1);
               no_of_days_before++;
            }; 
          } while (( has_earlier_arrival == 0 ) && ( no_of_days_before <= flexibility_days ) && ( earlier_arrival_date >= today_date ));

       }; 

       // --------------------------------------------------------------------------------
       // Inform requestor
       // --------------------------------------------------------------------------------

       var exact_dates_available   = (no_of_days_ahead == 0)    ? "Y" : "N";
       var earlier_dates_available = (has_earlier_arrival == 1) ? "Y" : "N";
       var later_dates_available   = (has_availability == 1)    ? "Y" : "N";

       htmlList= htmlList + "<tr><td valign='top' class='spacer'>";

       if ( (later_dates_available == "Y") || (earlier_dates_available == "Y") ) {
          htmlList= htmlList + "<input type='radio' value='" + villas_to_book[villa_idx] + "' name='Villa'>" };

       htmlList= htmlList + "</td><td class='f150bold' valign='top' width='120'>" + GetVillaName(villa_to_check) + 
                            "</td><td class='f100'><p align='justify'>";

       if ( (later_dates_available == "Y") || (earlier_dates_available == "Y") ) {

          if ( exact_dates_available == "Y" ) 
             html_statusofvilla= "<b>Is available from " + FormatStayPeriod(entered_arrival_date, no_of_nights) + 
                              "</b><br>Select this villa by clicking alongside, then complete the Booking Request details below.  " +
                              "We shall contact you further with prices and details of what to do next.";
           else {
           if ( provisional_booking_exists == "N" ) {
                html_statusofvilla= "<b>Although booked for your selected dates, the villa is available from ";
            } else {
                html_statusofvilla= "<b>Although there is a provisionally held booking for your selected dates, the villa is available from ";
           };

           // Check if availability before and potentially after selected dates...
           if ( earlier_dates_available == "Y" ) {
              html_statusofvilla= html_statusofvilla + FormatStayPeriod(earlier_arrival_date, no_of_nights);
              if ( later_dates_available == "Y") { html_statusofvilla= html_statusofvilla +  " or later from " + FormatStayPeriod(later_arrival_date, no_of_nights) };
            } else {
              html_statusofvilla= html_statusofvilla + FormatStayPeriod(later_arrival_date, no_of_nights)
           };

           html_statusofvilla= html_statusofvilla + "</b><br>Please select this villa <u>and</u> your new arrival date before completing the " +
                            "Booking Request details.  We shall contact you further with prices and details of what to do next.";
           }
        } 

        else 
        if (( has_availability == 0 ) && (provisional_booking_exists == "N")) {
           html_statusofvilla= "<b>Sorry, this villa is booked for your selected dates.</b><br>" + 
                            "Please try alternative dates, increase the flexibility of your arrival date or contact us for " +
                            "availability either side of this period";
          }
          else 
          html_statusofvilla="<b>This villa has a provisional option held for your selected dates.</b><br>" + 
                          "Please try alternative dates, increase the flexibility of your arrival date or contact us for " +
                          "availability should the option expire.";

        htmlList= htmlList + html_statusofvilla + "</p></td><td class='spacer'></td></tr><tr height='10'><td colspan='3'></td></tr>";

    } // each villa

    var htmlListRef= document.getElementById("htmlVillaStatusList");
    htmlListRef.innerHTML = htmlList + "</table>";

} // fn



//------------------------------------------------------------------------------------------------------------------------
// Ensure a valid arrival date has been specified and depending on specific
// periods of rental, determine availability.
//------------------------------------------------------------------------------------------------------------------------

function ShowVillaAvailability(thisform) {
 
  var entered_arrival_date = parseInt(thisform.ArrivalDate.value);
  var flexibility_days     = parseInt(thisform.ArrivalDateFlexibility.value);
  var id_arrival_date      = document.getElementById("ArrivalDateDisplay");
  var id_no_of_nights      = document.getElementById("Nights");
  var today_date           = new Date();

  if (( entered_arrival_date == "0" ) || ( id_arrival_date.innerHTML == "" )) { 
     window.alert('Please select an arrival date to check availability of the villas') }
   else 
   if ( thisform.ArrivalDate.value < today_date ) {
     window.alert('You have selected a date that has already passed.  Please select a future date for arriving.') }
    else {
    var no_of_nights   = parseInt(id_no_of_nights.options[id_no_of_nights.selectedIndex].value);
    var departure_date = parseInt(entered_arrival_date) + (no_of_nights * msecsInADay);
    ListVillaAvailability(entered_arrival_date, departure_date, no_of_nights, flexibility_days);
  };

};

// --------------------------------------------------------------------------------