// JavaScript Document
// ------------------------------------------------------------------------------------------------------
// WS_DateFinder_modified.js
// White Stone Marketing Date Setting Script - Copyright 2007
// Version 1.5 - 1/29/10
// Created by Scott Crumpton, Inspired by Lee Hinder, Modified for Webervations by Adam Strickland
//
// Version History
// 1.0 First Version
// 1.1 Added Field name Variables for Webervations
// 1.2 Added Fix for day field on FireFox for DOM2
// 1.3 Tweaked a few things for Res Service Selection
// 1.4 Fixed submit="action" field on Webervations
// 1.5 Changed it back to submit="submit"
// ------------------------------------------------------------------------------------------------------
// 
// ####### Instructions #######
// You need this code on the page:
// The following goes inbetween the <head></head> tags
// <script type="text/javascript" src="Scripts/WS_DateFinder_modified.js"></script>
//
// The onload="SetToToday('FirstSelect');" must be in the body tag!  Either Replace the body tag with
// the one provided below if there is nothing else in the <body> tag or simply copy the onload
// statement completely, onload="SetToToday('FirstSelect');" and add it to the <body> tag 
// <body onload="SetToToday('FirstSelect');">
// ####### End Instructions #######

ResSystem = "Webervations";

if (ResSystem == "Webervations") {
	// 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('January','February','March','April','May','June','July','August','September','October','November','December');


//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;
}
