﻿var hidFirstMonthStartDate;
var hidClickState;
var hidCheckinDay;
var hidCheckoutDay;
var txtCheckin;
var txtCheckout;
var hidMaxNights;
var hidMaxNightsErrorMsg;
var oReservationsStartDate;
var oAltStartDate, oAltEndDate;
var oLocations = new Object;
oLocations.Calendar = 0;
oLocations.Textbox = 1;
oLocations.PopupCalendar = 2;
var oClickState = new Object;
oClickState.Checkin = 0;
oClickState.Checkout = 1;
var bErrorMsgShownFromPopup = false;


jQueryIBE(document).ready(function(){
  jQueryIBE.extend(jQueryIBE.Tooltip.defaults, { track: true, delay: 0 });
  jQueryIBE('td.available, td.unavailable, td.minimum, td.closed, td.yourstay, td.CalAvailable, td.CalUnavailable, td.CalMinimum, td.CalClosed').Tooltip();
  
  hidFirstMonthStartDate = theForm.ctl00$cphBody$ucRoomAvailabilityCalendarTop$hidFirstMonthStartDate;
  hidClickState = theForm.ctl00$cphBody$ucReservationForm$hidAvailabilityCalendarClickState;
  hidCheckinDay = theForm.ctl00$cphBody$ucReservationForm$hidAvailabilityCalendarCheckinDay;
  hidCheckoutDay = theForm.ctl00$cphBody$ucReservationForm$hidAvailabilityCalendarCheckoutDay;
  txtCheckin = theForm.ctl00$cphBody$ucReservationForm$txtCheckIn;
  txtCheckout = theForm.ctl00$cphBody$ucReservationForm$txtCheckOut;
  hidMaxNights = theForm.ctl00$cphBody$ucReservationForm$hidMaxNights;
  hidMaxNightsErrorMsg = theForm.ctl00$cphBody$ucReservationForm$hidMaxNightsErrorMsg;
  hidInvalidDateErrorMsg = theForm.ctl00$cphBody$ucReservationForm$hidInvalidDateErrorMsg;
  oReservationsStartDate = GetCultureDate(theForm.ctl00$cphBody$hidReservationsStartDate.value);
  hidCheckoutBeforeCheckinErrorMsg = theForm.ctl00$cphBody$ucReservationForm$hidCheckoutBeforeCheckinErrorMsg;
  oAltStartDate = GetCultureDate(theForm.ctl00$cphBody$hidAltStartDate.value);
  oAltEndDate = GetCultureDate(theForm.ctl00$cphBody$hidAltEndDate.value);
  
  //if checkin has been selected then make sure the user hasn't changed the months
  //to something far away from the checkin date where they can't see the checkin date
  //because they'll start clicking to set the checkin date but they'll really be setting checkout
  //and they'll get the "max nights" error message
  if (txtCheckin.value != "" && txtCheckout.value == "")
  {
    if (!CheckinVislbe())
    {
      hidCheckinDay.value = ""
      txtCheckin.value = "";
      hidCheckoutDay.value = ""
      txtCheckout.value = "";
      hidClickState.value = oClickState.Checkin;
    }
  }
  
  //highlight on load incase posted back and have values in hiddens still
  HighlightCalendarDays();
});

//get first days of the calendar months and checkin selection, then compare their
//milliseconds to see if the checkin date is in one of the currently shown months
function CheckinVislbe()
{
  var oFirstMonthStartDate = GetCultureDate(hidFirstMonthStartDate.value);
  var msFirstMonthStartDate;
  var oSecondMonthStartDate;
  var msSecondMonthStartDate;
  var oCheckinDate = GetCultureDate(txtCheckin.value);
  var msCheckinDate;
  
  oFirstMonthStartDate.setDate(1);  //set first day of month
  msFirstMonthStartDate = oFirstMonthStartDate.getTime();

  oSecondMonthStartDate = oFirstMonthStartDate;
  oSecondMonthStartDate.setMonth(oSecondMonthStartDate.getMonth() + 1);
  msSecondMonthStartDate = oSecondMonthStartDate.getTime()
  
  oCheckinDate.setDate(1);
  msCheckinDate = oCheckinDate.setDate(1);
  
  if (msCheckinDate != msFirstMonthStartDate && msCheckinDate != msSecondMonthStartDate)
  {
    return false;
  }
  else
  {
    return true;
  }
}

function DaysBetween(oDate1, oDate2)
{
  var msOneDay = 86400000; //1000 * 60 * 60 * 24
  var msDate1 = oDate1.getTime();
  var msDate2 = oDate2.getTime();
  var msDateDifference = Math.abs(msDate1 - msDate2);
  return Math.round(msDateDifference/msOneDay);
}

function UpdateDatesFromCalendar(Month, Day, Year)
{  
  var oNewDate = new Date(Year, Month-1, Day);
  
  if (hidClickState.value == oClickState.Checkin)
  {
    SetCheckinDayNumber(oNewDate, oLocations.Calendar);    
  }
  else if (hidClickState.value == oClickState.Checkout)
  {
    SetCheckoutDayNumber(oNewDate, oLocations.Calendar)
  }
}

function SetCheckinDayNumber(oCheckinDate, iFromLocation)
{
  var iCheckinDay = DaysBetween(oReservationsStartDate, oCheckinDate);
  var iCheckoutDay = parseInt(hidCheckoutDay.value);
  var iMaxNights = parseInt(hidMaxNights.value);
  var sMaxNightsErrorMsg = hidMaxNightsErrorMsg.value;
  
  if (iFromLocation == oLocations.Calendar)
  {
    hidCheckinDay.value = iCheckinDay;
    UpdateCheckinText(oCheckinDate.getMonth()+1, oCheckinDate.getDate(), oCheckinDate.getFullYear());
    hidCheckoutDay.value = "";
    txtCheckout.value = "";
    hidClickState.value = oClickState.Checkout;
    HighlightCalendarDays();
  }
  else if (iFromLocation == oLocations.Textbox || iFromLocation == oLocations.PopupCalendar)
  {

    if (hidCheckoutDay.value != "")
    {
      if (iCheckoutDay - iCheckinDay <= iMaxNights)
      {
        hidCheckinDay.value = iCheckinDay;
        HighlightCalendarDays();
      }
      else
      {
        hidCheckinDay.value = iCheckinDay;
        txtCheckout.value = "";
        hidCheckoutDay.value = "";
        hidClickState.value = oClickState.Checkout;
        HighlightCalendarDays();        
        if (iFromLocation == oLocations.PopupCalendar)
        {
          bErrorMsgShownFromPopup = true;
          alert(sMaxNightsErrorMsg);
        }
        else if (iFromLocation == oLocations.Textbox && bErrorMsgShownFromPopup == true)
        {
          bErrorMsgShownFromPopup = false;          
        }
        else if (iFromLocation == oLocations.Textbox && bErrorMsgShownFromPopup == false)
        {
          alert(sMaxNightsErrorMsg);         
        }
      }
    }
    else
    {
      hidCheckinDay.value = iCheckinDay;
      UpdateCheckinText(oCheckinDate.getMonth()+1, oCheckinDate.getDate(), oCheckinDate.getFullYear());
      //preset checkout if its blank and user selected from popup calendar so that if they click
      //the checkout textbox for another popup the date will already be filled and relevant
      if (iFromLocation == oLocations.PopupCalendar)
      {
        hidCheckoutDay.value = iCheckinDay + 1;
        oCheckinDate.setDate(oCheckinDate.getDate() + 1);
        hidClickState.value = oClickState.Checkin;
        UpdateCheckoutText(oCheckinDate.getMonth()+1, oCheckinDate.getDate(), oCheckinDate.getFullYear());
      }
      else if (iFromLocation == oLocations.Textbox)
      {
        hidCheckoutDay.value = "";
        txtCheckout.value = "";
        hidClickState.value = oClickState.Checkout;
      }
      HighlightCalendarDays();
    }
  }
}

function SetCheckoutDayNumber(oCheckoutDate, iFromLocation)
{
  var iCheckoutDay = DaysBetween(oReservationsStartDate, oCheckoutDate);
  var iCheckinDay = parseInt(hidCheckinDay.value);
  var iMaxNights = parseInt(hidMaxNights.value);
  var sMaxNightsErrorMsg = hidMaxNightsErrorMsg.value;
  
  if (iCheckoutDay != iCheckinDay)
  {
    if (hidCheckinDay.value != "")
    {
      if (iCheckoutDay > iCheckinDay)
      {
        if (iCheckoutDay - iCheckinDay <= iMaxNights)
        {
          if (iFromLocation == oLocations.Calendar)
          {
            hidCheckoutDay.value = iCheckoutDay;
            UpdateCheckoutText(oCheckoutDate.getMonth()+1, oCheckoutDate.getDate(), oCheckoutDate.getFullYear());
            hidClickState.value = oClickState.Checkin;
            HighlightCalendarDays();
          }
          else if (iFromLocation == oLocations.Textbox || iFromLocation == oLocations.PopupCalendar)
          {
            hidCheckoutDay.value = iCheckoutDay;
            UpdateCheckoutText(oCheckoutDate.getMonth()+1, oCheckoutDate.getDate(), oCheckoutDate.getFullYear());
            hidClickState.value = oClickState.Checkin;
            HighlightCalendarDays();
          }
        }
        else  //night range greather than max nights
        {
          if (iFromLocation == oLocations.Calendar)
          {
            alert(sMaxNightsErrorMsg);
            hidClickState.value = oClickState.Checkout;
            HighlightCalendarDays();
          }
          else if (iFromLocation == oLocations.Textbox || iFromLocation == oLocations.PopupCalendar)
          {          
            hidCheckoutDay.value = "";
            hidClickState.value = oClickState.Checkout;
            HighlightCalendarDays();
            if (iFromLocation == oLocations.PopupCalendar)
            {
              bErrorMsgShownFromPopup = true;
              alert(sMaxNightsErrorMsg);
            }
            else if (iFromLocation == oLocations.Textbox && bErrorMsgShownFromPopup == true)
            {
              bErrorMsgShownFromPopup = false;          
            }
            else if (iFromLocation == oLocations.Textbox && bErrorMsgShownFromPopup == false)
            {
              alert(sMaxNightsErrorMsg);         
            }
          }
        }
      }
      else  //checkout before checkin
      {
        if (iFromLocation == oLocations.Calendar)
        {
          hidCheckinDay.value = iCheckoutDay;
          UpdateCheckinText(oCheckoutDate.getMonth()+1, oCheckoutDate.getDate(), oCheckoutDate.getFullYear());
          hidCheckoutDay.value = "";
          HighlightCalendarDays();
        }
        else if (iFromLocation == oLocations.Textbox || iFromLocation == oLocations.PopupCalendar)
        {
          hidCheckoutDay.value = "";
          HighlightCalendarDays();
          if (iFromLocation == oLocations.PopupCalendar)
          {
            bErrorMsgShownFromPopup = true;
            txtCheckout.blur();
            alert(hidCheckoutBeforeCheckinErrorMsg.value);
          }
          else if (iFromLocation == oLocations.Textbox && bErrorMsgShownFromPopup == true)
          {
            bErrorMsgShownFromPopup = false;
          }
          else if (iFromLocation == oLocations.Textbox && bErrorMsgShownFromPopup == false)
          {
            alert(hidCheckoutBeforeCheckinErrorMsg.value);        
          }
        }
      }
    }
    else //checkout is blank
    {
      hidCheckoutDay.value = iCheckoutDay;
      UpdateCheckoutText(oCheckoutDate.getMonth()+1, oCheckoutDate.getDate(), oCheckoutDate.getFullYear());
      hidClickState.value = oClickState.Checkin;
      HighlightCalendarDays();
    }
  }
  else  //if checkin and checkout are equal
  {
    if (iFromLocation == oLocations.Calendar)
    {
      hidCheckinDay.value = "";
      txtCheckin.value = "";
      hidCheckoutDay.value = "";
      txtCheckout.value = "";
      hidClickState.value = oClickState.Checkin;
      HighlightCalendarDays();
    }
    else if (iFromLocation == oLocations.Textbox || iFromLocation == oLocations.PopupCalendar)
    {          
      hidCheckoutDay.value = iCheckoutDay;
      HighlightCalendarDays();
    }
  }

}

//call from textbox onblur or popup calendar
function UpdateCheckInFromTextbox(iFromLocation)
{
  if (txtCheckin.value != "")
  {
    var oCheckinDate = GetCultureDate(txtCheckin.value);
    
    if (oCheckinDate != null)
    {
      SetCheckinDayNumber(oCheckinDate, iFromLocation);
    }
    else
    {
      alert(hidInvalidDateErrorMsg.value);
    }
  }
}

//call from textbox onblur or popup calendar
function UpdateCheckOutFromTextbox(iFromLocation)
{
  if (txtCheckout.value != "")
  {
    var oCheckoutDate = GetCultureDate(txtCheckout.value);
    
    if (oCheckoutDate != null)
    {
      SetCheckoutDayNumber(oCheckoutDate, iFromLocation);
    }
    else
    {
      alert(hidInvalidDateErrorMsg.value);
    }
  }
}

function HighlightCalendarDays()
{
  var daySelectors = "";  

  //unhighlight currently highlighted days
  jQueryIBE("td.CalDay").removeClass("HighlightDay");
  
  if (hidCheckinDay.value != "" || hidCheckoutDay.value != "")
  {
    //if have full selection range
    if (hidCheckinDay.value != "" && hidCheckoutDay.value != "")
    {
      if (parseInt(hidCheckinDay.value) < parseInt(hidCheckoutDay.value))
      {
        //generate td.Day# selector statements for all days we want to select
        for (var i = parseInt(hidCheckinDay.value); i <= parseInt(hidCheckoutDay.value); i++)
        {
          daySelectors += "td.Day"+i+",";
        }
        daySelectors = daySelectors.substring(0, daySelectors.length - 1);  //remove trailing comma
        
        //add the highlight class to all selected days
        jQueryIBE(daySelectors).addClass("HighlightDay")
      }
    }
    else if (hidCheckinDay.value != "")
    {
      jQueryIBE("td.Day"+hidCheckinDay.value).addClass("HighlightDay");
    }
  }
}


function GetCultureShortDateString(date)
{
  var sFormat = theForm.ctl00$cphBody$ucReservationForm$hidCultureShortDateFormat.value;
  var sSeparator = theForm.ctl00$cphBody$ucReservationForm$hidCultureShortDateSeparator.value;
  var sFormatSplit = sFormat.split(sSeparator);
  var sDate = "";
  
  for (i = 0; i < sFormatSplit.length; i++)
  {
    if (sFormatSplit[i].charAt(0) == "M")
    {
      sDate += sSeparator;
      if (sFormatSplit[i].length == 2 && (date.getMonth()+1) < 10)//pad if double format
        sDate += 0;
      sDate += (date.getMonth()+1);
    }
    else if (sFormatSplit[i].charAt(0) == "d")
    {
      sDate += sSeparator;
      if (sFormatSplit[i].length == 2 && date.getDate() < 10)//pad if double format
        sDate += 0;
      sDate += date.getDate();
    }
    else if (sFormatSplit[i].charAt(0) == "y")
      sDate += sSeparator + date.getFullYear();
  }
  
  sDate = sDate.substring(1, sDate.length); //remove leading slash
  
  return sDate;
}

function GetCultureDate(sDate)
{
  var sFormat = theForm.ctl00$cphBody$ucReservationForm$hidCultureShortDateFormat.value;
  var sSeparator = theForm.ctl00$cphBody$ucReservationForm$hidCultureShortDateSeparator.value;
  var sFormatSplit = sFormat.split(sSeparator);
  var sDateSplit = sDate.split(sSeparator);
  var m, d, y;
  var date;
  
  for (i = 0; i < sFormatSplit.length; i++)
  {
    if (sFormatSplit[i].charAt(0) == "M")
      m = sDateSplit[i];
    else if (sFormatSplit[i].charAt(0) == "d")
      d = sDateSplit[i];
    else if (sFormatSplit[i].charAt(0) == "y")
      y = sDateSplit[i];
  }
  
  date = new Date(y, (m-1), d);
  
  if (date == 'Invalid Date' || DateFieldsDontMatch(date, m, d, y))
    date = null;
     
  return date;
}

function DateFieldsDontMatch(date, Month, Day, Year)
{
  return ((Day!=date.getDate()) || ((Month-1)!=date.getMonth()) || (Year!=date.getFullYear()));  
}

function UpdateCheckinText(Month, Day, Year)
{
  var date = new Date(Year, Month-1, Day);
  //var date2 = GetCultureDate(theForm.ctl00$cphBody$ucReservationForm$txtCheckOut.value);
  
  theForm.ctl00$cphBody$ucReservationForm$txtCheckIn.value = GetCultureShortDateString(date)
  
  //if (date2 <= date)
  //{
  //  date.setDate(date.getDate() + 1);
  //  theForm.ctl00$cphBody$ucReservationForm$txtCheckOut.value = GetCultureShortDateString(date)
  //}
  
  bUpdatedDateFromPopup = true;
}

function UpdateCheckoutText(Month, Day, Year)
{
  //var date = GetCultureDate(theForm.ctl00$cphBody$ucReservationForm$txtCheckIn.value);
  var date2 = new Date(Year, Month-1, Day);
  
  theForm.ctl00$cphBody$ucReservationForm$txtCheckOut.value = GetCultureShortDateString(date2)
  
  //if (date2 <= date)
  //{
  //  date2.setDate(date2.getDate() - 1);
  //  theForm.ctl00$cphBody$ucReservationForm$txtCheckIn.value = GetCultureShortDateString(date2)
  //}
  
  bUpdatedDateFromPopup = true;
}

//used by the popup calendar to decide which dates to disable
function DateStatusHandler(date, y, m, d)
{
  var msReservationsStartDate = oReservationsStartDate.getTime();
  var msDate = date.getTime();
  
  if (msDate < msReservationsStartDate)
    return true;  //disable date
  else
    return false;
}
