// JavaScript Document
// ------------------------------------------------------------------------------------------------------
// WS_DateFinder.js
// White Stone Marketing Date Setting Script - Copyright 2007
// Version 1.3 - 6/26/08
// Created by Scott Crumpton, Inspired by Lee Hinder
// To use this script on your website, you must obtain written permission from White Stone Marketing LLC
// and this reference must remain in the script.
// http://www.whitestonemarketing.com
//
// Version History
// 1.0 First Version
// 1.1 Added Field name Variables for Webervations vs. AvailabilityOnline
// 1.2 Added Fix for day field on FireFox for DOM2
// 1.3 Tweaked a few things for Res Service Selection
// ------------------------------------------------------------------------------------------------------
// 
// ####### Instructions #######

// Regardless of the Res System used you need this code on the page:
// <script type="text/javascript" src="Scripts/WS_DateFinder.js"></script>
// <body onload="SetToToday('FirstSelect');">
/*
To install on AvailabilityOnline, use code like the following -- be sure to change hidden fields
to match client info.

	<script type="text/javascript" src="Scripts/WS_DateFinder.js"></script>
	</head>
	
	<body onLoad="SetToToday('FirstSelect');">
	
	<form name="Form1" method="Post" action="http://www.availabilityonline.com/availtable.asp">
	<input type="hidden" name="un" value="whitegate" />
	<input type="hidden" name="assocId" value="32" />
	<select name="monthselect" onChange="ChangeOptionDays('FirstSelect')">
		<script language="JavaScript">document.write(WriteMonthList());</script>
	</select>
	<select name="dayselect" onChange="ChangeOptionDays('FirstSelect')">
		<script language="JavaScript">document.write(WriteDayList());</script>
	</select>
	<select name="yearselect" onChange="ChangeOptionDays('FirstSelect')">
		<script language="JavaScript">document.write(WriteYearOptions());</script>
	</select>
	<input type="submit" value="  Check Availability  " name="submit" />
	</form>
*/

/*
To install on Webervations, use code like the following

<form name="Form1" method="Post" action="http://www.webervations.com/magic-scripts/resbook.asp" onsubmit="pageTracker._linkByPost(this)">
<table cellspacing="0" cellpadding="2" width="96%" align="center">
<tr><td align="center"><div align="center"><strong>Select Your Arrival Date &amp; Nights</strong></div>
</tr><tr valign="top"><td align="center" height="99"><div align="left">

<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td rowspan="4">&nbsp; &nbsp; &nbsp; &nbsp;</td>
<td><font face="Arial, Helvetica, sans-serif">Day </font></td>
<td><font face="Times New Roman, Times, serif" size="2">
	<select name="zday" onchange="ChangeOptionDays('FirstSelect')">
	<script language="JavaScript">document.write(WriteDayList());</script>
	</select>
</font></td></tr><tr>
<td><font face="Arial, Helvetica, sans-serif">Month</font></td>
<td><font face="Times New Roman, Times, serif" size="2">
	<select name="zmonth" onchange="ChangeOptionDays('FirstSelect')">
	<script language="JavaScript">document.write(WriteMonthList());</script>
	</select>
</font></td></tr><tr>
<td><font face="Arial, Helvetica, sans-serif">Year</font></td>
<td><font face="Times New Roman, Times, serif" size="2">
	<select name="zyear" onchange="ChangeOptionDays('FirstSelect')">
	<script language="JavaScript">document.write(WriteYearOptions());</script>
	</select>
</font></td></tr><tr>
<td><font face="Arial, Helvetica, sans-serif">Nights</font></td>
<td> <font face="Times New Roman, Times, serif" size="2">

	<select name="zlength">
	<option value="1">1 Night
	<option value="2" selected>2 Nights
	<option value="3">3 Nights
	<option value="4">4 Nights
	<option value="5">5 Nights
	<option value="6">6 Nights
	<option value="7">7 Nights
	<option value="8">8 Nights
	<option value="9">9 Nights
	<option value="10">10 Nights
	<option value="11">11 Nights
	<option value="12">12 Nights
	<option value="13">13 Nights
	<option value="14">14 Nights
	</select>

</font></td></tr></table>
<div align="center"><font face="Arial, Helvetica, sans-serif" size="2"><br>
<input type="hidden" name="memberid" value="21800">
<input type="hidden" name="zcaller" value="http://www.chesterfieldinn.com/">
<input type="hidden" name="cbp" value="">
<input type="hidden" name="showit" value="Y">
<input type="hidden" name="time" value="now()">
<script language="JavaScript">
<!--
	document.write('<input type="hidden" name="javaon" value="Y">');
// -->
     </script>

	<input type="submit" value="  Check Availability  " name="submit">
</font></div></div></tr></table></form>
*/
// ####### End Instructions #######



// ####### Customization Section #######

// Choose Reservation System (Webervations or AvailabilityOnline)
// by removing comments by the one you want and commenting out the other

ResSystem = "Webervations";
// ResSystem = "AO";

// ####### End Customization Section #######
// ####### Do Not Edit Below This Line #######




// Which Provider?

if (ResSystem == "Webervations") {
	// Webervations
	DayField = "zday";
	MonthField = "zmonth";
	YearField = "zyear";
}
else {
	// Availability Online
	DayField = "dayselect";
	MonthField = "monthselect";
	YearField = "yearselect";
}


// Set todays date
Now = new Date();
NowDay = Now.getDate();
NowMonth = Now.getMonth();
NowYear = Now.getYear();
if (NowYear < 2000) NowYear += 1900; //for Netscape

// Create Month Name Array
months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');


//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear)
{
  var vDaysInMonth = 31;
  if (WhichMonth == "Apr" || WhichMonth == "Jun" || WhichMonth == "Sep" || WhichMonth == "Nov") vDaysInMonth = 30;
  if (WhichMonth == "Feb" && (WhichYear/4) != Math.floor(WhichYear/4))	vDaysInMonth = 28;
  if (WhichMonth == "Feb" && (WhichYear/4) == Math.floor(WhichYear/4))	vDaysInMonth = 29;
  return vDaysInMonth;
}



//function to change the available days in a month when any date field is changed
function ChangeOptionDays(Which)
{
//  DaysObject = eval("document.Form1." + Which + "Day");
//  MonthObject = eval("document.Form1." + Which + "Month");
//  YearObject = eval("document.Form1." + Which + "Year");

  DaysObject = eval("document.Form1." + DayField);
  MonthObject = eval("document.Form1." + MonthField);
  YearObject = eval("document.Form1." + YearField);

  Month = MonthObject[MonthObject.selectedIndex].text;
  MonthNum = MonthObject[MonthObject.selectedIndex].value;
  Year = YearObject[YearObject.selectedIndex].text;
  
  	// change to next year if month selected is less than current month
	if (MonthNum < (NowMonth + 1)) {
		YearObject[0].selected = false;
		YearObject[1].selected = true;
		Year = YearObject[YearObject.selectedIndex].text;
	}


  // Set the number of days in the month
  DaysForThisSelection = DaysInMonth(Month, Year);
  CurrentDaysInSelection = DaysObject.length;
	
		//alert('Days in This Month: ' + DaysForThisSelection + ' - Days in Current Array: ' + CurrentDaysInSelection);
	
	// If there are more <option> lines than needed
  if (CurrentDaysInSelection > DaysForThisSelection)
		{
			for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
			{
				DaysObject.options[DaysObject.options.length - 1] = null
			}
		}

  // If there are less <option> lines than needed
  if (DaysForThisSelection > CurrentDaysInSelection)

		{
			for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
			{
				NewOption = new Option(DaysObject.options.length + 1);
				
				// This only works in IE => DaysObject.add(NewOption);
				
				// First try the DOM2 method for FireFox
				try {
					DaysObject.add(NewOption,null);
				}
				// ... And if that doesn't work use the IE-only method
				catch (e) {
					DaysObject.add(NewOption);
				}

			}
		}
		
			if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
			
}


//function to set options to today
function SetToToday(Which)
{
//  DaysObject = eval("document.Form1." + Which + "Day");
//  MonthObject = eval("document.Form1." + Which + "Month");
//  YearObject = eval("document.Form1." + Which + "Year");

  DaysObject = eval("document.Form1." + DayField);
  MonthObject = eval("document.Form1." + MonthField);
  YearObject = eval("document.Form1." + YearField);

  YearObject[0].selected = true;
  MonthObject[NowMonth].selected = true;

  ChangeOptionDays(Which);

  DaysObject[NowDay-1].selected = true;
}


//function to write months
function WriteMonthList()
{
  line = "";
  for (i=1; i<13; i++)
  {
    line += "<option value=\"" + i + "\">" + months[i - 1] + "</option>";
  }
  return line;
}

//function to write Days of the month array
function WriteDayList()
{
  line = "";
  for (i=1; i<DaysInMonth+1; i++)
  {
    line += "<option>" + i + "</option>";
  }
  return line;
}

//function to write option years plus x
function WriteYearOptions()
{
  YearsAhead = 2  // hardcoded due to ChangeOptionDays where it adds a year if month selected is before current month.
  line = "";
  for (i=0; i<YearsAhead; i++)
  {
    line += "<option>";
    line += NowYear + i + "</option>";
  }
  return line;
}


