//set todays date
Now = new Date();
NowDay = Now.getDate();
NowMonth = Now.getMonth();
NowYear = Now.getYear();

//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear)
{
  var DaysInMonth = 31;
  if (WhichMonth == "Apr" || WhichMonth == "Jun" || WhichMonth == "Sep" || WhichMonth == "Nov") DaysInMonth = 30;
  if (WhichMonth == "Feb" && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;
  if (WhichMonth == "Feb" && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;
  return DaysInMonth;
}

//function to change the available days in a months
function ChangeOptionDays(Which)
{
  DaysObject = eval("document.appl." + Which + "Day");
  MonthObject = eval("document.appl." + Which + "Month");
  YearObject = eval("document.appl." + Which + "Year");

  Month = MonthObject[MonthObject.selectedIndex].text;
  Year = YearObject[YearObject.selectedIndex].text;

  DaysForThisSelection = DaysInMonth(Month, Year);
  CurrentDaysInSelection = DaysObject.length;
  if (CurrentDaysInSelection > DaysForThisSelection)
  {
    for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
    {
      DaysObject.options[DaysObject.options.length - 1] = null
    }
  }
  if (DaysForThisSelection > CurrentDaysInSelection)
  {
    for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
    {
      NewOption = new Option(DaysObject.options.length + 1);
      DaysObject.add(NewOption);
    }
  }
    if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}

//function to set options to today
function SetToToday(Which)
{
  DaysObject = eval("document.appl." + Which + "Day");
  MonthObject = eval("document.appl." + Which + "Month");
  YearObject = eval("document.appl." + Which + "Year");

  YearObject[0].selected = true;
  MonthObject[NowMonth].selected = true;

  ChangeOptionDays(Which);

  DaysObject[NowDay-1].selected = true;
}

//function to write option years plus x
function WriteYearOptions(YearsAhead)
{
  line = "";
  for (i=0; i<YearsAhead; i++)
  {
    var myyear = NowYear + i;
    line += "<option value='" + myyear + "'>";
    line += myyear;
    line += "</option>";
  }
  return line;
}
//function to set hidden date info
function SetDate(fromorto)
{
  var greggy = "";
  if (fromorto == "From") {
   var greggy = greggy + document.appl.FirstSelectDay.options[document.appl.FirstSelectDay.selectedIndex].value;
   var greggy = greggy + " " + document.appl.FirstSelectMonth.options[document.appl.FirstSelectMonth.selectedIndex].value;
   var greggy = greggy + " " + document.appl.FirstSelectYear.options[document.appl.FirstSelectYear.selectedIndex].value;
   document.appl.DateFrom.value = greggy;
   };
  if (fromorto == "To") {
   var greggy = greggy + document.appl.LastSelectDay.options[document.appl.LastSelectDay.selectedIndex].value;
   var greggy = greggy + " " + document.appl.LastSelectMonth.options[document.appl.LastSelectMonth.selectedIndex].value;
   var greggy = greggy + " " + document.appl.LastSelectYear.options[document.appl.LastSelectYear.selectedIndex].value;
   document.appl.DateTo.value = greggy;
   };
  return true;
}
