var startDate;
var endDate;
var ONEDAY = 3600 * 24;

function resetDates() {
	startDate = endDate = null;
}

function filterDates1(cal) {
	startDate = cal.date.getTime();
	/* If they haven't chosen an 
	end date before we'll set it to the same date as the start date This
	way if the user scrolls in the start date 5 months forward, they don't
	need to do it again for the end date.
	*/

	if (endDate == null) { 
		Zapatec.Calendar.setup({
			inputField     :    "checkOutDate",
			button         :    "checkOutDatePopup",  // What will trigger the popup of the calendar
			ifFormat       :    "%m/%d/%Y",
			date           :     cal.date,
			showsTime      :     false,          //no time
			dateStatusFunc		:    disallowDateBefore, //the function to call
			onUpdate       :    filterDates2
		});
	}
}

function filterDates2(cal) {
	var date = cal.date;
	endDate = date.getTime()
}

/*
* This functions return true to disallow a date
* and false to allow it.
*/


/* 
* Can't choose days before today or before the
* end date
*/
function disallowDateBefore(date) {
	date = date.getTime();
	if ((startDate != null) && (date < (startDate + ONEDAY))) {
		//start date can't be prior to end date
		return true; 
	} 
	var now = new Date().getTime();
	if (date < (now - ONEDAY)) {
		//start date can't be prior to today
		return true;
	}

	return false;
}

/* 
* Can't choose days before today or before the
* start date
*/
function disallowDateAfter(date) {
	date = date.getTime();
	if ((endDate != null) && (date > (endDate - ONEDAY))) {
		//end date can't be before start date
		return true;
	} 

	var now = new Date().getTime();
	if (date < (now - ONEDAY)) {
		//end date can't be prior to today
		return true;
	}
	return false;
}

var cal = new Zapatec.Calendar.setup({

 inputField     :    "checkInDate",   // id of the input field
 button         :    "checkInDatePopup",  // What will trigger the popup of the calendar
 ifFormat       :    "%m/%d/%Y",       // format of the input field: Mar 18, 2005
 showsTime      :     false,          //no time
 dateStatusFunc    :    disallowDateAfter, //the function to call
 onUpdate       :    filterDates1

});

Zapatec.Calendar.setup({
 inputField     :    "checkOutDate",
 button         :    "checkOutDatePopup",  // What will trigger the popup of the calendar
 ifFormat       :    "%m/%d/%Y",       // format of the input field: Mar 18, 2005
 showsTime      :     false,          //no time
 dateStatusFunc    :    disallowDateBefore, //the function to call
 onUpdate       :    filterDates2
});

