
// autofill depature date by 2 days if blank

function calculateDeparture() { 

	  var arrival = document.form1.wdcArrival_input.value;
	  var departure = document.form1.wdcDeparture_input.value;

	  if (departure == "") {

       	var arrDate = arrival.split("/");
		var arrDateM = arrDate[0];
		var arrDateD = arrDate[1];
		var arrDateY = arrDate[2];

   		 var arrDateUTC = new Date(Date.UTC(arrDateY,
         (stripLeadingZeroes(arrDateM)-1),
         stripLeadingZeroes(arrDateD),
         stripLeadingZeroes(1),
         stripLeadingZeroes(1),
         stripLeadingZeroes(1)));
		 depDateUTC = ((arrDateUTC.getTime()+259200000)/1000.0);
 
  
 		var depDate = new Date(depDateUTC * 1000);
		//depDateGMT = depDate.toGMTString();
		
		var depDateM = addLeadingZeroes(depDate.getMonth()+1);
		var depDateD = addLeadingZeroes(depDate.getDate());
		var depDateY = depDate.getYear();
		if (depDateY < 2000) depDateY = 1900 + depDateY;

		depDate2 = depDateM+"/"+depDateD+"/"+depDateY;

 		document.form1.wdcDeparture_input.value = depDate2;

		}
	} 
	
	
	
	function stripLeadingZeroes(input)
	  {
		if((input.length > 1) && (input.substr(0,1) == "0"))
		  return input.substr(1);
		else
		  return input;
	  }
	  
	  function addLeadingZeroes(input)
	  {
		if(input < 10)
		  return "0" + input;
		else
		  return input;
	  }
	


 ///////////////////////////////////////////////////////////////////////
//Sample script for pulling selection criteria from marketing web site
//which is passed to the IRM and put in an iframe
///////////////////////////////////////////////////////////////////////




// construct the IRM url including all search criteria and dates
// source the irm iframe with this url

function callIRM()
{
    var url = "http://www.fernielodgingco.com//search?";
  // D6 SITE:  var url = "http://www.fernielodgingco.com//search??http://bookings.fernielodgingco.com/irmnet/res/resmain.aspx?";
  // BETA SITE: var url = "http://flc.driven.ca/search??http://bookings.fernielodgingco.com/irmnet/res/resmain.aspx?";
  // OLD SITE: var url = "http://www.fernielodgingco.com/fn_bookings.aspx??http://bookings.fernielodgingco.com/irmnet/res/resmain.aspx?";
   var foundOne = false;
      
   //Add guest requets if selected
   //These must correspond to GuestRequest1-9 values
  
   //Add arrival and departure dates if selected 
   var arrival = document.form1.wdcArrival_input.value;
   var departure = document.form1.wdcDeparture_input.value;
   if (arrival != "") 
   {
      //verify that arrival choice is not in the past
      if (new Date(arrival) < now)
      {
         alert("Arrival date must be later than today.\nPlease change your calendar dates and try again.");
         return false;
      }
      
      //for demo convert year to 1998
      var arrDate = arrival.split("/");
//	  arrival = arrDate[0]+"/"+arrDate[1]+"/1998";
	  arrival = arrDate[0]+"/"+arrDate[1]+"/"+arrDate[2];

      if (departure != "") 
      {
         //for demo convert year to 1998 (or 1999 if applicable)
         var depDate = departure.split("/");
         if (arrDate[0] == "12" && depDate[0] != "12")
//         { departure =  depDate[0]+"/"+depDate[1]+"/1999"; }
         { departure =  depDate[0]+"/"+depDate[1]+"/"+depDate[2]; }
         else
//         { departure =  depDate[0]+"/"+depDate[1]+"/1998"; }
         { departure =  depDate[0]+"/"+depDate[1]+"/"+depDate[2]; }
            
      
         //verify departure > arrival
         var nights = calcNights(arrival, departure);
         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;
         }
      }
      
      if (foundOne) url = url + "&";
      url = url + "Arrival=" + arrival;
      foundOne = true;      
      if (departure != "") 
      { url = url + "&Departure=" + departure; }
      else   // use default number of nights
      { url = url + "&Nights=" + numberNights; }
      
      //Add people selections
      url = url + "&People1=" + document.form1.ddAdult.value;
      var youth = document.form1.ddYouth.value;
      if (youth != "0") url = url + "&People2=" + youth;
      var child = document.form1.ddChild.value;
      if (child != "0") url = url + "&People3=" + child;
      var pet = document.form1.ddPet.value;
      if (pet != "0") url = url + "&People4=" + pet;
   }
   
   //Nothing was selected, get all rooms
   //if (!foundOne) url = url + "AllRooms=true";
   //alert(url);
   
   // put IRM.net in the iframe
//TM   var fFrame = document.getElementById('fernieFrame');
//TM   parent.fFrame.location.href = url;
   parent.location.href = url;
   
   // OR open a new window for the IRM  
   //window.open(url,'_new','height=900, width=850, scrollbars=yes, resizable=yes');
   
}  //callIRM

// caclulate number of nights from selected arrival and departure dates
function calcNights(arrival, departure)
{   
    var arrDate = arrival.split("/");
    var depDate = departure.split("/");
   	var newArrDate = new Date(arrDate[0]+"/"+arrDate[1]+"/"+arrDate[2]);
	var newDepDate = new Date(depDate[0]+"/"+depDate[1]+"/"+depDate[2]);
	var numNights = (Math.round((newDepDate-newArrDate)/86400000));
	return numNights;
}		

//////////////////////////////////////////
// Supporting date variables and functions
//////////////////////////////////////////
// Get today's current date. 
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;
	}
	

//Get actual values for date and month from DOM, For security purposes of demo year is not set to current//
var nowdate = 01;   //now.getDate();
var nowmonth = 2;  //now.getMonth(); // 2 = March see months array above//
var nowyear = 1998;

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

//For use with demo -will set the default departure date to x number of days ahead of arrival - remove or comment out for current date functionality;
var arr = 0;
var dep = 2;



//For use with current dates this function Adds 2 days for default Arrival Date & 4 days for default Departure Date.//
//For Demo use this function just returns proper format for default arrival and departure dates//
function checkDate (m, d, y, field){
	//var maxDays = DaysInMonth(m, y);
	var newDay = d + field;
	var newMonth = months[m];
	var newYear = fourdigits(y);
	//if ((newDay) > maxDays){
//		newDay = newDay - maxDays;
//		var nextMonth = m + 1;
//		if (nextMonth > 11){
//			nextMonth = 0;
//			newYear++;
//			}
//		newMonth = months[nextMonth];
//		}		
//	newDay = ((newDay<10) ? "0" : "")+ newDay;
	var fieldDate = newMonth + "/" +
					newDay + "/" +
					newYear;
	return fieldDate;
	}

// default arrival and departure dates	
var arrivalDate = checkDate(nowmonth, nowdate, nowyear, arr);
var departureDate = checkDate(nowmonth, nowdate, nowyear, dep);
//sets default number of nights
var numberNights = calcNights(arrivalDate, departureDate); 
