VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.1#0"; "MSCOMCTL.OCX"
Object = "{65E121D4-0C60-11D2-A9FC-0000F8754DA1}#2.0#0"; "mschrt20.ocx"
Begin VB.UserControl CapActivity 
   ClientHeight    =   8595
   ClientLeft      =   0
   ClientTop       =   0
   ClientWidth     =   16545
   ScaleHeight     =   8595
   ScaleWidth      =   16545
   Begin VB.Frame frm_main 
      Caption         =   "frm_main"
      Height          =   7740
      Left            =   3495
      TabIndex        =   5
      Tag             =   "frm_main"
      Top             =   210
      Width           =   12435
      Begin VB.CommandButton btn_quit 
         Caption         =   "#Quit"
         Height          =   525
         Left            =   11640
         TabIndex        =   28
         Tag             =   "btn_quit"
         Top             =   225
         Width           =   690
      End
      Begin VB.Frame frm_detail 
         Caption         =   "frm_general"
         Height          =   6585
         Index           =   2
         Left            =   1470
         TabIndex        =   20
         Tag             =   "frm_general"
         Top             =   1125
         Visible         =   0   'False
         Width           =   10200
         Begin Project1.ArmGrid grd_detail 
            Height          =   1305
            Index           =   2
            Left            =   210
            TabIndex        =   21
            Tag             =   "grd_general"
            Top             =   5115
            Width           =   9510
            _ExtentX        =   16775
            _ExtentY        =   2302
         End
         Begin MSChart20Lib.MSChart mch_general 
            Height          =   2625
            Index           =   1
            Left            =   4920
            OleObjectBlob   =   "CapActivity.ctx":0000
            TabIndex        =   23
            Tag             =   "G2"
            Top             =   165
            Width           =   5055
         End
         Begin MSChart20Lib.MSChart mch_general 
            Height          =   2625
            Index           =   0
            Left            =   120
            OleObjectBlob   =   "CapActivity.ctx":239B
            TabIndex        =   22
            Tag             =   "G1"
            Top             =   270
            Width           =   5055
         End
         Begin MSChart20Lib.MSChart mch_general 
            Height          =   2625
            Index           =   2
            Left            =   300
            OleObjectBlob   =   "CapActivity.ctx":4735
            TabIndex        =   24
            Tag             =   "G3"
            Top             =   2625
            Width           =   5055
         End
         Begin MSChart20Lib.MSChart mch_general 
            Height          =   2625
            Index           =   3
            Left            =   4950
            OleObjectBlob   =   "CapActivity.ctx":6AD5
            TabIndex        =   25
            Tag             =   "G4"
            Top             =   2595
            Width           =   5055
         End
         Begin VB.Image img_Min 
            Height          =   255
            Index           =   2
            Left            =   3765
            Picture         =   "CapActivity.ctx":8E75
            Top             =   45
            Visible         =   0   'False
            Width           =   300
         End
         Begin VB.Image img_Max 
            Height          =   270
            Index           =   2
            Left            =   0
            Picture         =   "CapActivity.ctx":92B3
            Top             =   0
            Width           =   300
         End
      End
      Begin VB.Frame frm_detail 
         Caption         =   "frm_projects"
         Height          =   6585
         Index           =   0
         Left            =   825
         TabIndex        =   10
         Tag             =   "frm_projects"
         Top             =   5580
         Visible         =   0   'False
         Width           =   10200
         Begin Project1.ArmGrid grd_detail 
            Height          =   1455
            Index           =   0
            Left            =   105
            TabIndex        =   14
            Tag             =   "grd_projects"
            Top             =   5040
            Width           =   9510
            _ExtentX        =   16775
            _ExtentY        =   2566
         End
         Begin MSChart20Lib.MSChart mch_project 
            Height          =   2625
            Index           =   0
            Left            =   60
            OleObjectBlob   =   "CapActivity.ctx":972D
            TabIndex        =   11
            Tag             =   "G1"
            Top             =   270
            Width           =   5055
         End
         Begin MSChart20Lib.MSChart mch_project 
            Height          =   2625
            Index           =   1
            Left            =   5265
            OleObjectBlob   =   "CapActivity.ctx":B559
            TabIndex        =   12
            Tag             =   "G2"
            Top             =   150
            Width           =   5055
         End
         Begin MSChart20Lib.MSChart mch_project 
            Height          =   2625
            Index           =   2
            Left            =   285
            OleObjectBlob   =   "CapActivity.ctx":D5CC
            TabIndex        =   13
            Tag             =   "G3"
            Top             =   2655
            Width           =   5055
         End
         Begin MSChart20Lib.MSChart mch_project 
            Height          =   2625
            Index           =   3
            Left            =   4935
            OleObjectBlob   =   "CapActivity.ctx":F614
            TabIndex        =   26
            Tag             =   "G4"
            Top             =   2520
            Width           =   5055
         End
         Begin VB.Image img_Min 
            Height          =   255
            Index           =   0
            Left            =   2160
            Picture         =   "CapActivity.ctx":11A60
            Top             =   -15
            Visible         =   0   'False
            Width           =   300
         End
         Begin VB.Image img_Max 
            Height          =   270
            Index           =   0
            Left            =   0
            Picture         =   "CapActivity.ctx":11E9E
            Top             =   0
            Width           =   300
         End
      End
      Begin VB.Frame frm_detail 
         Caption         =   "frm_customers"
         Height          =   6585
         Index           =   1
         Left            =   870
         TabIndex        =   15
         Tag             =   "frm_customers"
         Top             =   2940
         Visible         =   0   'False
         Width           =   10200
         Begin Project1.ArmGrid grd_detail 
            Height          =   1305
            Index           =   1
            Left            =   270
            TabIndex        =   16
            Tag             =   "grd_customers"
            Top             =   5040
            Width           =   9225
            _ExtentX        =   16272
            _ExtentY        =   2302
         End
         Begin MSChart20Lib.MSChart mch_customer 
            Height          =   2625
            Index           =   2
            Left            =   825
            OleObjectBlob   =   "CapActivity.ctx":12318
            TabIndex        =   19
            Tag             =   "G3"
            Top             =   2655
            Width           =   5055
         End
         Begin MSChart20Lib.MSChart mch_customer 
            Height          =   2625
            Index           =   1
            Left            =   1575
            OleObjectBlob   =   "CapActivity.ctx":1417C
            TabIndex        =   18
            Tag             =   "G2"
            Top             =   45
            Width           =   5055
         End
         Begin MSChart20Lib.MSChart mch_customer 
            Height          =   2625
            Index           =   0
            Left            =   2790
            OleObjectBlob   =   "CapActivity.ctx":15E2C
            TabIndex        =   17
            Tag             =   "G1"
            Top             =   735
            Width           =   5055
         End
         Begin VB.Image img_Min 
            Height          =   255
            Index           =   1
            Left            =   9885
            Picture         =   "CapActivity.ctx":17AEC
            Top             =   150
            Visible         =   0   'False
            Width           =   300
         End
         Begin VB.Image img_Max 
            Height          =   270
            Index           =   1
            Left            =   9480
            Picture         =   "CapActivity.ctx":17F2A
            Top             =   5025
            Width           =   300
         End
      End
      Begin MSComctlLib.TabStrip tb_entity 
         Height          =   6885
         Left            =   60
         TabIndex        =   9
         Top             =   690
         Width           =   12195
         _ExtentX        =   21511
         _ExtentY        =   12144
         _Version        =   393216
         BeginProperty Tabs {1EFB6598-857C-11D1-B16A-00C0F0283628} 
            NumTabs         =   3
            BeginProperty Tab1 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
               Caption         =   "#Projects"
               Key             =   "PRJ"
               ImageVarType    =   2
            EndProperty
            BeginProperty Tab2 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
               Caption         =   "#Customers"
               Key             =   "CST"
               ImageVarType    =   2
            EndProperty
            BeginProperty Tab3 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
               Caption         =   "#General"
               Key             =   "GEN"
               ImageVarType    =   2
            EndProperty
         EndProperty
      End
      Begin MSComctlLib.TabStrip tb_period 
         Height          =   390
         Left            =   4890
         TabIndex        =   6
         Top             =   240
         Width           =   6585
         _ExtentX        =   11615
         _ExtentY        =   688
         TabWidthStyle   =   2
         Style           =   1
         TabFixedWidth   =   2805
         _Version        =   393216
         BeginProperty Tabs {1EFB6598-857C-11D1-B16A-00C0F0283628} 
            NumTabs         =   4
            BeginProperty Tab1 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
               Caption         =   "#Weeks"
               Key             =   "W"
               ImageVarType    =   2
            EndProperty
            BeginProperty Tab2 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
               Caption         =   "#Months"
               Key             =   "M"
               ImageVarType    =   2
            EndProperty
            BeginProperty Tab3 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
               Caption         =   "#Quarters"
               Key             =   "Q"
               ImageVarType    =   2
            EndProperty
            BeginProperty Tab4 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
               Caption         =   "#Year"
               Key             =   "Y"
               ImageVarType    =   2
            EndProperty
         EndProperty
      End
      Begin Project1.ArmCombobox cbo_chartType 
         Height          =   345
         Left            =   1875
         TabIndex        =   8
         Tag             =   "cbo_chartType"
         Top             =   225
         Width           =   2535
         _ExtentX        =   4471
         _ExtentY        =   609
      End
      Begin VB.Label lbl_labels 
         Caption         =   "#Type of chart"
         Height          =   255
         Index           =   1
         Left            =   150
         TabIndex        =   7
         Tag             =   "lbl_chartType"
         Top             =   315
         Width           =   1560
      End
   End
   Begin VB.Frame frm_filters 
      Caption         =   "frm_filters"
      Height          =   7185
      Left            =   120
      TabIndex        =   0
      Tag             =   "frm_filters"
      Top             =   105
      Width           =   3135
      Begin VB.CommandButton btn_refresh 
         Caption         =   "#Refresh"
         Height          =   315
         Left            =   120
         TabIndex        =   27
         TabStop         =   0   'False
         Top             =   660
         Width           =   1335
      End
      Begin Project1.ArmCombobox cbo_year 
         Height          =   345
         Left            =   1245
         TabIndex        =   3
         Top             =   195
         Width           =   1755
         _ExtentX        =   3096
         _ExtentY        =   609
      End
      Begin MSComctlLib.TabStrip tb_reps 
         Height          =   390
         Left            =   60
         TabIndex        =   2
         Top             =   1050
         Width           =   2955
         _ExtentX        =   5212
         _ExtentY        =   688
         TabWidthStyle   =   2
         Style           =   1
         TabFixedWidth   =   2469
         _Version        =   393216
         BeginProperty Tabs {1EFB6598-857C-11D1-B16A-00C0F0283628} 
            NumTabs         =   2
            BeginProperty Tab1 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
               Caption         =   "#Active reps"
               Key             =   "N"
               ImageVarType    =   2
            EndProperty
            BeginProperty Tab2 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
               Caption         =   "#All reps"
               Key             =   "Y"
               ImageVarType    =   2
            EndProperty
         EndProperty
      End
      Begin Project1.ArmTreeView trw_navig 
         Height          =   4695
         Left            =   165
         TabIndex        =   1
         Top             =   2355
         Width           =   2820
         _ExtentX        =   4974
         _ExtentY        =   8281
      End
      Begin VB.Label lbl_labels 
         Caption         =   "#Year"
         Height          =   255
         Index           =   0
         Left            =   90
         TabIndex        =   4
         Tag             =   "lbl_year"
         Top             =   240
         Width           =   1080
      End
   End
End
Attribute VB_Name = "CapActivity"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Option Compare Text
'**********************************************************************************************************************************
'**********************************************************************************************************************************
' FRAMEWORK API FUNCTION DECLARES
'**********************************************************************************************************************************
'**********************************************************************************************************************************

Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Private Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long


#If ENV = LIVE Then
    Dim mo_Db As Object
#Else
    Dim mo_Db As ARMSYSCOMLib.ArmDb
#End If


Private ms_Language_Code    As String       'current user interface language
Private ms_LoginName        As String
Private ml_U_code           As Long         'U_Code (GEN_Systems_Users) of logged user
Private mb_InternalInit     As Boolean      'framework is doing some own control manipulation, all events should handle
Private ml_labels           As Long         ' screen constants for control


Private mb_Initialized      As Boolean      'framework is initialised or not

Private Const SEP = "®©"
Private Const SEP1 As String = "®"
Private Const SEP2 As String = "©"
Private Const SCREEN_NAME       As String = "CapActivity"
Private ms_DecimalSeparator             As String       'locale decimal separator
Private ms_ThousandSeparator            As String       'locale thousand separator

Private Const SW_SHOWNORMAL = 1
Private Const LOCALE_USER_DEFAULT = &H400
Private Const LOCALE_SDECIMAL = &HE ' Decimal separator
Private Const LOCALE_STHOUSAND = &HF ' Thousand separator

Const C_ERRORRAISE As Long = 50000

Private Enum ArmErr
    DBCnxFailed = C_ERRORRAISE + 1             ' Unable to connect to the database
    CPTAlreadyInitialized = C_ERRORRAISE + 2   ' We try to initialize a component that is already initialized
    CPTNotInitialized = C_ERRORRAISE + 3       ' We try to use or free that is not initialized yet
    InvalidArgument = C_ERRORRAISE + 4
    PropertyNotSet = C_ERRORRAISE + 5
    SQLFailure = C_ERRORRAISE + 6               ' A SQL runtime error has occured : syntax wrong....
    SQLBadRowAffectedCount = C_ERRORRAISE + 7   ' A SQL request has not affected the expected rowcount (ex: one Update do nothing)
    SQLBadRowExpectedCount = C_ERRORRAISE + 8   ' A SQL request does not return the expected rowcount : select an item return nothing...
    DrivingError = C_ERRORRAISE + 9
    CompFncFailed = C_ERRORRAISE + 10           ' when component function fail
    GridLoadFailed = C_ERRORRAISE + 11          ' load function failed ... bad sql
    InvalidValue = C_ERRORRAISE + 12          ' load function failed ... bad sql
End Enum

Private Enum ArmCusErr
    DuplicityDetected = C_ERRORRAISE + 2301                ' detected row with same unique id
End Enum

Private Type MaximizedObjT
    ent As Object
    Left As Long
    Top As Long
    Width As Long
    Height As Long
End Type

Private Enum GCIndex                ' indexes into GraphCursor.A
    G_Week = 0
    G_Month = 1
    G_Quarter = 2
    G_Year = 3
End Enum

Private Type GraphCursor
    Chart As MSChart                    ' small duplicity as we have chartArr in parent type
    a(3) As Long
    ma_fields() As String               ' field names
End Type

Private Type CapActDetail
    Key As String
    frmIndex As Long
    detGrdIndex As Long
    detGrdRequest As Variant
    chartArr As Object
    ma_graphCusrsors() As GraphCursor
End Type

Public Event quit()

Private mo_detail() As CapActDetail
Private mo_maxEnt As MaximizedObjT

'**********************************************************************************************************************************
'**********************************************************************************************************************************
' FRAMEWORK CUSTOM METHODS
' Change content of methods as you need, but do not add any new method or property here !
'**********************************************************************************************************************************
'**********************************************************************************************************************************

Private Function Load_A_COM_Custom() As Boolean
On Error GoTo ErrHandler
    ReDim mo_detail(0) As CapActDetail
    Set mo_maxEnt.ent = Nothing
    Load_A_COM_Custom = True
    Exit Function
ErrHandler:
    Call ErrorHandler("Load_A_COM_Custom")
End Function

Private Sub CloseCursors()
On Error GoTo ErrHandler
    Dim ll_i As Long
    Dim ll_tabIdx As Long
    If UBound(mo_detail) = 0 Then
        Exit Sub
    End If
    For ll_tabIdx = LBound(mo_detail) To UBound(mo_detail)
        For ll_i = LBound(mo_detail(ll_tabIdx).ma_graphCusrsors) To UBound(mo_detail(ll_tabIdx).ma_graphCusrsors)
            If mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Week) <> 0 Then
                Call mo_Db.Close(mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Week))
                mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Week) = 0
            End If
            
            If mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Month) <> 0 Then
                Call mo_Db.Close(mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Month))
                mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Month) = 0
            End If
            
            If mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Quarter) <> 0 Then
                Call mo_Db.Close(mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Quarter))
                mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Quarter) = 0
            End If
            
            If mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Year) <> 0 Then
                Call mo_Db.Close(mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Year))
                mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Year) = 0
            End If
        Next
    Next

    Exit Sub
ErrHandler:
    Call ErrorHandler("ClearCursors")
End Sub

Private Sub UnLoad_A_COM_Custom()
On Error GoTo ErrHandler
    Call CloseCursors
    Exit Sub
ErrHandler:
    Call ErrorHandler("UnLoad_A_COM_Custom")
End Sub

Private Sub Init_Custom()
On Error GoTo ErrHandler

    tb_entity.Visible = False

    'Initialization of the component of the form
    ReDim mo_detail(2) As CapActDetail
    mo_detail(0).Key = "PRJ"
    mo_detail(1).Key = "CST"
    mo_detail(2).Key = "GEN"
    
    mo_detail(0).frmIndex = 0
    mo_detail(1).frmIndex = 1
    mo_detail(2).frmIndex = 2
    
    mo_detail(0).detGrdIndex = 0
    mo_detail(1).detGrdIndex = 1
    mo_detail(2).detGrdIndex = 2
    
    Set mo_detail(0).chartArr = mch_project
    Set mo_detail(1).chartArr = mch_customer
    Set mo_detail(2).chartArr = mch_general
    
    ReDim mo_detail(0).ma_graphCusrsors(3) As GraphCursor          ' 4 graphs for project tab
    ReDim mo_detail(1).ma_graphCusrsors(2) As GraphCursor          ' 3 graphs for customers tab
    ReDim mo_detail(2).ma_graphCusrsors(3) As GraphCursor          ' 4 graphs for general tab
    
    ' init cursors to 0
    Dim ll_i As Long
    Dim ll_tabIdx As Long
    For ll_tabIdx = LBound(mo_detail) To UBound(mo_detail)
        For ll_i = LBound(mo_detail(ll_tabIdx).ma_graphCusrsors) To UBound(mo_detail(ll_tabIdx).ma_graphCusrsors)
            Set mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).Chart = mo_detail(ll_tabIdx).chartArr(ll_i)
            mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Week) = 0
            mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Month) = 0
            mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Quarter) = 0
            mo_detail(ll_tabIdx).ma_graphCusrsors(ll_i).a(G_Year) = 0
        Next
    Next
    
    ' init cursor fields
    ReDim mo_detail(0).ma_graphCusrsors(0).ma_fields(1) As String          ' for project tab/Graph1
    mo_detail(0).ma_graphCusrsors(0).ma_fields(0) = "VALUE1"
    mo_detail(0).ma_graphCusrsors(0).ma_fields(1) = "VALUE2"
    
    ReDim mo_detail(0).ma_graphCusrsors(1).ma_fields(2) As String          ' for project tab/Graph2
    mo_detail(0).ma_graphCusrsors(1).ma_fields(0) = "VALUE1"
    mo_detail(0).ma_graphCusrsors(1).ma_fields(1) = "VALUE2"
    mo_detail(0).ma_graphCusrsors(1).ma_fields(2) = "VALUE3"
    
    ReDim mo_detail(0).ma_graphCusrsors(2).ma_fields(2) As String          ' for project tab/Graph3
    mo_detail(0).ma_graphCusrsors(2).ma_fields(0) = "VALUE1"
    mo_detail(0).ma_graphCusrsors(2).ma_fields(1) = "VALUE2"
    mo_detail(0).ma_graphCusrsors(2).ma_fields(2) = "VALUE3"
    
    ReDim mo_detail(0).ma_graphCusrsors(3).ma_fields(3) As String          ' for project tab/Graph4
    mo_detail(0).ma_graphCusrsors(3).ma_fields(0) = "VALUE1"
    mo_detail(0).ma_graphCusrsors(3).ma_fields(1) = "VALUE2"
    mo_detail(0).ma_graphCusrsors(3).ma_fields(2) = "VALUE3"
    mo_detail(0).ma_graphCusrsors(3).ma_fields(3) = "VALUE4"
    
    
    ReDim mo_detail(1).ma_graphCusrsors(0).ma_fields(1) As String          ' for customer tab/Graph1
    mo_detail(1).ma_graphCusrsors(0).ma_fields(0) = "VALUE1"
    mo_detail(1).ma_graphCusrsors(0).ma_fields(1) = "VALUE2"
    
    ReDim mo_detail(1).ma_graphCusrsors(1).ma_fields(1) As String          ' for customer tab/Graph2
    mo_detail(1).ma_graphCusrsors(1).ma_fields(0) = "VALUE1"
    mo_detail(1).ma_graphCusrsors(1).ma_fields(1) = "VALUE2"
    
    ReDim mo_detail(1).ma_graphCusrsors(2).ma_fields(1) As String          ' for customer tab/Graph3
    mo_detail(1).ma_graphCusrsors(2).ma_fields(0) = "VALUE1"
    mo_detail(1).ma_graphCusrsors(2).ma_fields(1) = "VALUE2"
    
    
    ReDim mo_detail(2).ma_graphCusrsors(0).ma_fields(3) As String          ' for general tab/Graph1
    mo_detail(2).ma_graphCusrsors(0).ma_fields(0) = "VALUE1"
    mo_detail(2).ma_graphCusrsors(0).ma_fields(1) = "VALUE2"
    mo_detail(2).ma_graphCusrsors(0).ma_fields(2) = "VALUE3"
    mo_detail(2).ma_graphCusrsors(0).ma_fields(3) = "VALUE4"
    
    ReDim mo_detail(2).ma_graphCusrsors(1).ma_fields(3) As String          ' for general tab/Graph2
    mo_detail(2).ma_graphCusrsors(1).ma_fields(0) = "VALUE1"
    mo_detail(2).ma_graphCusrsors(1).ma_fields(1) = "VALUE2"
    mo_detail(2).ma_graphCusrsors(1).ma_fields(2) = "VALUE3"
    mo_detail(2).ma_graphCusrsors(1).ma_fields(3) = "VALUE4"
    
    ReDim mo_detail(2).ma_graphCusrsors(2).ma_fields(3) As String          ' for general tab/Graph3
    mo_detail(2).ma_graphCusrsors(2).ma_fields(0) = "VALUE1"
    mo_detail(2).ma_graphCusrsors(2).ma_fields(1) = "VALUE2"
    mo_detail(2).ma_graphCusrsors(2).ma_fields(2) = "VALUE3"
    mo_detail(2).ma_graphCusrsors(2).ma_fields(3) = "VALUE4"
    
    ReDim mo_detail(2).ma_graphCusrsors(3).ma_fields(3) As String          ' for general tab/Graph4
    mo_detail(2).ma_graphCusrsors(3).ma_fields(0) = "VALUE1"
    mo_detail(2).ma_graphCusrsors(3).ma_fields(1) = "VALUE2"
    mo_detail(2).ma_graphCusrsors(3).ma_fields(2) = "VALUE3"
    mo_detail(2).ma_graphCusrsors(3).ma_fields(3) = "VALUE4"
    

    ' toolbar init
    ' main toolbar
    
    Dim lo_grd As ArmGrid
    For Each lo_grd In grd_detail
        lo_grd.ExportTitles = True
        lo_grd.AllowExcelExport = True
        lo_grd.FetchAll = True
        lo_grd.AllowSort = True
        lo_grd.AllowPrint = False
    Next
    
    Call cbo_chartType.AddItem(Array(VtChChartType.VtChChartType2dBar, "2D Bar"), True)
    Call cbo_chartType.AddItem(Array(VtChChartType.VtChChartType3dBar, "3D Bar"))
    Call cbo_chartType.AddItem(Array(VtChChartType.VtChChartType2dLine, "2D Line"))
    Call cbo_chartType.AddItem(Array(VtChChartType.VtChChartType3dLine, "3D Line"))
    Call cbo_chartType.AddItem(Array(VtChChartType.VtChChartType2dArea, "2D Area"))
    Call cbo_chartType.AddItem(Array(VtChChartType.VtChChartType3dArea, "3D Area"))
    Call cbo_chartType.AddItem(Array(VtChChartType.VtChChartType2dStep, "2D Step"))
    Call cbo_chartType.AddItem(Array(VtChChartType.VtChChartType3dStep, "3D Step"))
    
    cbo_year.Request = "SELECT DISTINCT LTRIM(yy), yy FROM calendar WHERE yy<=YEAR(getdate()) ORDER BY 1"
    cbo_year.FirstBlankItem = False
    Call cbo_year.AddItem(Array(Year(Now), Year(Now)), True)
    
    Call LoadTreeView
    
    Set tb_entity.SelectedItem = tb_entity.Tabs(1)
    
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("Init_Custom")
End Sub

Private Function LoadTreeView() As Boolean
On Error GoTo ErrHandler
    trw_navig.Levels = 3
    trw_navig.StartDemandLevel = 1
    trw_navig.Images = Array(1, 1, 1)
    trw_navig.SelectedImages = Array(2, 2, 2)
    trw_navig.NodeDataNames(0) = ""
    trw_navig.NodeDataNames(1) = "BMK_CODE"
    trw_navig.NodeDataNames(2) = "REP_CODE"
    
     
    trw_navig.NodeRequests = Array("Cap_rpt_trw_L0", "Cap_rpt_trw_L1", Replace("exec CA_trw_L3 $0$, '$EXC$'", "$EXC$", tb_reps.SelectedItem.Key))
    
    If Not trw_navig.LoadTree(LoadTypeChildsDemand) Then
        LoadTreeView = False
    Else
        LoadTreeView = False
    End If
    Exit Function
ErrHandler:
    Call ErrorHandler("loadTreeview")
End Function

Private Property Let Detail(ByVal as_Key As String, ByRef ao_value As CapActDetail)
    Dim ll_Idx As Long
    
    For ll_Idx = LBound(mo_detail) To UBound(mo_detail)
        If mo_detail(ll_Idx).Key = as_Key Then
            mo_detail(ll_Idx).detGrdRequest = ao_value.detGrdRequest
            Exit For
        End If
    Next
End Property

Private Property Get Detail(ByVal as_Key As String) As CapActDetail
On Error GoTo ErrHandler
    Dim ll_Idx As Long
    
    For ll_Idx = LBound(mo_detail) To UBound(mo_detail)
        If mo_detail(ll_Idx).Key = as_Key Then
            Detail = mo_detail(ll_Idx)
            Exit For
        End If
    Next
    Exit Property
ErrHandler:
    Call ErrorHandler("Detail")
End Property

Private Property Get DetailIndex(ByVal as_Key As String) As Long
On Error GoTo ErrHandler
    Dim ll_Idx As Long
    
    For ll_Idx = LBound(mo_detail) To UBound(mo_detail)
        If mo_detail(ll_Idx).Key = as_Key Then
            DetailIndex = ll_Idx
            Exit For
        End If
    Next
    Exit Property
ErrHandler:
    Call ErrorHandler("DetailIndex")
End Property

'**********************************************************************************************************************************
'**********************************************************************************************************************************
' FRAMEWORK PROPERTIES
' please DO NOT change anything in this part of code
'**********************************************************************************************************************************
'**********************************************************************************************************************************
Public Property Let Visible(ByVal aVisible As Boolean)
    UserControl.Extender.Visible = aVisible
End Property

Public Property Get Visible() As Boolean
    Visible = UserControl.Extender.Visible
End Property

Public Property Get Initialized() As Boolean
    Initialized = mb_Initialized
End Property


Property Let U_Code(al_Code As Long)
ml_U_code = al_Code
End Property

Property Let LoginName(as_Login As String)
ms_LoginName = as_Login
End Property

Property Let Language_Code(AString As String)
ms_Language_Code = AString
End Property

Public Property Set ArmDb(ByRef local_connection As Object)
    If Not (local_connection Is Nothing) Then
        Set mo_Db = local_connection
    End If
End Property


'**********************************************************************************************************************************
'**********************************************************************************************************************************
' FRAMEWORK METHODS
' please DO NOT change anything in this part of code
'**********************************************************************************************************************************
'**********************************************************************************************************************************
Public Sub Move(ByVal aLeft As Single, ByVal aTop As Single, ByVal aWidth As Single, ByVal aHeight As Single)
On Error GoTo ErrHandler
    LockWindowUpdate UserControl.hwnd
    Call UserControl.Extender.Move(aLeft, aTop, aWidth, aHeight)
    Call Form_Resize
    LockWindowUpdate 0
    Exit Sub
ErrHandler:
    Call ErrorMessage("Move")
End Sub

Public Function Load_A_Com() As Boolean

Dim ll_Index As Long
Dim lo_Control As Object

On Error GoTo ErrHandler
    
    Load_A_Com = False
    
    If mb_Initialized Then
        Call Err.Raise(ArmErr.CPTAlreadyInitialized)
    End If
    
    mb_InternalInit = False
    ms_DecimalSeparator = Format(0, ".")
    
    ReDim mt_MainConfig(-1 To -1)
    ReDim mt_DetailConfig(-1 To -1)
    ReDim mt_SubdetailConfig(-1 To -1)
    
    Dim sBuffer As String
    Dim nBufferLen As Long
    nBufferLen = 255
    sBuffer = String$(nBufferLen, vbNullChar)
    nBufferLen = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sBuffer, nBufferLen)
    If nBufferLen > 0 Then
        ms_ThousandSeparator = Left$(sBuffer, nBufferLen - 1)
    End If
    
    For Each lo_Control In Controls
        Select Case UCase(TypeName(lo_Control))
        Case "ARMCOMBOBOX"
            Set lo_Control.ArmDb = mo_Db
            Call lo_Control.Load_A_Com
        Case "ARMPICKER"
            Set lo_Control.ArmDb = mo_Db
            Call lo_Control.Load_A_Com
        Case "TOOLBARCONTROL"
            lo_Control.Language = ms_Language_Code
            lo_Control.Load_A_Com
        Case "TOOLBR"
            Set lo_Control.ArmDb = mo_Db
            lo_Control.Language = ms_Language_Code
            Call lo_Control.Load_A_Com
        Case "ARMGRID"
            Set lo_Control.ArmDb = mo_Db
            Call lo_Control.Load_A_Com
        Case "ARMTREEVIEW"
            Set lo_Control.ArmDb = mo_Db
            lo_Control.Language = ms_Language_Code
            Call lo_Control.Load_A_Com
        Case "ARMCHECKVIEW"
            Set lo_Control.ArmDb = mo_Db
            Call lo_Control.Load_A_Com
        Case "A_CALOCX"
            lo_Control.Language = ms_Language_Code
            Call lo_Control.reinit_cal
        End Select
    Next
    Call Form_Resize
    mb_Initialized = Load_A_COM_Custom
    Load_A_Com = mb_Initialized
    Exit Function
ErrHandler:
    Load_A_Com = False
    Call ErrorMessage("Load_A_COM")
End Function

Public Function Unload_A_Com() As Boolean
    
On Error GoTo ErrHandler
    
    Dim lo_Control As Object
    Dim ll_Index As Long
    
    Call UnLoad_A_COM_Custom
    If ml_labels > 0 Then
        Call mo_Db.Close(ml_labels)
        ml_labels = 0
    End If
    
    For Each lo_Control In Controls
        Select Case UCase(TypeName(lo_Control))
        Case "ARMCOMBOBOX", "TOOLBARCONTROL", "ARMGRID", "ARMTREEVIEW", "ARMCHECKVIEW", "ARMPICKER"
            Call lo_Control.Unload_A_Com
        End Select
    Next
    
    
    Set mo_Db = Nothing
    mb_Initialized = False
    Unload_A_Com = True
    Exit Function
ErrHandler:
    If ml_labels > 0 Then
        Call mo_Db.Close(ml_labels)
        ml_labels = 0
    End If
    Unload_A_Com = False
    Call ErrorMessage("Unload_A_Com")
End Function

Private Sub Resize()
On Error GoTo ErrHandler
    LockWindowUpdate UserControl.hwnd
    Call Form_Resize
    LockWindowUpdate 0
    Exit Sub
ErrHandler:
    Call ErrorMessage("Resize")
End Sub

Public Function Init_control() As Boolean
Dim ll_Index As Long

On Error GoTo ErrHandler
    Init_control = False
    If Not CheckRights Then
        Exit Function
    End If
    
    mb_InternalInit = True
    Call Init_Custom
    mb_InternalInit = False
    
    Call ChangeCharset(Controls, GetCodePageFromLanguage(mo_Db, ms_Language_Code))
    Call LoadLabels(mo_Db, Me, SCREEN_NAME, ms_Language_Code)
    
    Init_control = True
    Exit Function
ErrHandler:
    mb_InternalInit = False
    Call ErrorMessage("Init_control")
End Function

Private Function CheckRights() As Boolean
Const CREQ As String = "SELECT CFG_Value FROM A_CONFIG WHERE CFG_key = 'CAP_ACT_ENABLED'"
On Error GoTo ErrHandler
    CheckRights = False
    Dim ll_Cursor As Long
    ll_Cursor = OpenSQLSafe(mo_Db, CREQ)
    Dim lsa_login() As String
    lsa_login = Split(mo_Db.GetFields(ll_Cursor, "CFG_Value"), ",", , vbTextCompare)
    Call mo_Db.Close(ll_Cursor)
    ll_Cursor = 0
    
    Dim ll_i As Long
    For ll_i = LBound(lsa_login) To UBound(lsa_login)
        If ms_LoginName Like lsa_login(ll_i) Then
            CheckRights = True
            Exit For
        End If
    Next
    Exit Function
ErrHandler:
    If ll_Cursor > 0 Then
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    End If
    Call ErrorHandler("CheckRights")
End Function

Private Function HasContainer(ByVal lo_Control As Control, ByRef lo_Container As Object) As Boolean
    Dim ll_Index As Long
    Dim lo_Object As Object

    On Error GoTo CleanUp   'not all controls support Container property
    HasContainer = False
    While Not (lo_Control Is Nothing)
        If lo_Control.Container Is lo_Container Then
            HasContainer = True
            Exit Function
        End If
        Set lo_Control = lo_Control.Container
    Wend

CleanUp:

End Function

Private Sub ResetFrameControls(ByRef ao_Frame As Frame)
On Error GoTo ErrHandler

Dim lo_Control As Control

    mb_InternalInit = True
    For Each lo_Control In Controls
    
        If HasContainer(lo_Control, ao_Frame) Then
            Select Case UCase(TypeName(lo_Control))
                Case "ARMCOMBOBOX"
                    Call lo_Control.Clear
                    lo_Control.Tag = ""
                    lo_Control.Request = ""
                    lo_Control.Visible = False
                Case "ARMPICKER"
                    Call lo_Control.Clear
                    lo_Control.Tag = ""
                    lo_Control.Visible = False
                Case "CHECKBOX"
                    lo_Control.Value = Unchecked
                    lo_Control.Tag = ""
                    lo_Control.Caption = ""
                    lo_Control.Visible = False
                Case "TEXTBOX"
                    lo_Control.Tag = ""
                    lo_Control.Text = ""
                    lo_Control.Visible = False
                Case "A_CALOCX"
                    Call lo_Control.reinit_cal
                    lo_Control.Tag = ""
                    lo_Control.Visible = False
                Case "LABEL"
                    lo_Control.Tag = ""
                    lo_Control.Caption = ""
                    lo_Control.FontBold = False
                    lo_Control.Visible = False
            End Select
        End If
    Next
    mb_InternalInit = False
    Exit Sub
ErrHandler:
    mb_InternalInit = False
    Call ErrorHandler("ResetFrameControls")
End Sub

' ************************************************************************************
' FRAMEWORK DB-ACCESS FUNCTIONS
' please do not change this code
' ************************************************************************************

' Return the result of a SQL request
' Convert SQL runtime errors and process errors to VB Error
#If ENV = LIVE Then
Private Function OpenSQLSafe(ByVal ao_DB As Object, ByVal as_Request As String, Optional ByVal al_RowExpectedCount = -1) As Long
#Else
Private Function OpenSQLSafe(ByVal ao_DB As ARMSYSCOMLib.ArmDb, ByVal as_Request As String, Optional ByVal al_RowExpectedCount = -1) As Long
#End If

On Error GoTo ErrHandler

    Dim lc_Data As Long
    lc_Data = ao_DB.OpenSQL(as_Request)
    
    If lc_Data = 0 Then
        Err.Raise ArmErr.SQLFailure, "SQL : " & as_Request, Join(ao_DB.SQLErrorCodes, SEP2) & SEP1 & Join(ao_DB.SQLErrorMessages, SEP2)
    End If
    
    If al_RowExpectedCount <> -1 Then
        ' Then check the rowcount
        If ao_DB.RowCount(lc_Data) <> al_RowExpectedCount Then
            Err.Raise ArmErr.SQLBadRowExpectedCount, "SQL : " & as_Request, al_RowExpectedCount & "<>" & ao_DB.RowCount(lc_Data)
        End If
    End If

    OpenSQLSafe = lc_Data

    Exit Function

ErrHandler:

    Call ErrorHandler("OpenSQLSafe")

End Function


' Execute a SQL request returning no data
' Convert SQL runtime errors and process errors to VB Error
' Params:
' ao_Db (Object)
' as_Request (String)
' al_RowAffectedCount (String)
#If ENV = LIVE Then
Private Sub ExecuteSQLSafe(ByVal ao_DB As Object, ByVal as_Request As String, Optional ByVal al_RowAffectedCount = -1, Optional ab_DuplicityCheck As Boolean = False)
#Else
Private Sub ExecuteSQLSafe(ByVal ao_DB As ARMSYSCOMLib.ArmDb, ByVal as_Request As String, Optional ByVal al_RowAffectedCount = -1, Optional ab_DuplicityCheck As Boolean = False)
#End If
On Error GoTo ErrHandler

    ' First execute the request
    If Not ao_DB.ExecuteSQL(as_Request) Then
        Err.Raise ArmErr.SQLFailure, "SQL : " & as_Request, Join(ao_DB.SQLErrorCodes, SEP2) & SEP1 & Join(ao_DB.SQLErrorMessages, SEP2)
    End If

    If al_RowAffectedCount <> -1 Then
        ' Then check the rowcount
        If ao_DB.SQLRowsAffected <> al_RowAffectedCount Then
            
            If ab_DuplicityCheck Then
                Err.Raise ArmCusErr.DuplicityDetected, "SQL : " & as_Request, al_RowAffectedCount & "<>" & ao_DB.SQLRowsAffected
            Else
                Err.Raise ArmErr.SQLBadRowAffectedCount, "SQL : " & as_Request, al_RowAffectedCount & "<>" & ao_DB.SQLRowsAffected
            End If
        End If
    End If

    Exit Sub

ErrHandler:
    Call ErrorHandler("ExecuteSQLSafe")
End Sub

Private Function GetDbError(ByVal lo_Db As ARMSYSCOMLib.ArmDb) As String
On Error GoTo ErrHandler
    
    If IsArray(lo_Db.SQLErrorMessages) Then
        Debug.Assert (IsArray(mo_Db.SQLErrorCodes))
        ' Display errors msgBox
        GetDbError = Join(lo_Db.SQLErrorCodes, ",") & vbCrLf & Join(lo_Db.SQLErrorMessages, vbCrLf)
    Else
        ' ExecuteSQL failed but no error message?
        GetDbError = "Unknown error"
    End If
    Exit Function
ErrHandler:
    Call ErrorHandler("GetDbError()")
End Function
' Standard error handler
Private Sub ErrorHandler(ByVal as_Fct As String)
  
    Call Err.Raise(Err.Number, as_Fct & SEP1 & Err.Source, Err.Description)
End Sub

' display standard error message
Private Sub ErrorMessage(ByVal as_Fct As String)
    Dim ls_ErrSource As String
    Dim ls_ErrDescription As String
    ls_ErrSource = as_Fct & SEP1 & Err.Source
    ls_ErrDescription = Err.Description
    
    Call LogMessage(SCREEN_NAME & " exception raised. Err.Number:" & Err.Number & ", Err.Source:" & ls_ErrSource & ", Err.Description " & ls_ErrDescription & ".", "E", False)
    Call MsgBox("Error occured, please contact IT. Application will now shutdown." & vbCrLf & ls_ErrSource & vbCrLf & "Description: " & ls_ErrDescription, vbCritical, App.Title & " " & App.Major & "." & App.Minor & "." & App.Revision)
    End
End Sub

' logs message to database
Private Sub LogMessage(ByVal as_logMsg As String, Optional ByVal as_logType As String = "E", Optional ab_ExitOnException As Boolean = False)
    Dim ll_errNumber As Long
    Dim ls_ErrDescription As String, ls_ErrSource As String
    
    ll_errNumber = Err.Number
    ls_ErrDescription = Err.Description
    ls_ErrSource = Err.Source

On Error GoTo ErrHandler

Const LOG_REQUEST As String = "EXEC A_log_ins $UCODE$,$LOGTYPE$,$MSG$,$APP$"
    Dim ls_Req As String
    Dim ll_Cursor As Long
    Dim ls_Source As String, ls_Msg As String
    
    ls_Source = SCREEN_NAME & " " & App.Title & " " & App.Major & "." & App.Minor & "." & App.Revision
    ls_Msg = as_logMsg
    If ll_errNumber <> 0 Then
        ls_Msg = ls_Msg & SEP1 & ll_errNumber & " : " & ls_ErrDescription & " - " & ls_ErrSource
    End If
    
    ls_Req = ReplacePlaceHolder(LOG_REQUEST, "$UCODE$", CStr(ml_U_code))
    ls_Req = ReplacePlaceHolder(ls_Req, "$LOGTYPE$", SQLStr(as_logType))
    ls_Req = ReplacePlaceHolder(ls_Req, "$MSG$", Left(Trim(SQLStr(ls_Msg)), 4000))
    ls_Req = ReplacePlaceHolder(ls_Req, "$APP$", Left(Trim(SQLStr(ls_Source)), 50))
    
    Call ExecuteSQLSafe(mo_Db, ls_Req)
    
    Err.Number = ll_errNumber
    Err.Description = ls_ErrDescription
    Err.Source = ls_ErrSource
    Exit Sub
    
ErrHandler:
    If ab_ExitOnException Then
        Call MsgBox("A fatal error occured. Unable to log error into database, the application will be close. Please report the following message to your IT support: " & vbCrLf & _
            "Number:" & Err.Number & vbCrLf & "Description:" & Err.Description, vbCritical, App.Title & " " & App.Major & "." & App.Minor & "." & App.Revision)
        End
    End If
    Err.Number = ll_errNumber
    Err.Description = ls_ErrDescription
    Err.Source = ls_ErrSource
End Sub

Private Function SQLStr(ByVal as_Data As String) As String
On Error GoTo ErrHandler

    SQLStr = "'" & Replace(as_Data, "'", "''") & "'"
    Exit Function
ErrHandler:
    Call ErrorHandler("SqlStr")
End Function

Private Function ReplacePlaceHolder(ByVal as_Request As String, ByVal as_PlaceHolder As String, ByVal as_DefaultValue As String) As String
On Error GoTo ErrHandler
    
    as_Request = Replace(as_Request, as_PlaceHolder, as_DefaultValue, , , vbTextCompare)
    
    ReplacePlaceHolder = as_Request
    Exit Function
ErrHandler:
    Call ErrorHandler("ReplacePlaceholder")
End Function

' procedure save/restore err object
Private Sub UpdateError(Optional ab_saveError As Boolean = False)
Static ls_ErrDesc As String
Static ls_ErrSource As String
Static ll_errnum As Long
    
    If ab_saveError Then
        ls_ErrDesc = Err.Description
        ls_ErrSource = Err.Source
        ll_errnum = Err.Number
    Else
        Err.Description = ls_ErrDesc
        Err.Source = ls_ErrSource
        Err.Number = ll_errnum
    End If
End Sub

Private Sub LockScreen(ByVal ab_lock As Boolean)

    Dim ll_errNumber As Long, ls_ErrSrc As String, ls_ErrDesc As String
    ll_errNumber = Err.Number
    ls_ErrSrc = Err.Source
    ls_ErrDesc = Err.Description

On Error GoTo ErrHandler
    Static ll_Count As Long
    Static ll_Mousepointer As Long
    Static lb_Locked As Boolean
      
      
    ll_Count = ll_Count + IIf(ab_lock, 1, -1)
    Debug.Assert (ll_Count >= 0)
    
    ' First lock
    If Not lb_Locked And ab_lock Then
        ll_Mousepointer = Screen.MousePointer
        Screen.MousePointer = vbHourglass
        UserControl.Enabled = False
        LockWindowUpdate UserControl.hwnd
        lb_Locked = True
    End If
    
    ' Unlock
    If ll_Count = 0 Then
        DoEvents ' Flush events
        LockWindowUpdate 0
        UserControl.Enabled = True
        UserControl.Refresh ' Repaint immediately
        Screen.MousePointer = ll_Mousepointer
        lb_Locked = False
    End If
    
    Err.Number = ll_errNumber
    Err.Source = ls_ErrSrc
    Err.Description = ls_ErrDesc
    Exit Sub
ErrHandler:
    Call ErrorHandler("LockScreen")
End Sub


' ************************************************************************************
' *************************** INTERNATIONAL FUNCTIONS ********************************
' ************************************************************************************

Private Function GetCodePageFromLanguage(ByRef ao_Armdb As Object, ByVal as_Language As String) As Long
On Error GoTo ErrHandler
Const C_REQ As String = "SELECT Code_Page FROM Language WHERE Language_Code = '$Language_Code$'"
    Dim ls_Req As String
    Dim ll_Cursor As Long
    Dim ll_codePage As Long
    
    ls_Req = ReplacePlaceHolder(C_REQ, "$Language_Code$", as_Language)

    ll_Cursor = OpenSQLSafe(ao_Armdb, ls_Req)
    Debug.Assert (ll_Cursor <> 0)
    
    ll_codePage = CLng(ao_Armdb.GetFields(ll_Cursor, "Code_Page"))
    Call ao_Armdb.Close(ll_Cursor)
    GetCodePageFromLanguage = ll_codePage
    Exit Function
    
ErrHandler:
    If ll_Cursor <> 0 Then Call ao_Armdb.Close(ll_Cursor)
    Call ErrorHandler("GetCodePageFromLanguage()")
End Function

'convert code page into charset integer
Private Function GetCharSetFromCodePage(ByVal al_CodePage As Long) As Long

On Error GoTo ErrHandler

    Select Case CStr(al_CodePage)
        Case 932 ' Japanese
            GetCharSetFromCodePage = 128
        Case 936 ' Simplified Chinese
            GetCharSetFromCodePage = 134
        Case 949 ' Korean
            GetCharSetFromCodePage = 129
        Case 950 ' Traditional Chinese
            GetCharSetFromCodePage = 136
        Case 1250 ' Eastern Europe
            GetCharSetFromCodePage = 238
        Case 1251 ' Russian
            GetCharSetFromCodePage = 204
        Case 1252 ' Western European Languages
            GetCharSetFromCodePage = 0
        Case 1253 ' Greek
            GetCharSetFromCodePage = 161
        Case 1254 ' Turkish
            GetCharSetFromCodePage = 162
        Case 1255 ' Hebrew
            GetCharSetFromCodePage = 177
        Case 1256 ' Arabic
            GetCharSetFromCodePage = 178
        Case 1257 ' Baltic
            GetCharSetFromCodePage = 186
        Case Else
            GetCharSetFromCodePage = 0
    End Select
    
    Exit Function
    
ErrHandler:
    Call ErrorHandler("GetCharSetFromCodePage()")
End Function

Private Sub ChangeCharset(ByRef ao_Container As Object, Optional ByVal aCodePage As Long)

On Error GoTo ErrHandler
   
    Dim lc_Control As Control
    Dim ll_Charset As Long
    
    On Error Resume Next
    ll_Charset = GetCharSetFromCodePage(aCodePage)
    
    For Each lc_Control In ao_Container
        Select Case UCase(TypeName(lc_Control))
        Case "TABSTRIP", "TEXTBOX", "LABEL", "FRAME", "COMMANDBUTTON", _
              "LISTVIEW", "CHECKBOX", "OPTIONBUTTON", _
              "ARMCHECKVIEW", "ARMTREEVIEW", "ARMGRID", "ARMCOMBOBOX", "ARMCHECKVIEW0", "ARMPICKER"
            lc_Control.Font.Name = "Arial"
            lc_Control.Font.Charset = ll_Charset
        Case "A_SEEK", "A_SRCHTXT"
            lc_Control.Charset = ll_Charset
        End Select
    Next
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("ChangeCharset")
End Sub

' Load the labels of a containers
Private Sub LoadLabels(ByRef ao_Armdb As ArmDb, ByRef ao_Container As Object, ByVal as_ScreenName As String, ByVal as_Language As String)
Dim lo_Control As Control   ' A control of the container
Dim li_Idx As Integer, li_Count As Integer
Dim li_Label As Integer      ' A label idx
Dim ls_Request As String
Dim lsa_ControlTag() As String
    
    On Error GoTo Trace_Err

    ls_Request = "exec screen_csts '" & as_ScreenName & "','" & as_Language & "'"
    ml_labels = OpenSQLSafe(ao_Armdb, ls_Request)
    
    If ml_labels = 0 Then
        Exit Sub
    End If
    
    On Error GoTo WithoutTag
    If ao_Container.Tag <> "" Then
        li_Label = ao_Armdb.Find(ml_labels, "FIELD_NAME", ao_Container.Tag, , 1)
        If li_Label >= 0 Then
            ao_Container.Caption = ao_Armdb.GetFields(ml_labels, "LOCAL_TEXT")
        End If
    End If
WithoutTag:
    
    On Error GoTo Trace_Err
    
    ' Iterate the container for loading the label of each element which has defined a tag
    For Each lo_Control In UserControl.Controls
        If HasContainer(lo_Control, ao_Container) Then
            Select Case UCase(TypeName(lo_Control))
                Case UCase("TabStrip") ' Component is a tabstrip, we load the caption of each tab defined
                    Dim lo_Tbs
                    Set lo_Tbs = lo_Control ' Cast for use of intellisense
                    li_Count = lo_Tbs.Tabs.Count
                    For li_Idx = 1 To li_Count
                        If lo_Tbs.Tabs(li_Idx).Tag <> "" Then
                            li_Label = ao_Armdb.Find(ml_labels, "FIELD_NAME", lo_Tbs.Tabs(li_Idx).Tag, , 1)
                            If li_Label >= 0 Then
                                lo_Tbs.Tabs(li_Idx).Caption = ao_Armdb.GetFields(ml_labels, "LOCAL_TEXT")
                            End If
                        End If
                    Next
                    Set lo_Tbs = Nothing
                
                Case UCase("ListView") ' Component is a listview, we load the caption of each columns
                    Dim lo_ListView As ListView
                    Set lo_ListView = lo_Control
                    li_Count = lo_ListView.ColumnHeaders.Count
                    For li_Idx = 1 To li_Count
                        If lo_ListView.ColumnHeaders(li_Idx).Tag <> "" Then
                            li_Label = ao_Armdb.Find(ml_labels, "FIELD_NAME", lo_ListView.ColumnHeaders(li_Idx).Tag, , 1)
                            If li_Label >= 0 Then
                                lo_ListView.ColumnHeaders(li_Idx).Text = ao_Armdb.GetFields(ml_labels, "LOCAL_TEXT")
                            End If
                        End If
                    Next
                    Set lo_ListView = Nothing
            
                Case UCase("TextBox")  ' Component is a textbox
                    Dim lo_TextBox As TextBox
                    Set lo_TextBox = lo_Control
                    If lo_TextBox.Tag <> "" Then
                        lsa_ControlTag = Split(lo_Control.Tag, SEP)
                        li_Label = ao_Armdb.Find(ml_labels, "FIELD_NAME", lsa_ControlTag(0), , 1)
                        If li_Label >= 0 Then
                            lo_TextBox.Text = ao_Armdb.GetFields(ml_labels, "LOCAL_TEXT")
                        End If
                    End If
                    Set lo_TextBox = Nothing
                
                Case UCase("Label"), UCase("Frame"), UCase("CommandButton"), UCase("CheckBox"), UCase("OptionButton")
                    If lo_Control.Tag <> "" Then
                        lsa_ControlTag = Split(lo_Control.Tag, SEP)
                        li_Label = ao_Armdb.Find(ml_labels, "FIELD_NAME", lsa_ControlTag(0), , 1)
                        If li_Label >= 0 Then
                            lo_Control.Caption = ao_Armdb.GetFields(ml_labels, "LOCAL_TEXT")
                        End If
                    End If
                Case UCase("ArmGrid")
                    li_Label = ao_Armdb.Find(ml_labels, "FIELD_NAME", lo_Control.Tag, , 1)
                    If li_Label >= 0 Then
                      Call lo_Control.LoadConstants(ptStatic, ao_Armdb.GetFields(ml_labels, "LOCAL_TEXT"), ctColumns)
                    End If
                    li_Label = ao_Armdb.Find(ml_labels, "FIELD_NAME", lo_Control.Tag & "_Title", , 1)
                    If li_Label >= 0 Then
                      lo_Control.Title = ao_Armdb.GetFields(ml_labels, "LOCAL_TEXT")
                    End If
                Case UCase("Menu")
                    If lo_Control.Name <> "" Then
                        li_Label = ao_Armdb.Find(ml_labels, "FIELD_NAME", lo_Control.Name, , 1)
                        If li_Label >= 0 Then
                            lo_Control.Caption = ao_Armdb.GetFields(ml_labels, "LOCAL_TEXT")
                        End If
                    End If
            End Select
        End If
    Next
    
    Exit Sub
    
Trace_Err:
End Sub

Function MsgText(ByVal aID As Long, ByVal aLang As String, ByVal aDefault As String, Optional ByVal aInfo As Variant) As String
On Error GoTo ErrHandler

Const DB_REQ As String = "SELECT message_text FROM error_message WHERE msgid = $id$ AND Language_code = '$lang$'"

    MsgText = ""
    
    Dim lRequest As String
    lRequest = ReplacePlaceHolder(DB_REQ, "$id$", aID)
    lRequest = ReplacePlaceHolder(lRequest, "$lang$", aLang)
    Dim lData As Long
    
    lData = OpenSQLSafe(mo_Db, lRequest)
    
    Dim lBuffer As String
    lBuffer = mo_Db.GetFields(lData, "message_text")
    mo_Db.Close (lData)
    If lBuffer = "" Then lBuffer = aDefault
    
    Dim li_Idx As Integer
    If Not IsMissing(aInfo) Then
        For li_Idx = 0 To UBound(aInfo)
            lBuffer = Replace(lBuffer, aInfo(li_Idx, 0), aInfo(li_Idx, 1), , , vbTextCompare)
        Next li_Idx
    End If
    
    
    MsgText = lBuffer
    Exit Function
ErrHandler:
    mo_Db.Close (lData)
    Call MsgBox("Connection failure accessing message information.", vbCritical, App.Title & " " & App.Major & "." & App.Minor & "." & App.Revision)
    MsgText = aDefault
End Function


Private Function InitGridByName(ByRef ao_grid As ArmGrid, ByVal as_Name As String) As Variant
On Error GoTo ErrHandler
Const C_GRIDINIT_REQ As String = "EXEC A_Grid_sel '$GRD_Name$', '$LANGUAGE$'"

    Dim ll_Cursor As Long
    Dim ls_Req As String
    Dim lv_retVal As Variant
    
    ao_grid.UnBound = False
    ls_Req = Replace(C_GRIDINIT_REQ, "$GRD_Name$", as_Name)
    ls_Req = Replace(ls_Req, "$LANGUAGE$", ms_Language_Code)
    ll_Cursor = OpenSQLSafe(mo_Db, ls_Req)
    If mo_Db.RowCount(ll_Cursor) > 0 Then
    
        ' Initialize the grid
        ao_grid.FetchAll = (mo_Db.GetFields(ll_Cursor, "GRD_FetchAll") = "X")
        ao_grid.FreeSelect = (mo_Db.GetFields(ll_Cursor, "GRD_FreeSelect") = "X")
        ao_grid.MultiSelect = (mo_Db.GetFields(ll_Cursor, "GRD_MultiSelect") = "X")
        ao_grid.ExportTitles = (mo_Db.GetFields(ll_Cursor, "GRD_ExportTitles") = "X")
        ao_grid.AllowExcelExport = (mo_Db.GetFields(ll_Cursor, "AllowExcelExport") = "X")
        ao_grid.AllowPrint = (mo_Db.GetFields(ll_Cursor, "AllowPrint") = "X")
        ao_grid.AllowSort = (mo_Db.GetFields(ll_Cursor, "GRD_AllowSort") = "X")
        ao_grid.AllowMultiSort = (mo_Db.GetFields(ll_Cursor, "GRD_AllowMultisort") = "X")
        ao_grid.AutoColumns = (mo_Db.GetFields(ll_Cursor, "GRD_AutoColumns") = "X")
        ao_grid.AutoColumnWidth = mo_Db.GetFields(ll_Cursor, "GRD_AutoColumnWidth")
        ao_grid.FiltersEnabled = (mo_Db.GetFields(ll_Cursor, "GRD_FiltersEnabled") = "X")
        ao_grid.WordWrap = (mo_Db.GetFields(ll_Cursor, "GRD_WordWrap") = "X")
        
        ao_grid.Title = mo_Db.GetFields(ll_Cursor, "GRD_Title")
        
        lv_retVal = CVar(Array(mo_Db.GetFields(ll_Cursor, "GRD_Title"), _
                               mo_Db.GetFields(ll_Cursor, "GRD_Request"), _
                               mo_Db.GetFields(ll_Cursor, "GRD_GridCountRequest"), _
                               mo_Db.GetFields(ll_Cursor, "GRD_MasterDetailRequest")))
        
        Dim ls_columnDef() As String
        ReDim ls_columnDef(mo_Db.RowCount(ll_Cursor) - 1) As String
        
        Dim ll_i As Long
        ll_i = 0
        While Not mo_Db.EOF(ll_Cursor)
            ls_columnDef(ll_i) = Join(Array(mo_Db.GetFields(ll_Cursor, "COL_ColumnName"), _
                                            mo_Db.GetFields(ll_Cursor, "COL_ColumnWidth"), _
                                            mo_Db.GetFields(ll_Cursor, "COL_Key"), _
                                            mo_Db.GetFields(ll_Cursor, "COL_ColumnFieldName"), _
                                            mo_Db.GetFields(ll_Cursor, "COL_ColumnTitle"), _
                                            mo_Db.GetFields(ll_Cursor, "FieldType"), _
                                            mo_Db.GetFields(ll_Cursor, "COL_FormatString"), _
                                            mo_Db.GetFields(ll_Cursor, "Alignemnt"), _
                                            IIf(mo_Db.GetFields(ll_Cursor, "COL_Group") = "X", "TRUE", "FALSE"), _
                                            mo_Db.GetFields(ll_Cursor, "COL_TotalVisible"), _
                                            mo_Db.GetFields(ll_Cursor, "Measure"), _
                                            mo_Db.GetFields(ll_Cursor, "COL_LinkedDataRequest") _
                                    ), SEP)
            Call mo_Db.Next(ll_Cursor)
            ll_i = ll_i + 1
        Wend
        
        If Not ao_grid.SetColumns(ls_columnDef) Then
            Call Err.Raise(CompFncFailed, "ao_grid.SetColumns")
        End If
    Else
        Call Err.Raise(ArmErr.DBCnxFailed, "ao_grid", "Grid missing or column definition. GRD_Name=" & as_Name)
    End If
    
    Call mo_Db.Close(ll_Cursor)
    ll_Cursor = 0
    InitGridByName = lv_retVal
    Exit Function
ErrHandler:
    If ll_Cursor <> 0 Then
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    End If
    Call ErrorHandler("InitGridByName()")
End Function


Private Sub Form_Resize()
    Dim ll_height As Long
    Dim ll_Width As Long
    ll_height = UserControl.Height
    ll_Width = UserControl.Width
    
    If ll_height < 6735 Then ll_height = 6735
    If ll_Width < 9390 Then ll_Width = 9390
    
    Call restoreMaximized
    
    Call frm_filters.Move(0, 0, frm_filters.Width, ll_height)
    Call trw_navig.Move(100, tb_reps.Top + tb_reps.Height + 50, frm_filters.Width - 200, frm_filters.Height - (tb_reps.Top + tb_reps.Height + 150))
    Call frm_Main.Move(trw_navig.Left + trw_navig.Width + 200, frm_filters.Top)
    frm_Main.Width = ll_Width - frm_Main.Left - 150
    frm_Main.Height = frm_filters.Height
    
    tb_entity.Left = 100
    tb_entity.Width = frm_Main.Width - 200
    tb_entity.Height = frm_Main.Height - tb_entity.Top - 100
    
    Dim lo_Frm As Frame
    For Each lo_Frm In frm_detail
        Call lo_Frm.Move(150, 1050, tb_entity.Width - 150, tb_entity.Height - 450)
    Next
    
    ' detail projects
    Dim lo_chart As MSChart
    For Each lo_chart In mch_project
        lo_chart.Width = Int((frm_detail(0).Width - 60) / 2)
        lo_chart.Height = Int((frm_detail(0).Height - grd_detail(0).Height - 280) / 2)
    Next
    mch_project(0).Left = 30
    mch_project(1).Left = mch_project(0).Width + 30
    mch_project(2).Left = 30
    mch_project(3).Left = mch_project(2).Width + 30
    
    mch_project(0).Top = 190
    mch_project(1).Top = 190
    mch_project(2).Top = mch_project(0).Height + mch_project(0).Top
    mch_project(3).Top = mch_project(1).Height + mch_project(1).Top
    
    Call grd_detail(0).Move(60, mch_project(2).Height + mch_project(2).Top, frm_detail(0).Width - 420)
    Call img_Max(0).Move(grd_detail(0).Left + grd_detail(0).Width, grd_detail(0).Top)
    Call img_Min(0).Move(img_Max(0).Left, mch_project(1).Top)
    
    
    ' detail customers
    grd_detail(1).Height = grd_detail(0).Height
    For Each lo_chart In mch_customer
        lo_chart.Width = Int((frm_detail(1).Width - 60) / 2)
        lo_chart.Height = Int((frm_detail(1).Height - grd_detail(1).Height - 280) / 2)
    Next
    mch_customer(0).Left = 30
    mch_customer(1).Left = mch_customer(0).Width + 30
    mch_customer(2).Left = 30
    
    mch_customer(0).Top = 190
    mch_customer(1).Top = 190
    mch_customer(2).Top = mch_customer(0).Height + mch_customer(0).Top
    
    Call grd_detail(1).Move(60, mch_customer(2).Height + mch_customer(2).Top, frm_detail(1).Width - 420)
    Call img_Max(1).Move(grd_detail(1).Left + grd_detail(1).Width, grd_detail(1).Top)
    Call img_Min(1).Move(img_Max(1).Left, mch_customer(1).Top)

    ' detail general
    grd_detail(2).Height = grd_detail(0).Height
    For Each lo_chart In mch_general
        lo_chart.Width = Int((frm_detail(2).Width - 60) / 2)
        lo_chart.Height = Int((frm_detail(2).Height - grd_detail(2).Height - 280) / 2)
    Next
    mch_general(0).Left = 30
    mch_general(1).Left = mch_general(0).Width + 30
    mch_general(2).Left = 30
    mch_general(3).Left = mch_general(1).Width + 30
    
    mch_general(0).Top = 190
    mch_general(1).Top = 190
    mch_general(2).Top = mch_general(0).Height + mch_general(0).Top
    mch_general(3).Top = mch_general(1).Height + mch_general(1).Top
    
    Call grd_detail(2).Move(60, mch_general(2).Height + mch_general(2).Top, frm_detail(2).Width - 420)
    Call img_Max(2).Move(grd_detail(2).Left + grd_detail(2).Width, grd_detail(2).Top)
    Call img_Min(2).Move(img_Max(2).Left, mch_general(1).Top)

    
'    grd_list.Top = tlb_main.Top + tlb_main.Height
'    grd_list.Width = Me.Width - 120
'    If Me.Height - (tlb_main.Top + tlb_main.Height) > 600 Then
'        grd_list.Height = Me.Height - (tlb_main.Top + tlb_main.Height) - 430
'    Else
'        grd_list.Height = 200
'    End If
    
'    frm_projectDetailCalc.Left = grd_list.Left
'    frm_projectDetailCalc.Width = grd_list.Width
'    lbl_projectDetailCalculation.Left = 60
'    lbl_projectDetailCalculation.Width = frm_projectDetailCalc.Width - 120
End Sub

Private Sub btn_Quit_Click()
    RaiseEvent quit
End Sub

Private Sub btn_refresh_Click()
On Error GoTo ErrHandler
    Call LockScreen(True)
    Call CloseCursors
    Call RefreshChart
    Call LockScreen(False)
    Exit Sub
ErrHandler:
    Call LockScreen(False)
    Call ErrorMessage("tb_entity_Click")
End Sub

Private Sub cbo_ChartType_ComboItemSelected()
On Error GoTo ErrHandler
    Call LockScreen(True)
    
    Dim lo_newChrtType As VtChChartType
    Dim lo_chart As MSChart
    lo_newChrtType = cbo_chartType.SelectedItem.Key
    
    For Each lo_chart In mch_project
        lo_chart.ChartType = lo_newChrtType
    Next
    For Each lo_chart In mch_customer
        lo_chart.ChartType = lo_newChrtType
    Next
    
    For Each lo_chart In mch_general
        lo_chart.ChartType = lo_newChrtType
    Next

    Call LockScreen(False)
    Exit Sub
ErrHandler:
    Call LockScreen(False)
    Call ErrorMessage("tb_entity_Click")
End Sub

Private Sub img_Max_Click(Index As Integer)
    ' DO NOT RELY ON index!!!!!!
    
    Dim ll_detIdx As Long
    ll_detIdx = DetailIndex(tb_entity.SelectedItem.Key)
    
    grd_detail(mo_detail(ll_detIdx).detGrdIndex).Top = img_Min(mo_detail(ll_detIdx).detGrdIndex).Top
    grd_detail(mo_detail(ll_detIdx).detGrdIndex).Height = grd_detail(mo_detail(ll_detIdx).detGrdIndex).Container.Height - img_Min(mo_detail(ll_detIdx).detGrdIndex).Top - 90
    
    ' hide charts
    Dim lo_chart As MSChart
    For Each lo_chart In mo_detail(ll_detIdx).chartArr
        lo_chart.Visible = False
    Next
    
    img_Min(mo_detail(ll_detIdx).detGrdIndex).Visible = True
    img_Max(mo_detail(ll_detIdx).detGrdIndex).Visible = False
End Sub

Private Sub img_Min_Click(Index As Integer)
    ' DO NOT RELY ON index!!!!!!
    
    Dim ll_detIdx As Long
    ll_detIdx = DetailIndex(tb_entity.SelectedItem.Key)
    
    grd_detail(mo_detail(ll_detIdx).detGrdIndex).Top = img_Max(mo_detail(ll_detIdx).detGrdIndex).Top
    grd_detail(mo_detail(ll_detIdx).detGrdIndex).Height = 1455

    ' hide charts
    Dim lo_chart As MSChart
    For Each lo_chart In mo_detail(ll_detIdx).chartArr
        lo_chart.Visible = True
    Next
    img_Min(mo_detail(ll_detIdx).detGrdIndex).Visible = False
    img_Max(mo_detail(ll_detIdx).detGrdIndex).Visible = True
End Sub

Private Sub maximizeEntity(ByRef ao_ent As Object)
    Set mo_maxEnt.ent = ao_ent
    mo_maxEnt.Left = ao_ent.Left
    mo_maxEnt.Top = ao_ent.Top
    mo_maxEnt.Width = ao_ent.Width
    mo_maxEnt.Height = ao_ent.Height
    Call ao_ent.Move(0, 0, ao_ent.Container.Width, ao_ent.Container.Height)
    ao_ent.Zorder

End Sub
Private Sub restoreMaximized()
    If Not mo_maxEnt.ent Is Nothing Then
        Call mo_maxEnt.ent.Move(mo_maxEnt.Left, mo_maxEnt.Top, mo_maxEnt.Width, mo_maxEnt.Height)
        Set mo_maxEnt.ent = Nothing
    End If
End Sub

' ********************** customers chart event handlers *******************************
Private Sub mch_customer_AxisLabelSelected(Index As Integer, axisID As Integer, AxisIndex As Integer, labelSetIndex As Integer, LabelIndex As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_customer_AxisSelected(Index As Integer, axisID As Integer, AxisIndex As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_customer_PlotSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_customer_LegendSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_customer_ChartSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_customer_GotFocus(Index As Integer)
    mch_customer(Index).Backdrop.Frame.Style = VtFrameStyleSingleLine
    mch_customer(Index).Backdrop.Shadow.Style = VtShadowStyleDrop
End Sub

Private Sub mch_customer_LostFocus(Index As Integer)
    mch_customer(Index).Backdrop.Frame.Style = VtFrameStyleNull
    mch_customer(Index).Backdrop.Shadow.Style = VtShadowStyleNull
End Sub

Private Sub mch_customer_PointActivated(Index As Integer, Series As Integer, DataPoint As Integer, MouseFlags As Integer, Cancel As Integer)
On Error GoTo ErrHandler
'    Call LockScreen(True)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    Call FillGrid(grd_detail(lt_detail.detGrdIndex), lt_detail.Key, lt_detail.detGrdRequest(1), mch_customer(Index), Series, DataPoint)
'    Call LockScreen(False)
    Exit Sub
ErrHandler:
'    Call LockScreen(False)
    Call ErrorMessage("mch_customer_PointActivated")
End Sub

Private Sub mch_customer_PointSelected(Index As Integer, Series As Integer, DataPoint As Integer, MouseFlags As Integer, Cancel As Integer)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    grd_detail(lt_detail.detGrdIndex).ClearGrid
    Call DefineDetailGrid(mch_customer(Index), Series, DataPoint)
End Sub

Private Sub mch_customer_SeriesActivated(Index As Integer, Series As Integer, MouseFlags As Integer, Cancel As Integer)
On Error GoTo ErrHandler
'    Call LockScreen(True)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    Call FillGrid(grd_detail(lt_detail.detGrdIndex), lt_detail.Key, lt_detail.detGrdRequest(1), mch_project(Index), Series, 0)
'    Call LockScreen(False)
    Exit Sub
ErrHandler:
'    Call LockScreen(False)
    Call ErrorMessage("mch_project_SeriesSelected")
End Sub

Private Sub mch_customer_SeriesSelected(Index As Integer, Series As Integer, MouseFlags As Integer, Cancel As Integer)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    grd_detail(lt_detail.detGrdIndex).ClearGrid
    Call DefineDetailGrid(mch_customer(Index), Series, -1)
End Sub

Private Sub mch_customer_TitleSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    If mo_maxEnt.ent Is Nothing Then
        mch_customer(Index).Backdrop.Frame.Style = VtFrameStyleSingleLine
        mch_customer(Index).Backdrop.Shadow.Style = VtShadowStyleDrop
        Call maximizeEntity(mch_customer(Index))
    Else
        Call restoreMaximized
    End If
    Cancel = True
End Sub
' ********************** customers chart event handlers ******************************* END

' ********************** project chart event handlers *******************************
Private Sub mch_project_AxisLabelSelected(Index As Integer, axisID As Integer, AxisIndex As Integer, labelSetIndex As Integer, LabelIndex As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_project_AxisSelected(Index As Integer, axisID As Integer, AxisIndex As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_project_PlotSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_project_LegendSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_project_ChartSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_project_GotFocus(Index As Integer)
    mch_project(Index).Backdrop.Frame.Style = VtFrameStyleSingleLine
    mch_project(Index).Backdrop.Shadow.Style = VtShadowStyleDrop
End Sub

Private Sub mch_project_LostFocus(Index As Integer)
    mch_project(Index).Backdrop.Frame.Style = VtFrameStyleNull
    mch_project(Index).Backdrop.Shadow.Style = VtShadowStyleNull
End Sub

Private Sub mch_project_PointActivated(Index As Integer, Series As Integer, DataPoint As Integer, MouseFlags As Integer, Cancel As Integer)
On Error GoTo ErrHandler
'    Call LockScreen(True)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    Call FillGrid(grd_detail(lt_detail.detGrdIndex), lt_detail.Key, lt_detail.detGrdRequest(1), mch_project(Index), Series, DataPoint)
'    Call LockScreen(False)
    Exit Sub
ErrHandler:
'    Call LockScreen(False)
    Call ErrorMessage("mch_project_PointSelected")
End Sub

Private Sub mch_project_PointSelected(Index As Integer, Series As Integer, DataPoint As Integer, MouseFlags As Integer, Cancel As Integer)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    grd_detail(lt_detail.detGrdIndex).ClearGrid
    Call DefineDetailGrid(mch_project(Index), Series, DataPoint)
End Sub

Private Sub mch_project_SeriesActivated(Index As Integer, Series As Integer, MouseFlags As Integer, Cancel As Integer)
On Error GoTo ErrHandler
'    Call LockScreen(True)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    Call FillGrid(grd_detail(lt_detail.detGrdIndex), lt_detail.Key, lt_detail.detGrdRequest(1), mch_project(Index), Series, 0)
'    Call LockScreen(False)
    Exit Sub
ErrHandler:
'    Call LockScreen(False)
    Call ErrorMessage("mch_project_SeriesSelected")
End Sub

Private Sub mch_project_SeriesSelected(Index As Integer, Series As Integer, MouseFlags As Integer, Cancel As Integer)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    grd_detail(lt_detail.detGrdIndex).ClearGrid
    Call DefineDetailGrid(mch_project(Index), Series, -1)
End Sub

Private Sub mch_project_TitleSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    If mo_maxEnt.ent Is Nothing Then
        mch_project(Index).Backdrop.Frame.Style = VtFrameStyleSingleLine
        mch_project(Index).Backdrop.Shadow.Style = VtShadowStyleDrop
        Call maximizeEntity(mch_project(Index))
    Else
        Call restoreMaximized
    End If
    Cancel = True
End Sub

' ********************** project chart event handlers ******************************* END

' ********************** general chart event handlers *******************************
Private Sub mch_general_AxisLabelSelected(Index As Integer, axisID As Integer, AxisIndex As Integer, labelSetIndex As Integer, LabelIndex As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_general_AxisSelected(Index As Integer, axisID As Integer, AxisIndex As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_general_PlotSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_general_LegendSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_general_ChartSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    Cancel = True
End Sub

Private Sub mch_general_TitleSelected(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    If mo_maxEnt.ent Is Nothing Then
        Call maximizeEntity(mch_general(Index))
    Else
        Call restoreMaximized
    End If
    Cancel = True
End Sub

Private Sub mch_general_GotFocus(Index As Integer)
    mch_general(Index).Backdrop.Frame.Style = VtFrameStyleSingleLine
    mch_general(Index).Backdrop.Shadow.Style = VtShadowStyleDrop
End Sub

Private Sub mch_general_LostFocus(Index As Integer)
    mch_general(Index).Backdrop.Frame.Style = VtFrameStyleNull
    mch_general(Index).Backdrop.Shadow.Style = VtShadowStyleNull
End Sub

Private Sub mch_general_ChartActivated(Index As Integer, MouseFlags As Integer, Cancel As Integer)
    If mo_maxEnt.ent Is Nothing Then
        Call maximizeEntity(mch_general(Index))
    Else
        Call restoreMaximized
    End If
End Sub

Private Sub mch_general_SeriesActivated(Index As Integer, Series As Integer, MouseFlags As Integer, Cancel As Integer)
On Error GoTo ErrHandler
'    Call LockScreen(True)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    Call FillGrid(grd_detail(lt_detail.detGrdIndex), lt_detail.Key, lt_detail.detGrdRequest(1), mch_general(Index), Series, 0)
'    Call LockScreen(False)
    Exit Sub
ErrHandler:
'    Call LockScreen(False)
    Call ErrorMessage("mch_general_SeriesSelected")
End Sub

Private Sub mch_general_SeriesSelected(Index As Integer, Series As Integer, MouseFlags As Integer, Cancel As Integer)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    grd_detail(lt_detail.detGrdIndex).ClearGrid
    Call DefineDetailGrid(mch_general(Index), Series, -1)
End Sub

Private Sub mch_general_PointActivated(Index As Integer, Series As Integer, DataPoint As Integer, MouseFlags As Integer, Cancel As Integer)
On Error GoTo ErrHandler
'    Call LockScreen(True)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    Call FillGrid(grd_detail(lt_detail.detGrdIndex), lt_detail.Key, lt_detail.detGrdRequest(1), mch_general(Index), Series, DataPoint)
'    Call LockScreen(False)
    Exit Sub
ErrHandler:
'    Call LockScreen(False)
    Call ErrorMessage("mch_general_PointSelected")
End Sub

Private Sub mch_general_PointSelected(Index As Integer, Series As Integer, DataPoint As Integer, MouseFlags As Integer, Cancel As Integer)
    Dim lt_detail As CapActDetail
    lt_detail = Detail(tb_entity.SelectedItem.Key)
    grd_detail(lt_detail.detGrdIndex).ClearGrid
    Call DefineDetailGrid(mch_general(Index), Series, DataPoint)
End Sub

' ********************** general chart event handlers ******************************* END

Private Sub tb_entity_Click()
On Error GoTo ErrHandler
    If mb_InternalInit Then Exit Sub
    
    Dim ll_selfrmInd As Long
    Dim lo_Frm As Frame
    Call LockScreen(True)
    Call restoreMaximized
    
    ll_selfrmInd = Detail(tb_entity.SelectedItem.Key).frmIndex
    For Each lo_Frm In frm_detail
        lo_Frm.Visible = (ll_selfrmInd = lo_Frm.Index)
    Next
    
    Call RefreshChart
    
    Call LockScreen(False)
    Exit Sub
ErrHandler:
    Call LockScreen(False)
    Call ErrorMessage("tb_entity_Click")
End Sub

Private Sub tb_period_Click()
On Error GoTo ErrHandler
    If mb_InternalInit Then Exit Sub
    
    Call LockScreen(True)
    If Not RefreshChart Then
        Dim ll_detIdx As Long
        ll_detIdx = DetailIndex(tb_entity.SelectedItem.Key)
        Call grd_detail(mo_detail(ll_detIdx).detGrdIndex).ClearGrid
        Call img_Min_Click(-1)                           ' do not care about index
    End If
    Call LockScreen(False)
    Exit Sub
ErrHandler:
    Call LockScreen(False)
    Call ErrorMessage("tb_period_Click")
End Sub

Private Function RefreshChart() As Boolean
On Error GoTo ErrHandler
    RefreshChart = False
    Call restoreMaximized

    Select Case tb_period.SelectedItem.Key
    Case "W"
        Call SetRowLabels(tb_period.SelectedItem.Key, IIf(DatePart("yyyy", Now, vbMonday) = CLng(cbo_year.SelectedItem.Key), DatePart("ww", Now, vbMonday), DatePart("ww", "31/12/" & cbo_year.SelectedItem.Key, vbMonday)), 1)
    Case "M"
        Call SetRowLabels(tb_period.SelectedItem.Key, IIf(DatePart("yyyy", Now, vbMonday) = CLng(cbo_year.SelectedItem.Key), DatePart("m", Now, vbMonday), 12), 1)
    Case "Q"
        Call SetRowLabels(tb_period.SelectedItem.Key, IIf(DatePart("yyyy", Now, vbMonday) = CLng(cbo_year.SelectedItem.Key), DatePart("q", Now, vbMonday), 4), 1)
    Case "Y"
        Call SetRowLabels(tb_period.SelectedItem.Key, 1, cbo_year.SelectedItem.Key)
    End Select
    Dim lb_return As Boolean
    lb_return = FillTab
    tb_entity.Visible = True
    RefreshChart = lb_return
    Exit Function
ErrHandler:
    Call ErrorHandler("RefreshChart")
End Function

Private Sub tb_reps_Click()
On Error GoTo ErrHandler
    Call LockScreen(True)
    Call LoadTreeView
    Call LockScreen(False)
    Exit Sub
ErrHandler:
    Call LockScreen(False)
    Call ErrorMessage("tb_entity_Click")
End Sub

Private Sub SetRowLabels(ByVal as_prefix As String, ByVal al_valCount As Long, ByVal al_current_val As Long)
On Error GoTo ErrHandler
    Call SetChartRowLabels(mch_project, as_prefix, al_valCount, al_current_val)
    Call SetChartRowLabels(mch_customer, as_prefix, al_valCount, al_current_val)
    Call SetChartRowLabels(mch_general, as_prefix, al_valCount, al_current_val)
    Exit Sub
ErrHandler:
    Call ErrorHandler("SetRowWeeksLabels")
End Sub

Private Sub SetChartRowLabels(ByRef ao_chartArr As Object, ByVal as_prefix As String, ByVal al_valCount As Long, ByVal al_current_val As Long)
On Error GoTo ErrHandler
    Dim lo_chart As MSChart
    Dim ll_val As Long
    Dim ll_labelVal As Long
    For Each lo_chart In ao_chartArr
        lo_chart.RowCount = al_valCount
        ll_labelVal = al_current_val
        For ll_val = 1 To al_valCount
            lo_chart.Row = ll_val
            lo_chart.RowLabel = as_prefix & ll_labelVal
            ll_labelVal = ll_labelVal + 1
        Next
    Next

    Exit Sub
ErrHandler:
    Call ErrorHandler("SetChartRowLabels")
End Sub

Private Sub ClearAll()
On Error GoTo ErrHandler
    CloseCursors
    Dim ll_tabIdx As Long
    For ll_tabIdx = LBound(mo_detail) To UBound(mo_detail)
        Call ClearChart(mo_detail(ll_tabIdx).chartArr)
    Next
    Dim lo_Grid As ArmGrid
    For Each lo_Grid In grd_detail
        Call lo_Grid.ClearGrid
    Next
    Exit Sub
ErrHandler:
    Call ErrorHandler("ClearAll")
End Sub

Private Sub ClearChart(ByRef ao_chartArr As Object)
On Error GoTo ErrHandler
    Dim lo_chart As MSChart
    Dim ll_Col As Long
    Dim ll_Row As Long
    For Each lo_chart In ao_chartArr
        For ll_Col = 1 To lo_chart.ColumnCount
            For ll_Row = 1 To lo_chart.RowCount
                lo_chart.Row = ll_Row
                lo_chart.Column = ll_Col
                lo_chart.Data = 0
            Next
        Next
    Next
    Exit Sub
ErrHandler:
    Call ErrorHandler("ClearChart")
End Sub

Private Function ReplacePlaceholders(ByVal as_req As String) As String
On Error GoTo ErrHandler

    Dim lo_Node As Node
    
    Set lo_Node = trw_navig.SelectedItem
    While Not lo_Node Is Nothing
        as_req = ReplacePlaceHolder(as_req, "$" & trw_navig.NodeDataNames(trw_navig.NodeInfo(lo_Node).ml_Level) & "$", Trim(trw_navig.NodeInfo(lo_Node).IDValue))
        Set lo_Node = lo_Node.Parent
    Wend
    as_req = ReplacePlaceHolder(as_req, "'$BMK_CODE$'", "NULL")
    as_req = ReplacePlaceHolder(as_req, "'$REP_CODE$'", "NULL")
    
    as_req = ReplacePlaceHolder(as_req, "$LANG$", ms_Language_Code)
    as_req = ReplacePlaceHolder(as_req, "$YEAR$", cbo_year.SelectedItem.Key)
    as_req = ReplacePlaceHolder(as_req, "$ALLREPS$", tb_reps.SelectedItem.Key)
    
    Select Case tb_period.SelectedItem.Key
        Case "W"
            as_req = ReplacePlaceHolder(as_req, "$PERIOD$", "Week")
        Case "M"
            as_req = ReplacePlaceHolder(as_req, "$PERIOD$", "Month")
        Case "Q"
            as_req = ReplacePlaceHolder(as_req, "$PERIOD$", "Quarter")
        Case "Y"
            as_req = ReplacePlaceHolder(as_req, "$PERIOD$", "Year")
    End Select
    
    ReplacePlaceholders = as_req
    
    Exit Function
ErrHandler:
    Call ErrorHandler("ReplacePlaceholders")
End Function

Private Function FillTab() As Boolean
On Error GoTo ErrHandler
    FillTab = False
    Dim ll_detIdx As Long
    ll_detIdx = DetailIndex(tb_entity.SelectedItem.Key)
    
    Call ClearChart(mo_detail(ll_detIdx).chartArr)
    LockWindowUpdate 0
    frm_detail(mo_detail(ll_detIdx).frmIndex).Visible = True
    frm_detail(mo_detail(ll_detIdx).frmIndex).Refresh
    LockWindowUpdate UserControl.hwnd
    
    If Not CheckDataAuthorization Then
        MsgBox MsgText(1504, ms_Language_Code, "#You are not authorized to see these data")
        Exit Function
    End If
    
    Dim ll_i As Long
    Dim lb_dataReloaded As Boolean
    lb_dataReloaded = False
    For ll_i = LBound(mo_detail(ll_detIdx).ma_graphCusrsors) To UBound(mo_detail(ll_detIdx).ma_graphCusrsors)
        lb_dataReloaded = lb_dataReloaded Or FillChart(mo_detail(ll_detIdx).Key, mo_detail(ll_detIdx).ma_graphCusrsors(ll_i))
    Next
    
    If lb_dataReloaded Then
        ' we have to clean and minimize the grid
        Call grd_detail(mo_detail(ll_detIdx).detGrdIndex).ClearGrid
        Call img_Min_Click(-1)                           ' do not care about index
    End If
    FillTab = lb_dataReloaded
    Exit Function
ErrHandler:
    Call ErrorHandler("FillWeekTab")
End Function

Private Function GetPeriodIdx(ByVal as_period As String) As GCIndex
On Error GoTo ErrHandler
    Select Case as_period
        Case "W"
            GetPeriodIdx = G_Week
        Case "M"
            GetPeriodIdx = G_Month
        Case "Q"
            GetPeriodIdx = G_Quarter
        Case "Y"
            GetPeriodIdx = G_Year
    End Select

    Exit Function
ErrHandler:
    Call ErrorHandler("GetPeriodIdx")
End Function

Private Function FillChart(ByVal as_TabKey As String, ByRef at_chartCursor As GraphCursor) As Boolean
On Error GoTo ErrHandler
    FillChart = False
    Dim lt_PeriodIdx As GCIndex
    lt_PeriodIdx = GetPeriodIdx(tb_period.SelectedItem.Key)
    
    If at_chartCursor.a(lt_PeriodIdx) = 0 Then
        Dim ls_Req As String
        ls_Req = "EXEC CA_$ENT$_$GRAPH$_$PERIOD$ '$LANG$', $YEAR$, '$ALLREPS$', '$BMK_CODE$', '$REP_CODE$'"
        ls_Req = ReplacePlaceholders(ls_Req)
        ls_Req = ReplacePlaceHolder(ls_Req, "$GRAPH$", at_chartCursor.Chart.Tag)
        ls_Req = ReplacePlaceHolder(ls_Req, "$ENT$", as_TabKey)
        
        
        at_chartCursor.a(lt_PeriodIdx) = OpenSQLSafe(mo_Db, ls_Req)
        FillChart = True
    Else
        Call mo_Db.First(at_chartCursor.a(lt_PeriodIdx))
    End If
    Dim ll_Cursor As Long
    ll_Cursor = at_chartCursor.a(lt_PeriodIdx)
    
    Dim ll_Col As Long
    Dim lb_colStyle As Boolean
    
    lb_colStyle = (mo_Db.GetFieldType(ll_Cursor, "COL") <> 0)

    While Not mo_Db.EOF(ll_Cursor)
        Dim a As ArmDb
        at_chartCursor.Chart.Row = mo_Db.GetFields(ll_Cursor, "ROW")
        If lb_colStyle Then
            at_chartCursor.Chart.Column = mo_Db.GetFields(ll_Cursor, "COL")
            at_chartCursor.Chart.Data = mo_Db.GetFields(ll_Cursor, "VALUE")
        Else
            For ll_Col = LBound(at_chartCursor.ma_fields) To UBound(at_chartCursor.ma_fields)
                at_chartCursor.Chart.Column = ll_Col + 1
                at_chartCursor.Chart.Data = mo_Db.GetFields(ll_Cursor, at_chartCursor.ma_fields(ll_Col))
            Next
        End If
        
        Call mo_Db.Next(ll_Cursor)
    Wend
    LockWindowUpdate 0
    at_chartCursor.Chart.Container.Refresh
    LockWindowUpdate UserControl.hwnd
    Exit Function
ErrHandler:
    Call ErrorHandler("FillChart")
End Function

Private Sub FillGrid(ByRef ao_grid As ArmGrid, ByVal as_entKey As String, ByVal as_reqTmpl As String, ByRef ao_dataChart As MSChart, ByVal al_Col As Long, ByVal al_Row As Long)
On Error GoTo ErrHandler
    Dim ls_Req As String
    ls_Req = ReplacePlaceholders(as_reqTmpl)
    ls_Req = ReplacePlaceHolder(ls_Req, "$ENT$", as_entKey)
    ls_Req = ReplacePlaceHolder(ls_Req, "$GRAPH$", ao_dataChart.Tag)
    ls_Req = ReplacePlaceHolder(ls_Req, "$COL$", al_Col)
    
    Dim lo_Tab As Object
    For Each lo_Tab In tb_period.Tabs
        If lo_Tab.Selected Then
            ls_Req = ReplacePlaceHolder(ls_Req, "$" & lo_Tab.Key & "$", IIf(al_Row = 0, "NULL", al_Row))
        Else
            ls_Req = ReplacePlaceHolder(ls_Req, "$" & lo_Tab.Key & "$", "NULL")
        End If
    Next
    Screen.MousePointer = vbHourglass
    If Not mo_maxEnt.ent Is Nothing Then
        ' maximize the grid
        Call img_Max_Click(-1)      ' do not care abut index
    End If

    ao_grid.Requests = ls_Req
    Call ao_grid.Execute
    
    Screen.MousePointer = vbArrow
    Exit Sub
ErrHandler:
    Call ErrorHandler("FillGrid")
End Sub

Private Sub DefineDetailGrid(ByRef ao_dataChart As MSChart, ByVal al_Col As Long, ByVal al_Row As Long)
On Error GoTo ErrHandler
    Dim lo_detail As CapActDetail
    lo_detail = Detail(tb_entity.SelectedItem.Key)
    
    Dim ls_GridName As String
    ls_GridName = "CapActivity_" & lo_detail.Key & "_" & ao_dataChart.Tag
    Dim ls_Title As String
    Select Case lo_detail.Key
    Case "PRJ"
        Select Case ao_dataChart.Tag
            Case "G1", "G2"
                ls_GridName = ls_GridName & "_Col" & al_Col
            Case "G3"
                Select Case al_Col
                Case 1
                    ls_GridName = ls_GridName & "_Col" & al_Col
                Case 2, 3
                    ls_GridName = ls_GridName & "_Col2_3"
                End Select
            Case "G4"       ' nothing to add
        End Select
    Case "CST"
        ls_GridName = ls_GridName & "_Col" & al_Col
    Case "GEN"          ' nothing to add
    End Select
    ao_dataChart.Column = al_Col
    If al_Row <> -1 Then ao_dataChart.Row = al_Row
    
    lo_detail.detGrdRequest = InitGridByName(grd_detail(lo_detail.detGrdIndex), ls_GridName)
    Detail(tb_entity.SelectedItem.Key) = lo_detail
    
    ls_Title = ReplacePlaceHolder(lo_detail.detGrdRequest(0), "$CHART$", ao_dataChart.TitleText)
    ls_Title = ReplacePlaceHolder(ls_Title, "$TREE$", trw_navig.SelectedItem.Text)
    ls_Title = ReplacePlaceHolder(ls_Title, "$FILTER$", ao_dataChart.ColumnLabel)
    ls_Title = ReplacePlaceHolder(ls_Title, "$YEAR$", cbo_year.SelectedItem.Key)
    ls_Title = ReplacePlaceHolder(ls_Title, "$PERIOD$", IIf(al_Row = -1, "-", ao_dataChart.RowLabel))
    Call grd_detail(lo_detail.detGrdIndex).LoadConstants(ptStatic, ls_Title & SEP & "LEFT", ctTopGrid)
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("DefineGrid")
End Sub

Private Sub trw_navig_NodeClick(ByVal Node As MSComctlLib.Node)
On Error GoTo ErrHandler
    ClearAll
    Exit Sub
ErrHandler:
    Call ErrorMessage("trw_navig_NodeClick")
End Sub

Private Function CheckDataAuthorization() As Boolean
On Error GoTo ErrorHandler

Dim ll_Cursor   As Long
Dim ls_Req      As String

    CheckDataAuthorization = False
    If trw_navig.SelectedItem Is Nothing Then
        Exit Function
    End If
    
    Select Case trw_navig.SelectedItem.Tag.ml_Level
    Case 0
        ls_Req = "SELECT 1 FROM Cap_Rep_Login WHERE Login_Name = '" & ms_LoginName & "' AND Autho_Level = 'Markets' AND Autho_Values IS NULL"
        ll_Cursor = OpenSQLSafe(mo_Db, ls_Req)
        If mo_Db.RowCount(ll_Cursor) > 0 Then
            CheckDataAuthorization = True
        End If
        Call mo_Db.Close(ll_Cursor)
        Exit Function
    Case 1
        ls_Req = "SELECT Autho_Values FROM Cap_Rep_Login WHERE Login_Name = '" & ms_LoginName & "' AND Autho_Level = 'Markets'"
        ll_Cursor = OpenSQLSafe(mo_Db, ls_Req)
        If mo_Db.RowCount(ll_Cursor) > 0 Then
            If mo_Db.GetFields(ll_Cursor, 0) = "" Then
                CheckDataAuthorization = True
                Call mo_Db.Close(ll_Cursor)
                Exit Function
            End If
            If InStr(1, mo_Db.GetFields(ll_Cursor, "Autho_Values"), Trim(trw_navig.NodeInfo(trw_navig.SelectedItem).GetData(0))) > 0 Then
                CheckDataAuthorization = True
                Call mo_Db.Close(ll_Cursor)
                Exit Function
            End If
        End If
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    Case 2
        ls_Req = "SELECT Autho_Values FROM Cap_Rep_Login WHERE Login_Name = '" & ms_LoginName & "' AND Autho_Level = 'Markets'"
        ll_Cursor = OpenSQLSafe(mo_Db, ls_Req)
        If mo_Db.RowCount(ll_Cursor) > 0 Then
            If mo_Db.GetFields(ll_Cursor, 0) = "" Then
                CheckDataAuthorization = True
                Call mo_Db.Close(ll_Cursor)
                Exit Function
            End If
            If InStr(1, mo_Db.GetFields(ll_Cursor, "Autho_Values"), Trim(trw_navig.NodeInfo(trw_navig.SelectedItem.Parent).GetData(0))) > 0 Then
                CheckDataAuthorization = True
                Call mo_Db.Close(ll_Cursor)
                Exit Function
            End If
        End If
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
        
        ls_Req = "SELECT Autho_Values FROM Cap_Rep_Login WHERE Login_Name = '" & ms_LoginName & "' AND Autho_Level = 'Countries'"
        ll_Cursor = mo_Db.OpenSQL(ls_Req)
        If mo_Db.RowCount(ll_Cursor) > 0 Then
            If InStr(1, mo_Db.GetFields(ll_Cursor, "Autho_Values"), Trim(trw_navig.NodeInfo(trw_navig.SelectedItem).GetData(3))) > 0 Then
                CheckDataAuthorization = True
                Call mo_Db.Close(ll_Cursor)
                Exit Function
            End If
        End If
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    
        ls_Req = "SELECT Autho_Values FROM Cap_Rep_Login WHERE Login_Name = '" & ms_LoginName & "' AND Autho_Level = 'Users'"
        ll_Cursor = mo_Db.OpenSQL(ls_Req)
        If mo_Db.RowCount(ll_Cursor) > 0 Then
            If InStr(1, mo_Db.GetFields(ll_Cursor, "Autho_Values"), Trim(trw_navig.NodeInfo(trw_navig.SelectedItem).GetData(0))) > 0 Then
                CheckDataAuthorization = True
                Call mo_Db.Close(ll_Cursor)
                Exit Function
            End If
        End If
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    End Select

    Exit Function
    
ErrorHandler:
    If ll_Cursor > 0 Then
        Call mo_Db.Close(ll_Cursor)
        ll_Cursor = 0
    End If
    Call ErrorHandler(Extender.Name & ".CheckDataAuthorization")
End Function
