
/**
 * This function shows and hides the appropriate subsections of the various departing
 * preferences.
 *
 * Author: peterl
 */
IDBSYachtSearch.Booking.selectDepartingPreferences = function(preferences_constant, preferences_option) {
	for(var loop = 0; loop < 4; loop++){
		if(loop == preferences_option){
			
			var visibleDepartingPreference = $('#search_type_id_'+loop);
			
			visibleDepartingPreference.css('display','');
			
			var dateInput = $(".smallDate");
			
			// Don't display the picker, just activate on click
			dateInput.datePicker( { createButton: false, clickInput: true } );
			
			switch (preferences_constant) {
				case 'DEPARTING':
					IDBSYachtSearch.Booking.initialiseDepartingDatePicker(visibleDepartingPreference, preferences_option, preferences_constant);				
					break;
				case 'BETWEEN':
					IDBSYachtSearch.Booking.initialiseBetweenDatePickers(visibleDepartingPreference, preferences_option, preferences_constant);
					break;
			}
		}
		else{
			$('#search_type_id_'+loop).css('display','none');
		}
	}
	/** initialize onchange **/
	
}

/**
 * This function initializes the date picker on the "Departing On" search type.
 *
 * Author: peterl
 */
IDBSYachtSearch.Booking.initialiseDepartingDatePicker = function(departingPreference, preferences_option, preferences_constant) {
	var number_of_days_between_dates = null;
}

/**
 * This function initializes the date pickers for the "Flexible Break, Between Dates" search type.
 *
 * Author: peterl
 */
IDBSYachtSearch.Booking.initialiseBetweenDatePickers = function(departingPreference, preferences_option, preferences_constant) {
	
	var number_of_days_between_dates = null;
	var dateInput = $(".smallDate");
	dateInput.change(function() {
		var d = Date.fromString($(this).val());
		dateInput.dpSetStartDate(displayDate(d));	
	});
	
	$("#date_from_"+preferences_option).datePicker();
	$("#date_from_"+preferences_option).change(function() {
		var from = Date.fromString($(this).val());
		var to = Date.fromString($("#date_to_"+preferences_option).val());
		$("#date_to_"+preferences_option).dpSetStartDate(displayDate(from));
		
		updateEndDate(preferences_option);
	});	
	
	
	$("#search_day_length_id_"+preferences_option).change(function() {
		updateEndDate(preferences_option)
	});
	
	$("#date_to_"+preferences_option).datePicker();
}

// Alter the end date to accomodate the 'x days' selection and the start date
function updateEndDate(preferences_option)
{	
	if ($("#date_from_"+preferences_option).val() == "")
	{
		return;
	}
	
	var from = Date.fromString($("#date_from_"+preferences_option).val());
	var days = parseInt($("#search_day_length_id_"+preferences_option).val());
	from.addDays(days);
	$("#date_to_"+preferences_option).val(displayDate(from));
}

function displayDate(d) {
	var new_month = two_digit(d.getMonth() + 1);
	var new_day = two_digit(d.getDate());
	var new_year = d.getFullYear().toString();	
	//new_year = new_year.substr(2,2);
	return new_day + "/" + new_month + "/" + new_year;
} 

function two_digit(num) {
	if (num < 10) {
		return "0" + num;
	}
	return num;
}