Algorithm Math Delphi

Title: How to calculate an Italian Fiscal Code
Question: In Italy, everyone gets from the State a Fiscal Code, that is calculated on his name, family name, birth date, gender, and place where is born. The code is 16 digits length and the last digit is a verify digit.
This is the way to calculate it.
Answer:
You must know the last name of a person (Cognome), the first name (Nome), the birth date (Nascita=Birth), the gender (Sesso), and the code (4 digits, 1 char + 3 numbers) of the town where is born (Provincia).
You can find a complete list of the italian towns that have a code and their codes in this zipped Access Database (MDB):
(file is currently offline, if you're interested in it, please mail me)
The function returns the correct string.
Type
TGender = ( tsMale, tsFemale );
Function CalcCodFisc( Cognome, Nome : String; Nascita : TDateTime; Sesso : TGender; Provincia : String ) : String;
Var
Vocali : String; // Vocali = "AEIOU"
Consonanti : String; // Consonanti = "BCDFGHJKLMNPQRSTVWXYZ"
IdX : Integer;
AppoNum : Integer;
tmpInt : Integer;
Anno : Word; // Anno = Year
Mese : Word; // Mese = Month
Giorno : Word; // Giorno = Day
Begin
Cognome := UpperCase( Cognome );
Vocali := '';
Consonanti := '';
DecodeDate( Nascita, Anno, Mese, Giorno );
For IdX := 1 To Length( Cognome ) Do Begin
If ( Pos( Copy( Cognome, IdX, 1 ), 'AEIOU' ) 0 ) Then
Vocali := Vocali + Copy( Cognome, IdX, 1 )
Else If ( Pos( Copy( Cognome, IdX, 1 ), 'BCDFGHJKLMNPQRSTVWXYZ' ) 0 ) Then
Consonanti := Consonanti + Copy( Cognome, IdX, 1 );
If ( Length( Consonanti ) = 3 ) Then Break;
End;
If ( Length( Consonanti ) If ( Length( Consonanti ) For IdX := 1 To ( 3 - Length( Consonanti ) ) Do
Consonanti := Consonanti + 'X';
Result := Consonanti;
Nome := UpperCase( Nome );
Vocali := '';
Consonanti := '';
For IdX := 1 To Length( Nome ) Do Begin
If ( Pos( Copy( Nome, IdX, 1 ), 'AEIOU' ) 0 ) Then
Vocali := Vocali + Copy( Nome, IdX, 1 )
Else If ( Pos( Copy( Nome, IdX, 1 ), 'BCDFGHJKLMNPQRSTVWXYZ' ) 0 ) Then
Consonanti := Consonanti + Copy( Nome, IdX, 1 );
If ( Length( Consonanti ) = 4 ) Then Break;
End;
If ( Length( Consonanti ) = 4 ) Then
Consonanti := Consonanti[ 1 ] + Copy( Consonanti, 3, 2 )
Else If ( Length( Consonanti ) 3 ) Then Begin
Consonanti := Copy( ( Consonanti + Vocali ), 1, 3 );
If ( Length( Consonanti ) Consonanti := Copy( ( Consonanti + 'XXX' ), 1, 3 );
End;
Result := Result + Consonanti;
Result := Result + Copy( IntToStr( Anno ), 3, 2 );
Result := Result + Copy( 'ABCDEHLMPRST', Mese, 1 );
If ( Sesso = tsFemale ) Then
Result := Result + IntToStr( Giorno + 40 )
Else
If ( Giorno Result := Result + '0' + IntToStr( Giorno )
Else
Result := Result + IntToStr( Giorno );
Result := Result + Provincia;
tmpInt := 0;
IdX := 1;
Repeat
AppoNum := Pos( Copy( Result, IdX, 1 ), 'B1A0KKPPLLC2QQD3RRE4VVOOSSF5TTG6UUH7MMI8NNJ9WWZZYYXX' );
tmpInt := tmpInt + ( ( AppoNum - 1 ) And ($7FFE) ) Div 2;
IdX := IdX + 1;
If ( IdX 15 ) Then Break;
AppoNum := Pos( Copy( Result, IdX, 1 ), 'A0B1C2D3E4F5G6H7I8J9KKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ' );
tmpInt := tmpInt + ( ( AppoNum - 1 ) And ($7FFE) ) Div 2;
IdX := IdX + 1;
Until ( Result = '' ); // Infinite loop, exits with the Break instruction.
tmpInt := tmpInt Mod 26;
Result := Result + Copy( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', ( tmpInt + 1 ), 1 );
End;