/* Booking Panel scripts */

function selectBookingDates(){

   var currentDate = new Date();
   var currentDayOfMonth = currentDate.getDate() - 1;
   document.forms.checkincheckout_ym.day.selectedIndex = currentDayOfMonth;
   selectCurrentDays();
}

function selectCurrentDays() {
   var yearmonthSelect = document.forms.checkincheckout_ym.yearmonth;
   var daySelect = document.forms.checkincheckout_ym.day;
   var month = yearmonthSelect.options[yearmonthSelect.selectedIndex].value.substring(5);
   var year = yearmonthSelect.options[yearmonthSelect.selectedIndex].value.substring(0, 4);
   var lengthSelectedMonth = daysInMonth(month - 1, year);

   if (daySelect.selectedIndex > lengthSelectedMonth)
      daySelect.selectedIndex = 0;

   for (var i = 28; i <= lengthSelectedMonth; i++) {
      if (i > daySelect.length) {
         daySelect.options[daySelect.length] = new Option(i, i);
      }      
   }
   var itemsInSelectBox = daySelect.length;
   for (var i = 28; i <= itemsInSelectBox; i++) {
      if (i > lengthSelectedMonth) {
         daySelect.options[daySelect.length-1] = null;
      }
   }
}

function daysInMonth(iMonth, iYear) {
   return 32 - new Date(iYear, iMonth, 32).getDate();
}

function writeBookOnlineMonthSelect(){

   var currentDate = new Date();
   var currentMonth = currentDate.getMonth();
   var currentYear = currentDate.getFullYear();

   var nextYear = currentYear + 1;

   var monthsOfYr = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

   var outputCode = "";

   var monthWithZero = "";

   //first line - open select
   //outputCode += "<select class='browntext' id='yearmonth'>";
   outputCode += "<select class='yearMonth' id='yearmonth' onchange='selectCurrentDays()' style='float:left; width:70px;' >";

   //ensure current month is selected
      if((currentMonth + 1) <= 9)
         monthWithZero = "0";
      else
         monthWithZero = "";
      outputCode += "<option selected='selected' value='" + currentYear + "-" + monthWithZero + (currentMonth + 1) + "'>" + monthsOfYr[currentMonth] + " " + currentYear.toString().substring(2, 4) + "</option>";

   //write remaining months of current year
   for(var i = currentMonth + 1; i<=11; i++){
      if(i <= 9)
         monthWithZero = "0";
      else
         monthWithZero = "";
      outputCode += "<option value=" + currentYear + "-" + monthWithZero + "" + (i + 1) + ">" + (monthsOfYr[i]) + " " + currentYear.toString().substring(2, 4) + "</option>";
   }

   //write months of next year, ensuring that 12 consecutive months are always written
   for(var j = 0; j<=((currentMonth + 10) - 11); j++){
      if(j <= 9)
         monthWithZero = "0";
      else
         monthWithZero = "";
      outputCode += "<option value=" + nextYear + "-" + monthWithZero + "" + (j + 1) + ">" + (monthsOfYr[j]) + " " + nextYear.toString().substring(2, 4) + "</option>";
   }

   outputCode += "</select>";

   document.write(outputCode);
   selectBookingDates();
   
}

function goToBE(){

   var arrivalYM = this.document.forms.checkincheckout_ym.yearmonth.value;
   var arrivalDay = this.document.forms.checkincheckout_ym.day.value;
   var numNights = this.document.forms.checkincheckout_ym.nights.value;
   
   var currentDate = new Date();
   var currentMonth = currentDate.getMonth();
   
   //var activeYear = currentDate.getFullYear();

   location.href = 'https://www.aro.ie/aroreservebookings/sites/muckrossparkhotelkillarney/?' + 'yearmonth=' + arrivalYM + '&day=' + arrivalDay + '&nights=' + numNights;
}



