// Global variables and functions for use with new calendars on travel pages
// Requires functions defined in CalendarPopup.js, AnchorPosition.js, PopupWindow.js and date.js

var today = new Date();
var tomorrow = today;
tomorrow.setDate(tomorrow.getDate() +1);

var todayString = formatDate(today,"dd/MM/yyyy");
var tomorrowString = formatDate(tomorrow,"dd/MM/yyyy");
var cal0 = new CalendarPopup("travelCal"); // used for outward date on all travel screens
var cal1 = new CalendarPopup("travelCal"); // used for return date on all travel screens
var xDate = today;
xDate.setMonth(xDate.getMonth() + 11);

var futureString = formatDate(xDate,"yyyy-MM-dd");


// If departure date is set, exclude on the return calendar days up to (but not including) the departure date.
// Otherwise, exclude up to and including yesterday.
function setReturnCalExcludedDates(retCal, excludeBeforeDateString){
	if(!isDate(excludeBeforeDateString,"dd/MM/yyyy")){
		excludeBeforeDateString = todayString;
	}
	retCal.disabledDatesExpression = "";  // clear any previously excluded dates to stop anomalies if departure date gets changed
	disableDaysBefore(retCal,excludeBeforeDateString);
}
// Set excluded dates up to but not including the supplied date.
function disableDaysBefore(cal,dateString){
	var aTime = getDateFromFormat(dateString,"dd/MM/yyyy");
	var dayBefore = new Date(aTime-86400000);
	cal.addDisabledDates(null,formatDate(dayBefore,"yyyy-MM-dd")); // addDisabledDates() only seems to work with yyyy-MM-dd format so use that when calling it.
	cal1.addDisabledDates(futureString, null);
}
// Start date for return calendar should either be the return date (if set) or the departure date (if set) or today if neither are set
function getReturnCalStartDate(depDateString, retDateString){
	var startDateString = "";
	if(isDate(retDateString,"dd/MM/yyyy")){
		startDateString = retDateString;
	}else if(isDate(depDateString,"dd/MM/yyyy")){
		startDateString = depDateString;
	}else{
		startDateString = formatDate(today,"dd/MM/yyyy");
	}
	return startDateString;
}
function setCalEventHandlers(outwardId, returnId, xPosition){
	var outwardInput = document.getElementById(outwardId);
	var outwardImage = document.getElementById(outwardId +"Trigger");
	outwardImage.onclick = function(){
		cal0.offsetX = xPosition;
		cal0.select(outwardInput,outwardId,'dd/MM/yyyy'); 
		return false;
	}
	var returnInput = document.getElementById(returnId);
	var returnImage = document.getElementById(returnId +"Trigger");
	returnImage.onclick = function(){
		cal1.offsetX = xPosition;
		setReturnCalExcludedDates(cal1, outwardInput.value);
		cal1.select(returnInput,returnId,'dd/MM/yyyy',getReturnCalStartDate(outwardInput.value, returnInput.value));
		return false;
	}
}
function initialiseCalendars(){
	disableDaysBefore(cal0,tomorrowString);
	cal0.offsetY = -10;
	cal0.setTodayText("Close");
	cal0.showNavigationDropdowns();
	cal1.offsetY = -10;
	cal1.setTodayText("Close");
	cal1.showNavigationDropdowns();
	
	cal0.addDisabledDates(futureString, null);
	cal0.setYearSelectStartOffset(1);
	cal1.setYearSelectStartOffset(1);

}
