function check_data(obj, mandatory)
{
  var str_date;    // data come stringa
  var str_date_array;  // parti della data (gg, mm, aa)
  var str_day;     // giorno come stringa
  var str_month;
  var str_year;
  var int_day;
  var int_month;
  var int_year;
  var boo_found = false;
  var str_separator_array = new Array("-","/",".");  // separatori data
  var int_element_nr;
  var err = 0;

  str_date = obj.value ;

  // data obbligatoria ?
  if (str_date.length < 1 && !mandatory)
    return (true);

  // che separatore ho usato ?
  // ritorna array con giorn, mese e anno
  for (int_element_nr = 0; int_element_nr < str_separator_array.length; int_element_nr++)
  {
    if (str_date.indexOf(str_separator_array[int_element_nr]) != -1)
    {
      str_date_array = str_date.split(str_separator_array[int_element_nr]);
      if (str_date_array.length != 3)
      {
         err = 1;
         return false;
      }
      else
      {
        str_day = str_date_array[0];
        str_month = str_date_array[1];
        str_year = str_date_array[2];
      }
      boo_found = true;  // trovato separatore !
    }
  }
  if (!boo_found)
  {
    if (str_date.length > 5) // provo a interpretare il dato come ddmmyyyy senza separatori
    {
      str_day = str_date.substr(0, 2);
      str_month = str_date.substr(2, 2);
      str_year = str_date.substr(4);
    }
    else
      err = 1 ;
      return (false) ;
  }
  if (str_year.length == 2)
    str_year = '20' + str_year;

  // giorno come numero
  int_day = parseInt(str_day, 10);
  if (isNaN(int_day))
  {
    err = 2;
    return (false);
  }

  // mese come numero
  int_month = parseInt(str_month, 10);
  if (isNaN(int_month))
  {
    err = 3;
    return (false);
  }
  int_year = parseInt(str_year, 10);
  if (isNaN(int_year))
  {
    err = 4;
    return (false);
  }
  // mese valido ?
  if (int_month > 12 || int_month < 1)
  {
    err = 5;
    return (false);
  }
  // giorno nel mese valido (mese di 31 giorni)
  if ((int_month == 1 || int_month == 3 || int_month == 5 || int_month == 7 || int_month == 8 ||
       int_month == 10 || int_month == 12) && (int_day > 31 || int_day < 1))
  {
    err = 6;
    return (false);
  }
  // giorno nel mese valido (mese di 30 giorni)
  if ((int_month == 4 || int_month == 6 || int_month == 9 || int_month == 11) && (int_day > 30 || int_day < 1))
  {
    err = 7;
    return (false);
  }
  // giorno nel mese valido (febbraio)
  if (int_month == 2)
  {
    if (int_day < 1)
    {
      err = 8;
      return (false);
    }
    // anno bisestile ==> max 29
    if (LeapYear(int_year))
    {
      if (int_day > 29)
      {
        err = 9;
        return (false);
      }
    }
    // anno non bisestile ==> max 28
    else
    {
      if (int_day > 28)
      {
        err = 10;
        return (false);
      }
    }
  }
  str_day = "0" + str_day ;
  str_month = "0" + str_month ;
  obj.value = str_day.substr(str_day.length-2) + "/" + str_month.substr(str_month.length-2) + "/" + str_year ;
  return (true);
}

function LeapYear(int_year)
{
  // anni secolari divisibili per 400 !
  if (int_year % 100 == 0)
  {
    if (int_year % 400 == 0)
      return (true);
  }
  else
  {
    if ((int_year % 4) == 0)
      return (true);
  }
  return (false);
}

