


//Check number of nights to stay is in range
function onChangeNights(form) {

	//change departure date
	booking_date =
			new Date(form.arrival_year.value, form.arrival_month.value-1,form.arrival_day.value);

	departure_date = new Date(booking_date);
	today = new Date();
//	departure_date.setDate(parseInt(booking_date.getDate())+parseInt(form.number_nights.selectedIndex+1));
departure_date.setDate(parseInt(booking_date.getDate())+parseInt(form.number_nights.value));

	form.departure_year.selectedIndex = departure_date.getFullYear()-today.getFullYear();
	form.departure_month.selectedIndex = parseInt(departure_date.getMonth());
	form.departure_day.selectedIndex = departure_date.getDate()-1;

//	form.departure_year.value = departure_date.getFullYear();
//	form.departure_month.value = parseInt(departure_date.getMonth()) + 1;
//	form.departure_day.value = departure_date.getDate();
	CalendarSetDepartureDate(form);
}

//Check for valid arrival date, and update departure and calendar fields
function onChangeArrivalDate(form) {

	var month = form.arrival_month.value;
	var year = form.arrival_year.value;
	var day = form.arrival_day.value;
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("That month doesn't have 31 days!");
		return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}

	today = new Date();

	//latest booking time is 6:00 pm today
	booking_date =
			new Date(form.arrival_year.value, form.arrival_month.value-1,form.arrival_day.value,18,0,0);

   //check booking date
	if ( booking_date.getTime() < today.getTime() ) {
		alert("It is too late to book for that arrival date.");
		return false;
	}

	too_far = new Date(today.getFullYear(),today.getMonth(),today.getDate());
	too_far.setMonth(parseInt(today.getMonth())+9);
	if(booking_date.getTime() >= too_far.getTime() ){
		alert('For bookings more than 9 months in the future please contact our central reservations team on 0800 917 3085');
		return false;
	}

   //set departure date
	booking_date.setDate(parseInt(booking_date.getDate())+parseInt(form.number_nights.value));
	form.departure_year.selectedIndex = booking_date.getFullYear()-today.getFullYear();
	form.departure_month.selectedIndex = booking_date.getMonth();
	form.departure_day.selectedIndex = parseInt(booking_date.getDate())-1;

//	booking_date.setDate(parseInt(booking_date.getDate())+parseInt(form.number_nights.selectedIndex+1));
//	form.departure_year.value = booking_date.getFullYear();
//	form.departure_month.value = parseInt(booking_date.getMonth()) + 1;
//	form.departure_day.value = booking_date.getDate();
	CalendarSetArrivalDate(form);
	CalendarSetDepartureDate(form);

}

//Check for valid departure date, and update departure and calendar fields
function onChangeDepartureDate(form) {

//	var month = form.departure_month.value;
//	var year = form.departure_year.value;
//	var day = form.departure_day.value;

	today = new Date();
	var month = form.departure_month.selectedIndex+1;
	var year = form.departure_year.selectedIndex+today.getFullYear();
	var day = form.departure_day.selectedIndex+1;
	var bmonth = form.arrival_month.selectedIndex+1;
	var byear = form.arrival_year.selectedIndex+today.getFullYear();
	var bday = form.arrival_day.selectedIndex+1;

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("That month doesn't have 31 days!");
		return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
	departure_date =
			new Date(year, month-1,day);

	booking_date =
			new Date(byear,bmonth-1,bday);

   //check booking date
	if ( booking_date.getTime() >= departure_date.getTime() ) {
		alert("You must specify a departure date after the arrival date.");
		return false;
	}
	var one_day=1000*60*60*24;
	var nights = (departure_date/one_day)-(booking_date/one_day);
	if( nights > 7 || nights < 1 ){
		alert("Please specify a number of days between 1 and 7 (inclusive).");
		return false;
	}

    //set nights
//	form.number_nights.selectedIndex = nights-1;
	form.number_nights.value = nights;
	CalendarSetDepartureDate(form);

}


//called when calendar has a new date
function ArrivalCalendarCallback(cal) {
	//form = document.main_form;
	form=document.getElementById("main_form");
	if (cal.dateClicked) {
		var str1 = cal.date.print("%d");
		var str2 = cal.date.print("%m");
		var re = /^0/g;

		today = new Date();
		str1 = str1.replace(re, "");
		str2 = str2.replace(re, "");
		form.arrival_day.selectedIndex = parseInt(str1)-1;
		form.arrival_month.selectedIndex = parseInt(str2)-1;
		form.arrival_year.selectedIndex = parseInt(cal.date.print("%Y"))-today.getFullYear();

//		form.arrival_day.value = str1.replace(re, "");
//		form.arrival_month.value = str2.replace(re, "");
//		form.arrival_year.value = cal.date.print("%Y");
	}
	cal.hide();
	onChangeArrivalDate(form);
}

//called when calendar has a new date
function DepartureCalendarCallback(cal) {
//	form = document.main_form;
	form=document.getElementById("main_form");

	if (cal.dateClicked) {
		var str1 = cal.date.print("%d");
		var str2 = cal.date.print("%m");
		var re = /^0/g;

		today = new Date();
		str1 = str1.replace(re, "");
		str2 = str2.replace(re, "");
		form.departure_day.selectedIndex = parseInt(str1)-1;
		form.departure_month.selectedIndex = parseInt(str2)-1;
		form.departure_year.selectedIndex = parseInt(cal.date.print("%Y"))-today.getFullYear();

//		form.departure_day.value = str1.replace(re, "");
//		form.departure_month.value = str2.replace(re, "");
//		form.departure_year.value = cal.date.print("%Y");
	}
	cal.hide();
	onChangeDepartureDate(form);
}

//set new date for calendar to start with
function CalendarSetArrivalDate(form) {
	var y = form.arrival_year.value;
	var m = form.arrival_month.value;
	var d = form.arrival_day.value;
	if(m < 10) m = "0" + m;
	if(d < 10) d = "0" + d;
	var str = y +"-"+ m +"-"+ d;
	form.arrival_cal.value = str;
}

//set new date for calendar to start with
function CalendarSetDepartureDate(form) {
//	var y = form.departure_year.value;
//	var m = form.departure_month.value;
//	var d = form.departure_day.value;
	today = new Date();
	var y = form.departure_year.selectedIndex+today.getFullYear();
	var m = form.departure_month.selectedIndex+1;
	var d = form.departure_day.selectedIndex+1;

	if(m < 10) m = "0" + m;
	if(d < 10) d = "0" + d;
	var str = y +"-"+ m +"-"+ d;
	form.departure_cal.value = str;
}

function selectTown(form){
	form.county.selectedIndex=0;
}

function selectCounty(form){
	//form.town.value='';
}

//if inn selected - empty search
function selectInn(form){
	form.county.selectedIndex=0;
	form.wedding.checked=false;
	form.golf.checked=false;
	form.conference.checked=false;
}

//if search item selected - empty inn
function selectSearch(form){
	form.inn.selectedIndex=0;
}

//check total of rooms selected is correct
function checkRooms(form,count,max){

	var i;
	var tot;
	var sel;
	tot=0;
	for(i=1;i<=count;i++){
		sel = document.getElementById('room_'+i);
		tot += sel.selectedIndex;
	}

	if( tot != max ){
		alert('Warning: You have selected '+tot+' rooms to book.\nPlease ensure you have selected '+max+' rooms before proceeding.');
		return false;
	} else
		return true;

}
