﻿registerEventNode('load', window, initialiseDateDropdowns);

function initialiseDateDropdowns()
{
	var selectedDate, dateParts, selectedCheckInDate, selectedCheckOutDate;
	// read the cookie for RFCheckInDate
	var checkInCookie = readCookie('RFCheckInDate');
	if (checkInCookie)
	{
		//var checkInDate = new Date(checkInCookie);
		dateParts = checkInCookie.split('/');
		selectedCheckInDate = new Date(dateParts[2], dateParts[1]-1, dateParts[0]);
	}
	// if non-existent, use today for checkin
	if (!selectedCheckInDate)
	{ selectedCheckInDate = new Date(); }

	// read the cookie for RFCheckOutDate
	var checkOutCookie = readCookie('RFCheckOutDate');
	if (checkOutCookie)
	{
		//var checkOutDate = new Date(checkOutCookie);
		dateParts = checkOutCookie.split('/');
		selectedCheckOutDate = new Date(dateParts[2], dateParts[1]-1, dateParts[0]);
	}
	// if non-existent, use checkin+1 for checkout
	if (!selectedCheckOutDate)
	{
		selectedCheckOutDate = new Date(selectedCheckInDate);
		selectedCheckOutDate.setDate(selectedCheckOutDate.getDate()+1);
	}
	
	// set checkin date fields
	if (document.getElementById('availcheckin'))
	{
		setDateDisplayFields('availcheckin', selectedCheckInDate);
		setDateDropdowns('availcheckin', selectedCheckInDate);
	}
	if (document.getElementById('bookcheckin'))
	{
		setDateDisplayFields('bookcheckin', selectedCheckInDate);
		setDateDropdowns('bookcheckin', selectedCheckInDate);
	}

	// set checkout date fields
	if (document.getElementById('availcheckout'))
	{
		setDateDisplayFields('availcheckout', selectedCheckOutDate);
		setDateDropdowns('availcheckout', selectedCheckOutDate);
	}
	if (document.getElementById('bookcheckout'))
	{
		setDateDisplayFields('bookcheckout', selectedCheckOutDate);
		setDateDropdowns('bookcheckout', selectedCheckOutDate);
	}
}

function isLeapYear(year)
{ return new Date(year,2-1,29).getDate() == 29; }

function changeDate(calendar, daysToAdd)
{
	var id = calendar.params.inputField.id;
	var selectedDate = calendar.date;
	
	// update the display fields
	setDateDisplayFields(id, selectedDate);
	setDateDropdowns(id, selectedDate);
	
	var currentDate = selectedDate.print("%d/%m/%Y");
	var currentWeekDay = selectedDate.print("%a").toUpperCase();
	switch (id)
	{
		case 'availcheckin':
		case 'bookcheckin':
			linkDate(id, selectedDate, daysToAdd);
			break;
	}

	// call the onClose handler
	if (calendar.dateClicked)
	{ calendar.callCloseHandler(); }
}

function linkDate(id, selectedDate, daysToAdd)
{
	var dependantId;
	var linkedDate = selectedDate;
	
	// modify the passed date if required (for updating second field based on first's selection)
	switch (id)
	{
		case 'availcheckin':
			dependantId = 'availcheckout';
			break;

		case 'bookcheckin':
			dependantId = 'bookcheckout';
			break;
		
		default:
			// do nothing
	}
	
	if (dependantId)
	{
		linkedDate.setDate(linkedDate.getDate() + daysToAdd);
		setDateDisplayFields(dependantId, linkedDate);
		setDateDropdowns(dependantId, linkedDate);
	}
}

function setDateDisplayFields(id, date)
{
	var inputField = document.getElementById(id);
	var weekField = document.getElementById(id+'weekday');
	// update values
	inputField.value = date.print("%d/%m/%Y");
	if (weekField)
	{ weekField.innerHTML = date.print("%a").toUpperCase(); }

	// save a cookie for the date field
	switch (id)
	{
		case 'availcheckin':
		case 'bookcheckin':
			createCookie('RFCheckInDate', date.print("%d/%m/%Y"));
			break;

		case 'availcheckout':
		case 'bookcheckout':
			createCookie('RFCheckOutDate', date.print("%d/%m/%Y"));
			break;
	}
}

function setDateDropdowns(id, date)
{
	// modify the dropdowns (hellish)
	var dayField = document.getElementById(id+'day');
	var monthField = document.getElementById(id+'month');
	
	var day = date.getDate();
	var month = date.getMonth()+1;
	var year = date.getFullYear();
	
	var monthYear = month+'-'+year;

	// update day
	for (n=dayField.length-1; n>=0; n--)
	{
		if (dayField[n].value == day)
		{
			dayField.selectedIndex = n;
			break;
		}
	}
	// update month/year
	for (n=monthField.length-1; n>=0; n--)
	{
		if (monthField[n].value == monthYear)
		{
			monthField.selectedIndex = n;
			break;
		}
	}
}

function validateSelectedDate(id)
{
	var maxDays = 0;
	var dayField = document.getElementById(id+'day');
	var monthField = document.getElementById(id+'month');
	
	var day = parseInt(dayField[dayField.selectedIndex].value);
	var monthYear = monthField[monthField.selectedIndex].value;
	var month = parseInt(monthYear.split('-')[0]);
	var year = parseInt(monthYear.split('-')[1]);
	
	// determine valid days by month
	switch (month)
	{
		case 2:
			// if a leap year, allow selection up to 29 days
			if (isLeapYear(year))
			{ maxDays = 29; }
			else
			{ maxDays = 28; }
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			maxDays = 30;
			break;
		default:
			maxDays = 31;
	}
	
	// cap selected days if required
	if (day > maxDays)
	{
		day = maxDays;
		for (n=dayField.length-1; n>=0; n--)
		{
			if (dayField[n].value == day)
			{
				dayField.selectedIndex = n;
				break;
			}
		}
	}

	// update hidden date field and weekday display	
	var confirmedDate = new Date(year, month-1, day);
	setDateDisplayFields(id, confirmedDate);
	linkDate(id, confirmedDate, 1);
}
