/* PROVIDES FUNCTIONS FOR VALID DATE INPUT */

// event handler for onDocumentReady event.
/*
$(document).ready(function(){

  $("a[title=Alle]").click(function(event){
    $("input[name=bereich]").attr("checked", true);
  });

  $("a[title=Keine]").click(function(event){
    $("input[name=bereich]").attr("checked", false);
  });
    
  WriteYearOptions(5, "vonJahr");
  WriteYearOptions(5, "bisJahr");
  
  SetToToday("von");
  
 });
 */
 
 
// function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear)
{
  var DaysInMonth = 31;

  if (WhichMonth == "04" || WhichMonth == "06" || WhichMonth == "09" || WhichMonth == "11") DaysInMonth = 30;
  if (WhichMonth == "02" && (WhichYear/4) != Math.floor(WhichYear/4))  DaysInMonth = 28;
  if (WhichMonth == "02" && (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.eventFilterForm." + Which + "Tag");
  MonthObject = eval("document.eventFilterForm." + Which + "Monat");
  YearObject = eval("document.eventFilterForm." + Which + "Jahr");
  
  selectedIndex = DaysObject.selectedIndex;

  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.options.length + 1);
      DaysObject.options[DaysObject.options.length] = NewOption;
    }
  }
  
  if (selectedIndex < 0)
  {
    DaysObject.selectedIndex = 0;
  }
    
  if (selectedIndex > DaysObject.options.length - 1)
  {
    DaysObject.selectedIndex = DaysObject.options.length - 1;
  }
}

function ChangeOptionDaysVon()
{
  DaysObject = eval("document.eventFilterForm.vonTag");
  MonthObject = eval("document.eventFilterForm.vonMonat");
  YearObject = eval("document.eventFilterForm.vonJahr");
  
  selectedIndex = DaysObject.selectedIndex;

  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.options.length + 1);
      DaysObject.options[DaysObject.options.length] = NewOption;
    }
  }
  
  if (selectedIndex < 0)
  {
    DaysObject.selectedIndex = 0;
  }
    
  if (selectedIndex > DaysObject.options.length - 1)
  {
    DaysObject.selectedIndex = DaysObject.options.length - 1;
  }
}


function ChangeOptionDaysBis()
{
  DaysObject = eval("document.eventFilterForm.bisTag");
  MonthObject = eval("document.eventFilterForm.bisMonat");
  YearObject = eval("document.eventFilterForm.bisJahr");
  
  selectedIndex = DaysObject.selectedIndex;

  Month = MonthObject[MonthObject.selectedIndex].text;
  Year = YearObject[YearObject.selectedIndex].text;

  DaysForThisSelection = DaysInMonth(Month, Year);
  CurrentDaysInSelection = DaysObject.length - 1;
  
  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,
                             DaysObject.options.length);
      DaysObject.options[DaysObject.options.length] = NewOption;
    }
  }
  
  if (selectedIndex < 0)
  {
    DaysObject.selectedIndex = 0;
  }
    
  if (selectedIndex > DaysObject.options.length - 1)
  {
    DaysObject.selectedIndex = DaysObject.options.length - 1;
  }
}


// function to set options to today
function SetToToday(Which)
{
  dateNow = new Date();

  DaysObject = eval("document.eventFilterForm." + Which + "Tag");
  MonthObject = eval("document.eventFilterForm." + Which + "Monat");
  YearObject = eval("document.eventFilterForm." + Which + "Jahr");

  YearObject[0].selected = true;
  MonthObject[dateNow.getMonth()].selected = true;

  ChangeOptionDays(Which);

  DaysObject[dateNow.getDate() - 1].selected = true;
}

// function to write option years plus x
function WriteYearOptions(YearsAhead, selectId)
{
  baseYear = 2011;  // we start in the year 2011
  
  dateNow = new Date();
  currentYear = dateNow.getYear();
  if (currentYear < 1900)
    currentYear += 1900;  // necessary for all browsers except IE
  
  for (year = baseYear; year <= currentYear + YearsAhead; year++)
  {
//    $('#'+selectId).append(new Option(year, year));

    $('#'+selectId).append($("<option></option>").attr("value", year).text(year));
  }
  
  // return line;
}
