// JavaScript Document
//////////////////////////////////////////
// Supporting date variables and functions
//////////////////////////////////////////
// Get today's current date. not necessary for this demo or current//
var now = new Date();

// Array list of days.
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

// Array list of months.
var months = new Array('01','02','03','04','05','06','07','08','09','10','11','12');

// Calculate four digit year.
function fourdigits(number)	{
	return (number < 1000) ? number + 1900 : number;
	}
	
//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear){
  var DaysInMonth = 31;
  if (WhichMonth == "04" || WhichMonth == "06" || WhichMonth == "09" || WhichMonth == "11") DaysInMonth = 30;
  if (WhichMonth == "02" && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;
  if (WhichMonth == "02" && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;
  return DaysInMonth;
}

function callIRM(){
	var url = "reservations.php?";
	var earliest = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 2);
	var arrival = document.form1.wdcArrival_input.value;
    var departure = document.form1.wdcDeparture_input.value;
	var nights = document.form1.wcNights_input.value;
	var location = document.form1.PropertyCode.value;
	var adults = document.form1.People1.value;
	if (arrival != "Select Date"){
		if (nights <= 0 ){
       alert("Departure date must be later than arrival date.\nPlease change your calendar dates and try again.")
       return false;
       }
    //additional checking
    if (nights > 10){  // for example enforce a maximum stay
       alert("Online Reservations cannot be made for more than a 10 night stay.\nPlease change your calendar dates, or call us about your extended stay.\nThank You");
       return false;
       }
	url = url + "Arrival=" + arrival;
	if (!departure) {
		url = url + "&Nights=" + nights;
		}
	else{
		url = url + "&Departure=" + departure;
		}
	}
	else{
		arrival = new Date(earliest.getFullYear(), earliest.getMonth(), earliest.getDate());
		var arriveField = (months[arrival.getMonth()]) + "/" +
								   ((arrival.getDate()<10) ? "0" : "")+ arrival.getDate() + "/" +    
								   fourdigits(arrival.getFullYear());
		departure = new Date(earliest.getFullYear(), earliest.getMonth(), earliest.getDate() + 2);
		var departField = (months[departure.getMonth()]) + "/" +
								   ((departure.getDate()<10) ? "0" : "")+ departure.getDate() + "/" +    
								   fourdigits(departure.getFullYear());
		url = url + "Arrival=" + arriveField;
		url = url + "&Departure=" + departField;
	}	
	
	if(!adults){
		adults=1;
	}
	url = url + "&People1=" + adults;
	if(location){
		url = url + "&PropertyCode=" + location;
	}
	window.location.assign(url);	
}//end callIRM()

function callIRM_All(){
	var url = "reservations.html?&People1=&PropertyCode=";
	window.location.assign(url);
}//end callIRM_All()

//Default Days ahead of todays date for arrival and departure set to current dates//
var arr = 2;
var dep = 4;

//Get actual values for date and month from DOM//
var nowdate = now.getDate();   
var nowmonth = now.getMonth();  
var nowyear = now.getFullYear();

var arrivalDate = new Date(nowyear, nowmonth, nowdate);
arrivalDate.setDate(arrivalDate.getDate() + arr);
var departureDate = new Date(nowyear, nowmonth, nowdate);
departureDate.setDate(departureDate.getDate() + dep);

var arrivalInit = (months[arrivalDate.getMonth()]) + "/" +
								   ((arrivalDate.getDate()<10) ? "0" : "")+ arrivalDate.getDate() + "/" +    
								   fourdigits(arrivalDate.getFullYear());
								   
var departureInit = (months[departureDate.getMonth()]) + "/" +
								   ((departureDate.getDate()<10) ? "0" : "")+ departureDate.getDate() + "/" +    
								   fourdigits(departureDate.getFullYear());
								   
var numberNights = Math.round(departureDate - arrivalDate)/86400000;

function calcDates(){
		var nights = parseInt(document.form1.wcNights_input.value);
		var arrivals = document.form1.arriveDefault.value;
		arrivals = arrivals.split("/");
		var newArrival = new Date(arrivals[0]+"/"+arrivals[1]+"/"+arrivals[2]);
		var newDeparture = new Date(arrivals[0]+"/"+arrivals[1]+"/"+arrivals[2]);
		if (!nights){
			return;
			}
		if (nights == 0){
			return;
			}
		if (nights > 10){
			nights = 10;
		}
		
		newDeparture.setDate(newDeparture.getDate() + nights);
		var departField = (months[newDeparture.getMonth()]) + "/" +
							   ((newDeparture.getDate()<10) ? "0" : "")+ newDeparture.getDate() + "/" +    
							   fourdigits(newDeparture.getFullYear());
		document.form1.departDefault.value = departField;
		document.form1.wdcDeparture_input.value = departField;
		document.form1.wcNights_input.value = nights;
		document.form1.nightsDefault.value = nights;
		} //end calcDates()