unit U_upPassword;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Menus, cxLookAndFeelPainters, cxButtons, DB, ADODB,
  Buttons, ExtCtrls, cxGraphics, cxLookAndFeels, dxSkinsCore,
  dxSkinsDefaultPainters;

type
  TfrmupPassword = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    userID: TEdit;
    oldPassword: TEdit;
    newPassword: TEdit;
    DnewPassword: TEdit;
    cxOK: TcxButton;
    cxClose: TcxButton;
    ADO_Cmd: TADOQuery;
    Panel3: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton12: TSpeedButton;
    procedure cxCloseClick(Sender: TObject);
    procedure cxOKClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure userIDKeyPress(Sender: TObject; var Key: Char);
    procedure oldPasswordClick(Sender: TObject);
    procedure newPasswordClick(Sender: TObject);
    procedure DnewPasswordClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
  private
    Fint:Integer;
    { Private declarations }
  public
    fuserID:string;
    fuserName:string;
    fPassWord:string;
    { Public declarations }
  end;

var
  frmupPassword: TfrmupPassword;

implementation
uses spmain,ElAES,AES;
{$R *.dfm}
procedure TfrmupPassword.cxCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmupPassword.cxOKClick(Sender: TObject);
begin
  if trim(oldPassword.text)<>'' then
    fPassWord:=trim(EncryptString(PAnsiChar( trim(oldPassword.text)),keys))
  else
    fPassWord:='';
  WITH ADO_Cmd Do
  BEGIN
    close;
    sql.Clear;
    sql.Add(' SELECT * ');
    sql.Add(' FROM SY_User A ');
    Sql.Add(' where UserID='+quotedstr(trim(fuserID)));
    Sql.Add(' and PassWord='+quotedstr(trim(fPassWord)));
    Sql.Add(' and Valid=''Y'' ');
    open;
  END;
  IF ADO_Cmd.IsEmpty THEN
  BEGIN
    Application.MessageBox('原密码错误！', '错误提示', MB_ICONERROR);
		oldPassword.Text := '';
    exit;
  END;
  if trim(newPassword.Text)<> trim(dnewPassword.Text) then
  begin
    application.MessageBox('新密码两次输入不一致！','错误信息',MB_ICONERROR);
    newPassword.Text:='';
    dnewPassword.Text:='';
    exit;
  end;

  if trim(newPassword.text)<>'' then
    fPassWord:=trim(EncryptString(PAnsiChar( trim(newPassword.text)),keys))
  else
    fPassWord:='';
  try
    WITH ADO_Cmd Do
    BEGIN
      close;
      sql.Clear;
      sql.Add(' SELECT * ');
      sql.Add(' FROM SY_User A ');
      Sql.Add(' where UserID='+quotedstr(trim(fuserID)));
      Sql.Add(' and Valid=''Y'' ');
      open;
    END;
    ADO_Cmd.Edit;
    ADO_Cmd.FieldByName('password').Value:=trim(fPassWord);
    ADO_Cmd.Post;
    ModalResult:=1;
    application.MessageBox('修改密码成功！','提示信息');
  except
    application.MessageBox('修改密码失败！','错误信息',MB_ICONERROR);
    exit;
  end;



end;

procedure TfrmupPassword.FormShow(Sender: TObject);
begin
  userID.Text:=trim(fuserID);
  oldPassword.SetFocus;
  Fint:=1;
end;

procedure TfrmupPassword.userIDKeyPress(Sender: TObject; var Key: Char);
begin
 Key:=UpCase(Key);
  if key=#13 then
  begin
    perform(WM_NEXTDLGCTL,0,0);
    Key := #0;
  end;
end;

procedure TfrmupPassword.oldPasswordClick(Sender: TObject);
begin
  Fint:=1;
end;

procedure TfrmupPassword.newPasswordClick(Sender: TObject);
begin
  Fint:=2;
end;

procedure TfrmupPassword.DnewPasswordClick(Sender: TObject);
begin
   Fint:=3;
end;

procedure TfrmupPassword.SpeedButton1Click(Sender: TObject);
begin
  if Fint=1 then
  begin
    oldPassword.Text:=Trim(oldPassword.Text)+Trim(TSpeedButton(Sender).Caption);
    oldPassword.SelectAll;
  end else
  if Fint=2 then
  begin
    newPassword.Text:=Trim(newPassword.Text)+Trim(TSpeedButton(Sender).Caption);
    newPassword.SelectAll;
  end else
  if Fint=3 then
  begin
    DnewPassword.Text:=Trim(DnewPassword.Text)+Trim(TSpeedButton(Sender).Caption);
    DnewPassword.SelectAll;
  end;
end;

procedure TfrmupPassword.SpeedButton12Click(Sender: TObject);
begin
  if Fint=1 then
  begin
    if Trim(oldPassword.Text)<>'' then
    begin
      oldPassword.Text:=Copy(Trim(oldPassword.Text),1,Length(Trim(oldPassword.Text))-1);
      oldPassword.SelectAll;
    end;
  end else
  if Fint=2 then
  begin
    if Trim(newPassword.Text)<>'' then
    begin
      newPassword.Text:=Copy(Trim(newPassword.Text),1,Length(Trim(newPassword.Text))-1);
      newPassword.SelectAll;
    end;
  end else
  if Fint=3 then  
  begin
    if Trim(DnewPassword.Text)<>'' then
    begin
      DnewPassword.Text:=Copy(Trim(DnewPassword.Text),1,Length(Trim(DnewPassword.Text))-1);
      DnewPassword.SelectAll;
    end;
  end;
end;

end.
