Function Logon_Events(CtrlEntId, Event, @PARAMS)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from Infineon.

    Name        :   Logon_Events

    Description :   This function acts as a commuter module for all events related to this window.

    Notes       :   Commuter Modules are automatically called from the Promoted_Events function which is called by the
                    application-specific promoted event handler. This makes it possible to add QuickEvents that need to
                    execute Basic+ logic without having use the Form Designer to make the association, although this is
                    limited to the events which are currently promoted.

                    If the form needs to call the commuter module directly then the QuickEvent parameters should be
                    formatted like this:

                        '@SELF','@EVENT',['@PARAM1','@PARAMx']

    Parameters  :
        CtrlEntId   [in] -- The fully qualified name of the control calling the promoted event
        Event       [in] -- The event being executed. See the Notes section regarding "PRE" events
        Param1-15   [in] -- Additional event parameter holders
        EventFlow  [out] -- Set to 1 or 0 so the calling event knows whether or not to chain forward. See comments in
                            EVENT_SETUP insert

    History     :   (Date, Initials, Notes)
        02/13/18    dmb     Created initial commuter module.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler
#Window LOGON

$insert APP_INSERTS
$insert EVENT_SETUP
$insert MSG_EQUATES
$insert LSL_USERS_EQUATES

Equ COMMA$ to ','

Declare subroutine  SRP_EditTable_Manager, SRP_Show_Window, Logging_Services, obj_Notes, GetEngineVersion
Declare subroutine  Database_Services, End_Window, ShowWindow, SRP_Run_Command, Placedialog
Declare function    SRP_EditTable_Manager, Database_Services, Security_Services, Logging_Services, Environment_Services
Declare function    GetPrivateProfileString, WritePrivateProfileString, GetPrivateProfileSection, SRP_Get_FileVersion
Declare function    Datetime

LogPath     = Environment_Services('GetApplicationRootPath') : '\LogFiles\Version'
LogDate     = Oconv(Date(), 'D4/')
LogTime     = Oconv(Time(), 'MTS')
LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' Version Log.csv'
Headers     = 'Logging DTM' : @FM : 'User' : @FM : 'Server' : @FM : 'Version'
objLog      = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, Comma$, Headers, '', False$, False$)
LoggingDTM  = LogDate : ' ' : LogTime   ; // Logging DTM

SubclassInfo    = Form_Services('FindSubclassControl')
Subclass        = SubclassInfo<1>

// Update the arguments so that the OpenInsight OLE event will treate the ActiveX event as a native event handler.
If Event EQ 'OLE' then
    Transfer Event to OIEvent
    Transfer Param1 to Event
    Transfer Param2 to Param1
    Transfer Param3 to Param2
*    Transfer Param4 to Param3
*    Transfer Param5 to Param4
*    Transfer Param6 to Param5
*    Transfer Param7 to Param6
*    Transfer Param8 to Param7
end

GoToEvent Event for CtrlEntID

Return EventFlow else EVENT_CONTINUE$


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Event WINDOW.CREATE(CreateParam)

    GoSub Setup_OLE_Controls

    Set_Property(@Window : '.PUB_CLOSE', 'NEXT', @Window : '.EDL_USERNAME')

    lpszSection     = 'Logon' : \00\
    lpszEntry       = 'Username' : \00\
    lpszDefault     = \00\
    lpszUsername    = Str(\00\, 4096)
    cbUsername      = Len(lpszUsername)
    lpszFileName    = 'OINSIGHT.INI' : \00\
    Valid           = GetPrivateProfileString(lpszSection, lpszEntry, lpszDefault, lpszUsername, cbUsername, lpszFileName)
    Convert \0020\ to \2000\ in lpszUsername
    lpszUsername = TrimB(lpszUsername)
    Convert \0020\ to \20FE\ in lpszUsername
    If lpszUsername NE '' then
        Set_Property(@Window : '.EDL_USERNAME', 'TEXT', lpszUsername)
        Set_Property(@Window : '.EDL_USERNAME', 'SELECTION', 1 : @FM : 999)
        Set_Property(@Window : '.EDL_PASSWORD', 'FOCUS', True$)
    end

    OCXDir     = ''
    Server     = Environment_Services('GetServer')
    
    Version      = ''
	GetEngineVersion(Version)
	MajorVersion = Version[1, 'F.']
	OIX          = (MajorVersion GE 10)
    
    Begin Case
        Case Server EQ 'MESSA005' or Server EQ 'MESSA012' or Server EQ 'MESSA01EC' or Server EQ 'MESTSA01EC' or Server EQ 'MESTSA09EC' or Server EQ 'MESTSA010EC' or Server EQ 'MESTSA011EC' or Server EQ 'MESTSA012EC'
        	If OIX then
        		OCXDir = Drive():'\SRPControls64.ocx'
        	end else
        		OCXDir = Drive():'\SRPControls.ocx'
        	end
        Case Server EQ 'MESST5201' or Server EQ 'MESST5202'
        	If OIX then
        		OCXDir = 'C:\Program Files\Revsoft\OIClient_10\SRPControls64.ocx'
        	end else
        		OCXDir = 'C:\Program Files (x86)\RevSoft\OIClient\SRPControls.ocx'
        	end
        Case Otherwise$
        	If OIX then
        		OCXDir = 'C:\Revsoft\OIClient_10\SRPControls64.ocx'
        	end else
        		OCXDir = 'C:\Revsoft\OIClient\SRPControls.ocx'
        	end
    End Case

    Version    = SRP_Get_FileVersion(OCXDir)
    LogData    = ''
    LogData<1> = LoggingDTM
    LogData<2> = @User4
    LogData<3> = Server
    LogData<4> = Version
    Logging_Services('AppendLog', objLog, LogData, @RM, @FM)

    LSL2Version                      = Database_Services('ReadDataRow', 'APP_INFO', 'LSL2_VERSION')
    LSLUserID                        = Security_Services('GetLSLUser', @User4)
    // If logging in from the DB (i.e. EC) server, SSO won't be used, so @User4 will have the LSL user already.
    If LSLUserID EQ '' then LSLUserID = @User4
    UserRec                          = Database_Services('ReadDataRow', 'LSL_USERS', LSLUserID)
    OCXVer                           = Get_Property(@Window:'.OLE_PIC_LOGO', 'OLE.Version')<1>
    UserRec<LSL_USERS_OCX_VERSION$>  = OCXVer
    UserRec<LSL_USERS_LAST_LOGIN$>   = Datetime()
    UserRec<LSL_USERS_LSL2_VERSION$> = LSL2Version
    Database_Services('WriteDataRow', 'LSL_USERS', LSLUserID, UserRec, True$, False$, True$)
    
    PlaceDialog(-2, -2)
    Set_Property(@Window, 'VISIBLE', 1)
    Set_Property(@Window:'.EDL_USERNAME', 'FOCUS', True$)
    
end event


Event EDL_USERNAME.LOSTFOCUS(Flag, FocusID)

    Form_Services('CloseControlMessage', CtrlEntId)

end event


Event EDL_USERNAME.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, AltKey)

    Form_Services('CloseControlMessage', CtrlEntId)
    Username    = Get_Property(CtrlEntId, 'TEXT')
    Password    = Get_Property(@Window : '.EDL_PASSWORD', 'TEXT')
    If (Username NE '') AND (Password NE '') then
        Set_Property(@Window : '.PUB_OK', 'ENABLED', True$)
    end else
        Set_Property(@Window : '.PUB_OK', 'ENABLED', False$)
    end

end event


Event EDL_PASSWORD.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, AltKey)

    PopupVis = Get_Property(@Window:'.OLE_POPUP', "OLE.Visible")
    If PopupVis then Form_Services('CloseControlMessage', CtrlEntId)
    Username    = Get_Property(@Window : '.EDL_USERNAME', 'TEXT')
    Password    = Get_Property(CtrlEntId, 'TEXT')
    If (Username NE '') AND (Password NE '') then
        Set_Property(@Window : '.PUB_OK', 'ENABLED', True$)
    end else
        Set_Property(@Window : '.PUB_OK', 'ENABLED', False$)
    end

end event


Event PUB_OK.CLICK()

    ShutdownFlag = Xlate('APP_INFO', 'APP_SHUTDOWN', '', 'X')
    If (ShutdownFlag EQ False$) then
        Username     = Get_Property(@Window : '.EDL_USERNAME', 'TEXT')
        Password     = Get_Property(@Window : '.EDL_PASSWORD', 'TEXT')
        Utility('CURSOR', 'H')
        Set_Property(@Window : '.EDL_USERNAME', 'BACKCOLOR', COLOR_BTNFACE$)
        Set_Property(@Window : '.EDL_PASSWORD', 'BACKCOLOR', COLOR_BTNFACE$)
        Set_Property(@Window : '.PUB_OK', 'ENABLED', False$)
        If Index(Username, '\', 1) then
            // User has specified their domain with their username. Parse this out and use this domain only.
            Domain      = Username[1, '\']
            Username    = Username[Col2() + 1, '\']
        end else
            Domain      = ''
        end
        AccessLevel = Security_Services('GetAccessLevel', Username, Password, Domain)
        Set_Property(@Window : '.EDL_USERNAME', 'BACKCOLOR', COLOR_WINDOW$)
        Set_Property(@Window : '.EDL_PASSWORD', 'BACKCOLOR', COLOR_WINDOW$)
        Utility('CURSOR', 'A')

        If Error_Services('NoError') AND AccessLevel NE -1 then
            @USER4          = Security_Services('GetLSLUser', Username)

            If @USER4 NE '' then
                lpszSection     = 'Logon' : \00\
                If Domain NE '' then Username = Domain : '\' : Username
                lpszEntry       = 'Username' : \00\
                lpszValue       = Username : \00\
                lpszFileName    = 'OINSIGHT.INI' : \00\
                Valid           = WritePrivateProfileString(lpszSection, lpszEntry, lpszValue, lpszFileName)
                End_Dialog(@Window, AccessLevel)
            end else
                Set_Property(@Window : '.EDL_PASSWORD', 'FOCUS', True$)
                Set_Property(@Window : '.EDL_PASSWORD', 'SELECTION', 1 : @FM : 999)
                Message = 'Unable to validate username. Please re-enter.'
                Form_Services('DisplayControlMessage', Message, 'MES Logon', @Window : '.EDL_USERNAME', 'VALIDATION', 'RGB(229,20,0)')
            end
        end else
            Set_Property(@Window : '.EDL_PASSWORD', 'FOCUS', True$)
            Set_Property(@Window : '.EDL_PASSWORD', 'SELECTION', 1 : @FM : 999)
            Message     = Error_Services('GetMessage')
            If Message EQ '' then Message = 'Unable to validate username. Please re-enter.'
            Form_Services('DisplayControlMessage', Message, 'MES Logon', @Window : '.EDL_USERNAME', 'VALIDATION', 'RGB(229,20,0)')
        end
    end else
        Set_Property(@Window : '.EDL_PASSWORD', 'FOCUS', True$)
        Set_Property(@Window : '.EDL_PASSWORD', 'SELECTION', 1 : @FM : 999)
        Message     = Error_Services('GetMessage')
        If Message EQ '' then Message = 'System unavailable.'
        Form_Services('DisplayControlMessage', Message, 'MES Logon', @Window : '.EDL_USERNAME', 'VALIDATION', 'RGB(229,20,0)')
    end

end event


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Setup_OLE_Controls:

    Qualify     = ''
    Qualify<1>  = 1
    Qualify<4>  = 0

return

