unit U_Registration;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, {te_controls, ksthemeedits,} StdCtrls, {ksthemebuttons,
  ksthemeforms,} Mask, siComp, siLngLnk, {Tolik}clipbrd, Buttons;

type
  TF_Registration = class(TForm)
    btnCancel: TButton;
    btnOk: TButton;
    edtAnswer_1: TEdit;
    edtAnswer2: TEdit;
    lblCaptionProgID: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    lblProgID: TEdit;
    edtAnswer3: TEdit;
    Label4: TLabel;
    edtAnswer4: TEdit;
    Label5: TLabel;
    OpenDialog1: TOpenDialog;
    edtAnswer1: TMaskEdit;
    lng_Forms: TsiLangLinked;
    Label6: TLabel;
    Label7: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Label8: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure edtAnswer_1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edtAnswer_1KeyPress(Sender: TObject; var Key: Char);
    procedure edtAnswer2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnOkKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edtAnswer2Change(Sender: TObject);
    procedure lblEmailClick(Sender: TObject);
    procedure Label4Click(Sender: TObject);
    procedure Label5Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure edtAnswer3KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edtAnswer4KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BitBtn1Click(Sender: TObject);  // Tolik 02/08/2021 --
    procedure BitBtn2Click(Sender: TObject); // Tolik 02/08/2021 --
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  function ShowRegistration: Boolean;

var
  F_Registration: TF_Registration;

implementation

uses U_ProtectionCommon, U_Protection, USCS_Main, {Tolik 30/09/2020}U_Main, U_BaseConstants,
{$IF Defined(PROCAT_SCS)}
U_MessEnd,
{$IFEND}
U_Common;
{$R *.dfm}

function ShowRegistration: Boolean;
var
  Answer: TAnswerscs;
  ConnNumb: string;
  s: string;
  ListSt: TList;
  ThPartTxt: string;
  TempB: PChar;
  KodInt1: integer;
  KodInt: integer;
  ThPart: integer;
  TempText: string;
  temp3: string;
  tempall: string;
  tempdata3: string;
  RegisteredOwnerStr: string;
begin
  result := False;
  FSCS_Main.aCopy.ShortCut := 0;
  FSCS_Main.aPaste.ShortCut := 0;
  RegisteredOwnerStr := ''; // Tolik 30/09/2020 --
  with TF_Registration.Create(Application) do
  begin
    try
      s := ProgID.Data1 + '-' + ProgID.Data2 + '-' +
                        ProgID.Data3 + '-' + ProgID.Data4 + '-' + DateID;
      lblProgID.Text := FormatForUser(s);
      edtAnswer1.Text := '';
      edtAnswer2.Text := '';
      edtAnswer3.Text := '';
      edtAnswer4.Text := '';
      Edit1.Text := '';
{$IF Defined(PROCAT_SCS)}
      Edit1.Text := GetReqCode;
{$IFEND}
      Edit2.Text := '';
      if ProgProtection.CheckIsVerls(PRO) or ProgProtection.CheckIsVerls(TRIAL) then
      begin
        try
          tempall := FormatForUser(ProgProtection.AnswerReg.Data1) +
             '-' + FormatForUser(ProgProtection.AnswerReg.Data2);
          temp3 := inttostr(strtoint('$' + FormatForProg(ProgProtection.AnswerReg.Data3)));
          while length(temp3) < 5 do
            insert('0', temp3, 1);
          edtAnswer1.Text := tempall + '-' + temp3 +
             '-' + FormatForUser(ProgProtection.AnswerReg.Data4);
          //Tolik 30/09/2020 --
          RegisteredOwnerStr := TF_Main(F_NormBase).DM.GetStringFromTableFirst('SETTINGS', 'USERINFO');
          if RegisteredOwnerStr <> '' then
          begin
            Label3.Caption := RegMsg01 + RegisteredOwnerStr;
          end;
          //
          //USERINFO
        except
        end;
//        edtAnswer2.Text := FormatForUser(ProgProtection.AnswerReg.Data2);
//        edtAnswer3.Text := FormatForUser(ProgProtection.AnswerReg.Data3);
//        edtAnswer4.Text := FormatForUser(ProgProtection.AnswerReg.Data4);
      end;
      if ShowModal = mrOk then
      begin
        result := True;
        try
          temptext := edtAnswer1.Text;
          Answer.Data1 := FormatForProg(Copy(TempText, 1, 4));
          Answer.Data2 := FormatForProg(Copy(TempText, 6, 4));
          temptext := copy(temptext, pos('-', temptext) + 1, $FF);
          temptext := copy(temptext, pos('-', temptext) + 1, $FF);
          Answer.Data3 := FormatForProg(Copy(TempText, 1, pos('-', temptext)));
          tempdata3 := inttohex(strtoint(Answer.Data3), 4);
          tempdata3 := FormatForProg(tempdata3);
          Answer.Data3 := tempdata3;
          Answer.Data4 := FormatForProg(Copy(TempText, pos('-', temptext)+ 1, $FF));
        except
        end;

//        Answer.Data2 := FormatForProg(edtAnswer2.Text);
//        Answer.Data3 := FormatForProg(edtAnswer3.Text);
//        Answer.Data4 := FormatForProg(edtAnswer4.Text);

        if ProgProtection.RegisterProgscs(Answer) then
        begin
          {$IF Defined(PROCAT_SCS)}
            if Edit2.Text <> '' then
            begin
              GReadOnlyMode := True;
              CheckCode(Edit1.Text, Edit2.Text);
            end;

          {$IFEND}
          ProgramRegisterPro := ProgProtection.CheckIsVerls(PRO);
          ProgramRegisterTrial := ProgProtection.CheckIsVerls(TRIAL);
          {$IF Defined(PROCAT_SCS)}
          if ProgramRegisterTrial then
          begin
            GReadOnlyMode := True;
            ProgramRegisterPro := True;
          end;
          {$IFEND}
          if ProgramRegisterPro or ProgramRegisterTrial then
          begin
            // Tolik 01/10/2020 --
            RegisteredOwnerStr := InputBox(LicenceOwnerInfoMsg, OwnerMsg, RegisteredOwnerStr);
            TF_Main(F_NormBase).DM.UpdateStrTableFieldAllRec('SETTINGS', 'USERINFO', RegisteredOwnerStr);
            //
          end;
          Application.ProcessMessages;
        end
        else
      end;
    finally
      Free;
    end;
  end;
  FSCS_Main.aCopy.ShortCut := 16451;
  FSCS_Main.aPaste.ShortCut := 16470;
end;

procedure TF_Registration.edtAnswer_1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_RETURN) then
  begin
    btnOK.Click;
  end;
//  if (Key = VK_RETURN) then
//  begin
//    ActiveControl := edtAnswer2;
//  end;
  if Key = VK_ESCAPE then
    btnCancel.Click;
end;

procedure TF_Registration.edtAnswer_1KeyPress(Sender: TObject;
  var Key: Char);
var
  tempstr: string;
  i: integer;
begin
  tempstr := '';
  tempstr := Key;
  tempstr := UpperCase(tempstr);
  Key := tempstr[1];
  case Key of
    //'F': Key := 'À';
    ',': Key := 'Á';
    '<': Key := 'Á';
    //'D': Key := 'Â';
    //'U': Key := 'Ã';
    //'L': Key := 'Ä';
    //'T': Key := 'Å';
  end;
end;

procedure TF_Registration.edtAnswer2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_RETURN) then
  begin
    ActiveControl := edtAnswer3;
  end;
  if Key = VK_ESCAPE then
    btnCancel.Click;
end;
//Tolik 02/08/2021 --
procedure TF_Registration.BitBtn1Click(Sender: TObject);
begin
  {
  lblProgID.CopyToClipboard;
  Edit1.CopyToClipboard;
  }
  Clipboard.Open;
  Clipboard.AsText :=  lblProgID.Text + #13#10 + Edit1.Text;
  Clipboard.Close;
end;

procedure TF_Registration.BitBtn2Click(Sender: TObject);
var s: TStringList;
begin
  Clipboard.Open;
  if Clipboard.HasFormat(CF_TEXT) then
  begin
    s := TStringList.Create;
    s.Text := Clipboard.AsText;
    if s.Count > 0 then
    begin
      case s.Count of
        1: edtAnswer1.Text := trim(s[0]);
        else
        begin
          edtAnswer1.Text := trim(s[0]);
          Edit2.Text := trim(s[1]);
        end;
      end;
    end;
    s.Free;
  end;
  ClipBoard.Close;
end;
//

procedure TF_Registration.btnOkKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_ESCAPE then
    btnCancel.Click;
end;

procedure TF_Registration.edtAnswer2Change(Sender: TObject);
var
  tempok: boolean;
begin
//  btnOk.Enabled := (length(edtAnswer1.Text) = 4) and (length(edtAnswer2.Text) = 4) and
//                   (length(edtAnswer3.Text) > 2) and (length(edtAnswer4.Text) = 4);
  tempok := false;
  if pos(' ', edtAnswer1.Text) = 0 then
    tempok := true;
  btnOk.Enabled := (length(edtAnswer1.Text) > 19) And TempOk;

  if ActiveControl = edtAnswer1 then
  begin
    if (length(edtAnswer1.Text) = 20) and tempok then
    begin
      ActiveControl := btnOk;
    end;
  end;
(*
  if ActiveControl = edtAnswer4 then
  begin
    if (length(edtAnswer4.Text) >= 4) then
    begin
      ActiveControl := btnOk;
    end;
  end;
  if ActiveControl = edtAnswer3 then
  begin
    if (length(edtAnswer3.Text) >= 4) then
    begin
      ActiveControl := edtAnswer4;
    end;
  end;
  if ActiveControl = edtAnswer2 then
  begin
    if (length(edtAnswer2.Text) >= 4) then
    begin
      ActiveControl := edtAnswer3;
    end;
  end;
  if ActiveControl = edtAnswer1 then
  begin
    if (length(edtAnswer1.Text) >= 4) then
    begin
      ActiveControl := edtAnswer2;
    end;
  end;
*)
end;

procedure TF_Registration.lblEmailClick(Sender: TObject);
var
  s: string;
begin
 {$IF Defined(FINAL_PRO_RU) or Defined(FINAL_CD_RU) or Defined(DEMO_RU)}
  s := 'office@expertsoft.ru?subject=request_answer_code=' +
            FormatForUser(ProgID.Data1) + '-' +
            FormatForUser(ProgID.Data2) + '-' +
            FormatForUser(ProgID.Data3) + '-' +
            FormatForUser(ProgID.Data4) + '-' +
            FormatForUser(DateID);
 {$ELSE}
  s := 'office@expertsoft.com.ua?subject=request_answer_code=' +
            FormatForUser(ProgID.Data1) + '-' +
            FormatForUser(ProgID.Data2) + '-' +
            FormatForUser(ProgID.Data3) + '-' +
            FormatForUser(ProgID.Data4) + '-' +
            FormatForUser(DateID);
 {$IFEND}
end;

procedure TF_Registration.Label4Click(Sender: TObject);
begin
  ModalResult := mrCancel;
  Application.ProcessMessages;
end;

procedure TF_Registration.Label5Click(Sender: TObject);
begin
  ModalResult := mrCancel;
  Application.ProcessMessages;
end;

procedure TF_Registration.FormCreate(Sender: TObject);
begin
{$IF ((Not Defined(PROCAT_SCS)) or Defined(TRIAL_SCS))}
      Label7.Visible := False;
      Edit1.Visible := False;
      Label6.Visible := False;
      Edit2.Visible := False;
{$IFEND}

  {$IF Defined(SCS_PE) or Defined(SCS_PANDUIT)}
     label8.Visible := false;
  {$ELSE}
    //Label7.Visible := False;
    //Edit1.Visible := False;
  {$IFEND}
  {$if Defined(DOCwIMAGES)}
    label4.Visible := False;
//    Label5.Top := 39;
  {$ifend}
end;

procedure TF_Registration.edtAnswer3KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_RETURN) then
  begin
    ActiveControl := edtAnswer4;
  end;
  if Key = VK_ESCAPE then
    btnCancel.Click;
end;

procedure TF_Registration.edtAnswer4KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_RETURN) then
    btnOK.Click;
  if Key = VK_ESCAPE then
    btnCancel.Click;
end;

end.
