Attribute VB_Name = "Module1"
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Sess As Object
Public ConnID As String
Public ConnMgr As Object
Public NumConnections As Long
Public LoggedOn As Boolean
Public HistoryString As String
Public UseHistory As Boolean
Public MailItem As Integer

Sub Main()
    ' Show splash screen
    frmSplash.Show
    frmSplash.Refresh
    Call Sleep(3000)
    Unload frmSplash
    
    ' Set up our PCOMM objects
    Set ConnMgr = CreateObject("PCOMM.autECLConnMgr")
    Set Sess = CreateObject("PCOMM.autECLSession")
    
    ' Show connection list dialog
    Call GetToOV
    
    ' Show the main window
    frmMain.Show
    
End Sub

Public Function GetToOV()
    Dim AtOVVM As Boolean
    Dim i As Integer

    ' Determine the number of connections
    ConnMgr.autECLConnList.Refresh
    NumConnections = ConnMgr.autECLConnList.Count
    
    ' Show the connections dialog to ask user to choose connection
    frmConnections.Show vbModal
    If Not frmConnections.OK Then
        'Login Failed so exit app
        End
    End If

    ' Set the session for the connection
    Sess.SetConnectionByName ConnID
    
    AtOVVM = False
    While Not AtOVVM
        ' Already at Office Vision main screen
        If Sess.autECLPS.SearchText("A00") Then
            AtOVVM = True
        ' Already at mail screen
        ElseIf Sess.autECLPS.SearchText("ML01") Then
            AtOVVM = True
        ' Logon if necessary
        ElseIf Sess.autECLPS.SearchText("VM/ESA ONLINE") Then
            LoggedOn = False
            i = 1
            While Not LoggedOn
                frmLogin.Show vbModal
                If Not frmLogin.OK Then
                    'Login Failed so exit app
                    End
                End If
                
                ' Catch a bad password.  Allow a retry only three times
                If Sess.autECLPS.SearchText("PASSWORD NOT AUTHORIZED") Then
                    Sess.autECLPS.SendKeys "logoff[enter]"
                    If i = 3 Then
                        MsgBox "You've exceeded the max. number of logon tries.  Ending program..."
                        End
                    Else
                        MsgBox "Invalid password.  Try again."
                    End If
                Else
                    ' Wait for VM ready screen
                    If WaitForScreen("Ready", "RUNNING", 500, 100) Then
                        LoggedOn = True
                    End If
                End If
                i = i + 1
            Wend
            Unload frmLogin
        ' At a ready prompt so need to get to Office Vision
        ElseIf Sess.autECLPS.SearchText("Ready;") Then
            Sess.autECLPS.SendKeys "profs[enter]"
            If Not WaitForScreen("A00", "OfficeVision", 500, 100) Then
                MsgBox "Couldn't get to OfficeVision"
                End
            End If
            Sess.autECLPS.SendKeys "[pf2]"
            If Not WaitForScreen("ML01", "In-Basket", 500, 100) Then
                MsgBox "Couldn't get to mail"
                End
            End If
        ' Make user get to Ready prompt
        Else
            MsgBox "Please go to the emulator window and get to a Ready; screen."
        End If
    Wend
    
    frmMain.Show
        
End Function

' Utility function that waits for a connection to finish starting.
' You may use this function for your needs.  Put it after a call
' to autECLConnMgr.StartConnection
Function WaitForConnection(ConnName As String) As Boolean
    Dim Conn As Object
    Dim Ready As Boolean
    Dim Hand As Long
    
        
    WaitForConnection = False
    Ready = False
    While Ready = 0
        ConnMgr.autECLConnList.Refresh
        Set Conn = ConnMgr.autECLConnList.FindConnectionByName(ConnName)
        If Conn Is Nothing Then
            Call Sleep(1000)
        Else
            Ready = Conn.Ready
        End If
    Wend
    
    WaitForConnection = True
End Function

' Utility function that waits for a screen to come up on the PS.
' You may use this function for your needs.  Put it after a call
' to autECLPS.SendKeys with a mnemonic key like "[enter]".
Public Function WaitForScreen(strScrID1 As String, strScrID2 As String, SleepTime As Integer, MaxSleep As Integer) As Boolean
    Dim i As Integer
    Dim ProgressString As String
    
    WaitForScreen = False
    i = 0
    While i <= MaxSleep
        ' This is VM specific.  It gets rid of the bulletin screens if any.
        If Sess.autECLPS.SearchText("MORE...") Then
            Sess.autECLPS.SendKeys "[clear]"
            Call Sleep(SleepTime)
        ' This is VM specific.  It gets rid of the bulletin screens if any.
        ElseIf Sess.autECLPS.SearchText("HOLDING") Then
            Sess.autECLPS.SendKeys "[clear]"
            Call Sleep(SleepTime)
        ' This is VM specific.  It connects if the VM session was disconnected.
        ElseIf Sess.autECLPS.SearchText("RECONNECTED AT") Then
            Sess.autECLPS.SendKeys "[clear]"
            Sess.autECLPS.SendKeys "b[enter]"
            Call Sleep(SleepTime)
        ' This one is pretty universal.
        ElseIf Sess.autECLPS.SearchText(strScrID1) And Sess.autECLPS.SearchText(strScrID2) Then
            WaitForScreen = True
            Exit Function
        Else
            Call Sleep(SleepTime)
        End If
        
        i = i + 1
    Wend
    
End Function

' Utility function that waits for the autECLOIA object to indicate
' that InputInhibited is false.  If it's false, we know we can enter
' text onto the PS.
Public Function WaitForInput() As Boolean
    Dim ReadyForInput As Boolean
    
    ReadyForInput = False
    While Not ReadyForInput
        If Sess.autECLOIA.InputInhibited = 0 Then
            ReadyForInput = True
        End If
    Wend

End Function

