Attribute VB_Name = "Schedule"
Option Explicit

Enum enumSelTypes
    stLeftTopCell
    stCell
    stRow
    stCol
    stRange
    stNoSelected = -1
End Enum

'Bit information for the cell color change
Public Const FLAGS_STATUS_STANDARD = &H1    'Presence of default setting
Public Const FLAGS_STATUS_WEEKOFDAY = &H2   'Presence of date of the week setting
Public Const FLAGS_STATUS_DATE = &H4        'Presence of daily setting
Public Const FLAGS_STATUS_SELECTED = &H8    'Precence of selection condition
Public Const FLAGS_STATUS_GRAY = &H10       'The date other than this month
'Index of the color information arrangement
Public Const IDX_STATUS_STANDARD = 0        'Default setting
Public Const IDX_STATUS_SUN = 1             'Sunday
Public Const IDX_STATUS_MON = 2             'Monday
Public Const IDX_STATUS_TUE = 3             'Tuesday
Public Const IDX_STATUS_WED = 4             'Wednesday
Public Const IDX_STATUS_THU = 5             'Thursday
Public Const IDX_STATUS_FRI = 6             'Friday
Public Const IDX_STATUS_SAT = 7             'Saturday
Public Const IDX_STATUS_DATE = 8            'In the day setting
Public Const IDX_STATUS_SELECTED = 9        'Selection condition
Public Const IDX_STATUS_NORMAL = 10         'Usual cell
Public Const IDX_STATUS_FIXED = 11          'Fixation cell
Public Const IDX_STATUS_STANDARD_GRAY = 12  'Default setting (gray)
Public Const IDX_STATUS_SUN_GRAY = 13       'Sunday (gray)
Public Const IDX_STATUS_MON_GRAY = 14       'Monday (gray)
Public Const IDX_STATUS_TUE_GRAY = 15       'Tuesday (gray)
Public Const IDX_STATUS_WED_GRAY = 16       'Wednesday (gray)
Public Const IDX_STATUS_THU_GRAY = 17       'Thursday (gray)
Public Const IDX_STATUS_FRI_GRAY = 18       'Friday (gray)
Public Const IDX_STATUS_SAT_GRAY = 19       'Saturday (gray)
Public Const IDX_STATUS_DATE_GRAY = 20      'In the day setting (gray)
Public Const IDX_STATUS_SELECTED_GRAY = 21  'Selection condition (gray)
Public Const IDX_STATUS_NORMAL_GRAY = 22    'Usual cell (gray)
Public Const IDX_STATUS_DEFAULT = 23

Public Const IDX_STATUS_MAX = IDX_STATUS_DEFAULT + 1    'For the size decision of the arrangement


Public m_lCol As Long           'Date grid selection start col
Public m_lColSel As Long        'Date grid selection end col
Public m_lRow As Long           'Date grid selection start row
Public m_lRowSel As Long        'Date grid selection end row
Public m_iSelType As Integer    'Grid selection differentiaon

Public m_lCols As Long          'Date grid col number
Public m_lRows As Long          'Date grid row number

Public m_dtSelectDate As Date   'Date of the cell that is selected

Public m_tcFoldersEnglish As Variant
Public m_tcFolders As Variant
Public m_tcWkDays As Variant
Public m_tcRowCap As Variant
Public m_iWkDays() As Variant
Public m_tcMonth() As Variant

Public m_sScenarioOutputFolder As String
Public m_lStartCol As Long              'col position which becomes the date of the week position in the day
Public m_lStartRow As Long              'col position in the day
Public m_tToday As Date                 'the date in the day
Public m_tFirstDate As Date             'the date on 1st in this month

Public m_iFixedColsWidth As Integer     'width totals of all the fixation col
Public m_iFixedRowsHeight As Integer    'width totals of all the dixation row
Public m_iMinGridWidth As Integer       'grid minimum width
Public m_iMinGridHeight As Integer      'grid minimum height
Public m_rcLeftTopCell As RECT          'on the date grid left for the cell size preservation of the corner

Public m_iConfigFlags() As Integer      'Arrangement of the flag for the cell color change
Public m_bScheduleFlags() As Boolean

Public m_CellColorsFr() As Long
Public m_CellColorsBk() As Long

Public m_bLeftTopCellClicked As Boolean 'Presence of the click of the cell of the corner on the date grid left

Public ScheduleCount As Integer
Type SCHEDULE_INFO
    Index As Integer    'index of the treveiw
    Key As String       'key neme of the treeview
    Text As String      'text of the treeview
    Kind As Integer     '0:default, 1:date of week, 2:date, -1:no schedule
    Name As String      'schedule file name
End Type
Public ScheduleInfo(MAX_PJ * 2) As SCHEDULE_INFO

'----------------------------------------------------------
'  Function     : CalChange
'  Return value : None
'  Argument     : None
'  Feature      : Update the display of the calender
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub CalChange()

    Dim iSelType As Integer
    
    iSelType = CalGridGetSelType(m_lCol, m_lCol, m_lRow, m_lRow)
    If (iSelType <> stNoSelected) Then
        'Cancellation of an old selection day
        Call CalGridSetSelCur(m_lCol, m_lCol, m_lRow, m_lRow, False, False)
    End If
    
    'Request the col position, row position in the new selection day
    Dim lCol As Long, lRow As Long
    Dim iStartDayOfWeek As Integer
    
    iStartDayOfWeek = Weekday(m_dtSelectDate, vbSunday)
    lCol = CLng(m_iWkDays(iStartDayOfWeek))
    
    Dim iWeeks As Integer
    Dim iMod As Integer
    
    iWeeks = Day(m_dtSelectDate) \ 7
    iMod = Day(m_dtSelectDate) Mod 7
    
    If (lCol >= iMod) Then
        lRow = iWeeks + 1
    Else
        lRow = iWeeks + 2
    End If
    If ((lCol = 6) And (iMod = 0)) Then
        lRow = iWeeks
    End If
    
    'Set of the cursor in the selection day
    Call CalGridSetSelCur(lCol, lCol, lRow, lRow, True, True)
    m_lCol = lCol
    m_lColSel = m_lCol
    m_lRow = lRow
    m_lRowSel = m_lRow
    
    'refresh of the view -> drawing dated the day
    Call ViewRefresh
    Call CalGridDispDays
    
End Sub

'----------------------------------------------------------
'  Function     : CalGridDispDays
'  Return value : None
'  Argument     : None
'  Feature      : Display in the day to the grid
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub CalGridDispDays()

    Dim tDay As Date
    tDay = CalGridRowColToDate(1, 1)
    
    Dim lCol As Long, lRow As Long, lCols As Long, lRows As Long
    lCols = MainForm.MSFlexGrid1.Cols
    lRows = MainForm.MSFlexGrid1.Rows
    
    For lRow = 1 To lRows - 1
        For lCol = 1 To lCols - 1
            MainForm.MSFlexGrid1.TextMatrix(lRow, lCol) = ""
        Next lCol
    Next lRow
    
    Dim sTitle As String
    lRow = 0
    For lCol = 0 To lCols - 1
        sTitle = m_tcWkDays(lCol)
        MainForm.MSFlexGrid1.TextMatrix(lRow, lCol) = sTitle
    Next lCol
    
    For lRow = 1 To lRows - 1
        For lCol = 1 To lCols - 1
            Dim iDay As Integer, iMonth As Integer, sDay As String
            iDay = Day(tDay)
            iMonth = Month(tDay)
            If ((iDay = 1) Or ((lRow = 1) And (lCol = 1))) Then
                sDay = CStr(iMonth) & "/" & CStr(iDay)
            Else
                sDay = CStr(iDay)
            End If
            If ((Year(tDay) <> Year(m_tFirstDate)) Or (Month(tDay) <> Month(m_tFirstDate))) Then
               sDay = ""
            End If
            MainForm.MSFlexGrid1.TextMatrix(lRow, lCol) = sDay
            tDay = tDay + 1
        Next lCol
    Next lRow

End Sub

'----------------------------------------------------------
'  Function     : CalGridGetSelCur
'  Return value : None
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Function CalGridGetSelCur(ByRef lCol As Long, ByRef lColSel As Long, ByRef lRow As Long, ByRef lRowSel As Long)

    lCol = MainForm.MSFlexGrid1.Col
    lColSel = MainForm.MSFlexGrid1.ColSel
    lRow = MainForm.MSFlexGrid1.Row
    lRowSel = MainForm.MSFlexGrid1.RowSel
    
End Function

'----------------------------------------------------------
'  Function     : CalGridGetSelectPath
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Function CalGridGetSelectPath() As String

    Dim sOutput As String
    
    sOutput = m_sScenarioOutputFolder
    
    Dim sFolder As String
    Dim tDay As Date
    Dim iStartDayOfWeek As Integer
    Dim lStartlCol As Long
    Dim lRow As Long, lCol As Long
    Dim iSelType As Integer
    
    iSelType = CalGridGetSelType(m_lCol, m_lColSel, m_lRow, m_lRowSel)
    
    Select Case (iSelType)
        Case stLeftTopCell
            sFolder = sOutput & "\" & CStr(m_tcFoldersEnglish(0))
        Case stCol
            sFolder = sOutput & "\" & CStr(m_tcFoldersEnglish(m_lCol))
        Case stCell
            iStartDayOfWeek = Weekday(m_tToday, vbSunday)
            lStartlCol = CLng(m_iWkDays(iStartDayOfWeek))
            For lRow = m_lRow To m_lRowSel
                For lCol = m_lCol To m_lColSel - 1
                    tDay = CalGridRowColToDate(lCol, lRow)
                    sFolder = sOutput & "\" & Format(tDay, "YYYYMMDD")
                Next lCol
            Next lRow
        Case stRange
        
        Case stRow
        
        Case stNoSelected
            sFolder = ""
    End Select
    
    CalGridGetSelectPath = sFolder
    
End Function

'----------------------------------------------------------
'  Function     : CalGridGetSelType
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Function CalGridGetSelType(ByVal lCol As Long, ByVal lColSel As Long, ByVal lRow As Long, ByVal lRowSel As Long) As Integer

    If ((lCol = 1 And lColSel = 7 And lRow = 1 And lRowSel = 6) Or _
       (lCol = 0 And lColSel = 0 And lRow = 0 And lRowSel = 0)) Then
        CalGridGetSelType = stLeftTopCell
        Exit Function
    ElseIf ((lRow = 1 And lRowSel = 6) Or (lRow = 0 And lRowSel = 0)) Then
        CalGridGetSelType = stCol
        Exit Function
    ElseIf (lCol = lColSel And lRow = lRowSel And lCol > 0 And lRow > 0) Then
        CalGridGetSelType = stCell
        Exit Function
    End If
    
    CalGridGetSelType = stNoSelected
    
End Function

'----------------------------------------------------------
'  Function     : CalGridInit
'  Return value : None
'  Argument     : None
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub CalGridInit()

    Dim lCols As Long, lRows As Long
    Dim lFixedCols As Long, lFixedRows As Long
    
    lCols = UBound(m_tcWkDays) + 1 '/ COLTEXTMAXBYTES
    lFixedCols = 1
    lRows = UBound(m_tcRowCap) + 1 '/ ROWTEXTMAXBYTES
    lFixedRows = 1
    
    m_lCols = lCols
    m_lRows = lRows
    
    MainForm.MSFlexGrid1.Cols = lCols
    MainForm.MSFlexGrid1.Rows = lRows
    MainForm.MSFlexGrid1.FixedCols = lFixedCols
    MainForm.MSFlexGrid1.FixedRows = lFixedRows
    
    Dim iTwipsPerPixelX As Integer, iTwipsPerPixelY As Integer
    iTwipsPerPixelX = GetTwipsPerPixelX(MainForm.hDC)
    iTwipsPerPixelY = GetTwipsPerPixelY(MainForm.hDC)
    
    Dim szText As Size
    GetTextExtentPoint32 MainForm.hDC, "      ", 6, szText
    MainForm.MSFlexGrid1.ColWidth(0) = CLng(szText.cx * iTwipsPerPixelX)
    MainForm.MSFlexGrid1.FixedAlignment(0) = 4
    
    Dim lFixedColsWidth As Long
    lFixedColsWidth = 0
    
    Dim iCol As Integer
    For iCol = 0 To lFixedCols - 1
        lFixedColsWidth = lFixedColsWidth + MainForm.MSFlexGrid1.ColWidth(iCol)
    Next iCol
    m_iFixedColsWidth = lFixedColsWidth / iTwipsPerPixelX
    
    Dim lFixedRowsHeight As Long
    lFixedRowsHeight = 0
    
    Dim iRow As Integer
    For iRow = 0 To lFixedRows - 1
        lFixedRowsHeight = lFixedRowsHeight + MainForm.MSFlexGrid1.RowHeight(iRow)
    Next iRow
    m_iFixedRowsHeight = lFixedRowsHeight / iTwipsPerPixelY
    
    GetTextExtentPoint32 MainForm.hDC, "    ", 4, szText
    m_iMinGridWidth = m_iFixedColsWidth + szText.cx * (lCols - lFixedCols)
    m_iMinGridHeight = m_iFixedRowsHeight + szText.cy * (lRows - lFixedRows)
    
    Dim lCol As Long, lRow As Long
    lCol = 0: lRow = 0
    
    lRow = 0
    For lCol = 0 To lCols - 1
        If (Len(m_tcWkDays(lCol)) <> 0) Then
            MainForm.MSFlexGrid1.TextMatrix(lRow, lCol) = CStr(m_tcWkDays(lCol))
        End If
        MainForm.MSFlexGrid1.ColAlignment(lCol) = 4
    Next lCol
    
    lCol = 0
    For lRow = 0 To lRows - 1
        If (Len(m_tcRowCap(lRow)) <> 0) Then
            MainForm.MSFlexGrid1.TextMatrix(lRow, lCol) = CStr(m_tcRowCap(lRow))
        End If
    Next lRow
    
    m_rcLeftTopCell.Left = 0
    m_rcLeftTopCell.Top = 0
    m_rcLeftTopCell.Right = MainForm.MSFlexGrid1.ColWidth(0) '/ iTwipsPerPixelX
    m_rcLeftTopCell.Bottom = MainForm.MSFlexGrid1.RowHeight(0) '/ iTwipsPerPixelY
    
    ReDim m_iConfigFlags(m_lRows * m_lCols) As Integer
    ReDim m_bScheduleFlags(m_lRows * m_lCols) As Boolean
    
    Call CalGridInitStatus(True)
    
    Call CalGridLoadStatusColor
    
    Call CalGridDispDays
    
End Sub

'----------------------------------------------------------
'  Function     : CalGridInitStatus
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub CalGridInitStatus(ByVal bReload As Boolean)

    Dim iPos As Integer, i As Integer
    Dim iFlag As Integer
    iFlag = 0
    
    Dim sFolder As String, sPath As String
    Dim sOutput As String
    sOutput = m_sScenarioOutputFolder
    
    Dim sTitle As String
'##### 2004.12.16 #####
    'sTitle = "Today : " & Format(m_tToday, "MM/DD/YYYY")
    sTitle = "Today : " & GetDateString(m_tToday)
'##### 2004.12.16 #####
'    MainForm.lblToday.Caption = sTitle
    
    Dim sYearTitle As String
    sYearTitle = Format(m_tFirstDate, "YYYY")
    MainForm.lblScheduleYear = sYearTitle
    
    Dim sMonthTitle As String
    sMonthTitle = m_tcMonth(Month(m_tFirstDate))
    MainForm.lblScheduleMonth = sMonthTitle

    Dim lRow As Long, lCol As Long
    
    Dim iFilter As Integer
    iFilter = FLAGS_STATUS_STANDARD Or FLAGS_STATUS_WEEKOFDAY Or FLAGS_STATUS_DATE Or FLAGS_STATUS_GRAY
    For i = 0 To UBound(m_iConfigFlags) - 1
        iFlag = m_iConfigFlags(i)
        iFlag = iFlag And (&HFFFFFFFF Xor iFilter)
        m_iConfigFlags(i) = iFlag
    Next i
    
    Call FindSchedule
    
End Sub

'----------------------------------------------------------
'  Function     : CalGridLoadStatusColor
'  Return value : None
'  Argument     : None
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub CalGridLoadStatusColor()

    ReDim m_CellColorsFr(IDX_STATUS_MAX) As Long
    ReDim m_CellColorsBk(IDX_STATUS_MAX) As Long
    Dim crFr As Long, crBk As Long, crGray As Long
    crGray = RGB(190, 190, 190)
    
    crFr = MainForm.MSFlexGrid1.ForeColor
    crBk = MainForm.MSFlexGrid1.BackColor
    m_CellColorsFr(IDX_STATUS_NORMAL) = crFr
    m_CellColorsBk(IDX_STATUS_NORMAL) = crBk
    
    m_CellColorsFr(IDX_STATUS_NORMAL_GRAY) = crGray
    m_CellColorsBk(IDX_STATUS_NORMAL_GRAY) = crBk
    
    m_CellColorsFr(IDX_STATUS_STANDARD) = crFr
    m_CellColorsBk(IDX_STATUS_STANDARD) = crBk
    
    m_CellColorsFr(IDX_STATUS_STANDARD_GRAY) = crGray
    m_CellColorsBk(IDX_STATUS_STANDARD_GRAY) = crBk
    
    crBk = RGB(64, 255, 255)
    m_CellColorsFr(IDX_STATUS_DEFAULT) = crFr
    m_CellColorsBk(IDX_STATUS_DEFAULT) = crBk
    
    crBk = MainForm.MSFlexGrid1.BackColorFixed
    m_CellColorsFr(IDX_STATUS_FIXED) = crFr
    m_CellColorsBk(IDX_STATUS_FIXED) = crBk
    
    crBk = RGB(0, 255, 0)
    m_CellColorsFr(IDX_STATUS_SUN) = crFr
    m_CellColorsBk(IDX_STATUS_SUN) = crBk
    
    m_CellColorsFr(IDX_STATUS_SUN_GRAY) = crGray
    m_CellColorsBk(IDX_STATUS_SUN_GRAY) = crBk
    
    crBk = RGB(0, 235, 0)
    m_CellColorsFr(IDX_STATUS_MON) = crFr
    m_CellColorsBk(IDX_STATUS_MON) = crBk
    
    m_CellColorsFr(IDX_STATUS_MON_GRAY) = crGray
    m_CellColorsBk(IDX_STATUS_MON_GRAY) = crBk
    
    crBk = RGB(0, 215, 0)
    m_CellColorsFr(IDX_STATUS_TUE) = crFr
    m_CellColorsBk(IDX_STATUS_TUE) = crBk
    
    m_CellColorsFr(IDX_STATUS_TUE_GRAY) = crGray
    m_CellColorsBk(IDX_STATUS_TUE_GRAY) = crBk
    
    crBk = RGB(0, 195, 0)
    m_CellColorsFr(IDX_STATUS_WED) = crFr
    m_CellColorsBk(IDX_STATUS_WED) = crBk
    
    m_CellColorsFr(IDX_STATUS_WED_GRAY) = crGray
    m_CellColorsBk(IDX_STATUS_WED_GRAY) = crBk
    
    crBk = RGB(0, 175, 0)
    m_CellColorsFr(IDX_STATUS_THU) = crFr
    m_CellColorsBk(IDX_STATUS_THU) = crBk
    
    m_CellColorsFr(IDX_STATUS_THU_GRAY) = crGray
    m_CellColorsBk(IDX_STATUS_THU_GRAY) = crBk
    
    crBk = RGB(0, 155, 0)
    m_CellColorsFr(IDX_STATUS_FRI) = crFr
    m_CellColorsBk(IDX_STATUS_FRI) = crBk
    
    m_CellColorsFr(IDX_STATUS_FRI_GRAY) = crGray
    m_CellColorsBk(IDX_STATUS_FRI_GRAY) = crBk
    
    crBk = RGB(0, 135, 0)
    m_CellColorsFr(IDX_STATUS_SAT) = crFr
    m_CellColorsBk(IDX_STATUS_SAT) = crBk
    
    m_CellColorsFr(IDX_STATUS_SAT_GRAY) = crGray
    m_CellColorsBk(IDX_STATUS_SAT_GRAY) = crBk

    crBk = RGB(128, 128, 255)
    m_CellColorsFr(IDX_STATUS_DATE) = crFr
    m_CellColorsBk(IDX_STATUS_DATE) = crBk
    
    m_CellColorsFr(IDX_STATUS_DATE_GRAY) = crGray
    m_CellColorsBk(IDX_STATUS_DATE_GRAY) = crBk

    crFr = GetSysColor(COLOR_HIGHLIGHTTEXT)
    crBk = RGB(255, 0, 0)
    m_CellColorsFr(IDX_STATUS_SELECTED) = crFr
    m_CellColorsBk(IDX_STATUS_SELECTED) = crBk
    
    m_CellColorsFr(IDX_STATUS_SELECTED_GRAY) = crGray
    m_CellColorsBk(IDX_STATUS_SELECTED_GRAY) = crBk

End Sub

'----------------------------------------------------------
'  Function     : CalGridRowColToDate
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Function CalGridRowColToDate(ByVal lCol As Long, ByVal lRow As Long) As Date

    If ((lRow < 1) Or (lCol < 1)) Then
        CalGridRowColToDate = 0
        Exit Function
    End If
    
    Dim dtDate As Date
    
    dtDate = m_tFirstDate
    
    Dim iAdd As Integer
    iAdd = CalGridRowColToIndex(lCol, lRow)
    iAdd = iAdd - CalGridRowColToIndex(m_iWkDays(Weekday(m_tFirstDate)), 1)
    
    Dim off As Integer
    off = lRow - 1
    iAdd = iAdd - off
    
    dtDate = dtDate + iAdd
    
    CalGridRowColToDate = dtDate
    
End Function

'----------------------------------------------------------
'  Function     : CalGridRowColToIndex
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Function CalGridRowColToIndex(ByVal lCol As Long, ByVal lRow As Long) As Long

    CalGridRowColToIndex = (lRow * m_lCols + lCol)
    
End Function

'----------------------------------------------------------
'  Function     : CalGridRowColToKeyString
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Function CalGridRowColToKeyString(ByVal lCol As Long, ByVal lRow As Long) As String

    If (lRow < 0 Or lCol < 0 Or lRow > m_lRows Or lCol > m_lCols) Then
        CalGridRowColToKeyString = ""
        Exit Function
    End If
    
    Dim iAdd As Integer, off As Integer
    Dim dtDate As Date
    Dim strKey As String
    
    If (lRow = 0) Then
        strKey = m_tcFoldersEnglish(lCol)
    Else
        iAdd = CalGridRowColToIndex(lCol, lRow)
        iAdd = iAdd - CalGridRowColToIndex(m_iWkDays(Weekday(m_tFirstDate)), 1)
        dtDate = m_tFirstDate
        off = lRow - 1
        iAdd = iAdd - off
        dtDate = dtDate + iAdd
        strKey = Format(dtDate, "YYYYMMDD")
    End If
    
    CalGridRowColToKeyString = strKey
    
End Function

'----------------------------------------------------------
'  Function     : CalGridSetSelCur
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub CalGridSetSelCur(ByVal lCol As Long, ByVal lColSel As Long, ByVal lRow As Long, ByVal lRowSel As Long, ByVal bSelect As Boolean, ByVal bSet As Boolean)

    Dim iCols As Integer
    iCols = MainForm.MSFlexGrid1.Cols
    
    Dim i As Integer, iFlag As Integer, iFlagOff As Integer
    
    Dim r As Long, c As Long
    If (bSelect) Then
        For r = lRow To lRowSel
            For c = lCol To lColSel
                i = r * iCols + c
                iFlag = m_iConfigFlags(i)
                iFlag = iFlag Or FLAGS_STATUS_SELECTED
                m_iConfigFlags(i) = iFlag
            Next c
        Next r
    Else
        For r = lRow To lRowSel
            For c = lCol To lColSel
                i = r * iCols + c
                iFlag = m_iConfigFlags(i)
                iFlagOff = (&HFFFFFFFF Xor FLAGS_STATUS_SELECTED)
                iFlag = iFlag And iFlagOff
                m_iConfigFlags(i) = iFlag
            Next c
        Next r
    End If
    If (bSet) Then
        MainForm.MSFlexGrid1.Col = lCol
        MainForm.MSFlexGrid1.Row = lRow
        MainForm.MSFlexGrid1.ColSel = lColSel
        MainForm.MSFlexGrid1.RowSel = lRowSel
    End If
    Call CalGridUpdateCellsColor(lCol, lColSel, lRow, lRowSel)
    
End Sub

'----------------------------------------------------------
'  Function     : CalGridUpdateCellsColor
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub CalGridUpdateCellsColor(ByVal lCol As Long, ByVal lColSel As Long, ByVal lRow As Long, ByVal lRowSel As Long)

    Dim lTempRow As Long, lTempCol As Long, lTempRowSel As Long, lTempColSel As Long
    lTempRow = MainForm.MSFlexGrid1.Row
    lTempCol = MainForm.MSFlexGrid1.Col
    lTempRowSel = MainForm.MSFlexGrid1.RowSel
    lTempColSel = MainForm.MSFlexGrid1.ColSel
    
    Dim r As Long, c As Long
    Dim i As Integer, iIdx As Integer, iFlag As Integer
    
    For r = lRow To lRowSel
        For c = lCol To lColSel
            i = r * m_lCols + c
            iFlag = m_iConfigFlags(i)
            If ((iFlag And FLAGS_STATUS_SELECTED) = FLAGS_STATUS_SELECTED) Then
                If ((iFlag And FLAGS_STATUS_GRAY) = FLAGS_STATUS_GRAY) Then
                    iIdx = IDX_STATUS_SELECTED_GRAY
                Else
                    iIdx = IDX_STATUS_SELECTED
                End If
            ElseIf ((iFlag And FLAGS_STATUS_DATE) = FLAGS_STATUS_DATE) Then
                If ((iFlag And FLAGS_STATUS_GRAY) = FLAGS_STATUS_GRAY) Then
                    iIdx = IDX_STATUS_DATE_GRAY
                Else
                    iIdx = IDX_STATUS_DATE
                End If
            ElseIf ((iFlag And FLAGS_STATUS_WEEKOFDAY) = FLAGS_STATUS_WEEKOFDAY) Then
                If ((iFlag And FLAGS_STATUS_GRAY) = FLAGS_STATUS_GRAY) Then
                    iIdx = c + 12
                Else
                    iIdx = c
                End If
            ElseIf ((iFlag And FLAGS_STATUS_STANDARD) = FLAGS_STATUS_STANDARD) Then
                If ((iFlag And FLAGS_STATUS_GRAY) = FLAGS_STATUS_GRAY) Then
                    iIdx = IDX_STATUS_STANDARD_GRAY
                Else
                    If (c = 0 And r = 0) Then
                        iIdx = IDX_STATUS_DEFAULT
                    Else
                        iIdx = IDX_STATUS_STANDARD
                    End If
                End If
            Else
                If ((c < 1) Or (r < 1)) Then
                    iIdx = IDX_STATUS_FIXED
                Else
                    If ((iFlag And FLAGS_STATUS_GRAY) = FLAGS_STATUS_GRAY) Then
                        iIdx = IDX_STATUS_NORMAL_GRAY
                    Else
                        iIdx = IDX_STATUS_NORMAL
                    End If
                End If
            End If
            MainForm.MSFlexGrid1.Row = r
            MainForm.MSFlexGrid1.Col = c
            MainForm.MSFlexGrid1.CellForeColor = m_CellColorsFr(iIdx)
            MainForm.MSFlexGrid1.CellBackColor = m_CellColorsBk(iIdx)
        Next c
    Next r
    
    MainForm.MSFlexGrid1.Row = lTempRow
    MainForm.MSFlexGrid1.Col = lTempCol
    MainForm.MSFlexGrid1.RowSel = lTempRowSel
    MainForm.MSFlexGrid1.ColSel = lTempColSel
    
End Sub

'----------------------------------------------------------
'  Function     : IsThisMonth
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Function IsThisMonth(ByVal tDay As Date) As Boolean

    If (Year(tDay) = Year(m_tFirstDate)) And (Month(tDay) = Month(m_tFirstDate)) Then
        IsThisMonth = True
    Else
        IsThisMonth = False
    End If

End Function

'----------------------------------------------------------
'  Function     : ViewRefresh
'  Return value : None
'  Argument     : None
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub ViewRefresh()

    'Call WaitDisable(True)     '##### 2004.12.16 #####
    
    Call CalGridInitStatus(False)
    Call CalGridUpdateCellsColor(0, m_lCols - 1, 0, m_lRows - 1)
    Call ScheduleItemSelect
    
    'Call WaitDisable(False)    '##### 2004.12.16 #####
    
End Sub

'##### 2004.12.16 #####
'----------------------------------------------------------
'  Function     : WaitDisable
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
'Sub WaitDisable(ByVal bDisable As Boolean)
'
'    If (bDisable) Then
'
'    Else
'
'    End If
'
'End Sub
'##### 2004.12.16 #####

'----------------------------------------------------------
'  Function     : InitialUpdate
'  Return value : None
'  Argument     : None
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub InitialUpdate()

    'Call WaitDisable(True)     '##### 2004.12.16 #####

    Call CalGridInit
    
    Call CalGridUpdateCellsColor(0, m_lCols - 1, 0, m_lRows - 1)
    
    Dim iStartDayOfWeek As Integer, iWeeks As Integer, iMod As Integer
    iStartDayOfWeek = Weekday(m_tToday)
    
    m_lStartCol = CLng(m_iWkDays(iStartDayOfWeek))
    
    iWeeks = Day(m_tToday) \ 7
    iMod = Day(m_tToday) Mod 7
    If (m_lStartCol >= iMod) Then
        m_lStartRow = iWeeks + 1
    Else
        m_lStartRow = iWeeks + 2
    End If
    
    Call CalGridSetSelCur(m_lStartCol, m_lStartCol, m_lStartRow, m_lStartRow, True, True)
    m_lCol = m_lStartCol
    m_lColSel = m_lCol
    m_lRow = m_lStartRow
    m_lRowSel = m_lRow
    
    m_dtSelectDate = m_tToday

    Call ScheduleItemSelect
    
    'Call WaitDisable(False)    '##### 2004.12.16 #####
    
End Sub

'----------------------------------------------------------
'  Function     : ScheduleItemSelect
'  Return value : None
'  Argument     : None
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub ScheduleItemSelect()

On Error Resume Next

    Dim lRow As Long, lCol As Long
    lRow = m_lRow: lCol = m_lCol
    
    Dim iSelType As Integer
    iSelType = CalGridGetSelType(m_lCol, m_lColSel, m_lRow, m_lRowSel)
    If (iSelType = stCol) Then
        lRow = 0
    End If
    
    Dim iPos As Integer
    iPos = CalGridRowColToIndex(lCol, lRow)
    
    Dim KeyString As String
    KeyString = CalGridRowColToKeyString(lCol, lRow)
    
    'clear schedule list
    MainForm.lstvwSchedule.ListItems.Clear
    
    Dim szFileName As String
    szFileName = App.Path & "\Schedule\" & KeyString & ".sch"
    
    Dim szKey As String, Ret As Long, i As Long, a
    Dim szReadBuf As String * 512, szData As String, tmp As String
    If Dir$(szFileName, 0) <> "" Then
        szKey = MainForm.ProjectorTree.SelectedItem.Text
        Ret = GetPrivateProfileSection(szKey, szReadBuf, Len(szReadBuf), szFileName)
        szData = ""
        For i = 1 To Ret
            tmp = Mid$(szReadBuf, i, 1)
            If (tmp = Chr$(0)) Then
                Dim itmX As ListItem, tmp1 As String, tmp2 As String
                'tmp1 = CutParam2(szData, 1)
                tmp1 = CutParam("=", szData, 1)
                'tmp2 = CutParam2(szData, 2)
                tmp2 = CutParam("=", szData, 2)
'##### 2004.12.16 #####
                'Set itmX = MainForm.lstvwSchedule.ListItems.Add(, , Format(CDate(tmp1), "Medium Time"))
                Set itmX = MainForm.lstvwSchedule.ListItems.Add(, , GetTimeString(CDate(tmp1)))
'##### 2004.12.16 #####
                itmX.SubItems(1) = tmp2
                szData = ""
            Else
                szData = szData & tmp
            End If
        Next i
    End If
    
    Dim Count As Integer
    Count = MainForm.lstvwSchedule.ListItems.Count
    MainForm.cmdScheduleDelete.Enabled = False
    For i = 1 To Count
        If (MainForm.lstvwSchedule.ListItems(i).Selected = True) Then
            If (Timers.ScheduleTimer.Enabled = False) Then
                MainForm.cmdScheduleDelete.Enabled = True
            Else
                MainForm.cmdScheduleDelete.Enabled = False
            End If
            Exit For
        End If
    Next i
    
End Sub

'----------------------------------------------------------
'  Function     : FindSchedule
'  Return value : None
'  Argument     : None
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub FindSchedule()

    On Error Resume Next
    
    Dim iPos As Integer, i As Integer
    Dim iFlag As Integer
    iFlag = 0
    
    Dim sFolder As String, sPath As String
    Dim sOutput As String, szSection As String
    Dim bFind As Boolean, Ret As Long
    Dim szReadTmp As String * 256
    
    sOutput = App.Path & "\Schedule"
    
    Dim lRow As Long, lCol As Long
    
    Dim iFilter As Integer
    iFilter = FLAGS_STATUS_STANDARD Or FLAGS_STATUS_WEEKOFDAY Or FLAGS_STATUS_DATE Or FLAGS_STATUS_GRAY
    For i = 0 To UBound(m_iConfigFlags) - 1
        iFlag = m_iConfigFlags(i)
        iFlag = iFlag And (&HFFFFFFFF Xor iFilter)
        m_iConfigFlags(i) = iFlag
    Next i
    
    sPath = sOutput & "\" & m_tcFoldersEnglish(0) & ".sch"
    szSection = MainForm.ProjectorTree.SelectedItem.Text
    'iFlag = m_iConfigFlags(0)
    If (Dir$(sPath, 0) <> "") Then
        Ret = GetPrivateProfileSection(szSection, szReadTmp, 256, sPath)
        If (Ret <> 0) Then
            bFind = True
        Else
            bFind = False
        End If
    Else
        bFind = False
    End If
    
    Dim dtTmp As Date
    
    If (bFind) Then
        iFlag = m_iConfigFlags(0)
        iFlag = iFlag Or FLAGS_STATUS_STANDARD
        m_iConfigFlags(0) = iFlag
        For lRow = 1 To m_lRows - 1
            For lCol = 1 To m_lCols - 1
                i = CalGridRowColToIndex(lCol, lRow)
                iFlag = m_iConfigFlags(i)
                iFlag = iFlag Or FLAGS_STATUS_STANDARD
                dtTmp = CalGridRowColToDate(lCol, lRow)
                If (dtTmp < m_tToday Or Month(dtTmp) <> Month(m_tFirstDate)) Then
                    iFlag = iFlag Or FLAGS_STATUS_GRAY
                End If
                m_iConfigFlags(i) = iFlag
            Next lCol
        Next lRow
    Else
        For lRow = 1 To m_lRows - 1
            For lCol = 1 To m_lCols - 1
                i = CalGridRowColToIndex(lCol, lRow)
                iFlag = m_iConfigFlags(i)
                iFlag = iFlag And (&HFFFFFFFF Xor FLAGS_STATUS_STANDARD)
                dtTmp = CalGridRowColToDate(lCol, lRow)
                If (dtTmp < m_tToday Or Month(dtTmp) <> Month(m_tFirstDate)) Then
                    iFlag = iFlag Or FLAGS_STATUS_GRAY
                End If
                m_iConfigFlags(i) = iFlag
            Next lCol
        Next lRow
    End If
    
    For lCol = 1 To m_lCols - 1
        sPath = sOutput & "\" & m_tcFoldersEnglish(lCol) & ".sch"
        szSection = MainForm.ProjectorTree.SelectedItem.Text
        If (Dir$(sPath, 0) <> "") Then
            Ret = GetPrivateProfileSection(szSection, szReadTmp, 256, sPath)
            If (Ret <> 0) Then
                bFind = True
            Else
                bFind = False
            End If
        Else
            bFind = False
        End If
    
        If (bFind) Then
            For lRow = 0 To m_lRows - 1
                iPos = CalGridRowColToIndex(lCol, lRow)
                iFlag = m_iConfigFlags(iPos)
                iFlag = iFlag Or FLAGS_STATUS_WEEKOFDAY
                m_iConfigFlags(iPos) = iFlag
            Next lRow
        Else
            Dim iFlag1 As Integer
            iFlag1 = 0
            For lRow = 0 To m_lRows - 1
                iPos = CalGridRowColToIndex(lCol, lRow)
                iFlag = m_iConfigFlags(iPos)
                iFlag1 = (&HFFFFFFFF Xor FLAGS_STATUS_WEEKOFDAY)
                iFlag = iFlag And (&HFFFFFFFF Xor FLAGS_STATUS_WEEKOFDAY)
                m_iConfigFlags(iPos) = iFlag
            Next lRow
        End If
    Next lCol
    
    Dim tDay As Date, szDay As String
    For lRow = 1 To m_lRows - 1
        For lCol = 1 To m_lCols - 1
            tDay = CalGridRowColToDate(lCol, lRow)
            If (IsThisMonth(tDay)) Then
                szDay = Format(tDay, "YYYYMMDD")
                sPath = sOutput & "\" & szDay & ".sch"
                iPos = CalGridRowColToIndex(lCol, lRow)
                iFlag = m_iConfigFlags(iPos)
                If (Dir$(sPath, 0) <> "") Then
                    Ret = GetPrivateProfileSection(szSection, szReadTmp, 256, sPath)
                    If (Ret <> 0) Then
                        bFind = True
                    Else
                        bFind = False
                    End If
                Else
                    bFind = False
                End If
                If (bFind) Then
                    iFlag = iFlag Or FLAGS_STATUS_DATE
                Else
                    iFlag = iFlag And (&HFFFFFFFF Xor FLAGS_STATUS_DATE)
                End If
                m_iConfigFlags(iPos) = iFlag
            End If
        Next lCol
    Next lRow

End Sub

'----------------------------------------------------------
'  Function     : ScheduleSave
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub ScheduleSave()

    Dim szKey As String
    Dim lCol As Long, lColSel As Long
    Dim lRow As Long, lRowSel As Long
    lCol = 0: lColSel = 0
    lRow = 0: lRowSel = 0

    Call CalGridGetSelCur(lCol, lColSel, lRow, lRowSel)
    szKey = CalGridRowColToKeyString(lCol, lRow)

    Dim szScheduleFile As String
    Dim Count As Integer, i As Integer
    Dim szText As String, Section As String
    If (szKey <> "") Then
        szScheduleFile = App.Path & "\Schedule\" & szKey & ".sch"
        Count = MainForm.lstvwSchedule.ListItems.Count
        Section = MainForm.ProjectorTree.SelectedItem.Text
        szText = ""
        For i = 1 To Count
            szText = szText & Format(CDate(MainForm.lstvwSchedule.ListItems(i).Text), "HH:mm") & "="
            szText = szText & MainForm.lstvwSchedule.ListItems(i).SubItems(1) & Chr$(0)
        Next i
        WritePrivateProfileSection Section, szText, szScheduleFile
    End If
    
    Call ScheduleSelect
    
End Sub

'----------------------------------------------------------
'  Function     : ScheduleSelect
'  Return value :
'  Argument     :
'  Feature      :
'  Description  :
'  Remarks      :
'----------------------------------------------------------
Sub ScheduleSelect()

    Dim szTodaySch As String
    Dim szWeekSch As String
    Dim szDefaultSch As String
    
    szTodaySch = Format(Now, "YYYYMMDD")
    Select Case Weekday(Now, vbSunday)
        Case vbSunday
            szWeekSch = "Sunday"
        Case vbMonday
            szWeekSch = "Monday"
        Case vbTuesday
            szWeekSch = "Tuesday"
        Case vbWednesday
            szWeekSch = "Wednesday"
        Case vbThursday
            szWeekSch = "Thursday"
        Case vbFriday
            szWeekSch = "Friday"
        Case vbSaturday
            szWeekSch = "Saturday"
    End Select
    szDefaultSch = "default"
    
    Dim nCount As Integer
    Dim i As Integer
    Dim szKey As String, szText As String
    Dim szBuffer As String * 512
    Dim Ret As Long
    Dim szScheduleFile As String
    
    ScheduleCount = 0
    
    nCount = MainForm.ProjectorTree.Nodes.Count
    For i = 1 To nCount
        szKey = MainForm.ProjectorTree.Nodes(i).Key
        szText = MainForm.ProjectorTree.Nodes(i).Text
        If (Left$(szKey, 1) <> "r") Then
            szScheduleFile = App.Path & "\Schedule\" & szTodaySch & ".sch"
            Ret = GetPrivateProfileSection(szText, szBuffer, 512, szScheduleFile)
            If (Ret = 0) Then
                szScheduleFile = App.Path & "\Schedule\" & szWeekSch & ".sch"
                Ret = GetPrivateProfileSection(szText, szBuffer, 512, szScheduleFile)
                If (Ret = 0) Then
                    szScheduleFile = App.Path & "\Schedule\" & szDefaultSch & ".sch"
                    Ret = GetPrivateProfileSection(szText, szBuffer, 512, szScheduleFile)
                    If (Ret = 0) Then
'                        ScheduleInfo(ScheduleCount).Index = i
'                        ScheduleInfo(ScheduleCount).Key = szKey
'                        ScheduleInfo(ScheduleCount).Text = szText
'                        ScheduleInfo(ScheduleCount).Kind = -1
'                        ScheduleInfo(ScheduleCount).Name = ""
'                        ScheduleCount = ScheduleCount + 1
                    Else
                        ScheduleInfo(ScheduleCount).Index = i
                        ScheduleInfo(ScheduleCount).Key = szKey
                        ScheduleInfo(ScheduleCount).Text = szText
                        ScheduleInfo(ScheduleCount).Kind = 0    'Default schedule
                        ScheduleInfo(ScheduleCount).Name = szDefaultSch
                        ScheduleCount = ScheduleCount + 1
                    End If
                Else
                    ScheduleInfo(ScheduleCount).Index = i
                    ScheduleInfo(ScheduleCount).Key = szKey
                    ScheduleInfo(ScheduleCount).Text = szText
                    ScheduleInfo(ScheduleCount).Kind = 1    'Date of the week schedule
                    ScheduleInfo(ScheduleCount).Name = szWeekSch
                    ScheduleCount = ScheduleCount + 1
                End If
            Else
                ScheduleInfo(ScheduleCount).Index = i
                ScheduleInfo(ScheduleCount).Key = szKey
                ScheduleInfo(ScheduleCount).Text = szText
                ScheduleInfo(ScheduleCount).Kind = 2    'Date schedule
                ScheduleInfo(ScheduleCount).Name = szTodaySch
                ScheduleCount = ScheduleCount + 1
            End If
        End If
    Next i

End Sub

