/**
 * Returns the last day of a month
 *
 * @param int month if not set current month is used
 * @param int year if not set current year is used
 * @return int last day of given month
 */
function idf_getLastDayOfMonth(month, year)
{
  if(!year || !month)
  {
    var nowDate = new Date();
  }
  if(!year)
  {
    year = nowDate.getFullYear();
  }
  if(!month)
  {
    month = nowDate.getMonth();
  }
  var fDate = new Date(year, month, 0);
  return fDate.getDate();
}

/**
 * Checks the fields with given fieldName
 * the function expects existing fields with ids fieldName_day, fieldName_month, fieldName_year
 *
 * @param string fieldName
 */
function idf_checkDateInput(fieldName)
{
  checkDateInput(fieldName + '_day', fieldName + '_month', fieldName + '_year', fieldName);
}


/**
 * Checks the fields with given fieldNames
 *
 * @param string dayFieldName
 * @param string monthFieldName
 * @param string yearFieldName
 */
function checkDateInput(dayFieldId, monthFieldId, yearFieldId, errorField)
{
  var daySelect  = $(dayFieldId);
  var monthSelect = $(monthFieldId);
  var yearSelect   = $(yearFieldId);

  var errorMessage = 'Fehlerhafte Eingabe. <br />Das Datum wurde angepasst.';

  if(!daySelect || !monthSelect || !yearSelect)
  {
    return;
  }

  var year    = yearSelect.options[yearSelect.selectedIndex].value;
  var month   = monthSelect.options[monthSelect.selectedIndex].value;
  var day     = daySelect.options[daySelect.selectedIndex].value;
  var lastDay = idf_getLastDayOfMonth(month, year);

  var length = daySelect.options.length;
  if(length > lastDay)
  {
    var oldSelectedIndex = daySelect.selectedIndex;
    value = daySelect.options[length - 1].value;
    while(value > lastDay)
    {
      daySelect.options[length - 1] = null;
      length = daySelect.options.length;
      value = daySelect.options[length - 1].value;
    }
    if(oldSelectedIndex >= length)
    {
      daySelect.selectedIndex = length - 1;
      show_error(errorField, errorMessage);
    }
    return;
  }
  if(length < lastDay)
  {
    length = daySelect.options.length;
    value = daySelect.options[length - 1].value;
    while(value < lastDay)
    {
      value = (daySelect.options[length - 1].value)*1 + 1;
      newOption = new Option(value, value, false, false);
      daySelect.options[length] = newOption;
      length = daySelect.options.length;
    }
  }
  hide_error(errorField);
}


/**
 * Shows a message in given errorField
 * errorField must be named 'error_for_' + errorField
 * this can be combined with form_element_container (errorField will be name given to form_element_container)
 *
 * if msg is empty, field will be hidden
 *
 * @param string errorField name of field to which the error belongs
 * @param string msg message to display
 */
function show_error(errorField, msg)
{
  if(!errorField)
  {
    return;
  }
  errorField = $("error_for_" + errorField);
  if(!msg)
  {
    msg = '';
    display_style = 'none';
  }
  else
  {
    msg = msg;
    display_style = 'block';
  }
  errorField.update(msg);
  errorField.setStyle({
    display: display_style
  });
  errorField.up().setStyle({
    display: display_style
  });
}


/**
 * Hides the errorField
 * errorField must be named 'error_for_' + errorField
 *
 * @param string errorField name of field to which the error belongs
 */
function hide_error(errorField)
{
  show_error(errorField);
}