// CALENDAR SCRIPT: v. 1.0.1 - Scott Blevins
/*
 * Requires: date.js, time.js
 * 
 * History: 
 * 
 * 1.0 : Added complete calendar with functionality that mimics the old BOTW calendar by Zach
 * 1.0.1 : Fixed a bug that was causing the date checking not to work in-between page posts.
 * 
 */


// Set the current date variables outside of the function to make them global.
var curStDate = ''; var curEnDate = '';

$(document).ready(function() {
	// Initialize the datepickers.
	$("#checkIn").datepicker({minDate: 0, numberOfMonths: 2});
	$("#checkOut").datepicker({minDate: 0, numberOfMonths: 2});
	
	// Make the icons clickable to show pickers.
	$("#checkInIcon").click(function () { $("#checkIn").datepicker('show');	});
	$("#checkOutIcon").click(function () { $("#checkOut").datepicker('show');	});
	
	// Grab the values of the current dates picked for comparison to new dates possibly being picked.
	curStDate = Date.parse($("#checkIn").attr('value'));
	curEnDate = Date.parse($("#checkOut").attr('value'));
	
	$("#checkIn").change(function() {
		// The check-in date has changed.
		
		// Grab today's date.
		var today = Date.today();
		
		// Grab the contents of the check in and check out dates. Parse them afterwards.
		var stDate = Date.parse($("#checkIn").attr('value'));
		var enDate = Date.parse($("#checkOut").attr('value'));
		
		// Debugging line:
		//alert('today='+today+' stDate='+stDate+' enDate='+enDate);
		
		// Make sure the check in date isn't before today.
		if ((stDate).compareTo(today) == -1) {
			$("#checkIn").attr('value', Date.today().toString("M/d/yyyy"));
		}
		
		// If the dates are both already set.
		if (curStDate && curEnDate) {
			
			// If they are, let's find out the span between them.
			var ts = new TimeSpan(curEnDate - curStDate);
			var chkts = new TimeSpan(stDate - curStDate);
			var chknewspan = new TimeSpan(stDate - enDate);
			var newspan = chknewspan.getDays();
			var days = ts.getDays();
			var chkdays = chkts.getDays();
			
			// If there is a difference between them and we've moved the check in date forward, move the check out to match.
			if (chkdays > 0) {
				if (days > 0) {
					$("#checkOut").attr('value', Date.parse(stDate).add(days).days().toString("M/d/yyyy"));
				}
			}
			// If the check in date is moved back
			if (chkdays < 0) {
				// See if it is more than 14 days...
				if (newspan < -14) {
					// If so, bring the end date back.
					$("#checkOut").attr('value', Date.parse(stDate).add(14).days().toString("M/d/yyyy"));
				}
			}
			
		}else{
			// If the dates have not both been set before:
			
			// See if the end date was set before the check in date:
			if ($("#checkOut").attr('value')) {
				var stDate = Date.parse($("#checkIn").attr('value'));
				var enDate = Date.parse($("#checkOut").attr('value'));
				if ((stDate).compareTo(enDate) == 1) {
					$("#checkIn").attr('value', (enDate).toString("M/d/yyyy"));
				}
			}else{
				
				// Otherwise, set the check out date two days from the check in date.
				var stDate = Date.parse($("#checkIn").attr('value'));
				$("#checkOut").attr('value', Date.parse(stDate).add(2).days().toString("M/d/yyyy"));
			}
		}
		
		// Set the current variables 
		curStDate = Date.parse($("#checkIn").attr('value'));
		curEnDate = Date.parse($("#checkOut").attr('value'));
		
	});
	$("#checkOut").change(function() {
		// The check-out date has changed.
		
		// Grab today's date.
		var today = Date.today();
		
		// Grab the contents of the check in and check out dates. Parse them afterwards.
		var stDate = Date.parse($("#checkIn").attr('value'));
		var enDate = Date.parse($("#checkOut").attr('value'));
		
		// Make sure the check out time is not before today
		var todaychk = new TimeSpan(today - enDate);
		if ((enDate).compareTo(today) == -1) {
			$("#checkOut").attr('value', Date.today().toString("M/d/yyyy"));
		}
		
		// Make sure the check out date is not before the check in date
		if ((stDate).compareTo(enDate) == 1)
		{
			$("#checkIn").attr('value', (enDate).toString("M/d/yyyy"));
		}
		
		//Make sure the check out date is not more than 14 days from the check in date
		var ts = new TimeSpan(enDate - stDate);
		var days = ts.getDays();
		if (days > 13) {
			var overage = days - 13;
			alert('If you would like to stay for more than 14 days please contact one of our representatives at (800)785-1665.');
			$("#checkOut").attr('value', Date.parse(curEnDate).toString("M/d/yyyy"));
		}
		
		// Set the current variables 
		curStDate = Date.parse($("#checkIn").attr('value'));
		curEnDate = Date.parse($("#checkOut").attr('value'));
	});
});
