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

// JavaScript Document
function toggleMe(targetId){
	if (document.getElementById){
		target = document.getElementById( targetId );
		if (target.style.display == "none"){
				target.style.display = "";
		} else {
			target.style.display = "none";
		}
	}	 
}
 

// construct the IRM url including all search criteria and dates
// source the irm iframe with this url
function callIRM()
{

var url = "https://res.windsurfercrs.com/bbe/page2.aspx?propertyID=5376&pcode=U3PURI&langID=1";
//&langID=1&rate=RoomOnly&corp=&checkin=08/01/2010&checkout=08/03/2010&adults=2&rooms=1&children=1&childage1=12
var foundOne = false;
      
   //Add guest requets if selected
   //These must correspond to GuestRequest1-9 values
/*   var location = document.form1.ddLocation.value;
   if (location != "*")
   {
      url = url + "Request2=" + location;
      foundOne = true;
   }
   var bedrooms = document.form1.ddBedrooms.value;
   if (bedrooms != "*") 
   {
       if (foundOne) url = url + "&";
       url = url + "Request1=" + bedrooms;
       foundOne = true;
   }
   var pricerange = document.form1.ddPrice.value;
   if (pricerange != "*")
   {
      if (foundOne) url = url + "&";
      url = url + "Request3=" + pricerange;
      foundOne = true;
   }
   var smoking = document.form1.ddSmoking.value;
   if (smoking != "*")
   {
      if (foundOne) url = url + "&";
      url = url + "Request4=" + smoking;
      foundOne = true;
   }*/
  
   //Add checkin and checkout dates if selected 
   var checkin = document.form1.checkin.value;
   var checkout = document.form1.checkout.value;
   if (checkin != "") 
   {
      //verify that checkin choice is not in the past
      if (new Date(checkin) < now)
      {
         alert("Your arrival date must be later than today.\nPlease change your calendar dates and try again.");
         return false;
      }
      
      //for demo convert year to 2010
/**/      var arrDate = checkin.split("/");
	  checkin = arrDate[0]+"/"+arrDate[1]+"/2010";

      if (checkout != "") 
      {
         //for demo convert year to 2010 (or 1999 if applicable)
         var depDate = checkout.split("/");
         if (arrDate[0] == "12" && depDate[0] != "12")
         { checkout =  depDate[0]+"/"+depDate[1]+"/2010"; }
         else
         { checkout =  depDate[0]+"/"+depDate[1]+"/2010"; }
            
      
         //verify checkout > checkin
         var nights = calcNights(checkin, checkout);
         if (nights <= 0 )
         {
            alert("Your departure date must be later than your 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 + "&checkin=" + checkin;
      foundOne = true;      
      if (checkout != "") 
      { url = url + "&checkout=" + checkout; }
      else   // use default number of nights
      { url = url + "&Nights=" + numberNights; }
      
      //Add people selections
      url = url + "&adults=" + document.form1.Adults.value;
/*      url = url + "&children=" + document.form1.Children.value;
      url = url + "&childage1=" + document.form1.Childage1.value;
      url = url + "&childage2=" + document.form1.Childage2.value;
      url = url + "&childage3=" + document.form1.Childage3.value;
      url = url + "&childage4=" + document.form1.Childage4.value;
		  
*/	  
      var children = document.form1.Children.value;
      if (children != "0") url = url + "&children=" + children;
	  
      var childage1 = document.form1.Childage1.value;
      if (childage1 != "0") url = url + "&childage1=" + childage1;
      var childage2 = document.form1.Childage2.value;
      if (childage2 != "0") url = url + "&childage2=" + childage2;
      var childage3 = document.form1.Childage3.value;
      if (childage3 != "0") url = url + "&childage3=" + childage3;
      var childage4 = document.form1.Childage4.value;
      if (childage4 != "0") url = url + "&childage4=" + childage4;
	  
	  
      //Add Rooms# selections
	  
      var rooms = document.form1.rooms.value;
      if (rooms != "0") url = url + "&rooms=" + rooms;
   }
   
   //Nothing was selected, get all rooms
   if (!foundOne) url = url + "AllRooms=true";
   //alert(url);   
   
   // put IRM.net in the iframe
   var irmFrame = document.getElementById('irmFrame');
 //   parent.irmFrame.location.href = url;
   
   // OR open a new window for the IRM  
  window.open(url,'_blank');
   
}  //callIRM

// caclulate number of nights from selected checkin and checkout dates
function calcNights(checkin, checkout)
{   
    var arrDate = checkin.split("/");
    var depDate = checkout.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 = now.getDate();
var nowmonth = now.getMonth(); // 2 = March see months array above//
var nowyear = now.getYear();
//var nowyear = 2010;
//Default Days ahead of todays date for checkin and checkout set to current dates//
var arr = 3;
var dep = 5;

//For use with demo -will set the default checkout date to x number of days ahead of checkin - 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 checkin Date & 4 days for default checkout Date.//
//For Demo use this function just returns proper format for default checkin and checkout 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 checkin and checkout dates	
var checkinDate = checkDate(nowmonth, nowdate, nowyear, arr);
var checkoutDate = checkDate(nowmonth, nowdate, nowyear, dep);
//sets default number of nights
var numberNights = calcNights(checkinDate, checkoutDate); 



