// JavaScript Document

function controlladata(data)
{
return controlladata_dmy(data);
}

function controlladata_dmy(data)
        {
        if (data.length != 10)
                {
                return (1);  // Lunghezza illegare
                }
        else
                {
                
                // ----------
                // Giorno
                // ----------
                appo = data.substr(0,2);
                appo = appo * 1 ;
                if (appo < 1 || appo > 31)
                        return (2);   // Giorno illegale
                else
                        {
                        // ---------
                        // Mese
                        // ---------
                        appo = data.substr(3,2);
                        appo = appo * 1;
                        if (appo < 1 || appo > 12)
                                return(4);   // mese illegale
                        else
                                {
                                appo = data.substr(6,4);
                                appo = appo * 1;
                                if (appo < 1900 || appo > 2100)
                                        return(5);  // anno illegale
                                else
					{
					// Controllo di nuovo il giorno
					
					giorno = data.substr(0,2);
                        		giorno = giorno * 1;
                        		
					mese = data.substr(3,2);
                        		mese = mese * 1;
					
					anno = data.substr(6,4);
                                	anno = anno * 1;
					
					if (mese == 1 || mese == 3 || mese == 5 || mese == 7 || mese == 8 || mese == 10 || mese == 12)
						{
						max = 31;
						}
					else
						{
						if (mese == 2)
							{
							// verifico se l'anno è bisestile
							if (anno % 4 == 0 && anno % 100 != 0 || anno % 400 == 0)
								{
								max = 29;
								}
							else
								{
								max = 28;
								}
							}
						else
							{
							max = 30;
							}
						}
					if (giorno > max)
						{
						return(6);  // di nuovo giorno illegale
						}				
					}
                                }
                        }
                }

        return(0);
        }

function controlladata_mdy(data)
        {
        if (data.length != 10)
                {
                return (1);  // Lunghezza illegare
                }
        else
                {
                
                // ----------
                // Giorno
                // ----------
                appo = data.substr(3,2);
                appo = appo * 1 ;
                if (appo < 1 || appo > 31)
                        return (2);   // Giorno illegale
                else
                        {
                        // ---------
                        // Mese
                        // ---------
                        appo = data.substr(0,2);
                        appo = appo * 1;
                        if (appo < 1 || appo > 12)
                                return(4);   // mese illegale
                        else
                                {
                                appo = data.substr(6,4);
                                appo = appo * 1;
                                if (appo < 1900 || appo > 2100)
                                        return(5);  // anno illegale
                                else
					{
					// Controllo di nuovo il giorno
					
					giorno = data.substr(3,2);
                        		giorno = giorno * 1;
                        		
					mese = data.substr(0,2);
                        		mese = mese * 1;
					
					anno = data.substr(6,4);
                                	anno = anno * 1;
					
					if (mese == 1 || mese == 3 || mese == 5 || mese == 7 || mese == 8 || mese == 10 || mese == 12)
						{
						max = 31;
						}
					else
						{
						if (mese == 2)
							{
							// verifico se l'anno è bisestile
							if (anno % 4 == 0 && anno % 100 != 0 || anno % 400 == 0)
								{
								max = 29;
								}
							else
								{
								max = 28;
								}
							}
						else
							{
							max = 30;
							}
						}
					if (giorno > max)
						{
						return(6);  // di nuovo giorno illegale
						}				
					}
                                }
                        }
                }

        return(0);
        }

function normalizzadata(data)
{
return normalizzadata_dmy(data);
}

function normalizzadata_mdy(data)
{
return normalizzadata_dmy(data);
}

function normalizzadata_dmy(data)
{
if (data.length == 0)
	{
	return '';
	}
if (data.length == 10)
	{
	return data.substr(0,2) + "/" + data.substr(3,2) + "/" + data.substr(6,4);
	}
if (data.length == 8)
      {
      a = data.substr(2,1);
      b = data.substr(5,1);

      if (! ng_isdigit(a) && ! ng_isdigit(b) ) 
            {
            x = data.substr(6,2);
            if (x > 50)
                  return data.substr(0,6) + '19' + data.substr(6,2) ;
            else
                  return data.substr(0,6) + '20' + data.substr(6,2) ;
            }
      if (ng_isdigit(data) )
            return data.substr(0,2) + '/' + data.substr(2,2) + '/' + data.substr(4,4);
      }
if (data.length == 6 && ng_isdigit(data) )
      {
      x = data.substr(4,2);
      if (x > 50)
            return data.substr(0,2) + '/' + data.substr(2,2) + '/19' + data.substr(4,2);
      else
            return data.substr(0,2) + '/' + data.substr(2,2) + '/20' + data.substr(4,2);
      }    

return data;
   
}

function ng_isdigit(a)
{
for (i = 0 ; i < a.length ; i++)
        {
        b = a.substr(i,1);
        if (b < '0')
        	{
                return false;
        	}
	if (b > '9')
		{
                return false;
                }
        }
return true;

}

