//On DOM is ready:
$.ready = function(){ 

	//When a tour info is first loaded, all the available dates for the next 60 days are grabbed from the alpro server
	//These dates are output in a string in a hidden ouput
	//When the page is loaded, we eval this string, which will give us an array of available dates
	var availableDates = eval("["+ $('#availableDates').val() + "]");
	
	//Set the jquery UI calendar
	//minDate: +1 => minimum date is tomorrow
	//maxDate: +60 => maximum date is 60 days later
	//beforeShowDay => before showing a date as available in the calendar, cross-reference the availableDate array
	//                 
	$("#tourDate").datepicker( 	{ minDate: +1,
																maxDate: +365, 
															  beforeShowDay: function(date) { 
															  	//this is the variable we use for counting loops
															   	var x = 0;
															   	//this is the variable we use to store a date during each loop
															  	var d;
															  	//Loop through available dates
																	for(x = 0; x < availableDates.length; x++){
																		//each available date is in this format: yyyy,mm,dd => we turn it into an array
																		var dateElements = eval("["+availableDates[x]+"]");
																		//We can now create a date object
																		d = new Date(dateElements[0], dateElements[1], dateElements[2]);
																		//And finally we can compare accurately 2 date objects' time
																		if (d.getTime() == date.getTime()){
																			//if date == one of the dates in the available dates array
																			return [true,''];  
																		}
																	}
											        		//date was not found
											        		return [false,''];
											        	}
											        }
	 													); 
	 													
}//end $.ready function


//Reload tour info from tour code and newly selected
function doTour(){
	//Grab the tourCode and tourDate and perform the ajax work that will update the session with new tour info
	//This new session info will then be rolled into the Reservation object on subsequent booking flow pages
	var tourCode = document.getElementById('tourCode').value;
	var tourDate = document.getElementById('tourDate').value;
	
	//Show the loading gif
	$('#tourSelected').html('<img src="/img/functions/loading.gif" width="16" height="16" alt="Loading tour details..." style="float:left; padding-top:2px;" /><strong>Loading tour details...</strong>')
	
	//Start the ajax routine
	//The php page (/alpro/load-tour-data.php) outputs javascript code. It is eval'ed on success.
	//We also recalculate the cost from the hidden inputs on success
	$.ajax({
		type: "POST",
		url: "/alpro/load-tour-data.php",
		data: "tourCode="+tourCode+"&tourDate="+tourDate,
		success: function(response){
			eval(response);
			recalculateCost();
		}
	});
}

//This function is called when the user clicks on the calendar icon next to the tourDate input
//It resets the focus to the tourDate input, which has all the jquery UI functionalies (the dynamic calendar)
//attached to it.
function showCalendar(){
	$('#tourDate').blur();
	$('#tourDate').focus();
}

function recalculateCost(){
	
	//Subtotal is total count without discount and taxes
	var subtotal = ($('#nbAdults').val() * $('#rateAdults').val()) + ($('#nbChildren').val() * $('#rateChildren').val());
	
	//Format and show updated subtotal
	$('#subtotal').html("\$"+subtotal.toFixed(2));
	
	//If applyDiscount is set to "true", calculate and show discount
	if($('#applyDiscount').val() == "true"){
		
		//Savings = nb adults * ( regular adult rate - discounted adult rate ) + nb adults * ( regular child rate - discounted child rate ) 
		var savings = ($('#nbAdults').val() * ($('#rateAdults').val() - $('#discountedRateAdults').val())) + ($('#nbChildren').val() * ($('#rateChildren').val() - $('#discountedRateChildren').val()));
		
		//Format and show savings
		$('#savings').html("-\$"+savings.toFixed(2));
		
		//The discount html element is used to explain why a discount is not applied. Not used in this case. Make sure it's blank.
		$('#discount').html(' ');
	
	//If applyDiscount is set to "false", calculate and show discount
	}else{
	
		//Format and show savings of $0.00
		$('#savings').html("-\$0.00");
		
		//Display a message explaining why the discount is 0.
		$('#discount').html('<p>Discounts only for tours booked 7 days or more in advance</p>');
	}
}

function showProcessingMessage(){
	$('#btn').html('Processing...');
}