

var reservationcalendar_availability;
var reservationcalendar_datepickers;
if (typeof reservationcalendar_availability == 'undefined')
{
  reservationcalendar_availability = [];
  reservationcalendar_datepickers = [];
}

if (typeof jQuery == 'undefined')
{
  document.writeln('<scri'+'pt src="'+ reservationcalendar_getBaseUrlWithReservation() + 'common/jquery/jquery-1.7.min.js'+'" type="text/javascript"></sc'+'ript>');
}

if (typeof jQuery == 'undefined' || typeof jQuery.datepicker_availability == 'undefined')
{
  document.writeln('<scri'+'pt src="'+ reservationcalendar_getBaseUrlWithReservation() + 'common/jquery/jquery-ui-1.8.5.custom.js'+'" type="text/javascript"></sc'+'ript>' +
                   '<link href="'+ reservationcalendar_getBaseUrlWithReservation() + 'common/jquery/css/ui-lightness/jquery-ui-1.8.5.custom.css'+'" rel="stylesheet">');
}

function calendar(activityId, field, local)
{
  showAvailabilityCalendar2(activityId, field, { local: local, webBooking: true });
}

// Replaced by showAvailabilityCalendar but is used in old external/agencyexternal code.
function showAvailabilityCalendar(activityId, agencyId, fieldId, anchorId, local)
{
  if (local == null) local = true;
  if (!/^\d+$/.test(activityId))
  {
    alert('Please select activity');
    return;
  }

  reservationcalendar_loadCalendarContent(activityId, agencyId, false/*blocksOnly*/, fieldId, anchorId, local, true/*webBooking*/);
}

function showAvailabilityCalendar2(activityId, fieldId, options)
{
  var agencyId = (options['agencyId'] != null ? options['agencyId'] : 0);
  var blocksOnly = (options['blocksOnly'] != null ? options['blocksOnly'] : false);
  var anchorId = (options['anchorId'] != null ? options['anchorId'] : null);
  var local = (options['local'] != null ? options['local'] : false);
  var webBooking = (options['webBooking'] != null ? options['webBooking'] : true);

  if (!/^\d+$/.test(activityId))
  {
    alert('Please select activity');
    return;
  }

  reservationcalendar_loadCalendarContent(activityId, agencyId, blocksOnly, fieldId, anchorId, local, webBooking);
}

function showSimpleCalendar(fieldId, anchorId)
{
  var $field = jQuery('#' + fieldId);
  var fieldElement = $field.get(0);
  var anchorElement = jQuery('#' + anchorId).get(0);

  if (!reservationcalendar_datepickers[fieldId] || reservationcalendar_datepickers[fieldId]['type'] != 'simple')
  {
    $field.datepicker_availability('destroy');
    $field.datepicker_availability(
    {
      showOn: 'none',
      showAnim: ''
    });

    reservationcalendar_datepickers[fieldId] = { type: 'simple' };
  }

  jQuery.datepicker_availability._showDatepicker(fieldElement, anchorElement);
}

function reservationcalendar_getBaseUrlWithReservation()
{
  var url = 'https://www.hawaiifun.org/reservation/';
  if (!/reservation\/$/.test(url) && !/reservation_test\/$/.test(url)) url += 'reservation/';
  if (/perfecthawaiivacationguide.com$/.test(window.location.hostname) || /^(\w+\.)*hawaiifun\.org$/.test(window.location.hostname)) url = 'https://www.hawaiifun.org/reservation/';
  return url;
}

function reservationcalendar_loadCalendarContent(activityId, agencyId, blocksOnly, fieldId, anchorId, local, webBooking)
{
  var baseurl = (local ? "" : reservationcalendar_getBaseUrlWithReservation());
  var $field = jQuery('#' + fieldId);
  var fieldElement = $field.get(0);
  var anchorElement = jQuery('#' + anchorId).get(0);

  if (!reservationcalendar_datepickers[fieldId]
          || reservationcalendar_datepickers[fieldId]['type'] != 'availability'
          || reservationcalendar_datepickers[fieldId]['activityId'] != activityId
          || reservationcalendar_datepickers[fieldId]['agencyId'] != agencyId
          || reservationcalendar_datepickers[fieldId]['blocksOnly'] != blocksOnly
          || reservationcalendar_datepickers[fieldId]['webBooking'] != webBooking)
  {
    $field.datepicker_availability('destroy');
    $field.datepicker_availability(
    {
      showOn: 'none',
      showAnim: '',
      availabilityLoaded: function(year_month)
      {
        return typeof reservationcalendar_availability[activityId + '_' + agencyId + '_' + year_month] != 'undefined';
      },
      loadAvailability: function(year_months, readyfunction)
      {
        var ym = '';
        for (var i=0; i<year_months.length; i++)
        {
          if (ym != '') ym += '|';
          ym += year_months[i];
        }
        jQuery.ajax({
          type: 'GET',
          url: baseurl + 'companyservlet',
          dataType: 'jsonp',
          success: function(data)
          {
            for (i=0; i<year_months.length; i++)
            {
              reservationcalendar_availability[activityId + '_' + agencyId + '_' + year_months[i]] = data['yearmonth_' + year_months[i]];
            }
            readyfunction();
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) { alert(baseurl + ' ' + textStatus + ' ' + errorThrown); },
          data: {
            action:'COMMON_AVAILABILITYCHECKJSON',
            activityid:activityId,
            agencyid:agencyId,
            blocksonly:blocksOnly,
            year_months:ym,
            webbooking:webBooking
          }
        });
      },
      beforeShowDay: function(date)
      {
        var m = date.getMonth() + 1;
        var d = date.getDate();
        var y = date.getFullYear();

        var idx = activityId + '_' + agencyId + '_' + y + '_' + m;

        if (typeof reservationcalendar_availability[idx]['d' + d] == 'undefined')
        {
          return [false];
        }
        else if (reservationcalendar_availability[idx]['d' + d] <= 0)
        {
          return [false, 'un', 'Not Available'];
        }
        else
        {
          return [true];
        }
      }
    });

    reservationcalendar_datepickers[fieldId] = {
      type: 'availability', activityId: activityId, agencyId: agencyId,
      blocksOnly: blocksOnly, webBooking: webBooking
    };
  }

  jQuery.datepicker_availability._showDatepicker(fieldElement, anchorElement);
}

