// JavaScript Document
// ------------------------------------------------------------------------------------------------------
// WS_DateFinder.js
// White Stone Marketing Date Setting Script - Copyright 2007
// Version 1.2 - 5/3/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
// ------------------------------------------------------------------------------------------------------
// 
/*
To install on AvailabilityOnline, use code like the following

	<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

	<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>
*/

// Field Variable Names - Change for different forms
// Availability Online
// DayField = "dayselect"
// MonthField = "monthselect"
// YearField = "yearselect"

// Webervations
DayField = "zday"
MonthField = "zmonth"
YearField = "zyear"

// 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;
}


