function makeDate(d) {
	day = d.getDate() + 0;
	mon = d.getMonth() + 1;
	yr = d.getFullYear();

	return mon+'/'+day+'/'+yr;
}

function countNights(startID,stopID,nightID,peakID,peakErrorID,minimumNights) {
	startDate = document.getElementById(startID).value.split("/");
	startDate[2] = 2000 + Math.ceil(startDate[2]);
	stopDate = document.getElementById(stopID).value.split("/");
	stopDate[2] = 2000 + Math.ceil(stopDate[2]);

	var message = '';

	if ((stopDate.length == 3)&&(startDate.length==3)) {
		var startJS = new Date;
		startJS.setTime(0);
		startJS.setFullYear(startDate[2]);
		startJS.setMonth(startDate[0]-1,startDate[1]);
		startJS.setDate(startDate[1]);

		var stopJS = new Date;
		stopJS.setTime(0);
		stopJS.setFullYear(stopDate[2]);
		stopJS.setMonth(stopDate[0]-1,stopDate[1]);
		stopJS.setDate(stopDate[1]);

		var one_day=1000*60*60*24
		var myDays = Math.ceil((stopJS.getTime()-startJS.getTime())/(one_day));

		//Calculate difference btw the two dates, and convert to days
		if (myDays < 1) {
			message = 'Your start date appears to be after your stop date.';
		} else if (myDays < minimumNights) {
			message = 'Your check in and check out dates must be at least '+minimumNights+' days apart.';
		} else {
			message = '';

			var myPeak = 0;

			var loopDate = startJS;
			for(var i = 0; i < myDays; i++){
				if (Calendar._PEAKSEASON[makeDate(loopDate)] > 0) {
					myPeak += Number(Calendar._PEAKSEASON[makeDate(loopDate)]);
				}
				if (Calendar._SOLDOUT[makeDate(loopDate)] > 0) {
					message = 'Some of your selected dates are not available.';
				}
				loopDate.setDate(loopDate.getDate() + 1);
			}

			document.getElementById(nightID).value = myDays;
			document.getElementById(peakID).value = myPeak;
			document.getElementById(peakErrorID).value = '';
		}
	} else {
		message = 'Please enter a valid start and stop date.';
		
	}
	if (message != '') {
		alert(message);
		document.getElementById(nightID).value = minimumNights;
		document.getElementById(peakID).value = '0';
		document.getElementById(peakErrorID).value = 'ERROR: '+message;
	}
}
