// --------------------------------------------------------------------------------
/*
   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.
   31/07/11  JJW  Re-worked for v10.

*/
// --------------------------------------------------------------------------------

var months= "January|February|March|April|May|June|July|August|September|October|November|December".split("|");

// --------------------------------------------------------------------------------


function Interpret(enchex, keyword)
{

 var strInterpreted    = "";
 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;
    strInterpretedChar = String.fromCharCode(intCharCode);
    strInterpreted = strInterpreted + strInterpretedChar;
    intKeywordIndex--;
    intIndex++;
   }
  return strInterpreted;
}


//------------------------------------------------------------------------------------------------------------------------

function ShowBookingDetails(bookingparty)
{
 var bookingref  = Interpret(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  = "<h4>" + bookingparty + " (" + bookingref.substring(0,5) + ")" + "</h4>";
 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><td height='24'><b>" + Interpret(this.eventdate, bookingparty) + "</b></td>" +
               "<td></td><td><b>" + Interpret(this.eventdesc, bookingparty) + "</b></td><td></td>" +
               "<td align='right'><b>" + Interpret(this.amount, bookingparty)  + "</b></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 border='0' cellspacing='2' cellpadding='0'>";
     events_html = events_html + "<tr><td height='30' width='100'>Date</td><td class='spacer'></td>" +
                                 "<td>Booking Event</td><td class='spacer'></td><td width='120' align='right'>Amount</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 ( (Interpret(bkevent[eventidx].ref.substring(0,10), bookingparty) == bookingref) && 
            (Interpret(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_pictureframe_inset8';
            }; //if
            events_html = events_html + bkevent[eventidx].ShowBookingEvent(bookingparty);
         }; //if
    }; //for

    events_html = events_html + "</table>"
    tblEventsList.innerHTML = events_html;
 }; //if
}

//------------------------------------------------------------------------------------------------------------------------

