unit uFormChangePW;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Buttons, MServClients;

type
  TFormChangePW = class(TForm)
    EditOldPW: TLabeledEdit;
    EditNewPW: TLabeledEdit;
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    EditNewPW2: TLabeledEdit;
    procedure BtnOKClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function Execute():Boolean;
  end;

var
  FormChangePW: TFormChangePW;

implementation

{$R *.dfm}

function TFormChangePW.Execute():Boolean;
begin
  try
    Result:=False;
    EditOldPW.Text:='';
    EditNewPW.Text:='';
    EditNewPW2.Text:='';

    if ShowModal()=mrOK then begin

      Result:=True;
    end;

  except
    on E:Exception do begin
      E.Message:='TFormChangePW.Execute: '+E.Message;
      raise;
    end;
  end;
end;

procedure TFormChangePW.BtnOKClick(Sender: TObject);
begin
  try
    ModalResult:=mrNone;

    if EditNewPW.Text='' then begin
      Application.MessageBox('You have to define a password! The new password cannot be empty!', 'New password cannot be empty!', mb_IconExclamation or mb_OK);
      Exit;
    end;

    if EditNewPW.Text<>EditNewPW2.Text then begin
      Application.MessageBox('The repetition of your new password is not the same as your first entry. Seems you have made a typo?!', 'New passwords do not match!', mb_IconExclamation or mb_OK);
      Exit;
    end;

    if EditOldPW.Text<>MServClient.User.Pwd then begin
      Application.MessageBox('Your current password is not correct! You can only change your password if you type in the right current password.', 'Current password not correct!', mb_IconExclamation or mb_OK);
      Exit;
    end;

    MServClient.ChangePassword(EditOldPW.Text, EditNewPW.Text);

    ModalResult:=mrOK;
  except
    on E:Exception do begin
      E.Message:='TFormChangePW.BtnOKClick: '+E.Message;
      raise;
    end;
  end;
end;

procedure TFormChangePW.FormShow(Sender: TObject);
begin
  try
    EditOldPW.SetFocus();
  except
    on E:Exception do begin
      E.Message:='TFormChangePW.FormShow: '+E.Message;
      raise;
    end;
  end;
end;

end.
