// --------------------------------------------------------------------------------
/*
   Program: a1villa_bookingsummary.js
   
   Author:  James Whitfield
   Date:    15 October 2006
   
   Description:
   
   Shows guest events for their booking

   Amendments:

   26/11/06  JJW  Booking events encrypted.
   02/03/09  JJW  Correction when arrival date is 1-9th of the month.
   06/07/09  JJW  Renamed from 'a1villa_guestlogin.js' and re-vamped.
   14/08/10  JJW  Corrected issue with the departure date going over 1 January.

*/
// --------------------------------------------------------------------------------


function Decrypt(enchex, keyword)
{

 var strDecrypted    = "";
 var intKeywordIndex = -1;
 var intCharCode     = 0;

 for (intIndex = 0; intIndex < enchex.length -1; intIndex++)
   {
    if ( intKeywordIndex < 0) { 
       intKeywordIndex = keyword.length -1; 
    }
    bytKeywordCode = keyword.charCodeAt(intKeywordIndex);
    intCharCode = parseInt("0x" + enchex.substring(intIndex, intIndex +2)) - bytKeywordCode + 48;
    strDecryptedChar = String.fromCharCode(intCharCode);
    strDecrypted = strDecrypted + strDecryptedChar;
    intKeywordIndex--;
    intIndex++;
   }
  return strDecrypted;
}


//------------------------------------------------------------------------------------------------------------------------


function ShowBookingDetails(bookingparty)
{
 var bookingref  = Decrypt(this.ref, bookingparty);

 intArrivalYear  = bookingref.charCodeAt(0) - 64 +2000;  
 intArrivalMonth = bookingref.charCodeAt(1) - 65;

 if ( bookingref.charAt(2) == "0" ) {
      intArrivalDay  = parseInt(bookingref.charAt(3)) }
  else {
  intArrivalDay  = parseInt(bookingref.substring(2,4)); };

 intNoOfNights = bookingref.charCodeAt(5) - 64;
 dtArrival   = new Date(intArrivalDay + " " + months[intArrivalMonth] + " " + intArrivalYear);
 
 var dtDeparture = new Date(dtArrival);
 dtDeparture = new Date(dtDeparture.setDate(dtDeparture.getDate() + intNoOfNights));
// dtDeparture = new Date((intArrivalDay + intNoOfNights) + " " + months[intArrivalMonth] + " " + intArrivalYear);

 document.getElementById("idBookingParty").innerHTML  = bookingparty + " (" + bookingref.substring(0,5) + ")";
 document.getElementById("idArrivalDate").innerHTML   = FormatStayPeriod(dtArrival, 0);
 document.getElementById("idNoOfNights").innerHTML    = intNoOfNights;
 document.getElementById("idDepartureDate").innerHTML = FormatStayPeriod(dtDeparture, 0);
 document.getElementById("idVilla").innerHTML         = GetVillaName(bookingref.charAt(4));
}

//------------------------------------------------------------------------------------------------------------------------

function ShowBookingEvent(bookingparty)
{
  event_html = "<tr class='f100bold' height=18><td width=100>" + Decrypt(this.eventdate, bookingparty) + "</td>" +
               "<td>" + Decrypt(this.eventdesc, bookingparty) + "</td>" +
               "<td width=100><p align='right'>" + Decrypt(this.amount, bookingparty)  + "</p></td></tr>";
  return event_html;
}

//------------------------------------------------------------------------------------------------------------------------

function BookingEvent(ref, name, eventdate, eventdesc, amount)
{
 this.ref = ref;
 this.name = name;
 this.eventdate = eventdate;
 this.eventdesc = eventdesc;
 this.amount = amount;
 this.ShowBookingEvent = ShowBookingEvent;
 this.ShowBookingDetails = ShowBookingDetails;
}

//------------------------------------------------------------------------------------------------------------------------


function DisplayBookingEvents(loginform)
{
 
 var formid = document.getElementById(loginform);
 if (formid) {


     var bookingref   = formid.bookingref.value.toUpperCase();
     var bookingparty = formid.bookingparty.value.toUpperCase();
     var bookingshown = "N";
     var eventidx     = 0;

     // Read through current booking events and compare against booking ref and name entered
     events_html = "<table cellspacing=0 cellpadding=4>";
     events_html = events_html + "<tr><td>Date</td><td>Event</td><td><p align='right'>Amount</p></td></tr>";
     tblEventsList = document.getElementById("idBookingEvents");

     document.getElementById("idBookingParty").innerHTML  = "";
     document.getElementById("idArrivalDate").innerHTML   = "";
     document.getElementById("idNoOfNights").innerHTML    = "";
     document.getElementById("idDepartureDate").innerHTML = "";
     document.getElementById("idVilla").innerHTML         = "";

     for (eventidx = 0; eventidx < bkevent.length; eventidx++) {
         if ( (Decrypt(bkevent[eventidx].ref.substring(0,10), bookingparty) == bookingref) && 
            (Decrypt(bkevent[eventidx].name, bookingref.substring(0,4)) == bookingparty) ) {
            if (bookingshown == "N") {
               bkevent[eventidx].ShowBookingDetails(bookingparty);
               bookingshown = "Y";
               var bookingsummary= document.getElementById("divBookingSummary");
               bookingsummary.style.display = '';
                 bookingsummary.parentNode.className = 'bkg_border_orange';
            }; //if
            events_html = events_html + bkevent[eventidx].ShowBookingEvent(bookingparty);
         }; //if
    }; //for

    events_html = events_html + "</table>"
    tblEventsList.innerHTML = events_html;
 }; //if
}

//------------------------------------------------------------------------------------------------------------------------
