/* Generate the calendars for the Hotels Booking Form. */

$(function() {
//trigger date pickers on hotel form
$('#departureDateHotel').keyup(function() {
	changeDate(this,'dd/mm/yy');
}).click(function() {
	initElementIds('departureDateHotel','returnDateHotel');
	calOpen = 'departDateHotel';
	displayCalendarOnClick(this,'dd/mm/yy','departDateHotel','returnDateHotel',1,'','',hotelBookingDays);
}).select(function() {
	initElementIds('departureDateHotel','returnDateHotel');
	calOpen = 'departDateHotel';
	displayCalendar(this,'dd/mm/yy','departDateHotel','returnDateHotel',1,'','',hotelBookingDays);
});
$('#returnDateHotel').keyup(function() {
	changeDate(this,'dd/mm/yy');
}).click(function() {
	initElementIds('departureDateHotel','returnDateHotel');
	calOpen = 'returnDateHotel';
	displayCalendarOnClick(this,'dd/mm/yy','returnDateHotel','departureDateHotel',1,'','',hotelBookingDays);
}).select(function() {
	initElementIds('departureDateHotel','returnDateHotel');
	calOpen = 'returnDateHotel';
	displayCalendar(this,'dd/mm/yy','returnDateHotel','departureDateHotel',1,'','',hotelBookingDays);
});

// initialize octopus hotel form default date (6 days ahead) and stay (1 night)
defaultDelay = 6;
var stayLength = 1;

var a = new Date();
var b = new Date();
a.setDate(a.getDate()+defaultDelay);

adjustCheckout(a);

$('#checkin').val(formatDate(a));
$('#nights').val(stayLength);

// only display a checkout date if it can be set (no point showing if JS is off)
$('#checkout').show();

$('#checkin').blur(function() {
	if (this.value != formatDate(a)) {
		temp = this.value.split('/');
		a.setDate(temp[0]);
		a.setMonth(temp[1]-1);
		a.setFullYear('20'+temp[2]);
		adjustCheckout(a);
	}
});

$('#nights').blur(function() {
	if (this.value != stayLength) {
		stayLength = parseInt(this.value);
		adjustCheckout(a);
	}
});

// trigger date pickers on octopus hotel form - checkin
$('#bookingForm #checkin').keyup(function() {
	changeDate(this,'dd/mm/yy');
}).click(function() {
	initElementIds('checkin');
	calOpen = 'checkinHotel';
	displayCalendarOnClick(this,'dd/mm/yy','checkinHotel',null,1,'','',hotelBookingDays);
}).select(function() {
	initElementIds('checkin');
	calOpen = 'checkinHotel';
	displayCalendar(this,'dd/mm/yy','checkinHotel',null,1,'','',hotelBookingDays);
});	

// helper functions for octopus hotel dates

// format date to dd/mm/yy
function formatDate(date) {	
	return pad(date.getDate()) + date.getDate() + '/' + pad((date.getMonth()+1)) + (date.getMonth()+1) + '/' + date.getFullYear().toString().substr(2);
}

// pad a leading zero onto day or month if < 10
function pad(val) {
	p = (val.toString().length == 1) ? '0' : '';
	return p;
}

// calculate and display checkout date when checkin date or length of stay changes
function adjustCheckout(date) {
	b.setTime(date.valueOf());
	b.setDate(b.getDate()+stayLength);
	$('#checkout span').html(formatDate(b));
}

// trigger date pickers on US hotel form  - in / out
$('#in').keyup(function() {
	changeDate(this,'dd/mm/yy');
}).click(function() {
	initElementIds('in','out');
	calOpen = 'in';
	displayCalendarOnClick(this,'dd/mm/yy','in','out',1,'','',hotelBookingDays);
}).select(function() {
	initElementIds('in','out');
	calOpen = 'in';
	displayCalendar(this,'dd/mm/yy','in','out',1,'','',hotelBookingDays);
});
$('#out').keyup(function() {
	changeDate(this,'dd/mm/yy');
}).click(function() {
	initElementIds('in','out');
	calOpen = 'out';
	displayCalendarOnClick(this,'dd/mm/yy','out','in',1,'','',hotelBookingDays);
}).select(function() {
	initElementIds('in','out');
	calOpen = 'out';
	displayCalendar(this,'dd/mm/yy','out','in',1,'','',hotelBookingDays);	
});
});





