/******************************************************************************
	Function library for online booking form
******************************************************************************/

function isLeapYear( year ) {
	return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}


function daysInMonth( year, month, day ) {
	var maxDays = 31;
	var selectedDay = day.selectedIndex;
				
	switch( month[ month.selectedIndex ].value ) {
		case "4":
		case "6":
		case "9":
		case "11":
			maxDays = 30;
			break;
		case "2":
			maxDays = isLeapYear( year[ year.selectedIndex ].value ) ? 29 : 28;
			break;
		default:
			break;
	}
	
	//Verify previous selection
	if ( selectedDay > maxDays ) { 
		selectedDay = 0;
	}

	//Empty select list
	day.options.length = 0;
	
	//Create new options
	for ( i = 0; i < maxDays; i++ ) {
		day[ i ] = new Option(i+1);
		day[ i ].value = i+1;
	}
	
	//Pre select day
	day.selectedIndex = selectedDay;
	return true;
}


function todaysDate( year, month, day ) {
	var today = new Date();
	
	//Set month and year
	month.selectedIndex = today.getMonth();
	for ( var i = 0; i < year.options.length; i++ ) {
		if ( year[ i ].value == today.getFullYear() ) {
			year.selectedIndex = i;
		}
	}
	
	//Adjust number of available days and set to todays day
	daysInMonth( year, month, day );
	day.selectedIndex = today.getDate() - 1;
}


function isFutureDate( year, month, day ) {
	var arrivalDate = new Date();
	var today = new Date();

	arrivalDate.setDate( day[ day.selectedIndex ].value );
	arrivalDate.setMonth( month[ month.selectedIndex ].value - 1 );
	arrivalDate.setFullYear( year[ year.selectedIndex ].value );

	if ( arrivalDate.getTime() < today.getTime() ) {
		alert( 'Please select an Arrival Date in the future' );
		return false;
	}
	else {
		return true;
	}
}

function limitOccupancy( rooms, occupants ) {
	var adultsPerRoom = 3;
	var maxAdults = rooms[ rooms.selectedIndex ].value * adultsPerRoom;

	var selectedAdults = occupants.selectedIndex;
	
	//Empty select list
	occupants.options.length = 0;
	
	//Create new options
	for ( i = 0; i < maxAdults; i++ ) {
		occupants[ i ] = new Option(i+1);
		occupants[ i ].value = i+1;
	}
	
	//Pre select day
	occupants.selectedIndex = selectedAdults;
	return true;
}