/**
 * Returns the amount of days in a given month and year, considering leap years.
 * 
 * @param int month
 *        The month for which to determine the amount of days (from 1 to 12).
 * @param int year
 *        The year for which to determine the amount of days (relevant for the 
 *        calculation of leap years).
 * @return int
 *        The amount of days in the given month and year, or 0 if an invalid
 *        month was given.
 */
function nlDaysInMonth(month, year)
{
  switch (month) {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
      return 31;
    case 4: case 6: case 9: case 11:
      return 30;
    case 2:
      // Leap year calculation:
      // Basically: years evenly divisible by 4 are leap years.
      // Exceptions: Years evenly divisible by 100 are not leap years, unless 
      // they are also evenly divisible by 400, in which case they are leap years.
      return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 29 : 28;
    default:
      return 0;
  }
}

/**
 * Determines if the given date string presents a valid date.
 * 
 * @param string dateString
 *        The date string (e.g. user input) that should be checked.
 * @return bool
 *        true, if the given date string is a valid date; otherwise false.
 */
function nlIsValidDate(dateString)
{
  var separator = '.';
  
  // the date string must only contain numbers and the separator
  for (var i = 0; i < dateString.length; i++) {
    var character = dateString.charAt(i);
    if (character != separator && (character < '0' || character > '9')) {
      return false;
    }
  }

  // split the date string into three seperate parts, more than three is an 
  // error, and parse these parts (day, month, year) into integers
  var dateStringParts = dateString.split(separator);
  if (dateStringParts.length != 3) {
    return false;
  }
  day = parseInt(dateStringParts[0], 10);
  month = parseInt(dateStringParts[1], 10);
  year = parseInt(dateStringParts[2], 10);

  if (isNaN(year) || isNaN(month) || isNaN(day)) {
    return false;
  }

  if (month < 1 || month > 12) {
    return false;
  }
  
  if (day < 1 || day > nlDaysInMonth(month, year)) {
    return false;
  }
  
  return true;
}

/**
 * Checks the birthday input element inside the ContentItemNL input form.
 * 
 * @param object input
 *        The DOM element for the birthday input.
 * @param string errorString
 *        The error string that should be displayed if the validation fails.
 * @return boolean
 *        true, if the date was validated successfully; otherwise false.
 */
function nlCheckBirthday(input, errorString)
{
  inputElement = document.getElementById(input);
  if (inputElement != null) {
    inputValue = inputElement.value;
    // trim whitespace
    inputValue = inputValue.replace('/^\s+/', '').replace('/\s+$/', '');
    
    if (inputValue == '' || nlIsValidDate(inputValue)) {
      return true;
    }
  }
  alert(errorString);
  inputElement.focus();
  return false;
}

