unit AccountInformation;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, Buttons, StdCtrls, jpeg, ExtCtrls, Menus;

type
  TfrmUserInformation = class(TForm)
    gbUserInfo: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    txtName: TEdit;
    txtPosition: TEdit;
    txtAddress: TEdit;
    txtCnum: TEdit;
    btnNew: TBitBtn;
    gbSearch: TGroupBox;
    gbUserAcc: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    txtUName: TEdit;
    txtPass: TEdit;
    txtRtype: TEdit;
    cmbUAccess: TComboBox;
    rightClick: TPopupMenu;
    popChangePass: TMenuItem;
    popDeactivate: TMenuItem;
    btnSave: TBitBtn;
    btnCancel: TBitBtn;
    popEditUserInfo: TMenuItem;
    txtSearch: TEdit;
    lvAcc: TListView;
    btnEdit: TBitBtn;
    Label9: TLabel;
    txtIDNum: TEdit;
    cmbSearch: TComboBox;
    btnGo: TBitBtn;
    btnRefresh: TBitBtn;
    Label10: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Panel3: TPanel;
    Panel4: TPanel;
    procedure btnSaveClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure Save();
    procedure Edited();
    procedure Deleted();
    procedure DoubleClick();
    procedure Search();
    procedure VerifyAccount();
    procedure CheckIDNumber();
    //procedure AutoPrime();
    procedure Clear();
    //procedure Browse();
    //procedure LoadImage();
    procedure Deactivate();
    procedure ReActivate();
    procedure FormActivate(Sender: TObject);
    procedure btnNewClick(Sender: TObject);
    procedure popDeleteClick(Sender: TObject);
    procedure lvAccDblClick(Sender: TObject);
    procedure popEditUserInfoClick(Sender: TObject);
    procedure popChangePassClick(Sender: TObject);
    procedure txtSearchChange(Sender: TObject);
    procedure txtNameKeyPress(Sender: TObject; var Key: Char);
    procedure txtPositionKeyPress(Sender: TObject; var Key: Char);
    procedure txtAddressKeyPress(Sender: TObject; var Key: Char);
    procedure txtCnumKeyPress(Sender: TObject; var Key: Char);
    procedure txtUNameKeyPress(Sender: TObject; var Key: Char);
    procedure txtPassKeyPress(Sender: TObject; var Key: Char);
    procedure txtRtypeKeyPress(Sender: TObject; var Key: Char);
    procedure cmbUAccessKeyPress(Sender: TObject; var Key: Char);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure popDeactivateClick(Sender: TObject);
    procedure txtIDNumKeyPress(Sender: TObject; var Key: Char);
    procedure btnClearClick(Sender: TObject);
    procedure btnRefreshClick(Sender: TObject);
    procedure btnGoClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure txtSearchKeyPress(Sender: TObject; var Key: Char);
    procedure Timer1Timer(Sender: TObject);
    procedure lvAccCustomDrawSubItem(Sender: TCustomListView;
      Item: TListItem; SubItem: Integer; State: TCustomDrawState;
      var DefaultDraw: Boolean);
    procedure CountColumns();
  private
    { Private declarations }
  public
    { Public declarations }
      acStat:String;
      acStat2:String;
      flename:string;
      status:string;
      primary:string;
      sample:integer;
      uname:string;
      IDNum:string;
      pk_id:integer;
      userstatus:String;
      count:integer;
    procedure loadAccountInfo();
  end;

  type
   TWordTriple = Array[0..2] of Word;
    function FileEncrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
    function FileDecrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
    function TextEncrypt(const s: string; Key: TWordTriple): string;
    function TextDecrypt(const s: string; Key: TWordTriple): string;
    function MemoryEncrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key:TWordTriple): boolean;
    function MemoryDecrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key:TWordTriple): boolean;

var
  frmUserInformation: TfrmUserInformation;
  //acStat:='Active';

implementation

uses ChangePass, DM, Main;

{$R *.dfm}
function MemoryEncrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean;
var
  pIn, pOut: ^byte;
  i : Cardinal;
begin
    if SrcSize = TargetSize then
    begin
        pIn := Src;
        pOut := Target;
        for i := 1 to SrcSize do
        begin
          pOut^ := pIn^ xor (Key[2] shr 8);
          Key[2] := Byte(pIn^ + Key[2]) * Key[0] + Key[1];
          inc(pIn);
          inc(pOut);
        end;
              Result := True;
    end else
              Result := False;
end;

function MemoryDecrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean;
var
  pIn, pOut: ^byte;
  i : Cardinal;
begin
  if SrcSize = TargetSize then
  begin
    pIn := Src;
    pOut := Target;
    for i := 1 to SrcSize do
    begin
      pOut^ := pIn^ xor (Key[2] shr 8);
      Key[2] := byte(pOut^ + Key[2]) * Key[0] + Key[1];
      inc(pIn);
      inc(pOut);
    end;
    Result := True;
  end else
    Result := False;
end;

function TextCrypt(const s: string; Key: TWordTriple; Encrypt: Boolean): string;
var
  bOK: Boolean;
begin
  SetLength(Result, Length(s));
  if Encrypt then
    bOK := MemoryEncrypt(PChar(s), Length(s), PChar(Result), Length(Result), Key)
  else
    bOK := MemoryDecrypt(PChar(s), Length(s), PChar(Result), Length(Result), Key);
  if not bOK then Result := '';
end;

function FileCrypt(InFile, OutFile: String; Key: TWordTriple; Encrypt: Boolean): boolean;
var
  MIn, MOut: TMemoryStream;
begin
  MIn := TMemoryStream.Create;
  MOut := TMemoryStream.Create;
  Try
    MIn.LoadFromFile(InFile);
    MOut.SetSize(MIn.Size);
    if Encrypt then
      Result := MemoryEncrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key)
    else
      Result := MemoryDecrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key);
    MOut.SaveToFile(OutFile);
  finally
    MOut.Free;
    MIn.Free;
  end;
end;
function TextEncrypt(const s: string; Key: TWordTriple): string;
begin
  Result := TextCrypt(s, Key, True);
end; 

function TextDecrypt(const s: string; Key: TWordTriple): string;
begin
  Result := TextCrypt(s, Key, False);
end;

function FileEncrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
begin
  Result := FileCrypt(InFile, OutFile, Key, True);
end;

function FileDecrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
begin
  Result := FileCrypt(InFile, OutFile, Key, False);
end;


procedure TfrmUserInformation.FormActivate(Sender: TObject);
begin
  Label10.Visible := False;
  if(frmMain.StatusBar1.Panels.Items[0].Text = 'Administrator') then begin
     btnNew.Enabled := True;
     btnEdit.Enabled := True;
     end
     else begin
     btnNew.Enabled := False;
     btnEdit.Enabled := False;
     popEditUserInfo.Enabled := False;
  end;

end;

procedure TfrmUserInformation.CountColumns();
begin
      if DModule.TR.InTransaction then
        DModule.TR.Commit;

          DModule.QR.SQL.Clear;
          DModule.QR.SQL.Add('SELECT COUNT(UI_ID)');
          DModule.QR.SQL.Add('FROM UsersInfo');
          DModule.QR.Prepare;
          DModule.QR.Open();
            count:=DModule.QR.Fields.AsInteger[0];
          //StatusBar1.Panels.Items[1].Text:='Columns: '+IntToStr(count);
      if DModule.TR.InTransaction then
        DModule.TR.Commit;
end;

procedure TfrmUserInformation.btnSaveClick(Sender: TObject);
begin
      if(btnSave.Caption='&Save')then begin
          Save();
      end
      else begin
          Edited();
     end
end;
procedure TfrmUserInformation.btnCancelClick(Sender: TObject);
begin
      Clear();
      lvAcc.Enabled:=True;
end;

procedure TfrmUserInformation.btnNewClick(Sender: TObject);
begin
      gbUserInfo.Enabled:=true;
      gbUserAcc.Enabled:=true;
      lvAcc.Enabled:=true;
      Clear();
      txtIDNum.SetFocus;
      btnSave.Caption:='&Save';
      btnSave.Enabled:=true;
      btnCancel.Enabled:=true;
end;
procedure TfrmUserInformation.txtSearchChange(Sender: TObject);
begin
        //Search();
end;
procedure TfrmUserInformation.lvAccDblClick(Sender: TObject);
begin
      DoubleClick();
end;
procedure TfrmUserInformation.btnDeleteClick(Sender: TObject);
begin
      Deleted();
end;

procedure TfrmUserInformation.btnEditClick(Sender: TObject);
begin
      DoubleClick();
end;
procedure TfrmUserInformation.popDeleteClick(Sender: TObject);
begin
      Deleted();
end;
procedure TfrmUserInformation.popEditUserInfoClick(Sender: TObject);
begin
      DoubleClick();
end;
procedure TfrmUserInformation.btnClearClick(Sender: TObject);
begin
      Clear();
end;

procedure TfrmUserInformation.btnRefreshClick(Sender: TObject);
begin
      loadAccountInfo();
end;

procedure TfrmUserInformation.btnGoClick(Sender: TObject);
begin
      Search();
end;

procedure TfrmUserInformation.popChangePassClick(Sender: TObject);
begin
      Label10.Caption := lvAcc.ItemFocused.SubItems[0];
      frmChangePass.lbName.Caption:= lvAcc.ItemFocused.SubItems[0];
        if(Label10.Caption <> frmMain.StatusBar1.Panels.Items[1].Text) then begin
           MessageDlg('Access Denied. You dont have permission to change his/her password',mtError,[mbOK],0);
        end
        else begin
          frmChangePass.showModal;
        end;
end;


//..........................dirty works here
procedure TfrmUserInformation.Clear();
begin
        txtSearch.Text:='';
        txtIDNum.Clear;
        txtName.Clear;
        txtPosition.Clear;
        txtAddress.Clear;
        txtCNum.Clear;
        txtUName.Clear;
        txtPass.Clear;
        txtRtype.Clear;
        cmbUAccess.ItemIndex := -1;
end;
procedure TfrmUserInformation.loadAccountInfo();
    var newState : TListItem;
begin


     if DModule.TR.InTransaction then
        DModule.TR.Commit;

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear;
    DModule.QR.SQL.Add('SELECT UI_ID,FULLNAME, DESIGNATION, ADDRESS, CONTACTNUM, USERNAME,DATECREATED, USERACCESS, MODIFIEDON, STATUS');
    DModule.QR.SQL.Add('FROM UsersInfo');
    DModule.QR.Prepare;
    DModule.QR.Open();

    lvAcc.Items.BeginUpdate;
    lvAcc.Items.Clear;

    if DModule.QR.Fields.IsNull[0] then Exit;
    while not DModule.QR.Eof do begin
        newState := lvAcc.Items.Add;
        newState.Caption := DModule.QR.Fields.AsString[0];
        newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[3]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[6]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[7]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[8]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[9]);
    DModule.QR.Next;
    end;
    if DModule.TR.InTransaction then
        DModule.TR.Commit;
        lvAcc.Items.EndUpdate;
end;

procedure TfrmUserInformation.Save();
var
    endec:TWordTriple;
begin
    endec[0]:=1000;
    endec[1]:=9292;
    endec[2]:=3333;

acStat:='1';
CheckIDNumber();
VerifyAccount();

            if(txtIDNum.Text = IDNum)then begin
             MessageDlg('ID number already exist',mtError,[mbOK],0);
              txtIDNum.SetFocus;
            end
            else if(txtIDNum.Text = '')then begin
             MessageDlg('Field(s) required',mtInformation,[mbOK],0);
              txtIDNum.SetFocus;
            end

            else if(txtName.Text = '')then begin
             MessageDlg('Field(s) required',mtInformation,[mbOK],0);
              txtName.SetFocus;
            end
            else if(txtPosition.Text='')then begin
             MessageDlg('Field(s) required',mtInformation,[mbOK],0);
               txtPosition.SetFocus;
            end
            else if(txtAddress.Text = '')then begin
             MessageDlg('Field(s) required',mtInformation,[mbOK],0);
              txtAddress.SetFocus;
            end
            else if(txtUName.Text = '' )then begin
             MessageDlg('Field(s) required',mtInformation,[mbOK],0);
              txtUName.SetFocus;
            end
            else if(uname = txtUName.Text)then begin
             MessageDlg('Username already exist',mtError,[mbOK],0);
              txtUName.SetFocus;
            end
            else if(txtPass.Text='')then begin
             MessageDlg('Field(s) required',mtInformation,[mbOK],0);
              txtPass.SetFocus;
            end
            else if(txtRtype.Text='')then begin
             MessageDlg('Field(s) required',mtInformation,[mbOK],0);
              txtRtype.SetFocus;
            end
            else if(txtPass.Text <> txtRtype.Text) then begin
               MessageDlg('Password did not match',mtError,[mbOK],0);
               txtRtype.SetFocus;
            end
            else if(cmbUAccess.Text='')then begin
              cmbUAccess.SetFocus;
            end
     else
     if MessageDlg('Do you want to save?',mtConfirmation,[mbYes, mbNo], 0)= mrYes then begin

        DModule.QR.SQL.Clear();
        DModule.QR.SQL.Add('INSERT INTO UsersInfo');
        DModule.QR.SQL.Add('(UI_ID,Fullname,Designation,Address,ContactNum,Username,uPassword,DateCreated,UserAccess,Status)');
        DModule.QR.SQL.Add(' VALUES (:a, :b, :c, :d, :e, :f, :g, :h, :i, :j) ');

        DModule.QR.Params.AsString[0]:= trim(txtIDNum.Text);
        DModule.QR.Params.AsString[1] := trim(UpperCase(txtName.Text));
        DModule.QR.Params.AsString[2] := trim(UpperCase(txtPosition.Text));
        DModule.QR.Params.AsString[3] := trim(UpperCase(txtAddress.Text));
        DModule.QR.Params.AsString[4] := trim(txtCNum.Text);
        DModule.QR.Params.asString[5] := trim(txtUName.Text);
        DModule.QR.Params.asString[6] := trim(TextEncrypt(txtPass.Text, endec));
        DModule.QR.Params.AsString[7] := DateToStr(now());
        DModule.QR.Params.asString[8] := cmbUAccess.Text;
        DModule.QR.Params.asString[9] := acStat;

            MessageDlg('Information has been save',mtConfirmation,[mbOK],0);
        DModule.QR.Prepare;
        DModule.QR.open();

        if DModule.TR.InTransaction then
          DModule.TR.Commit;
        loadAccountInfo;
        lvAcc.Enabled:=true;
        Clear;

     end
     else begin
      MessageDlg('Nothing saved',mtInformation,[mbOK],0);
      Clear();
     end
end;

procedure TfrmUserInformation.Edited();
begin
acStat:='1';
     if (txtIDNum.Text = '')or (txtName.Text = '')or(txtPosition.Text='')or (txtAddress.Text = '') or (txtUName.Text = '')or(cmbUAccess.Text='')   then
      begin
        MessageDlg('Field(s) required',mtInformation,[mbOK],0);

            if(txtIDNum.Text = '')then begin
              txtIDNum.SetFocus;
            end
            else if(txtName.Text = '')then begin
              txtName.SetFocus;
            end
            else if(txtPosition.Text='')then begin
              txtPosition.SetFocus;
            end
            else if(txtAddress.Text = '')then begin
              txtAddress.SetFocus;
            end
            else if(txtUName.Text = '')then begin
              txtUName.SetFocus;
            end
            else if(cmbUAccess.Text='')then begin
              cmbUAccess.SetFocus;
            end
     end
     else
     if MessageDlg('Do you want to edit?',mtConfirmation,[mbYes, mbNo], 0)= mrYes then begin

       primary:= lvAcc.ItemFocused.Caption;
       if DModule.TR.InTransaction then
            DModule.TR.Commit;
       DModule.QR.SQL.Clear;
       DModule.QR.SQL.Add('UPDATE usersInfo SET UI_ID=:A, Fullname=:B, Designation=:C, Address=:D, ContactNum=:E, UserName=:F, UserAccess=:G, ModifiedOn=:H, Status=:I WHERE UI_ID =:J');

       DModule.QR.Params.AsString[0] := trim(txtIDNum.Text);
       DModule.QR.Params.AsString[1] := trim(UpperCase(txtName.Text));
       DModule.QR.Params.AsString[2] := trim(UpperCase(txtPosition.Text));
       DModule.QR.Params.AsString[3] := trim(UpperCase(txtAddress.Text));
       DModule.QR.Params.AsString[4] := trim(txtCNum.Text);
       DModule.QR.Params.AsString[5] := trim(txtUName.Text);
       DModule.QR.Params.AsString[6] := cmbUAccess.Text;
       DModule.QR.Params.AsString[7] := DateToStr(now);
       DModule.QR.Params.AsString[8] := acStat;
       DModule.QR.Params.AsString[9] := primary;

     MessageDlg('Account has been updated',mtInformation,[mbOK],0);
        DModule.QR.Prepare;
        DModule.QR.Open();

       if DModule.TR.InTransaction then
            DModule.TR.Commit;
          loadAccountInfo;
          lvAcc.Enabled:=true;
          Clear();
    end
    else begin
      MessageDlg('Nothing updated',mtInformation,[mbOK],0);
      Clear();
    end
end;

procedure TfrmUserInformation.Deleted();
begin
   if lvAcc.ItemFocused  <> nil then begin
    if MessageDlg('Do you want to delete?',mtConfirmation,[mbYes,mbNo],0)=mrYes then begin

       if DModule.TR.InTransaction then
          DModule.TR.Commit;
       DModule.TR.StartTransaction;
       DModule.QR.SQL.Clear();
       DModule.QR.SQL.Add('DELETE FROM UsersInfo where UI_ID =:A ');

       DModule.QR.Params.AsInteger[0]:=StrToInt(lvAcc.ItemFocused.Caption);
       DModule.QR.Prepare;
       DModule.QR.Open();
       MessageDlg('User has been successfully deleted',mtConfirmation,[mbOK],0);
         if DModule.TR.InTransaction then
            DModule.TR.Commit;
       loadAccountInfo;
       Clear();
    end
    else begin
       MessageDlg('Not deleted',mtConfirmation,[mbOK],0);
    end
    end
end;

procedure TfrmUserInformation.DoubleClick();
begin
      if lvAcc.ItemFocused  <> nil then begin
        if MessageDlg('Do you want to edit?',mtConfirmation,[mbYes,mbNo],0) = mrYes then begin

            gbUserInfo.Enabled:=true;
            gbUserAcc.Enabled:=true;
            btnSave.Caption:='&Update';
            btnSave.Enabled:=true;
            btnCancel.Enabled:=true;
            btnNew.Enabled:=true;
            txtPass.Enabled:=false;
            txtRtype.Enabled:=false;

            txtIDNum.Text := lvAcc.ItemFocused.Caption;
            txtName.Text := lvAcc.ItemFocused.SubItems[0];
            txtPosition.Text := lvAcc.ItemFocused.SubItems[1];
            txtAddress.Text := lvAcc.ItemFocused.SubItems[2];
            txtCNum.Text := lvAcc.ItemFocused.SubItems[3];
            txtUName.Text := lvAcc.ItemFocused.SubItems[4];
            cmbUAccess.Text := lvAcc.ItemFocused.SubItems[6];
            lvAcc.Enabled:=false;
        end
        else begin
        end
      end
      else begin
        ShowMessage('Nothing selected');
      end
end;

procedure TfrmUserInformation.Search();
    var newState : TListItem;
begin
     if DModule.TR.InTransaction then
            DModule.TR.Commit;

if (cmbSearch.Text='ID') then begin
    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear;
    DModule.QR.SQL.Add('SELECT UI_ID, FULLNAME, DESIGNATION, ADDRESS, CONTACTNUM, USERNAME,DATECREATED, USERACCESS, ModifiedOn, STATUS');
    DModule.QR.SQL.Add('FROM USERSINFO WHERE UI_ID LIKE (:a)');
    DModule.QR.Params.AsString[0] := Trim('%'+UpperCase(txtSearch.Text)+'%');
    DModule.QR.Prepare;
    DModule.QR.Open();

    lvAcc.Items.BeginUpdate;
    lvAcc.Items.Clear;

    if DModule.QR.Fields.IsNull[0] then Exit;
      if DModule.QR.Eof then begin
      MessageDlg('Search not found',mtError,[mbOK],0);
   end;

    while not DModule.QR.Eof do begin
        newState := lvAcc.Items.Add;
        newState.Caption := DModule.QR.Fields.AsString[0];
        newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[3]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[6]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[7]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[8]);
        DModule.QR.Next;
    end;
         if DModule.TR.InTransaction then
            DModule.TR.Commit;
        lvAcc.Items.EndUpdate;
end
//----------------------------------------------
else if (cmbSearch.Text='Name') then begin
    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear;
    DModule.QR.SQL.Add('SELECT UI_ID, FULLNAME, DESIGNATION, ADDRESS, CONTACTNUM, USERNAME,DATECREATED, USERACCESS, ModifiedOn, STATUS');
    DModule.QR.SQL.Add('FROM USERSINFO WHERE FULLNAME LIKE (:a)');
    DModule.QR.Params.AsString[0] := Trim('%'+UpperCase(txtSearch.Text)+'%');
    DModule.QR.Prepare;
    DModule.QR.Open();

    lvAcc.Items.BeginUpdate;
    lvAcc.Items.Clear;

    if DModule.QR.Fields.IsNull[0] then Exit;
    if DModule.QR.Eof then begin
      MessageDlg('Search not found',mtError,[mbOK],0);
   end;

    while not DModule.QR.Eof do begin
        newState := lvAcc.Items.Add;
        newState.Caption := DModule.QR.Fields.AsString[0];
        newState.SubItems.Add(DModule.QR.Fields.AsString[1]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[2]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[3]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[4]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[5]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[6]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[7]);
        newState.SubItems.Add(DModule.QR.Fields.AsString[8]);
        DModule.QR.Next;
    end;
         if DModule.TR.InTransaction then
            DModule.TR.Commit;
        lvAcc.Items.EndUpdate;
end
end;
procedure TfrmUserInformation.VerifyAccount();
begin
     if DModule.TR.InTransaction then
            DModule.TR.Commit;

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear;
    DModule.QR.SQL.Add('SELECT *');
    DModule.QR.SQL.Add('FROM USERSINFO WHERE Username =:a');

    DModule.QR.Params.AsString[0] := txtUName.Text;

    DModule.QR.Prepare;
    DModule.QR.Open();

    uname:=DModule.QR.Fields.AsString[6];

end;
procedure TfrmUserInformation.CheckIDNumber();
begin
     if DModule.TR.InTransaction then
            DModule.TR.Commit;

    DModule.TR.StartTransaction;
    DModule.QR.SQL.Clear;
    DModule.QR.SQL.Add('SELECT *');
    DModule.QR.SQL.Add('FROM USERSINFO WHERE UI_ID =:a');

    DModule.QR.Params.AsString[0] := txtIDNum.Text;

    DModule.QR.Prepare;
    DModule.QR.Open();

    IDNum:=DModule.QR.Fields.AsString[0];

end;

{procedure TfrmUserInformation.AutoPrime();
    var a:integer;
begin
a:=1;
 if DModule.TR.InTransaction then
        DModule.TR.Commit;
    DModule.QR.SQL.Clear();
    DModule.QR.SQL.Add('SELECT UI_ID');
    DModule.QR.SQL.Add('FROM USERS');
    DModule.QR.Prepare;
    DModule.QR.Open();
   if DModule.QR.Fields.Eof then
    sample:=a
      else begin
       DModule.QR.SQL.Clear;
        DModule.QR.SQL.Add('SELECT MAX(UI_ID) FROM USERS');
        DModule.QR.Prepare;
        DModule.QR.Open();
        sample := DModule.QR.Fields.AsInteger[0] + 1;
        pk_id:=sample;
        end;
         if DModule.TR.InTransaction then
       DModule.TR.Commit;

end;}

//Keypress...................................................
procedure TfrmUserInformation.txtIDNumKeyPress(Sender: TObject;
  var Key: Char);
begin
      if Key = #13 then begin
          SelectNext(Sender as tWinControl, True, True );
        if Sender = txtIDNum then
          btnSave.Click; // let the button click itself
         Key := #0;
        end;
end;
procedure TfrmUserInformation.txtNameKeyPress(Sender: TObject; var Key: Char);
begin
        if Key = #13 then begin
          SelectNext(Sender as tWinControl, True, True );
        if Sender = txtName then
          btnSave.Click; // let the button click itself
         Key := #0;
        end;
end;
procedure TfrmUserInformation.txtPositionKeyPress(Sender: TObject; var Key: Char);
begin
        if Key = #13 then begin
          SelectNext(Sender as tWinControl, True, True );
        if Sender = txtPosition then
          btnSave.Click; // let the button click itself
          Key := #0;
        end;
end;
procedure TfrmUserInformation.txtAddressKeyPress(Sender: TObject;
  var Key: Char);
begin
        if Key = #13 then begin
          SelectNext(Sender as tWinControl, True, True );
        if Sender = txtAddress then
         btnSave.Click; // let the button click itself
          Key := #0;
        end;
end;
procedure TfrmUserInformation.txtCnumKeyPress(Sender: TObject;
  var Key: Char);
begin
        if Key = #13 then begin
          SelectNext(Sender as tWinControl, True, True );
        if Sender = txtCNum then
          btnSave.Click; // let the button click itself
          Key := #0;
        end;
end;
procedure TfrmUserInformation.txtUNameKeyPress(Sender: TObject;
  var Key: Char);
begin
        if Key = #13 then begin
          SelectNext(Sender as tWinControl, True, True );
        if Sender = txtUName then
          btnSave.Click; // let the button click itself
          Key := #0;
        end;
end;
procedure TfrmUserInformation.txtPassKeyPress(Sender: TObject;
  var Key: Char);
begin
        if Key = #13 then begin
          SelectNext(Sender as tWinControl, True, True );
        if Sender = txtPass then
          btnSave.Click; // let the button click itself
          Key := #0;
        end;
end;
procedure TfrmUserInformation.txtRtypeKeyPress(Sender: TObject;
  var Key: Char);
begin
         if Key = #13 then begin
            SelectNext(Sender as tWinControl, True, True );
         if Sender = txtRtype then
            btnSave.Click; // let the button click itself
            Key := #0;
         end;
end;
procedure TfrmUserInformation.cmbUAccessKeyPress(Sender: TObject;
  var Key: Char);
begin
         if Key = #13 then begin
            SelectNext(Sender as tWinControl, True, True );
         if Sender = cmbUAccess then
            btnSave.Click; // let the button click itself
            Key := #0;
         end;
end;
{
procedure TfrmUserInformation.btnBrowseClick(Sender: TObject);
begin
     Browse();
end;
procedure TfrmUserInformation.Browse();
var Stream:TFileStream;
begin
      if opendialog1.Execute then begin
         Stream:= TFileStream.Create(OpenDialog1.FileName, fmOpenRead);

         if DModule.TR.InTransaction then
            DModule.TR.Commit;

         DModule.TR.StartTransaction;
         DModule.QR.SQL.Clear;
         DModule.QR.SQL.Add('UPDATE Users SET Image =:blob WHERE UI_ID =:a');
         DModule.QR.ParamsSetBlob('blob', Stream);
         DModule.QR.Params.AsInteger[0] :=StrToInt('txtIDNum.Text');
         DModule.qr.execSQL;
         Stream.Free;

         if DModule.TR.InTransaction then
            DModule.TR.Commit;
         LoadImage();
      end;
end;
procedure TfrmUserInformation.LoadImage();
var Stream: TMemoryStream;
    Jpg: TJPEGImage;
begin
      UserImage.Picture := nil;

      if DModule.TR.InTransaction then
        DModule.TR.Commit;

      DModule.TR.StartTransaction;
      DModule.QR.SQL.Clear;
      DModule.QR.SQL.Add('SELECT Image FROM USERS WHERE UI_ID := a');
      DModule.QR.Params.AsInteger[0] := StrToInt('txtIDNum.Text');
      DModule.QR.Prepare;
      DModule.QR.Open();

        if DModule.QR.Fields.BlobData[0] <> nil then begin
           Stream := TMemoryStream.Create;
           Jpg := TJPEGImage.Create;
            try
              DModule.QR.ReadBlob('Image', Stream);
              Jpg.LoadFromStream(Stream);
              UserImage.Picture.Graphic := Jpg;
            finally
              Stream.Free;
              Jpg.Free;
            end;
        end
    else begin
        UserImage.Picture := nil;
    end;

      if DModule.TR.InTransaction then;
        DModule.TR.Commit;
end;  }

procedure TfrmUserInformation.popDeactivateClick(Sender: TObject);
begin
       if (lvAcc.ItemFocused.SubItems[8]='1') THEN BEGIN
          popDeactivate.Caption:='&Deactivate';
          Deactivate();
       end

       else if(lvAcc.ItemFocused.SubItems[8]='0') THEN BEGIN
           popDeactivate.Caption:='&Activate';
           ReActivate();
       end
end;
procedure TfrmUserInformation.Deactivate();
begin
acStat2:='0';

    if MessageDlg('Do you want to Deactivate this account?',mtConfirmation,[mbYes, mbNo], 0)= mrYes then begin

      primary:= lvAcc.ItemFocused.Caption;
       if DModule.TR.InTransaction then
        DModule.TR.Commit;

       DModule.QR.SQL.Clear;
       DModule.QR.SQL.Add('UPDATE usersinfo SET Status=:A WHERE UI_ID =:B');
       DModule.QR.Params.AsString[0] := acStat2;
       DModule.QR.Params.AsString[1] := primary;

        MessageDlg('Account has been Deactivated',mtInformation,[mbOK],0);
        DModule.QR.Prepare;
        DModule.QR.Open();

       if DModule.TR.InTransaction then
            DModule.TR.Commit;

          loadAccountInfo;
          popDeactivate.Caption:='&Activate';
          rightClick.Items[0].Caption:= '&Activate';
    end
    else begin

    end
end;
procedure TfrmUserInformation.ReActivate();
begin
acStat:='1';

      if MessageDlg('Do you want to Re-Activate this account?',mtConfirmation,[mbYes, mbNo], 0)= mrYes then begin

      primary:= lvAcc.ItemFocused.Caption;
       if DModule.TR.InTransaction then
        DModule.TR.Commit;

       DModule.QR.SQL.Clear;
       DModule.QR.SQL.Add('UPDATE usersinfo SET Status=:A WHERE UI_ID =:B');
       DModule.QR.Params.AsString[0] := acStat;
       DModule.QR.Params.AsString[1] := primary;

        MessageDlg('Account has been Activated',mtInformation,[mbOK],0);
        DModule.QR.Prepare;
        DModule.QR.Open();

       if DModule.TR.InTransaction then
            DModule.TR.Commit;
          loadAccountInfo;
          popDeactivate.Caption:='&Deactive';
          rightClick.Items[0].Caption:= '&Deactive';
    end
    else begin

    end
end;

procedure TfrmUserInformation.FormShow(Sender: TObject);
begin
        Clear();
        //loadAccountInfo();
        gbUserInfo.Enabled:=false;
        gbUserAcc.Enabled:=False;
        btnSave.Caption:='&Save';
        btnSave.Enabled:=false;
        btnCancel.Enabled:=false;

        {StatusBar1.Panels.Items[3].Text:='Time: ' + DateTimeToStr(Now);
        StatusBar1.Panels.Items[0].Text:='User: ' + frmMain.fullname;
        StatusBar1.Panels.Items[1].Text:='Columns: '+ IntToStr(count);}
end;

procedure TfrmUserInformation.txtSearchKeyPress(Sender: TObject;
  var Key: Char);
begin
        if Key = #13 then begin
            SelectNext(Sender as tWinControl, True, True );
         if Sender = txtSearch then
            btnGo.Click; // let the button click itself
            Key := #0;
         end;
end;

procedure TfrmUserInformation.Timer1Timer(Sender: TObject);
begin
    //StatusBar1.Panels.Items[3].Text:='Time: '+TimeToStr(Now);
end;

procedure TfrmUserInformation.lvAccCustomDrawSubItem(
  Sender: TCustomListView; Item: TListItem; SubItem: Integer;
  State: TCustomDrawState; var DefaultDraw: Boolean);
begin
      if (Item.SubItems.Strings[8]='1')then begin
          Sender.Canvas.Font.Color:=clBlue;
      end
      else if(Item.SubItems.Strings[8]='0')then begin
        Sender.Canvas.Font.Color:=clMaroon;
      end;
end;

end.
