// JavaScript Document

// used for the popup date picker
var calendar1 = new CalendarPopup("mydiv"); 
var calendar2 = new CalendarPopup("mydiv");

calendar1.setCssPrefix("CAL");
calendar2.setCssPrefix("CAL");

/**
 * DHTML date validation script as a Singleton. 
 * Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 * Modified heavily for SonoranGetaways.com
 *
 * @access public
 */
var myDateFn = function(){
	
	// Declaring valid date character, minimum year and maximum year
	var dtCh = "/";
	var minYear=1900;
	var maxYear=2100;
	
	/**
	 * Validates provided string as integers.
	 * @access private
	 */
	function isInteger(s){
		var i;
		for (i = 0; i < s.length; i++){   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		// All characters are numbers.
		return true;
	}
	
	/**
	 * Search through string's characters one by one.
	 * If character is not in bag, append to returnString.
	 * @access private
	 */
	function stripCharsInBag(s, bag){
		var i;
		var returnString = "";

		for (i = 0; i < s.length; i++){   
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	}
	
	/**
	 * February has 29 days in any year evenly divisible by four,
	 * EXCEPT for centurial years which are not also divisible by 400.
	 * @access private
	 */
	function daysInFebruary(year){
		return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}
	
	/**
	 * Determines how many days are in the provided month.
	 * @access private
	 */
	function DaysArray(n) {
		for (var i = 1; i <= n; i++) {
			this[i] = 31
			if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
			if (i==2) {this[i] = 29}
	   } 
	   return this
	}
	
	/**
	 * Checks if provided string is a valid date.
	 * @access private
	 */
	function isDate(dtStr){
		var daysInMonth = DaysArray(12)
		var pos1=dtStr.indexOf(dtCh)
		var pos2=dtStr.indexOf(dtCh,pos1+1)
		var strMonth=dtStr.substring(0,pos1)
		var strDay=dtStr.substring(pos1+1,pos2)
		var strYear=dtStr.substring(pos2+1)
		strYr=strYear
		if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
		for (var i = 1; i <= 3; i++) {
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}
		month=parseInt(strMonth)
		day=parseInt(strDay)
		year=parseInt(strYr)
		if (pos1==-1 || pos2==-1){
			alert("The date format should be : mm/dd/yyyy")
			return false
		}
		if (strMonth.length<1 || month<1 || month>12){
			alert("Please enter a valid month")
			return false
		}
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
			alert("An invalid date was selected for either the Start date or the Stop date.")
			return false
		}
		if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
			alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
			return false
		}
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
			alert("Please enter a valid date")
			return false
		}
		return true
	}
	
	/**
	 * Returns current date in format MM/DD/YYYY.
	 * @access private
	 */
	function CurrentDate(){
		var mydate=new Date()
		var year=mydate.getYear()
		if (year < 1000)
			year+=1900
		var day=mydate.getDay()
		var month=mydate.getMonth()+1
		if (month<10)
		month="0"+month
		var daym=mydate.getDate()
		if (daym<10)
		daym="0"+daym
		return month+"/"+daym+"/"+year
	}			
	
	return {
		
		/**
		 * Performs validation on provided date range.
		 * @access 	public
		 * @param	string	start date
		 * @param	string	end date
		 * @param	int		minimum day between start and stop dates
		 * @param	bool
		 */
		DateCheck: function(dt1, dt2, minDay){
			
			if (minDay < 0){
				minDay = 2;
			}
			
			if (isDate(dt1)==false){
				return false;
			}
			if (isDate(dt2)==false){
				return false;
			}
			
			if (minDay > 0 && Date.parse(dt1) >= Date.parse(dt2)) {
				alert("The check out date must occur after the check in date.");
				return false;
			}	
			
			if (Date.parse(dt1) < Date.parse(CurrentDate())) {
				alert("The check in date cannot occur before today's date.");
				return false;
			}	
		
		
			// The number of milliseconds in one day
			var ONE_DAY = 1000 * 60 * 60 * 24
			
			// Create date objects
			var date1_ms = new Date(dt1)
			var date2_ms = new Date(dt2)
		
			// Convert both dates to milliseconds
			var date1_ms = date1_ms.getTime()
			var date2_ms = date2_ms.getTime()
		
			// Calculate the difference in milliseconds
			var difference_ms = Math.abs(date1_ms - date2_ms)
			
			// Convert back to days and return
			var num_nights = Math.round(difference_ms/ONE_DAY);
			
			// Check the minimum number of nights
			if (num_nights < minDay) {
				alert("There is a " + minDay + " night minimum stay. Please select another Check-Out date.");
				return false;
			}	
		
			return true;
		}			
		
	};
}();
