var countrycode_has_result = false;

var update_trips = function(){
	var chosenDate = $("#id_date").val();
	if(chosenDate in trip_data)
	{
		var trips = trip_data[chosenDate], trip = null, time = null;
		var options = '';
		for (var i = 0; i < trips.length; i++) 
		{
			trip = trips[i];
			time = trip[1];
			options += '<option value="' + trip[0] + '">' + time + '</option>';
		}
		$("#id_trip_id").html(options);
		$("#id_trip_id").attr("disabled",false);
	}
};

var select_trip = function(trip_id){
	$("#id_trip_id").val(trip_id);
};

function zeroPad(n, digits) {
	n = n.toString();
	while (n.length < digits) {
		n = '0' + n;
	}
	return n;
}

function disabledDays(date) {

	key = zeroPad(date.getFullYear(),2) + '-' + zeroPad(date.getMonth()+1,2) + '-' + zeroPad(date.getDate(),2)

	//console.log('aaa ' + key);
	if( !(key in trip_data))
		return [false, ''];
	return [true, ''];
}

var cancel_submit = function (e) {
	if(e.which == 13)
	{
		e.preventDefault();
		return false;
	}
};

// Clear textbox if invalid country was selected
var countrycode_result_func = function(event, ui) {
	countrycode_has_result = true;
};

var countrycode_closefunc = function(event, ui) {
	if(!countrycode_has_result)
		$("#id_visitor_nationality").val("");
	countrycode_has_result = false;
};

// assert there are seats on this trip
var assert_availability = function() {
	var num_adults = parseInt($("#id_num_adults").val());
	var num_children = parseInt($("#id_num_teen").val());
	if(isNaN(num_adults))
	{
		alert("Number of adults should be a number");
		return false;
	}
	else if(isNaN(num_children))
	{
		alert("Number of children should be a number");
		return false;
	}

	// Need to do this to work around uncomfortable datastructure from feed
	var trip_list = trip_data[$("#id_date").val()];
	if(!trip_list)
	{
		alert("No departure date selected, please select a departure date and time");
		return false;
	}
	var selected_trip_id = parseInt($("#id_trip_id").val());
	if(!selected_trip_id || isNaN(selected_trip_id))
	{
		alert("No departure time selected, please select a departure date and time");
		return false;
	}

	vacancies = -1;
	for(var i = 0; i < trip_list.length; i++)
	{
		if(trip_list[i][0] == selected_trip_id)
		{
			vacancies = trip_list[i][2];
			break;
		}
	}
	if(vacancies < 0)
	{
		alert("An error occured, please refresh the page and try again");
		return false;
	}

	var total_passengers = num_adults + num_children;
	if(vacancies < total_passengers)
	{
		alert("There are not enough vacancies for this trip, you requested " + total_passengers + " passengers but there are only " + vacancies + " spots left on the trip. Please try another date or time");
		return false;
	}
	return true;
};

function terms_and_conditions_popup() {
	var $link = $(this);
	window.open($link.attr('href'), $link.attr('id'), "status=1, height=500, width=1050, scrollbars=1" );
	return false;
	}
$(document).ready(function(){
	$("#id_date").datepicker({
			showAnim: 'fadeIn'
			, numberOfMonths: 2
			, beforeShowDay: disabledDays
			});
	$("#id_date").change(update_trips);
	// Country
	$("#id_visitor_nationality").autocomplete({
		source: countries
		, delay: 10
		, select: countrycode_result_func
		, close: countrycode_closefunc
		, selectFirst: true
		});
	$("#id_visitor_nationality").keypress( cancel_submit );
	$("#userform").submit(assert_availability);
	$("#terms_and_conditions_link").bind('click', terms_and_conditions_popup);

	// When changing a booking or when a validation error occurs
	if(prepopulated)
	{
		var selected_trip = $("#id_trip_id").val();
		update_trips();
		select_trip(selected_trip);
	}
});

