<HTML>
<!-- Copyright (c) Microsoft Corporation.  All rights reserved.-->
<HEAD>

<TITLE>Hostname Setting Page</TITLE>

<SCRIPT LANGUAGE="VBScript">
<!--
    Option Explicit
    
    'Windows constants for key codes
    Public Const RightArrow = 39
    Public Const LeftArrow = 37
    Public Const EnterKey = 13
    Public Const EscapeKey = 27
    Public Const UpArrow = 38
    Public Const DownArrow = 40
    
    
    'Timer for idle timeout
    Dim iIdleTimeOut
    
    'Flag for errors
    Dim bInErrorMode
    
    'Reboot confirmation mode
    Dim bInConfirmationMode
    
    'Domain error text
    Dim strDomainErrorText
    
    'Unknown error text
    Dim strUnknownErrorText

    'Reboot confirmation text    
    Dim strRebootConfirmationText
    
    'Duplicate machine name error text
    Dim strDuplicateErrorText
    
    'Password reset error text
    Dim strRebootErrorText
    
    'Confirmation text for resetting password
    Dim strConfirmRebootText
    
    'Shutdown text
    Dim strShutdownText

    'Header for computer name
    Dim strComputerNameHeader
    
    'Is machine part of domain
    Dim bPartOfDomain
    
    'Is the name entered duplicate
    Dim bInDuplicateMode

    'Flag for status of the page
    Dim bRebootingMachine
        
    '----------------------------------------------------------------------------
    ' Function:         Window_OnLoad
    ' Description:      Initialization routine for the page
    ' Input Variables:  None
    ' Output Variables: None
    ' Return Values:    None
    ' Global Variables: iIdleTimeOut,strWaitText,bInErrorMode,strIpAddressInvalidText
    '----------------------------------------------------------------------------

    Sub Window_OnLoad()
    
        'Localization manager object
        Dim objLocMgr
        
        'Replacement strings
        Dim varReplacementStrings
    
        'SaHelper component object
        Dim objSaHelper
        
        'Current hostname
        Dim strHostname
        
        'Resource ID for unknown error text
        Const UNKNOWN_ERROR_TEXT = "&H40020004"
        
        'Resource ID for reboot error text
        Const REBOOT_CONFIRMATION_TEXT = "&H40020003"
        
        'Resource ID for domain error text
        Const DOMAIN_ERROR_TEXT = "&H40020001"
        
        'Resource ID for duplicate machine name error text
        Const DUPLICATE_ERROR_TEXT = "&H40020002"

        'Resource ID for reboot error text
        Const REBOOT_ERROR_TEXT = "&H4002000E"
        
        'Resource ID for shutting down text
        Const SHUTTINGDOWN_TEXT = "&H40020011"

        'Resource ID for computer name header
        Const COMPNAMEHEADER_TEXT = "&H40020014"
        
        On Error Resume Next
        Err.Clear
    
        
        bInErrorMode = false
        bInConfirmationMode = false
        bInDuplicateMode = false
        bRebootingMachine = false
        
        'Set the max size for computer name
        HostNameEntry.MaxSize = 16
        'Set the char set
        HostNameEntry.TextCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
        
        Err.Clear
        
        'Create the localization manager
        Set objLocMgr = CreateObject("ServerAppliance.LocalizationManager")
        
        If Err.number = 0 Then
            'Get the strings
            strUnknownErrorText = objLocMgr.GetString("salocaluimsg.dll",UNKNOWN_ERROR_TEXT,varReplacementStrings)
            strRebootConfirmationText = objLocMgr.GetString("salocaluimsg.dll",REBOOT_CONFIRMATION_TEXT,varReplacementStrings)
            strDomainErrorText = objLocMgr.GetString("salocaluimsg.dll",DOMAIN_ERROR_TEXT,varReplacementStrings)
            strDuplicateErrorText = objLocMgr.GetString("salocaluimsg.dll",DUPLICATE_ERROR_TEXT,varReplacementStrings)
            strRebootErrorText = objLocMgr.GetString("salocaluimsg.dll",REBOOT_ERROR_TEXT,varReplacementStrings)
            strShutdownText = objLocMgr.GetString("salocaluimsg.dll",SHUTTINGDOWN_TEXT,varReplacementStrings)
            strComputerNameHeader = objLocMgr.GetString("salocaluimsg.dll",COMPNAMEHEADER_TEXT,varReplacementStrings)
            Set objLocMgr = Nothing
        End If        
        
        Err.Clear
        
        'get the current configuration
        Set objSaHelper = CreateObject("ServerAppliance.SAHelper")
        If Err.number = 0 Then
            
            bPartOfDomain = objSaHelper.IsPartOfDomain
            If Err.number <> 0 Then
                InformationText.innerText = strUnknownErrorText
                InformationText.style.display = ""        
                bInErrorMode = true
            ElseIf bPartOfDomain = true Then
                InformationText.innerText = strDomainErrorText
                InformationText.style.display = ""        
            Else
                Err.Clear
                strHostname = objSaHelper.HostName
                If Err.number = 0 Then
                    HostNameEntry.TextValue = strHostname
                    HostNameEntry.style.display = ""
                    HeaderText.innerText = strComputerNameHeader
                    HeaderText.style.display = ""
                    HostNameEntry.focus
                Else
                    InformationText.innerText = strUnknownErrorText
                    InformationText.style.display = ""        
                    bInErrorMode = true
                End If
                
            End If
            
            Set objSaHelper = Nothing
        Else
            InformationText.innerText = strUnknownErrorText
            InformationText.style.display = ""        
            bInErrorMode = true
        End If        

        'set the information to display during reboot
        ServeLocalUILogo
        SaShutdownText.innerText = strShutdownText
        
        'start the timer for idle timeout        
        iIdleTimeOut = window.SetTimeOut("IdleHandler()",300000)

        On Error Resume Next
        Err.Clear
        'set the key codes for the page
        Dim objKeypad
        Set objKeypad = CreateObject("Ldm.SAKeypadController")
        If Err.number = 0 Then

            objKeypad.Setkey 0,UpArrow,TRUE
            objKeypad.Setkey 1,DownArrow,FALSE
            objKeypad.Setkey 2,LeftArrow,FALSE
            objKeypad.Setkey 3,RightArrow,FALSE
            objKeypad.Setkey 4,EscapeKey,FALSE
            objKeypad.Setkey 5,EnterKey,FALSE
    
            Set objKeypad = Nothing
        End If

    End Sub

    '----------------------------------------------------------------------------
    ' Function:         IdleHandler
    ' Description:      Goes back to main page when timeout expires
    ' Input Variables:  None
    ' Output Variables: None
    ' Return Values:    None
    ' Global Variables: None
    '----------------------------------------------------------------------------
    Sub IdleHandler()
    
        window.history.go(-2)
        
    End Sub
    
    '----------------------------------------------------------------------------
    ' Function:         ServerLocalUILogo
    ' Description:      Gets the name of the logo file from elementmgr
    ' Input Variables:  None
    ' Output Variables: None
    ' Return Values:    None
    ' Global Variables: salogo
    '----------------------------------------------------------------------------
    Sub ServerLocalUILogo()
    
        Dim objLogoElementCol
        Dim objLogoElement
        
        Dim objRetriever    
        Dim strLogoFileName

        Dim iSmallestMerit
        Dim iCurrentMerit
        
        On Error Resume Next

        'Merit for our localui logo
        iSmallestMerit = 300
        
        strLogoFileName = ""
        'Create elementmgr and get resource elements
        Set objRetriever = CreateObject("Elementmgr.ElementRetriever")
        If Err.Number = 0 Then

            Set objLogoElementCol = objRetriever.GetElements(1, "OemLocalUILogo")
            If Err.Number = 0 Then
                'count the icon and text resources
                For Each objLogoElement in objLogoElementCol
                    
                    iCurrentMerit = CInt(objLogoElement.GetProperty("Merit"))
                    If iCurrentMerit <= iSmallestMerit Then
                        iSmallestMerit = iCurrentMerit 
                        strLogoFileName = objLogoElement.GetProperty("ElementGraphic")
                    End If
                    
                Next
            End If
        End If
        
        Err.Clear
        
        'Set the logo file
        If strLogoFileName <> "" Then
            SaLogo.src = strLogoFileName
        End If
        
        Set objRetriever = Nothing
        Set objLogoElement = Nothing
        Set objLogoElementCol = Nothing


    End Sub
    '----------------------------------------------------------------------------
    ' Function:         HostNameEntry_OperationCanceled
    ' Description:      Handles escape key press for hostname entry control
    '                   Goes back to tasks page
    ' Input Variables:  None
    ' Output Variables: None
    ' Return Values:    None
    ' Global Variables: None
    '----------------------------------------------------------------------------
    Sub HostNameEntry_OperationCanceled()

        If bInDuplicateMode = true Then
            bInDuplicateMode = false
            Exit Sub
        End If
        
        window.history.go(-1)

    End Sub

    '----------------------------------------------------------------------------
    ' Function:         HostNameEntry_KeyPressed
    ' Description:      Handles any key press for hostname entry control
    '                   Resets the idle timeout timer
    ' Input Variables:  None
    ' Output Variables: None
    ' Return Values:    None
    ' Global Variables: None
    '----------------------------------------------------------------------------
    Sub HostNameEntry_KeyPressed()

        window.clearTimeOut(iIdleTimeOut)
        iIdleTimeOut = window.SetTimeOut("IdleHandler()",300000)

    End Sub

    '----------------------------------------------------------------------------
    ' Function:         HostNameEntry_DataEntered
    ' Description:      Handles enter key press for hostname entry control
    '                   Displays confirmation for reboot
    ' Input Variables:  None
    ' Output Variables: None
    ' Return Values:    None
    ' Global Variables: None
    '----------------------------------------------------------------------------
    Sub HostNameEntry_DataEntered()
        
        'SaHelper component object
        Dim objSaHelper

        If bInDuplicateMode = true Then
            bInDuplicateMode = false
            Exit Sub
        End If
        
        On Error Resume Next
        Err.Clear
        
        
        'Hide the data entry control
        HostNameEntry.style.display ="none"
        HeaderText.style.display ="none"
        
        Set objSaHelper = CreateObject("ServerAppliance.SAHelper")
        
        If Err.number = 0 Then
        
            bInDuplicateMode = objSaHelper.IsDuplicateMachineName(HostNameEntry.TextValue)
            If Err.number <> 0 Then
                InformationText.innerText = strUnknownErrorText
                InformationText.style.display = ""        
                bInErrorMode = true
            ElseIf bInDuplicateMode = true Then
                InformationText.innerText = strDuplicateErrorText
                InformationText.style.display = ""        
            Else
                'Display the confirmation text

                InformationText.innerText = strRebootConfirmationText
                InformationText.style.display = ""
                InformationText.focus
                bInConfirmationMode = true
            End If    
            Set objSaHelper = Nothing                
            
        Else
            InformationText.innerText = strUnknownErrorText
            InformationText.style.display = ""        
            bInErrorMode = true
        End If
        
    End Sub
        
    '----------------------------------------------------------------------------
    ' Function:         KeyDown
    ' Description:      Handles key presses
    ' Input Variables:  None
    ' Output Variables: None
    ' Return Values:    None
    ' Global Variables: None
    '----------------------------------------------------------------------------
    Sub KeyDown()
    
        'SaHelper component object
        Dim objSaHelper
        On Error Resume Next
        Err.Clear
        
        'clear the timeout and restart it
        window.clearTimeOut(iIdleTimeOut)
        iIdleTimeOut = window.SetTimeOut("IdleHandler()",300000)
    
        'if we are rebooting, ignore all of the keys
        If bRebootingMachine = false Then
        
            'If we are in error mode go to tasks page
            If bInErrorMode = true or bPartOfDomain = true Then

                If window.event.keycode = EscapeKey or window.event.keycode = EnterKey Then
                    window.history.go(-1)
                End If
            End If
            'If the name entered is duplicate, display the data entry component
            If bInDuplicateMode = true Then
                If window.event.keycode = EscapeKey or window.event.keycode = EnterKey Then
                    InformationText.style.display = "none"        
                    HostNameEntry.style.display =""
                    HeaderText.style.display =""
                    HostNameEntry.focus
                End If
            End If        
        
            If bInConfirmationMode = true Then
                'User canceled go back to hostname data control
                If window.event.keycode = EscapeKey Then
                    window.history.go(-1)
                End If
                
                If window.event.keycode = EnterKey Then
                    Set objSaHelper = CreateObject("ServerAppliance.SAHelper")
        
                    If Err.number = 0 Then
        
                        objSaHelper.HostName = HostNameEntry.TextValue
                        If Err.number <> 0 Then
                            bInConfirmationMode = false
                            InformationText.innerText = strUnknownErrorText
                            bInErrorMode = true
                        Else 
                            bInConfirmationMode = false
                            bRebootingMachine = true
                            RebootTheMachine
                        End If    
                        Set objSaHelper = Nothing                
                        
                    Else
                        bInConfirmationMode = false
                        InformationText.innerText = strUnknownErrorText
                        bInErrorMode = true
                    End If            
                End If
            End If
        End If            
    End Sub
    
    '----------------------------------------------------------------------------
    ' Function:         RebootTheMachine
    ' Description:      Reboots the machine
    ' Input Variables:  None
    ' Output Variables: None
    ' Return Values:    None
    ' Global Variables: None
    '----------------------------------------------------------------------------
    Sub RebootTheMachine
        
        'TaskContext component
        Dim objTaskContext
        
        'ApplianceServices component
        Dim objAS
        
        'result of the execution
        Dim rc
        
        'SAHelper component
        Dim objSAHelper
         
        'result of privilege operation
        Dim bModifiedPrivilege
        
        'privilege to modify
        Const CONST_SHUTDOWNPRIVILEGE = "SeShutdownPrivilege"

        bModifiedPrivilege = FALSE

        'shutdown method name
        Const strMethodName = "ShutdownAppliance"
        
        'hide the confirmation text and display the shutdown page
        InformationText.style.display = "none"
        SaLogo.style.display = ""
        SaShutdownText.style.display = ""
        SaDownArrow.style.display = ""
        
        On Error Resume Next
        Err.Clear

        'Create SAHelper object
        Set objSAHelper = CreateObject("ServerAppliance.SAHelper")	
        If err.number = 0 Then
           bModifiedPrivilege = objSAHelper.SAModifyUserPrivilege(CONST_SHUTDOWNPRIVILEGE, TRUE)
        End If

        bInErrorMode = true

        Set objTaskContext = CreateObject("Taskctx.TaskContext")
        If Err.Number = 0 Then
            Set objAS = CreateObject("Appsrvcs.ApplianceServices")
            If Err.Number = 0 Then
                'set the parameters
                objTaskContext.SetParameter "Method Name", strMethodName
                objTaskContext.SetParameter "SleepDuration", 2000
                objTaskContext.SetParameter "PowerOff", "0"

                If Err.Number = 0 Then
                    'initialize the component
                    objAS.Initialize()
                    If Err.Number = 0 Then
                        bInErrorMode = false
                        'execute the task
                        rc = objAS.ExecuteTaskAsync("ApplianceShutdownTask", objTaskContext)
                    End If
                    
                End If
                Set objAS = Nothing

            End If
            Set objTaskContext = Nothing
            
        End If

        'revert back the privilege
        If (bModifiedPrivilege) Then
           bModifiedPrivilege = objSAHelper.SAModifyUserPrivilege(CONST_SHUTDOWNPRIVILEGE, FALSE)
        End If
        
        Set objSAHelper = Nothing
        
        If bInErrorMode = true Then
            bRebootingMachine = false
            InformationText.innerText = strRebootErrorText
            'display error text
            InformationText.style.display = ""
            'hide the shutdown page
            SaLogo.style.display = "none"
            SaShutdownText.style.display = "none"
            SaDownArrow.style.display = "none"
            
        End If
        
        
    End Sub    
-->
</SCRIPT>

</HEAD>
<BODY RIGHTMARGIN=0 LEFTMARGIN=0 OnKeydown="KeyDown">

    <OBJECT STYLE="position:absolute; top:32; left=0; WIDTH=128; HEIGHT=32; display=none;"
        ID="HostNameEntry" CLASSID="CLSID:538D1B58-8D5A-47C5-9867-4B6230A94EAC" VIEWASTEXT>
    </OBJECT>

    <IMG ID="SaLogo" STYLE="position:absolute; top:0; left=0; display:none"
          SRC="localui_salogo.bmp" BORDER=0>

    <A ID="HeaderText" OnKeydown="KeyDown"
        STYLE="position:absolute; top:16; left:0; font-size:10; font-family=arial; display=none;" > 
    </A>

    <A ID="InformationText" OnKeydown="KeyDown"
        STYLE="position:absolute; top:0; left:0; font-size:10; font-family=arial; display=none;" > 
    </A>

    <A ID="SaShutdownText" STYLE="position:absolute; top:36; left:0; 
        font-size:10; font-family=arial; display:none"> 
    </A>

    <IMG ID="SaDownArrow" STYLE="position:absolute; top:48; left=0; display:none"
          SRC="localui_shutting_down.bmp" BORDER=0>
    
</BODY>

</HTML>