// Contribuído por Fernando Carvalho

function IsNumber(s: string): Boolean;
var
  i: Integer;
begin
  Result := False;
  for i := 1 to Length(s) do
    case s[i] of '0'..'9':;
     else Exit;
    end;
  Result := True;
end;


// Função para validar Nº idenf. Fiscal
function isValidoNIF(const strNIF:string):boolean;
var
  checkdigito, i : integer;
begin
  if (length(strNIF) = 9) and (isNumber(strNIF)) then
   begin
    if (strNIF[1] in ['1','2','5','6','8','9'] )  then
     begin
         checkdigito := strtoint(strNIF[1]) * 9;
        for i := 3 to 9 do
         checkdigito := checkdigito + (strtoint(strNIF[i-1])  * (11 - i));
         checkdigito := 11 - (checkDigito mod 11);
        If (checkDigito >= 10) then checkDigito := 0;
        If (checkDigito = strtoint(strNIF[9])) then result := True;
      end;
    end
   else
    result := false;
end;


// Função para validar Nº idenf. Seg social
function isValidoNISS(const strNISS:string):boolean;
const
 factor : array[1..10] of byte = (29,23,19,17,13,11,7,5,3,2);
var
 soma, i : integer;
begin
  soma := 0;
  if (length(strNISS) = 11) and (isNumber(strNISS)) then
   begin
    if (strNISS[1] in ['1','2'] ) then
     begin
        for i := 1 to 10 do
         soma  := soma + ((strtoint(strNISS[i])  * factor[i]));
         If (strtoint(strNISS[11]) = (9 - (soma mod 10))) then result := True;
      end;
    end
   else
    result := false;
end;


// Valida nº BI
function isValidoBI(const strBI:string):boolean;
var
  soma : integer;
  TamanhoBI, i, cont : byte;
begin
  soma := 0;
  TamanhoBI := length(strBI);
  if ((tamanhoBI > 6) and (TamanhoBI < 11)) and (isNumber(strBI)) then
   begin
        cont := 0;
        for i := TamanhoBI downto 1 do
         begin
          inc(cont);
          soma  := soma + ((strtoint(strBI[i])  * cont));
         end;
         if ((Soma mod 11 = 0) or (soma mod 11 = 10)) then result := True;
    end
   else
    result := false;
end;
