
// global variables used by the calendars
	var tmpStartDate = new Date()
	var tmpEndDate = new Date()

//jQuery code to initialise calendars
$(function()
{
	$('.date-pick').datePicker()
	
	//departures calendar
	$('#lstDepartureDates').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#rtnDate').dpSetStartDate(d.addDays(1).asString());
				
			}
		}
		
		// bind an event to the dateSelected handler of the calendar
		// this fires when a date is selected on the departure date calendar
		// it sets the values of the departure field, sets the return date provisionally at 7 days
		// and calls a function to calculate and update the hidden duration field required by BYO
	).bind(
					'dateSelected'
					,function(e, selectedDate, $td, state){
						tmpStartDate.setTime(selectedDate);
						// advance the departure date by seven days by default
						tmpEndDate.setTime(selectedDate.addDays(7))
						updateDuration()
						document.getElementById("rtnDate").value = tmpEndDate.asString()
						});
	// return calendar
	$('#rtnDate').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#lstDepartureDates').dpSetEndDate(d.addDays(-1).asString());
			}
		}
		// bind an event to update the duration when a new return date is selected
	).bind(
					'dateSelected'
					,function(e, selectedDate, $td, state){tmpEndDate = selectedDate;updateDuration()});
});
	
	

			
			
	function updateDuration(){
		
		// calculates the difference between the start date and the return date
		// and sets the display and hidden field accordingly
		
		var one_day=1000*60*60*24; //24hrs in milliseconds
			
		Diff=Math.ceil((tmpEndDate.getTime()-tmpStartDate.getTime())/(one_day)); 
			
		document.getElementById("duration").value = Diff
		document.getElementById("strDuration").innerHTML = "Duration: " + Diff
			
	}






