Logging on to a VMS host (VBScript)

In this example, a VT session is embedded in the browser window, and VBScript, which is available only in Internet Explorer, is used to log on to the host computer. Two <form> fields on the web page are used to get the user's name and password before performing the logon. Before the logon is actually initiated, a loop with a time delay is used to determine if Reflection is initialized and ready for additional commands. A related example shows a JavaScript version of this script.

<html>
<head>
<title>Sample Logon Script for VT (VBScript)</title>
<script language="VBScript">
<!--
    ' Script-level variables shared by various functions.
    Dim strUname
    Dim strPword
    Dim intInited
    Dim intCount
    Dim objAppletTarget
    Dim objAPI
    
    '****************************************************
    ' Handle a click on the Logon button. This first 
    ' initializes the script-level variables, and then validates
    ' the form inputs. If the form inputs are okay, 
    ' DoTryInit is called to start the action.
    '****************************************************
    Sub Logon_OnClick()
        Call DoInitVariables()
        If blnValidateInputs() = True Then
            Call DoTryInit()
        Else
            Exit Sub
        End If
    End Sub
    
    '****************************************************
    ' Initialize the script-level variables.
    '****************************************************
    Sub DoInitVariables()
        strUname = ""
        strPword = ""
        intInited = 0
        intCount = 0
        objAppletTarget = Null
        objAPI = Null
    End Sub
    
    '****************************************************
    ' Check for a username and password, and if either is
    ' not present, display a message box and return False.
    ' If the username and password are filled in, return
    ' True.
    '****************************************************
    Function blnValidateInputs()
        strUname = Document.LogonForm.Username.Value
        strPword = Document.LogonForm.Password.Value
        If strUname = "" Then
            Alert "You must enter a Username to log on!"
            Document.LogonForm.Username.Focus()
            validateInputs = False
            Exit Function
        End If
        
        If strPword = "" Then
            Alert "You must enter a Password to log on!"
            Document.LogonForm.Password.Focus()
            blnValidateInputs = False
            Exit Function
        End If
        blnValidateInputs = True
    End Function

    '****************************************************
    ' This procedure gets a reference to the Reflection
    ' applet on the page and stores it in "objAppletTarget".
    ' If that succeeds, it then calls DoGetAPI() to get
    ' the Reflection API.
    Sub DoTryInit()
        On Error Resume Next
        Set objAppletTarget = Document.VT
        If objAppletTarget = Null Then
            Alert "Could not get applet reference. The script is stopping now."
            Exit Sub
        Else
            DoGetAPI
        End If
    End Sub
    
    '***************************************************
    ' This procedure gets the Reflection JSAPI object for
    ' the terminal session on the web page. If the API
    ' object is null, it rechecks every 2 seconds. When
    ' the API object is available, the procedure then
    ' gets the Reflection session's initialization status,
    ' again pausing 2 seconds between attempts, before calling
    ' DoConnect. For more information about determining
    ' Reflection's initialization status, see Using an onLoad
    ' Handler to Determine When Reflection Is Initialized.
    '****************************************************
    Sub DoGetAPI()
       Set objAPI = objAppletTarget.getAPI("JSAPI", "VT")
       If objAPI = Null Then
          SetTimeout "DoGetAPI()", 2000
       Else
          On Error Resume Next
          intInited = objAPI.getInitialized()
          If intInited = 0 Then
             If intCount < 10 Then
                intCount = intCount + 1
                SetTimeout "DoGetAPI()", 2000
             Else
                Alert "Reflection is not initialized. Unable to continue."
             End If
          ElseIf intInited > 0 Then
             DoConnect
          Else
             Alert "Reflection initialization failed. Unable to continue."
          End If
       End If
    End Sub
    
    '****************************************************
    ' This function starts the actual logon tasks. It 
    ' determines if Reflection is connected, and if so, 
    ' calls the function that finds the Username and 
    ' Password prompts on the display and transmits the 
    ' user's data.
    '****************************************************
    Sub DoConnect()

        ' If there's no connection, try to connect.
        If objAPI.isConnected() = False Then
            objAPI.connect()
        Else
            Alert "You're already connected!"
            Exit Sub
        End If

        ' Wait 5 seconds for the Username prompt.
        If objAPI.waitForString( "Username:", 5000 ) = False Then
            Alert "Unable to find Username prompt. The script will stop, " & _ 
                    "but you may continue manually."
            Exit Sub
        End If

        ' Username prompt was found, so transmit the user's username
        ' followed by a carriage return. The VBScript string constant
        ' "vbCR" is used to represent the carriage return. You can
        ' also use the expression "Chr(13)," which is the decimal 
        ' representation of the carriage return character.
        objAPI.transmitString( strUname & vbCR )

        ' Wait 5 seconds for the Password prompt.
        If objAPI.waitForString( "Password:", 5000 ) = False Then
            Alert "Unable to find Password prompt. The script will stop, " & _
                    "but you can try completing the logon manually."
            Exit Sub
        End If

        ' Password prompt was found, so transmit the user's password,
        ' followed by a carriage return. As above, the VBScript constant
        ' "vbCR" is used for the carriage return character.
        objAPI.transmitString( strPword & vbCR )
        
        ' Set the focus to the terminal display.
        objAPI.requestDisplayFocus()

    End Sub
    
// -->
</script>
</head>
<body>
<h1>Reflection for the Web -- VT Logon</h1>
<p>
<!--
   Create a basic VT applet on the page but don't connect until
   the user fills in the username and password information.
   To use this example, change the hostURL parameter to one 
   appropriate for your network.
-->
<applet mayscript name="VT"
        codebase="./ex/"
        code="com.wrq.rweb.Launcher.class"
        width="600" height="400"
        archive="Launcher.jar">
    <param name="hostURL" value="telnet://payroll">
    <param name="autoconnect" value="false">
    <param name="frame" value="false">
    <param name="launcher.sessions" value="VT">
</applet>
</p>
<!--
   Use <form> items to accept the Username and Password, and to create
   a Logon button.
-->
<form name="LogonForm">
<p>Username: <input type="text" name="Username" default size="25"></p>
<p>Password: <input type="password" name="Password" size="25"></p>
<p><input type="button" name="logon" value="Logon"></p>
</form>
</body>
</html>