VERSION 5.00
Object = "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}#1.1#0"; "shdocvw.dll"
Begin VB.Form frmMain 
   Appearance      =   0  'Flat
   AutoRedraw      =   -1  'True
   BackColor       =   &H00C0C0C0&
   BorderStyle     =   0  'None
   ClientHeight    =   11520
   ClientLeft      =   0
   ClientTop       =   0
   ClientWidth     =   15360
   ControlBox      =   0   'False
   BeginProperty Font 
      Name            =   "Arial"
      Size            =   8.25
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   LinkTopic       =   "Main"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   Moveable        =   0   'False
   NegotiateMenus  =   0   'False
   PaletteMode     =   2  'Custom
   ScaleHeight     =   768
   ScaleMode       =   3  'Pixel
   ScaleWidth      =   1024
   ShowInTaskbar   =   0   'False
   WindowState     =   2  'Maximized
   Begin VB.CommandButton cmdSetup 
      Caption         =   "Setup"
      Height          =   615
      Left            =   13230
      TabIndex        =   14
      Top             =   8220
      Width           =   1275
   End
   Begin VB.CommandButton light_a 
      BackColor       =   &H00C0C0C0&
      Height          =   900
      Index           =   0
      Left            =   1500
      MaskColor       =   &H00FFFFFF&
      Style           =   1  'Graphical
      TabIndex        =   11
      Tag             =   "err"
      Top             =   1920
      Visible         =   0   'False
      Width           =   1200
   End
   Begin VB.CommandButton light_b 
      BackColor       =   &H00C0C0C0&
      Height          =   900
      Index           =   0
      Left            =   2400
      MaskColor       =   &H00FFFFFF&
      Style           =   1  'Graphical
      TabIndex        =   10
      Tag             =   "err"
      Top             =   1920
      Visible         =   0   'False
      Width           =   1200
   End
   Begin VB.TextBox light_status 
      Height          =   285
      Index           =   0
      Left            =   990
      TabIndex        =   9
      TabStop         =   0   'False
      Tag             =   "err"
      Text            =   "Unknown"
      Top             =   2250
      Visible         =   0   'False
      Width           =   345
   End
   Begin VB.CommandButton light_cmdExecuteEvent 
      Caption         =   "Execute Event"
      Height          =   660
      Left            =   7350
      TabIndex        =   8
      Tag             =   "err"
      Top             =   7335
      Visible         =   0   'False
      Width           =   1410
   End
   Begin VB.ListBox light_lstEvent 
      BackColor       =   &H00C0C0C0&
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   9.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   1980
      Left            =   570
      TabIndex        =   7
      Tag             =   "err"
      Top             =   7350
      Visible         =   0   'False
      Width           =   6660
   End
   Begin VB.CommandButton light_MessageCommand 
      Caption         =   "Update Status"
      Height          =   660
      Left            =   7350
      TabIndex        =   6
      Tag             =   "err"
      Top             =   8040
      Visible         =   0   'False
      Width           =   1410
   End
   Begin VB.Timer Timer3 
      Enabled         =   0   'False
      Interval        =   1000
      Left            =   300
      Tag             =   "0"
      Top             =   10110
   End
   Begin VB.Timer Timer2 
      Enabled         =   0   'False
      Interval        =   1000
      Left            =   180
      Top             =   10200
   End
   Begin VB.Timer Timer1 
      Enabled         =   0   'False
      Interval        =   10
      Left            =   75
      Tag             =   "0"
      Top             =   10305
   End
   Begin SHDocVwCtl.WebBrowser light_icon 
      Height          =   615
      Index           =   0
      Left            =   3660
      TabIndex        =   13
      TabStop         =   0   'False
      Tag             =   "err"
      Top             =   2070
      Visible         =   0   'False
      Width           =   465
      ExtentX         =   820
      ExtentY         =   1085
      ViewMode        =   0
      Offline         =   0
      Silent          =   0
      RegisterAsBrowser=   0
      RegisterAsDropTarget=   0
      AutoArrange     =   0   'False
      NoClientEdge    =   0   'False
      AlignLeft       =   0   'False
      ViewID          =   "{0057D0E0-3573-11CF-AE69-08002B2E1262}"
      Location        =   ""
   End
   Begin VB.PictureBox light_Label 
      Height          =   915
      Index           =   0
      Left            =   870
      ScaleHeight     =   855
      ScaleWidth      =   525
      TabIndex        =   12
      Tag             =   "err"
      Top             =   1920
      Visible         =   0   'False
      Width           =   585
   End
   Begin VB.Label always_Custom1 
      BackColor       =   &H00FFFFFF&
      BackStyle       =   0  'Transparent
      Caption         =   "Custom1"
      BeginProperty Font 
         Name            =   "Century Gothic"
         Size            =   15.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H004F3933&
      Height          =   375
      Left            =   3750
      TabIndex        =   4
      Tag             =   "err"
      Top             =   240
      Width           =   9600
   End
   Begin VB.Label always_Time 
      Alignment       =   1  'Right Justify
      BackColor       =   &H00FFFFFF&
      BackStyle       =   0  'Transparent
      Caption         =   "Time"
      BeginProperty Font 
         Name            =   "Century Gothic"
         Size            =   15.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H0075544A&
      Height          =   375
      Left            =   8100
      TabIndex        =   3
      Top             =   9600
      Width           =   7230
   End
   Begin VB.Label always_Date 
      BackColor       =   &H00FFFFFF&
      BackStyle       =   0  'Transparent
      Caption         =   "Date"
      BeginProperty Font 
         Name            =   "Century Gothic"
         Size            =   15.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H0075544A&
      Height          =   375
      Left            =   75
      TabIndex        =   2
      Top             =   9600
      Width           =   7710
   End
   Begin VB.Image cmd_Exit 
      Appearance      =   0  'Flat
      Height          =   405
      Left            =   30
      Top             =   11070
      Width           =   1155
   End
   Begin VB.Image optMediaControl 
      Height          =   1155
      Left            =   11940
      Top             =   10185
      Width           =   2550
   End
   Begin VB.Image optWebControl 
      Height          =   1170
      Left            =   8655
      Top             =   10185
      Width           =   2385
   End
   Begin VB.Image optMP3Control 
      Height          =   1140
      Left            =   5175
      Top             =   10200
      Width           =   2595
   End
   Begin VB.Image optLightControl 
      Height          =   1185
      Left            =   1695
      Top             =   10185
      Width           =   2685
   End
   Begin VB.Label light_column_label 
      Alignment       =   2  'Center
      BackColor       =   &H00FFC0C0&
      BorderStyle     =   1  'Fixed Single
      BeginProperty Font 
         Name            =   "Century Gothic"
         Size            =   15.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00800000&
      Height          =   435
      Left            =   1215
      TabIndex        =   1
      Top             =   1275
      Visible         =   0   'False
      Width           =   2250
   End
   Begin VB.Shape light_border 
      BorderColor     =   &H00FFC0C0&
      BorderWidth     =   2
      Height          =   7860
      Left            =   540
      Shape           =   4  'Rounded Rectangle
      Top             =   1530
      Visible         =   0   'False
      Width           =   14640
   End
   Begin VB.Label always_lblControl 
      Alignment       =   2  'Center
      BackColor       =   &H00FFFFFF&
      BackStyle       =   0  'Transparent
      Caption         =   "Main Control"
      BeginProperty Font 
         Name            =   "Century Gothic"
         Size            =   20.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   -1  'True
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00400000&
      Height          =   600
      Left            =   60
      TabIndex        =   0
      Top             =   60
      Width           =   2850
   End
   Begin VB.Label always_Custom2 
      Alignment       =   1  'Right Justify
      BackColor       =   &H00FFFFFF&
      BackStyle       =   0  'Transparent
      Caption         =   "Custom2"
      BeginProperty Font 
         Name            =   "Century Gothic"
         Size            =   15.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H004F3933&
      Height          =   375
      Left            =   3750
      TabIndex        =   5
      Tag             =   "err"
      Top             =   240
      Width           =   11520
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim WithEvents RemoteFile As DataConnection 'Class used to communicate with Web Command Server
Attribute RemoteFile.VB_VarHelpID = -1
Dim WebSvr As String                'Holds the initial URL with IP and Port of Web Command Server
Dim Header As String                'Holds the HTML header to be stripped from received messages
Dim Footer As String                'Holds the HTML footer to be stripped from received messages
Dim Web1Busy As Boolean             'Global Flag to indicate a Web message is being processed
Dim Web1Response As String          'Holds the incoming messages from the Web Command Server
Dim inifile As String               'Holds location of INI file
Dim HostIP As String                'INI -Actual IP address of Web Command Server
Dim HostPort As String              'INI -Port number to use for Web Command Server
Dim Skin As String                  'INI -Name of the current skin in use
Dim Dbg As Boolean                  'INI -Indicates if Debug message should shown
Dim SkinPath As String              'Holds path to current skin
Dim imgpath As String               'Hold path to images for skinning

Dim LabelFont As String             'INI -Default label font name for rotated labels
Dim FontMod As Long
Dim OnStr As String                 'INI -Filename for default On status graphic
Dim OffStr As String                'INI -Filename for default Off status graphic
Dim UnknownStr As String            'INI -Filename for default Unknown status graphic
Dim DefWidth As String              'INI or Skin -Default width for a/b buttons (on/off)
Dim DefHeight As String             'INI or Skin -Default height for a/b buttons (on/off)
Dim LightButtonW As Long
Dim LightButtonH As Long
Dim DefOnButton As Picture
Dim DefOffButton As Picture
Dim ButtonCount As Long
Dim DefOnNotSet As Boolean
Dim DefOffNotSet As Boolean
Dim SavedHeight As Long
Dim SavedWidth As Long
Dim DevCount As Long
Dim DevName() As String
Dim DevCmd() As String
Dim DevLoc() As String
Dim EventCount As Long
Dim EventName() As String
Dim onX, onY, offX, offY

Private m_ButtonRegion(30) As Long
Private m_OldFont As Long
Private m_NewFont As Long

Private Sub Form_Load()
    Dim temptag As String               'Temporary holder for values read from INI file
    Set RemoteFile = New DataConnection 'Create new instance of the Web communication class
    StartWinsock vbNullString           'Initialize the Web communication class
    Footer = "<BR></BODY></HTML>"       'and setup Header and Footer variables
    Header = "<HTML><HEAD><TITLE>---</TITLE><META HTTP-EQUIV=" & Chr(34) & "Pragma" & Chr(34) & " CONTENT=" & Chr(34) & "no-cache" & Chr(34) & "><META HTTP-EQUIV=" & Chr(34) & "Expires" & Chr(34) & " CONTENT=" & Chr(34) & "-1" & Chr(34) & "></HEAD><BODY>"

    Web1Busy = False                    'Initialize the Web communication variables
    Web1Response = ""
    Screen.MousePointer = vbHourglass   'Make mouse cursor an hourglass
    Dbg = False                         'Turn of Debugging by default
    imgpath = App.Path & "\images\"     'Set two initial paths
    inifile = App.Path & "\HomePanel.ini"
    
    fReadValue inifile, "Options", "MakeSkin", "S", "0", temptag
    If temptag = "1" Then PrintControl frmMain, "Section1": frmMusic.Show: Unload frmMain: Exit Sub
    
    always_Date.Caption = ""            'Initialize the Date and Time captions
    always_Time.Caption = ""
    always_Custom1.Caption = ""         'Initialize the Custom Functions captions
    always_Custom2.Caption = ""
    
    SavedHeight = 0                     '???
    SavedWidth = 0                      '???
    DefOnNotSet = True                  '???
    DefOffNotSet = True                 '???
    DefWidth = 0                        '???
    DefHeight = 0                       '???
    LightButtonW = 0                    '???
    LightButtonH = 0                    '???

    'By default all Sections are turned off.  These four Flags are Global variables
    Section1 = False: Section2 = False: Section3 = False: Section4 = False
    
    'Read each Section's Enabled flag and set it appropriately
    fReadValue inifile, "Section1", "Enabled", "S", "False", temptag
    If LCase(temptag) = "true" Or temptag = "1" Then Section1 = True
    fReadValue inifile, "Section2", "Enabled", "S", "False", temptag
    If LCase(temptag) = "true" Or temptag = "1" Then Section2 = True
    fReadValue inifile, "Section3", "Enabled", "S", "False", temptag
    If LCase(temptag) = "true" Or temptag = "1" Then Section3 = True
    fReadValue inifile, "Section4", "Enabled", "S", "False", temptag
    If LCase(temptag) = "true" Or temptag = "1" Then Section4 = True
    
    'Now find the first Section that should be displayed.  Screen 1 by default.
    DefSection = 1
    fReadValue inifile, "Options", "StartSection", "S", "1", temptag
    DefSection = CLng(temptag)
    
    'If it is in the INI file, set the overall Background picture.
    'Note this is overridden by the Skin module.
    fReadValue inifile, "Section1", "Background", "S", "err", temptag
    If temptag <> "err" Then frmMain.Picture = LoadPicture(imgpath & temptag)
    
    'Now go apply the Skin for Section 1
    SkinMe "Section1", frmMain
    Screen.MousePointer = vbDefault         'Reset the mouse pointer
    If DefSection = 1 Then                  'If the Default section is 1 then open it.
        Section1 = True                     'First force it to be enabled
        optLightControl_Click               'Now simulate a Click to load everything
        TimerStart                          'Finally start the regular timers
    End If
    'If any of the other Default sections are slected, then enable them and click the button
    If DefSection = 2 Then Section2 = True: optMP3Control_Click
    If DefSection = 3 Then Section3 = True: optWebControl_Click
    If DefSection = 4 Then Section4 = True: optMediaControl_Click
End Sub

Public Sub optLightControl_Click()  'This gets called when the form is loaded
    Dim temptag As String           'Temporary string to hold data from INI
    If Section1 Then                'Make sure Section1 is allowed.
        LoadTags                    'Go Load all the Tags from the INI file
        If Skin <> "" Then          'If there is a skin set, then load the background
            fReadValue Skin, "Section1", "Background", "S", "", temptag
            If temptag <> "" Then frmMain.Picture = LoadPicture(SkinPath & temptag)
        End If
        RedrawRegion                'This will redraw all the odd shaped buttons
        'Fill in the Main Lable at the top of the screen
        fReadValue inifile, "Section1", "MainLabel", "S", "Section 1", temptag
        Me.always_lblControl.Caption = temptag
        'And the Column1 label - this will be changed in the future
        fReadValue inifile, "Section1", "Column1", "S", "Column 1", temptag
        Me.light_column_label.Caption = temptag
        
        ControlOff ("light")
        UpdateHSStatus
    End If
End Sub

Public Sub LoadTags()   'Big procedure to load all the tags from the INI file for buttons
    Dim temptag As String, s As String, i As Long, x As Long, y As Long
    Dim btn As Control, lbl As Control, W As Long
    
    'First lets setup the Web Command Server's address
    fReadValue inifile, "Options", "HostName", "S", "err", HostIP
    fReadValue inifile, "Options", "HostPort", "S", 8888, HostPort
    WebSvr = "http://" & HostIP & ":" & HostPort & "/"
    
    'Now read the Default Label Fonts
    fReadValue inifile, "Options", "DefaultLabelFont", "S", "Arial", LabelFont
    
    'Now check if the application should be run full screen or in a window, default is fullscreen
    fReadValue inifile, "Options", "FullScreen", "S", "1", temptag
    If temptag = "1" Then                   'If fullscreen
        MakeBorderstyle_vbBSNone frmMain    'Remove any exisitng Border and make it none
        frmMain.WindowState = vbMaximized   'Then make it fullscreen by maximizing it
    End If
    
    'Read the default On, Off, and Unknown status graphics from the INI file
    fReadValue inifile, "HomeSeerButtons", "DefaultOn", "S", "light_on.gif", OnStr
    fReadValue inifile, "HomeSeerButtons", "DefaultOff", "S", "light_off.gif", OffStr
    fReadValue inifile, "HomeSeerButtons", "DefaultUnknown", "S", "light_unknown.gif", UnknownStr
    
    'If the default width and height are not already set by skin them read them from INI
    'These widths and height refer to the a/b buttons
    If DefWidth = 0 Then fReadValue inifile, "HomeSeerButtons", "DefaultWidth", "S", 25, DefWidth
    If DefHeight = 0 Then fReadValue inifile, "HomeSeerButtons", "DefaultHeight", "S", 25, DefHeight
    
    'Load the default pictures for On and Off buttons so we can quickly reuse them
    fReadValue inifile, "HomeSeerButtons", "DefaultOnButton", "S", "err", temptag
    If temptag <> "err" And DefOnNotSet Then Set DefOnButton = LoadPicture(imgpath & temptag): DefOnNotSet = False
    fReadValue inifile, "HomeSeerButtons", "DefaultOffButton", "S", "err", temptag
    If temptag <> "err" And DefOffNotSet Then Set DefOffButton = LoadPicture(imgpath & temptag): DefOffNotSet = False
    
    'Read how many buttons we have created from the INI file
    fReadValue inifile, "HomeSeerButtons", "ButtonCount", "S", "0", temptag
    ButtonCount = Val(temptag)
    
    'Now go create that many buttons.
    CreateButtonFrame (ButtonCount)
    
    'Loop through the number of buttons created and set the settings for each
    For i = 1 To ButtonCount
        Set btn = Me.light_a(i)     'Get the "a" button
        'First read if an alternate image is needed for button
        fReadValue inifile, "HomeSeerButtons", "Button" & i & "aImage", "S", "err", temptag
        If temptag <> "err" Then                            'if an alternate image is needed
            btn.Picture = LoadPicture(imgpath & temptag)    'Go load it from the imagepath
        Else
            btn.Picture = DefOnButton                       'Otherwise, load the default
        End If
        'Now lookup the button's default command (code) and apply it.
        fReadValue inifile, "HomeSeerButtons", "Button" & i & "aCode", "S", "err", temptag
        btn.Tag = temptag
        
        Set btn = Me.light_b(i)     'Get the "b" button
        'First read if an alternate image is needed for button
        fReadValue inifile, "HomeSeerButtons", "Button" & i & "bImage", "S", "err", temptag
        If temptag <> "err" Then                            'if an alternate image is needed
            btn.Picture = LoadPicture(imgpath & temptag)    'Go load it from the imagepath
        Else
            btn.Picture = DefOffButton                      'Otherwise, load the default
        End If
        'Now lookup the button's default command (code) and apply it.
        fReadValue inifile, "HomeSeerButtons", "Button" & i & "bCode", "S", "err", temptag
        btn.Tag = temptag
      
        Set btn = Me.light_icon(i)  'Get the icon's control
        If Left(temptag, 2) = "ex" Or Left(temptag, 2) = "ev" Then  'Verify the command is valid
            btn.Tag = Mid(temptag, 1, 5)                            'if it is, set it to the tag
        Else
            btn.Tag = "err"                                         'Otherwise mark the command as "err"
        End If
      
        Set lbl = Me.light_Label(i) 'Get the "a" label
        lbl.Tag = temptag           'Put the code here too
        'Set the mode to Pixels, turn on AutoRedraw, and clear it out
        lbl.ScaleMode = vbPixels: lbl.AutoRedraw = True: lbl.Cls
        'Read the label from INI
        fReadValue inifile, "HomeSeerButtons", "Button" & i & "LabelLine1", "S", "", temptag
        x = 1: y = -1   'Force text to rotate 90 degrees (y=-1) and be the first line (x=1)
        'Draw the text label line 1.
        'The FontMod comes from the Skin and LabelFont from either Skin or INI
        DrawRotatedText lbl, temptag, x, y, LabelFont, 14 + FontMod, 700, 900, False, False, False
        'Draw the text Label line 2.
        fReadValue inifile, "HomeSeerButtons", "Button" & i & "LabelLine2", "S", "", temptag
        x = ((lbl.Width) / 2) + (FontMod / 2) + 1 'Now line 2 is placed based on FontMod
        DrawRotatedText lbl, temptag, x, y, LabelFont, 14 + FontMod, 700, 900, False, False, False
    Next i
    
    'Next set the commands for the two custom text commands.
    fReadValue inifile, "Section1", "InfoCustom1", "S", "err", temptag: always_Custom1.Tag = temptag
    fReadValue inifile, "Section1", "InfoCustom2", "S", "err", temptag: always_Custom2.Tag = temptag
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim i As Long                           'Temp counter
    For i = 1 To (2 * ButtonCount)          'for removing the buttons that we created
        DeleteObject m_ButtonRegion(i)
    Next i
    Set RemoteFile = Nothing                'FreeUp the Web Communication control
    EndWinsock                              'and close all Socket calls
    Set frmMain = Nothing                   'finally destroy the form
End Sub

Private Sub cmdSetup_Click()
    frmOptions.Show                         'Go open the options screen
End Sub

Private Sub light_MessageCommand_Click()
    UpdateHSStatus                          '
End Sub

Private Sub cmd_Exit_Click()
    Unload frmMusic                         'Unload any forms open and end the program
    Unload frmMediaControl
    Unload frmWeb
    Unload frmMain
End Sub

Private Sub light_a_Click(Index As Integer) 'The a button is for on type actions
    btnrun light_a(Index).Tag, Index, "on"
End Sub

Private Sub light_b_Click(Index As Integer) 'The b button is for off type actions
    btnrun light_b(Index).Tag, Index, "off"
End Sub

Private Sub light_cmdExecuteEvent_Click()   '???
    Dim rtn As String
    If Me.light_lstEvent.SelCount > 0 Then
        rtn = SendCmd("ev" & Me.light_lstEvent)
    End If
End Sub

Private Sub btnrun(MeTag As String, bnum As Integer, MeStat As String)
    'This will actually send the button's command to the Web Command Server
    WebCmd1 MeTag       'Sending the tag allows us to send command from the INI file
    OneDeviceSet Format(bnum, "00"), MeStat
End Sub

Public Sub TimerStart()     'Enable the form's timers
    Timer2.Enabled = True:  Timer3.Enabled = True
End Sub

Public Sub TimerStop()      'Disable the form's timers
    Timer2.Enabled = False:  Timer3.Enabled = False
End Sub

Public Sub optMP3Control_Click()
    If Section2 Then
        frmMain.TimerStop
        frmMusic.Show
        frmMain.Hide
        frmMusic.TimerStart
    End If
End Sub

Public Sub optWebControl_Click()
    If Section3 Then
        frmMain.TimerStop
        frmWeb.Show
        frmMain.Hide
        frmWeb.TimerStart
    End If
End Sub

Public Sub optMediaControl_Click()
    If Section4 Then
        frmMain.TimerStop
        frmMediaControl.Show
        frmMain.Hide
        frmMediaControl.TimerStart
    End If
End Sub

Public Sub ControlOff(t As String)      'This loops through all the control and hides those hat should be
    Dim tempctl As Control, x As String
    For Each tempctl In frmMain
        x = tempctl.Name                    'Get selected control's name
        If LCase(Left(x, 5)) = "light" Then 'Start by hiding all the light... controls
           tempctl.Visible = False
        Else
            'Otherwise unhide things with always... or opt... names
            If t <> "all" And x <> "winsock1" And Left(x, 5) <> "Timer" And Left(x, 4) <> "Inet" Then tempctl.Visible = True
        End If
        
        'Now unhide the controls given in the t variable.
        If t = Left(x, Len(t)) And tempctl.Tag <> "err" Then tempctl.Visible = True
        
        'And unhide the light_status... controls
        If Left(x, 13) = "light_status_" Then tempctl.Visible = False
        
        'Finally set the light_icons to the proper Web address.  They are used for animated web icons.
        If Left(x, 10) = "light_icon" Then tempctl.Navigate "about:<html><body leftmargin='0' topmargin='0' scroll='no' bgcolor=#DADADA></body></html>"
    Next tempctl
End Sub

Public Sub UpdateHSStatus() 'This loops through and gets the latest status from Web Command Server
    Dim t1 As Control, t2 As Control 't1 gets icon and t2 gets the status
    Dim d As String, i As Long
    For i = 1 To ButtonCount
        Set t1 = light_icon(i)
        Set t2 = light_status(i)
        d = t1.Tag                  'Get the tag for the icon - this holds the command
        If Left(d, 2) = "ex" Or Left(d, 2) = "ev" Then 'The command must be an execute or an event type
            t1.Visible = True               'Valid command so show icon
            OneDeviceSet Format(i, "00")    'And read everything for device from INI
        Else
            t2 = "err"                      'Not a valid command
            t1.Visible = False              'Just hide the icon and keep going
        End If
    Next i
End Sub

Public Sub OneDeviceSet(i As String, Optional s As String)
    'This will draw the icon based on status and what was in the INI file
    Dim t1 As Control, t2 As Control, d As String, x As String
    Set t1 = light_icon(CInt(i))
    Set t2 = light_status(CInt(i))
    d = t1.Tag
    If IsMissing(s) Or s = "" Then s = StatusCode(Mid(d, 3, 3))
    t2 = s
    Select Case s
    Case "on"
        fReadValue inifile, "HomeSeerButtons", "Button" & CInt(i) & "Image_Width", "S", DefWidth, x
        t1.Width = CInt(x) + 4
        fReadValue inifile, "HomeSeerButtons", "Button" & CInt(i) & "Image_Height", "S", DefHeight, x
        t1.Height = CInt(x) + 4
        fReadValue inifile, "HomeSeerButtons", "Button" & CInt(i) & "Image_On", "S", OnStr, x
        t1.Navigate "about:<html><body leftmargin='0' topmargin='0' scroll='no' bgcolor=#DADADA><img src='" & imgpath & x & "'></img></body></html>"
    Case "off"
        fReadValue inifile, "HomeSeerButtons", "Button" & CInt(i) & "Image_Width", "S", DefWidth, x
        t1.Width = CInt(x) + 4
        fReadValue inifile, "HomeSeerButtons", "Button" & CInt(i) & "Image_Height", "S", DefHeight, x
        t1.Height = CInt(x) + 4
        fReadValue inifile, "HomeSeerButtons", "Button" & CInt(i) & "Image_Off", "S", OffStr, x
        t1.Navigate "about:<html><body leftmargin='0' topmargin='0' scroll='no' bgcolor=#DADADA><img src='" & imgpath & x & "'></img></body></html>"
    Case "unknown"
        fReadValue inifile, "HomeSeerButtons", "Button" & CInt(i) & "Image_Width", "S", DefWidth, x
        t1.Width = CInt(x) + 4
        fReadValue inifile, "HomeSeerButtons", "Button" & CInt(i) & "Image_Height", "S", DefHeight, x
        t1.Height = CInt(x) + 4
        fReadValue inifile, "HomeSeerButtons", "Button" & CInt(i) & "Image_Unknown", "S", UnknownStr, x
        t1.Navigate "about:<html><body leftmargin='0' topmargin='0' scroll='no' bgcolor=#DADADA><img src='" & imgpath & x & "'></img></body></html>"
    Case Else
        t1.Visible = False
    End Select
End Sub

Public Function gc(n As String) As Control
    'Simple get control function, return the handle for the requested control
    Dim tempctl As Control
    For Each tempctl In frmMain
        If tempctl.Name = n Then Set gc = tempctl: Exit For
    Next tempctl
End Function

Public Function StatusCode(s As String) As String
    'Gets the current status code for the given device in S
    Dim tmpStr As String
    tmpStr = WebCmd1("st" & s)
    StatusCode = "unknown"
    If tmpStr = "on" Or tmpStr = "off" Then StatusCode = tmpStr
End Function

Public Function SendCmd(s As String) As String
    'Function to send the command tot eh Web command server
    Dim tmpStr As String
    tmpStr = WebCmd1(s)
    SendCmd = "?"
    If tmpStr <> "none" Then SendCmd = tmpStr
End Function

Private Sub Timer2_Timer() 'Timer to update the date and time
    If Me.always_Date.Caption <> Format(Date, "mmm-dd-yyyy") Then Me.always_Date.Caption = Format(Date, "mmm-dd-yyyy")
    Me.always_Time.Caption = Format(Now, "hh:mm:ss AM/PM")
End Sub

Private Sub Timer3_Timer() 'This time will update the Status and send the custom commands
    Timer3.Enabled = False
    If Timer3.Tag = 2 Then
        Timer3.Interval = 65535
        Timer3.Tag = 50
        UpdateHSStatus
        GetDeviceList
    End If
    If Timer3.Tag < 2 Then
        Timer3.Tag = Timer3.Tag + 1
    Else
        If Me.always_Custom1.Tag <> "err" Then Me.always_Custom1.Caption = SendCmd(Me.always_Custom1.Tag)
        If Me.always_Custom2.Tag <> "err" Then Me.always_Custom2.Caption = SendCmd(Me.always_Custom2.Tag)
    End If
    Timer3.Enabled = True
End Sub

Private Sub CreateButtonFrame(Index As Long) 'Creates a number of buttons equal to Index
    Dim temptag As String, i As Long, pos As Variant
    For i = 1 To Index
        On Error Resume Next
        Load Me.light_a(i): Load Me.light_b(i): Load Me.light_icon(i)
        Load Me.light_Label(i): Load Me.light_status(i)
        fReadValue inifile, "HomeSeerButtons", "Button" & i & "_PosXY", "S", "err", temptag
        If temptag <> "err" And temptag <> "" Then
            pos = Split(temptag, ",", 2)
            If Err.Number <> 0 Then Err.Clear
            Me.light_Label(i).Move pos(0), pos(1)
          Else
            Me.light_Label(i).Move Me.light_Label(i - 1).Left, Me.light_Label(i - 1).Top + Me.light_Label(i - 1).Height + 3
        End If
        Me.light_a(i).Move Me.light_Label(i).Left + Me.light_Label(i).Width, Me.light_Label(i).Top + (Me.light_Label(i).Height - Me.light_a(i).Height) / 2
        Me.light_b(i).Move Me.light_a(i).Left + LightButtonW, Me.light_a(i).Top
        Me.light_icon(i).Move Me.light_b(i).Left + Me.light_b(i).Width, Me.light_Label(i).Top + (Me.light_Label(i).Height - Me.light_icon(i).Height) / 2
        Me.light_Label(i).Visible = True: Me.light_a(i).Visible = True
        Me.light_b(i).Visible = True: Me.light_icon(i).Visible = True
    Next i
End Sub

Private Sub SizeForm()
    Dim x As Long, y As Long
    If frmMain.WindowState <> vbMinimized Then
        If frmMain.Width < 12050 Then frmMain.Width = 12050
        If frmMain.Height < 9350 Then frmMain.Height = 9350
        x = frmMain.ScaleWidth
        y = frmMain.ScaleHeight
        If x = SavedWidth And y = SavedHeight Then Exit Sub
        SavedWidth = x: SavedHeight = y
    End If
End Sub

Private Sub RedrawRegion()  'This masks the region specified to allow for polygon button shapes
    Dim shape_on(1 To 4) As POINTAPI, shape_off(1 To 4) As POINTAPI
    Dim btn As Control, i As Long
    For i = 1 To 4
        shape_on(i).x = Val(onX(i - 1)): shape_on(i).y = Val(onY(i - 1))
        shape_off(i).x = Val(offX(i - 1)): shape_off(i).y = Val(offY(i - 1))
    Next i
    For i = 1 To ButtonCount
        m_ButtonRegion(i) = CreatePolygonRgn(shape_on(1), UBound(shape_on), ALTERNATE)
        Set btn = light_a(i)
        SetWindowRgn btn.hwnd, m_ButtonRegion(i), True
        m_ButtonRegion(i + 15) = CreatePolygonRgn(shape_off(1), UBound(shape_off), ALTERNATE)
        Set btn = light_b(i)
        SetWindowRgn btn.hwnd, m_ButtonRegion(i + 15), True
    Next i
End Sub

Private Sub DrawRotatedText(ByVal target As Object, ByVal txt As String, ByVal x As Single, _
    ByVal y As Single, ByVal font_name As String, ByVal size As Long, _
    ByVal weight As Long, ByVal escapement As Long, ByVal use_italic As Boolean, _
    ByVal use_underline As Boolean, ByVal use_strikethrough As Boolean)
    'Draws Rotated text of the specified font size and face.
    Dim newfont As Long, oldfont As Long, W As Long
    newfont = CreateFont(size, 0, escapement, escapement, weight, use_italic, _
        use_underline, use_strikethrough, 0, 0, CLIP_LH_ANGLES, 2, 0, font_name)
    oldfont = SelectObject(target.hdc, newfont)
    If x < 0 Then x = (target.Width + target.TextHeight(txt)) / 2
    If y < 0 Then y = (target.Height + target.TextWidth(txt)) / 2
    
    target.CurrentX = x: target.CurrentY = y
    target.Print txt
    newfont = SelectObject(target.hdc, oldfont)
    DeleteObject newfont
End Sub

Public Sub SkinMe(Section As String, frm As Form)   'Applies the Skin to the form
    Dim ctl As Control              'Temporary Control variable
    Dim temptag As String           'Temporary String for values from INI files
    Dim pos As Variant              'A two digit array for x,y position
    Dim size As Variant             'A two digit array for height and width
    Dim c As Variant                'A three digit array for RGB color
    Skin = "": SkinPath = ""        'First clear the exisitng Skin and SkinPath
    'Now read the skin from the INI file
    fReadValue inifile, "Options", "Skin", "S", "", Skin
    If Skin = "" Then Exit Sub      'Exit if no skin is specified - should not happen
    
    'Set the skin's path off the skin directory and specify the Skin INI file
    SkinPath = App.Path & "\skin\" & Skin & "\"
    Skin = SkinPath & "skin.ini"
    
    'Now read the FontMod value - note this overrides any value in the program INI file
    fReadValue Skin, Section, "FontSizeMod", "S", "0", temptag
    FontMod = Val(temptag)
    
    'Now read the default button width and height
    fReadValue Skin, Section, "LightButtonWidth", "S", 80, LightButtonW
    fReadValue Skin, Section, "LightButtonHeight", "S", 60, LightButtonH
    
    'Loop through each control on the page to get it position, size, color, and other attributes
    'Also, trap any errors that may occur since not all controls will have all these attributes
    For Each ctl In frm
        On Error Resume Next    'If an Error occurs just keep going
        Err.Clear               'An clear any prior errors
        
        'Read if any default images are defined for the control and load the pictures if there are
        fReadValue Skin, Section, "Image_" & ctl.Name, "S", "", temptag
        'If no picture was defined then make sure the control is cleared
        If LCase(temptag) = "none" Then ctl.Picture = LoadPicture(): temptag = ""
        'If a picture is defined try to load it.
        If temptag <> "" Then ctl.Picture = LoadPicture(SkinPath & temptag)
        If Err.Number <> 0 Then Err.Clear       'If an error was raised, clear it.
        
        'Set the control's font size (modified by the FontMod value)
        ctl.FontSize = ctl.FontSize + FontMod
        If Err.Number <> 0 Then Err.Clear       'If an error was raised, clear it.
        
        'Read the control's x,y position
        fReadValue Skin, Section, "PosXY_" & ctl.Name, "S", "", temptag
        If temptag <> "" Then                   'if there is an x,y position defined
            pos = Split(temptag, ",", 2)        'parse it into the pos array
            If Err.Number <> 0 Then Err.Clear   'If an error was raised, clear it.
            ctl.Move pos(0), pos(1)             'try to move the control to the x,y location
            If Err.Number <> 0 Then Err.Clear   'If an error was raised, clear it.
        End If
        
        'Read the FontName defined for the control
        fReadValue Skin, Section, "FontName_" & ctl.Name, "S", "", temptag
        If temptag <> "" Then ctl.Font = temptag    'If a font was specified, apply it
        If Err.Number <> 0 Then Err.Clear       'If an error was raised, clear it.
        
        'Read the RGB value for the control
        fReadValue Skin, Section, "FontColorRGB_" & ctl.Name, "S", "", temptag
        c = Split(temptag, ",")                 'Parse the RGB
        If Err.Number <> 0 Then Err.Clear: temptag = ""  'If an error was raised, clear it.
        If temptag <> "" Then                       'If a color was specified,
            ctl.ForeColor = RGB(c(0), c(1), c(2))   'Try to apply it
            If Err.Number <> 0 Then Err.Clear       'If an error was raised, clear it.
        End If
        
        'Read the Font Size specified for the Control.  This overrides any FontMod.
        fReadValue Skin, Section, "FontSize_" & ctl.Name, "S", "", temptag
        If temptag <> "" Then ctl.FontSize = Val(temptag)   'if specified, apply it
        If Err.Number <> 0 Then Err.Clear       'If an error was raised, clear it.
        
        'Read any border color/style specified
        fReadValue Skin, Section, "BorderColorRGB_" & ctl.Name, "S", "", temptag
        If LCase(temptag) = "none" Then         'None means transparent border style
            ctl.BorderStyle = 0
            If Err.Number <> 0 Then Err.Clear   'If an error was raised, clear it.
        Else
            c = Split(temptag, ",")             'Parse the RGB color
            If Err.Number <> 0 Then Err.Clear: temptag = "" 'If an error was raised, clear it.
            If temptag <> "" Then
                ctl.BorderColor = RGB(c(0), c(1), c(2))
                If Err.Number <> 0 Then Err.Clear           'If an error was raised, clear it.
            End If
        End If
        
        'Read any background color specified
        fReadValue Skin, Section, "BackColorRGB_" & ctl.Name, "S", "", temptag
        If LCase(temptag) = "none" Then         'None means transparent
            ctl.BackStyle = 0
            If Err.Number <> 0 Then Err.Clear   'If an error was raised, clear it.
        Else
            If temptag <> "" Then
                ctl.BackStyle = 1               'Otherwise the style is filled with color
                If Err.Number <> 0 Then Err.Clear           'If an error was raised, clear it.
                c = Split(temptag, ",")                     'Parse the color
                If Err.Number <> 0 Then Err.Clear: temptag = ""
                If temptag <> "" Then
                    ctl.BackColor = RGB(c(0), c(1), c(2))   'Apply the color
                    If Err.Number <> 0 Then Err.Clear
                End If
            End If
        End If

        'Read the height and width for control
        fReadValue Skin, Section, "SizeWH_" & ctl.Name, "S", "", temptag
        'If not size is specified for light control apply defaults
        If temptag = "" And Left(ctl.Name, 7) = "light_B" Then temptag = LightButtonW & "," & LightButtonH
        'Otherwise try to process the size value given
        If temptag <> "" Then
            size = Split(temptag, ",", 2)                   'parse the size
            If Err.Number <> 0 Then Err.Clear               'If an error was raised, clear it.
            ctl.Move ctl.Left, ctl.Top, size(0), size(1)    'Set the control's size
            If Err.Number <> 0 Then Err.Clear               'If an error was raised, clear it.
            'If the control is a line, then we need to use the size to specify its length
            If Left(ctl.Name, 5) = "line_" Then
                ctl.X1 = pos(0): ctl.X2 = pos(0) + size(0)  'The line's length
                ctl.Y1 = pos(1): ctl.Y2 = pos(1)
            End If
        End If
    Next ctl
    
    'Read the form's overall Background graphic
    fReadValue Skin, Section, "Background", "S", "", temptag
    If temptag <> "" Then frm.Picture = LoadPicture(SkinPath & temptag)
    
    'The following read the Regions specified for the polygon buttons
    fReadValue Skin, Section, "OnXRegion", "S", "5,75,64,5", temptag
    onX = Split(temptag, ",")
    If Err.Number <> 0 Then Err.Clear       'If an error was raised, clear it.
    fReadValue Skin, Section, "OnYRegion", "S", "5,5,55,55", temptag
    onY = Split(temptag, ",")
    If Err.Number <> 0 Then Err.Clear       'If an error was raised, clear it.
    fReadValue Skin, Section, "OffXRegion", "S", "16,75,75,5", temptag
    offX = Split(temptag, ",")
    If Err.Number <> 0 Then Err.Clear       'If an error was raised, clear it.
    fReadValue Skin, Section, "OffYRegion", "S", "5,5,55,55", temptag
    offY = Split(temptag, ",")
    If Err.Number <> 0 Then Err.Clear       'If an error was raised, clear it.
    
    'Read the default On/Off button graphics and default width/height if they are defined in the Skin
    fReadValue Skin, Section, "DefaultOnButton", "S", "err", temptag
    If temptag <> "err" Then Set DefOnButton = LoadPicture(SkinPath & temptag): DefOnNotSet = False
    fReadValue Skin, Section, "DefaultOffButton", "S", "err", temptag
    If temptag <> "err" Then Set DefOffButton = LoadPicture(SkinPath & temptag): DefOffNotSet = False
    fReadValue Skin, Section, "DefaultWidth", "S", 0, DefWidth
    fReadValue Skin, Section, "DefaultHeight", "S", 0, DefHeight
End Sub

Private Sub GetDeviceList()
    Dim i As Long, temptag As String
    temptag = SendCmd("device:count"): DevCount = Val(temptag)
    temptag = SendCmd("et:count"): EventCount = Val(temptag)
    ReDim DevName(DevCount + 1): ReDim DevCmd(DevCount + 1)
    ReDim DevLoc(DevCount + 1): ReDim EventName(EventCount + 1)
    For i = 1 To DevCount
        DevName(i) = SendCmd("device:name:" & i)
        DevCmd(i) = "ex" & SendCmd("device:code:" & i)
        DevLoc(i) = SendCmd("device:location:" & i)
    Next i
    Me.light_lstEvent.Clear
    For i = 1 To EventCount
        EventName(i) = SendCmd("et:name:" & i)
        Me.light_lstEvent.AddItem EventName(i)
    Next i
    
End Sub

Private Sub PrintControl(frm As Form, sect As String)       'Used in Development mode to create skins
    Dim c As Control, x As String, temptag As String
    Skin = "": SkinPath = ""
    fReadValue inifile, "Options", "Skin", "S", "", Skin
    If Skin = "" Then Exit Sub
    SkinPath = App.Path & "\skin\" & Skin & "\"
    Skin = SkinPath & "skin.ini"
    For Each c In frm
        x = c.Name
        If LCase(Left(x, 5)) <> "timer" And LCase(Left(x, 5)) <> "winso" And LCase(Left(x, 5)) <> "line_" Then
            x = "PosXY_" & c.Name
            fReadValue Skin, sect, x, "S", "err", temptag
            If temptag <> "err" Then fWriteValue Skin, sect, x, "S", c.Left & "," & c.Top
            x = "SizeWH_" & c.Name
            fReadValue Skin, sect, x, "S", "err", temptag
            If temptag <> "err" Then fWriteValue Skin, sect, x, "S", c.Width & "," & c.Height
            x = "FontSize_" & c.Name
            fReadValue Skin, sect, x, "S", "err", temptag
            If temptag <> "err" Then fWriteValue Skin, sect, x, "S", c.FontSize
            x = "FontColorRGB_" & c.Name
            fReadValue Skin, sect, x, "S", "err", temptag
            If temptag <> "err" Then
                fWriteValue Skin, sect, x, "S", RGBS(c.ForeColor)
            End If
            x = "BorderColorRGB_" & c.Name
            fReadValue Skin, sect, x, "S", "err", temptag
            If temptag <> "err" Then
                fWriteValue Skin, sect, x, "S", RGBS(c.BorderColor)
            End If
            x = "BackColorRGB_" & c.Name
            fReadValue Skin, sect, x, "S", "err", temptag
            If temptag <> "err" Then
                fWriteValue Skin, sect, x, "S", RGBS(c.BackColor)
            End If
        End If
        If LCase(Left(x, 5)) = "line_" Then
            x = "PosXY_" & c.Name
            fReadValue Skin, sect, x, "S", "err", temptag
            If temptag <> "err" Then fWriteValue Skin, sect, x, "S", c.X1 & "," & c.Y1
            x = "SizeWH_" & c.Name
            fReadValue Skin, sect, x, "S", "err", temptag
            If temptag <> "err" Then fWriteValue Skin, sect, x, "S", (c.X2 - c.X1) & "," & (c.Y2 - c.Y1)
        End If
    Next c
End Sub

Private Function WebCmd1(strURL As String) As String    'Sends command to server and
    Dim tmpStr As String                                '  gets an answer
    Do While Web1Busy               'Are we already running another Web command?
        DoEvents                    'if yes, loop until it is cleared
    Loop
    tmpStr = FetchURL(strURL)       'Call the actual Fetch routine with Command, get answer
    If Len(tmpStr) > 3 Then         'hopefully the answer is always greater than 3 characters
        tmpStr = Replace(Replace(tmpStr, Header, ""), Footer, "")   'Remove Header and Footer
    End If
    WebCmd1 = tmpStr                'Return the answer
End Function

Private Function WebCmdSilent(strURL As String)         'Sends command to server and
    Dim tmpStr As String                                '  ignores the answer
    Do While Web1Busy               'Are we already running another Web command?
        DoEvents                    'if yes, loop until it is cleared
    Loop
    tmpStr = FetchURL(strURL)       'Call the actual Fetch routine with Command
End Function

Private Function FetchURL(tmpStr As String) As String   'Here is actual routine to talk to server
RestartURL:                         'Return here on retries
    Web1Busy = True                 'Set the Web1Busy flag so we don't step on each other
    Web1Response = ""               'Clear the response variable
    With RemoteFile                 'Use the Web communication class object to...
        .DownloadType = dtToBuffer  'Set the Type to Buffer - best for our transmissions
        .AutoRedirect = True        'Really doesn't matter but should be set anyway
        .AllowCache = False         'Do not want to cache pages or we will get lots of bad data
        .UseResume = False          'Clear the Resume flag so we can safely Disconnect
        .Disconnect                 'Make sure any current connections are cleared
        .UseHttpAuthorization = True    'Really doesn't matter but should be set anyway
        .UseResume = True           'Will try to resume failed downloads - rarely used
        'If we are getting the entire list then increase the packet size for speed
        If tmpStr = "winamp:playlist:title:all" Or tmpStr = "winamp:playlist:file:all" Then
            .PacketSize = 2048      '2K is probably enough.  Good for lists over 3000 songs
        Else
            .PacketSize = 1024      '1K is good for most communications
        End If
        .FetchURLString WebSvr & tmpStr 'Set the URL to be sent
        .MaxDownload = -1               'There is no limit on the size of the message we can get
    End With
    Do While Web1Busy               'Now loop until we get a complete answer
        DoEvents                    'Be sure to keep doing normal events
    Loop
    If Web1Response = "error" Or Web1Response = "" Then GoTo RestartURL  'Retry
    FetchURL = Web1Response         'Send Response
    Web1Response = ""               'Clear it for the next message
End Function

Private Sub RemoteFile_Disconnected(ID As Long)     'Event fired when Disconnected
    If RemoteFile.DownloadType = dtToBuffer Then    'If we are using buffer then
        Web1Response = RemoteFile.GetBufferAsString 'get the message and
        RemoteFile.ClearBuffer                      'clear it
    End If
    Web1Busy = False                                'No longer waiting for answer!
End Sub

Private Sub RemoteFile_DownloadFailed(Why As DOWNLOAD_FAILURE, ID As Long)
    'Uhoh... the download failed :(
    If Dbg Then If MsgBox("Communication failure:" & vbCrLf & RemoteFile.FailureToString(Why) & vbCrLf & vbCrLf & "Do you want to quit?", vbYesNo + vbExclamation, "Comm failure.") = vbYes Then End: Exit Sub
    Web1Response = "error"                          'This will trigger a retry
    Web1Busy = False                                'No longer waiting for answer!
End Sub


