unit uParentalLockPasswordLogout;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  ExtCtrls, StdCtrls, IniFiles, Mask, Buttons, GR32_Image;

type
  TFormParentalLockPasswordLogout = class(TForm)
    ButtonOk: TButton;
    ButtonCancel: TButton;
    LabelEnterPassword: TLabel;
    Password: TMaskEdit;
    LabelConfirmPassword: TLabel;
    ConfirmPassword: TMaskEdit;
    LabelTopMsg: TLabel;
    TopImage: TImage32;
    procedure ButtonCancelClick(Sender: TObject);
    procedure ButtonOkClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure PasswordKeyPress(Sender: TObject; var Key: Char);
    procedure ConfirmPasswordKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    CurrentPassword: String;
    { Public declarations }
  end;

var
  FormParentalLockPasswordLogout: TFormParentalLockPasswordLogout;

implementation

uses uMain, uCommon;

{$R *.dfm}

procedure TFormParentalLockPasswordLogout.ButtonCancelClick(
  Sender: TObject);
begin
  FormMain.MenuParentalLock.Tag:= 1;
  Close;
end;

procedure TFormParentalLockPasswordLogout.ButtonOkClick(Sender: TObject);
begin
  if ConfirmPassword.Visible then
     begin
       if ConfirmPassword.Text = '' then
          begin
            ConfirmPassword.SelectAll;
            ConfirmPassword.SetFocus;
            Exit;
          end;

       if Password.Text <> ConfirmPassword.Text then
          begin
            GenerateMessage('Password Confirmation', 'The password is incorrect! Please, try again.', 2);
            Password.SelectAll;
            Password.SetFocus;
            Exit;
          end;
     end;

  case FormMain.ValidateParentalLockPassword(Password.Text, Password.Tag) of
    True:
      begin
        case Password.Tag of
          0: //this is for login (no changes). Will deactivate if password is OK
            begin
              FormMain.MenuParentalLock.Tag:= 0;
              Close;
            end;
          1: //this is for changing the password
            begin
              if Password.Text <> '' then
                 begin
                    FormMain.ParentalLockPasswordString:= Password.Text;
                    FormMain.MenuParentalLock.Tag:= 0;
                    Close;
                 end
              else
                 GenerateMessage('No Password', 'The password is empty! Please, enter a valid password.', 2);
            end;
        end;
      end;
    False:
      begin
        case Password.Tag of
          0:
            begin
              GenerateMessage('Error', 'The password is incorrect! Please, try again.', 2);
              Password.SetFocus;
              Password.SelectAll;
            end;
        end;
      end;
  end;
end;

procedure TFormParentalLockPasswordLogout.FormShow(Sender: TObject);
begin
  FormMain.LoadTopBar(TopImage, 'password.png');
  ConfirmPassword.Clear;
  Password.SetFocus;
  case Password.Tag of
    0: begin
         LabelEnterPassword.Caption:= 'Enter Password';
         Password.Clear;
         ConfirmPassword.Clear;
         LabelConfirmPassword.Visible:= False;
         ConfirmPassword.Visible:= False;
       end;
    1: begin
         LabelTopMsg.Caption:= 'Create/Edit Password';
         LabelEnterPassword.Caption:= 'Enter New Password';
         Password.Text:= FormMain.ParentalLockPasswordString;
         Password.SelectAll;
       end;
  end;
end;

procedure TFormParentalLockPasswordLogout.PasswordKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key in [';', '.', ',', '<', '>', '[', ']', '{', '}', '(', ')', '=', '+', '*',
             '@', '$', '^', '~', '#', '%', '¨', ':', '/', '|', '\', '?', '!', '&',
             '´', '`', '''', '"'] then
     Key:= Char(0)
  else
  if Key = #13 then
     begin
       case ConfirmPassword.Visible of
         True:
           begin
             ConfirmPassword.Clear;
             ConfirmPassword.SetFocus;
           end;
         False: ButtonOk.Click;
       end;
     end
  else
  if Key = #27 then
     ButtonCancel.Click;
end;

procedure TFormParentalLockPasswordLogout.ConfirmPasswordKeyPress(
  Sender: TObject; var Key: Char);
begin
  if Key in [';', '.', ',', '<', '>', '[', ']', '{', '}', '(', ')', '=', '+', '*',
             '@', '$', '^', '~', '#', '%', '¨', ':', '/', '|', '\', '?', '!', '&',
             '´', '`', '''', '"'] then
     Key:= Char(0)
  else
  if Key = #13 then
     ButtonOk.Click
  else
  if Key = #27 then
     ButtonCancel.Click;
end;

end.
