Compile function NDW_SEC_GROUPS_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        :   NDW_Sec_Groups_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)
        11/01/24    djs     Created initial commuter module.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_SEC_GROUPS

$Insert EVENT_SETUP
$Insert APP_INSERTS
$Insert MSG_EQUATES
$Insert POPUP_EQUATES
$Insert SEC_GROUPS_EQUATES
$Insert LSL_USERS_EQUATES

Declare function   Admin_User, Database_Services, Error_Services, Active_Directory_Services, SRP_Array
Declare subroutine PlaceDialog, Error_Services, Database_Services, Btree.Extract, ErrMsg

GoToEvent Event for CtrlEntId else
	// Event not implemented
end

Return EventFlow or 1

//-----------------------------------------------------------------------------
// EVENT HANDLERS
//-----------------------------------------------------------------------------

Event WINDOW.CREATE(CreateParam)
	
	AdminUser = Xlate('LSL_USERS', @USER4, LSL_USERS_ADMIN_USER$, 'X')
	
	If Not(AdminUser) then
		ErrMsg('Improper security to enter Security Groups')
		Post_Event(@Window, 'CLOSE')
	end else
		PlaceDialog(-2, -2)
		ColorArray    = Send_Message(@Window:'.EDT_LSL_USERS', "COLOR_BY_POS", 2, 0)
		ColorArray<1> = READONLY_GREEN$
		Send_Message(@Window:'.EDT_LSL_USERS', "COLOR_BY_POS", 1, 0, ColorArray)
		Send_Message(@Window:'.EDT_LSL_USERS', "COLOR_BY_POS", 2, 0, ColorArray)
		Send_Message(@Window:'.EDT_AD_GROUPS', "COLOR_BY_POS", 1, 0, ColorArray)
		Send_Message(@Window:'.EDT_AD_GROUPS', "COLOR_BY_POS", 2, 0, ColorArray)	
		Send_Message(@Window:'.EDT_AD_GROUPS', "COLOR_BY_POS", 3, 0, ColorArray)		
	end
	
End Event


Event WINDOW.READ()
	
	EventFlow = EVENT_CONTINUE$
	OrigRec   = Get_Property(@Window, '@RECORD')
	EditRec   = Get_Property(@Window, '@EDIT_RECORD')
	
	If OrigRec NE EditRec then
		Response = Msg(@Window, '', 'YES_NO_CANCEL', '', 'OpenInsight':@FM:'Would you like to save changes to the entry?')
		Begin Case
			Case Response EQ Yes$
				// Save changes and read new record
				Send_Event(@Window, 'WRITE')
			Case Response EQ No$
				// Abandon changes and read new form
				Null
			Case Otherwise$
				// Cancel - Stop Read Event
				EventFlow = EVENT_STOP$
		End Case
	end
	
	If EventFlow EQ EVENT_CONTINUE$ then
		
		GoSub UnlockRec	
		
		Key    = Get_Property(@Window:'.EDL_GROUP_ID', 'TEXT')
		NewRec = Get_Property(@Window, '@NEW_REC')
		If Key EQ '' then
			Key = Get_Property(@Window, '@NEW_KEY')
			Set_Property(@Window:'.EDL_GROUP_ID', 'TEXT', NewKey)
			Set_Property(@Window, '@NEW_KEY', '')
		end		
		Begin Case
			Case Key EQ '' 
				Set_Property(@Window:'.EDT_LSL_USERS', 'ENABLED', False$)
			Case RowExists('SEC_GROUPS', Key)
				// Populate form
				HaveLock  = Database_Services('GetKeyIDLock', 'SEC_GROUPS', Key, True$)
				If HaveLock then
					Set_Property(@Window, '@HAVE_LOCK', HaveLock)
					Set_Property(@Window, '@LOCK_KEY', Key)
					
					Set_Property(@Window:'.EDL_GROUP_NAME', 'ENABLED', True$)
					Set_Property(@Window:'.CHK_USE_AD', 'ENABLED', True$)
					Set_Property(@Window:'.CHK_LIMIT_TO_ACTIVE_SHIFT', True$)
					Set_Property(@Window:'.PUB_CLEAR', 'ENABLED', True$)
					Set_Property(@Window:'.PUB_DELETE', 'ENABLED', True$)
					Set_Property(@Window:'.PUB_SAVE', 'ENABLED', True$)
					GroupRec = Database_Services('ReadDataRow', 'SEC_GROUPS', Key)
					If Error_Services('NoError') then
						Set_Property(@Window, '@RECORD', GroupRec)
						Set_Property(@Window, '@EDIT_RECORD', GroupRec)
						Set_Property(@Window:'.EDL_GROUP_NAME', 'TEXT', GroupRec<SEC_GROUPS_GROUP_NAME$>)
						UseAD        = GroupRec<SEC_GROUPS_USE_ACTIVE_DIRECTORY$>
						Set_Property(@Window:'.CHK_USE_AD', 'DEFPROP', UseAD)
						
						// Populate Table Rights
						Tables           = GroupRec<SEC_GROUPS_TABLES$>
						Rights           = GroupRec<SEC_GROUPS_RIGHTS$>
						TableRightsArray = Tables : @FM : Rights
						Set_Property(@Window:'.EDT_TABLE_RIGHTS', 'ARRAY', TableRightsArray)
						
						LSLUsers      = GroupRec<SEC_GROUPS_USER$>
						LSLUsernames  = Xlate('LSL_USERS', LSLUsers, 'FIRST_LAST', 'X')
						LSLUsersArray = LSLUsers : @FM : LSLUsernames
						Set_Property(@Window:'.EDT_LSL_USERS', 'ARRAY', LSLUsersArray)
						ADGroups      = GroupRec<SEC_GROUPS_ACTIVE_DIRECTORY_GROUPS$>
						GroupList     = ''
						If ADGroups NE '' then
							For each ADGroup in ADGroups using @VM setting vPos
								GroupList<-1> = Active_Directory_Services('GetADGroupsByString', ADGroup, 'INFINEON')
							Next ADGroup
						end
						Set_Property(@Window:'.EDT_AD_GROUPS', 'LIST', GroupList)
						GoSub EnableControls
					end else
						Msg(@Window, '', 'OK', '', 'Error':@FM:'Error locking SEC_GROUPS record "':Key:'" for update!')
					end
				end else
					Msg(@Window, '', 'OK', '', 'Error':@FM:'Error reading SEC_GROUPS record "':Key:'"!')
				end
			Case NewRec
				Null
			Case Otherwise$
				// User is creating a new record
				Set_Property(@Window, 'REDRAW', False$)
				Send_Event(@Window, 'CLEAR')
				Set_Property(@Window:'.EDL_GROUP_ID', 'TEXT', Key)
				Set_Property(@Window, '@LOCK_KEY', Key)
				Set_Property(@Window, '@NEW_REC', True$)
				GoSub EnableControls
				Set_Property(@Window:'.PUB_LU_GROUP', 'FOCUS', True$)
				Set_Property(@Window, 'REDRAW', True$)				
		End Case
	end
	
end event


Event WINDOW.CLEAR(bSaveKey, bSuppressWarning, bMaintainFocus)
	
	EventFlow = EVENT_CONTINUE$
	OrigRec   = Get_Property(@Window, '@RECORD')
	EditRec   = Get_Property(@Window, '@EDIT_RECORD')
	
	If OrigRec NE EditRec then
		Response = Msg(@Window, '', 'YES_NO_CANCEL', '', 'OpenInsight':@FM:'Would you like to save changes to the entry?')
		Begin Case
			Case Response EQ 1
				// Yes - Save changed and close form
				Send_Event(@Window, 'WRITE')
			Case Response EQ 0
				// No - Abandon changes and clear form
				Null
			Case Otherwise$
				// Cancel - Stop Clear Event
				EventFlow = EVENT_STOP$
		End Case
	end
	
	If EventFlow EQ EVENT_CONTINUE$ then
		
		GoSub UnlockRec
		Forward_Event()
		Set_Property(@Window, '@RECORD', '')
		Set_Property(@Window, '@EDIT_RECORD', '')
		
		GoSub EnableControls
		Set_Property(@Window:'.PUB_DELETE'    , 'ENABLED', False$)			
		Set_Property(@Window:'.PUB_CLEAR'     , 'ENABLED', True$)
		Set_Property(@Window:'.EDL_GROUP_NAME', 'ENABLED', False$)
		Set_Property(@Window:'.CHK_USE_AD'    , 'ENABLED', False$)
		Set_Property(@Window:'.PUB_ADD_USERS' , 'ENABLED', False$)
		Set_Property(@Window:'.REM_ADD_USERS' , 'ENABLED', False$)
		Set_Property(@Window:'.GRB_AD_GROUPS' , 'ENABLED', False$)
		Set_Property(@Window:'.EDT_AD_GROUPS' , 'ENABLED', False$)
		Set_Property(@Window:'.PUB_ADD_GROUPS', 'ENABLED', False$)
		Set_Property(@Window:'.REM_ADD_GROUPS', 'ENABLED', False$)
		
	end
	
end event


Event WINDOW.WRITE()
	
	Key                                           = Get_Property(@Window, '@LOCK_KEY')
	GroupRec                                      = Get_Property(@Window, '@RECORD')
	GroupRec<SEC_GROUPS_GROUP_NAME$>              = Get_Property(@Window:'.EDL_GROUP_NAME', 'TEXT')
	TableRightsArray                              = Get_Property(@Window:'.EDT_TABLE_RIGHTS', 'ARRAY')
	Tables                                        = TableRightsArray<1>
	Rights                                        = TableRightsArray<2>
	GroupRec<SEC_GROUPS_TABLES$>                  = Tables
	GroupRec<SEC_GROUPS_RIGHTS$>                  = Rights
	UserArray                                     = Get_Property(@Window:'.EDT_LSL_USERS', 'ARRAY')
	UserIds                                       = UserArray<1>
	GroupRec<SEC_GROUPS_USER$>                    = UserIds
	GroupArray                                    = Get_Property(@Window:'.EDT_AD_GROUPS', 'ARRAY')
	GroupIds                                      = GroupArray<1>
	GroupRec<SEC_GROUPS_ACTIVE_DIRECTORY_GROUPS$> = GroupIds
	GroupRec<SEC_GROUPS_USE_ACTIVE_DIRECTORY$>    = Get_Property(@Window:'.CHK_USE_AD', 'DEFPROP')
	Database_Services('WriteDataRow', 'SEC_GROUPS', Key, GroupRec, True$, False$, True$)
	If Error_Services('NoError') then
		GoSub UnlockRec
		Set_Property(@Window, '@RECORD', GroupRec)
		Set_Property(@Window, '@EDIT_RECORD', GroupRec)
		Send_Event(@Window, 'CLEAR')
	end else
		Msg(@Window, '', 'OK', '', 'Error':@FM:Error_Services('GetMessage'))
	end
	
end event


Event WINDOW.CLOSE(CancelFlag, CloseFlags)
	
	EventFlow = EVENT_CONTINUE$
	OrigRec  = Get_Property(@Window, '@RECORD')
	EditRec  = Get_Property(@Window, '@EDIT_RECORD')
	If OrigRec NE EditRec then
		Response = Msg(@Window, '', 'YES_NO_CANCEL', '', 'OpenInsight':@FM:'Would you like to save changes to the entry?')
		Begin Case
			Case Response EQ 1
				// Yes - Save changed and close form
				Send_Event(@Window, 'WRITE')
			Case Response EQ 0
				// No - Abandon changes and close form
				Null
			Case Otherwise$
				// Cancel - Stop Close Event
				EventFlow = EVENT_STOP$
		End Case
	end
	
	If EventFlow EQ EVENT_CONTINUE$ then GoSub UnlockRec
	
end event


Event PUB_LU_GROUP.CLICK()
	
	EventFlow = EVENT_CONTINUE$
	OrigRec   = Get_Property(@Window, '@RECORD')
	EditRec   = Get_Property(@Window, '@EDIT_RECORD')
	If OrigRec NE EditRec then
		Response = Msg(@Window, '', 'YES_NO_CANCEL', '', 'OpenInsight':@FM:'Would you like to save changes to the entry?')
		Begin Case
			Case Response EQ Yes$
				// Save changes and read new record
				Send_Event(@Window, 'WRITE')
			Case Response EQ No$
				// Abandon changes and read new form
				Null
			Case Otherwise$
				// Cancel - Stop Read Event
				EventFlow = EVENT_STOP$
		End Case
	end
	
	If EventFlow EQ EVENT_CONTINUE$ then	
	
		TypeOver           = ''
		TypeOver<PSELECT$> = 1	; // Only allow one group to be selected
		GroupID            = Popup(@WINDOW, TypeOver, 'SEC_GROUPS')
		IF GroupID NE '' then
			Set_Property(@Window, '@RECORD', '')
			Set_Property(@Window, '@EDIT_RECORD', '')
			Set_Property(@Window:'.EDL_GROUP_ID', 'TEXT', GroupID)
			Post_Event(@Window, 'READ')
		end
		
	end
	
end event


Event PUB_SAVE.CLICK()
	
	Post_Event(@Window, 'WRITE')
	
end event


Event PUB_CLEAR.CLICK()
	
	GoSub UnlockRec
	Post_Event(@Window, 'CLEAR')
	
end event


Event EDT_TABLE_RIGHTS.OPTIONS()
	
	SelPos = Get_Property(CtrlEntId, 'SELPOS')
	Begin Case
		Case Selpos<1> EQ 1
			CurrRow = Get_Property(CtrlEntId, 'ROWDATA')
			RetVal  = Popup(@Window, '', 'TABLENAMES')
			If RetVal then
				Convert @VM to '' in RetVal
				CurrRow<1> = RetVal
				Set_Property(CtrlEntId, 'ROWDATA', CurrRow)
				Post_Event(CtrlEntID, 'CHANGED')
			end				
		Case Selpos<1> EQ 2
			CurrRow = Get_Property(CtrlEntId, 'ROWDATA')
			RetVal  = Popup(@Window, '', 'SECURITY_RIGHTS')
			If RetVal then
				Convert @VM to '' in RetVal
				RetVal     = OConv(RetVal, '[RIGHTS_CONV]')
				CurrRow<2> = RetVal
				Set_Property(CtrlEntId, 'ROWDATA', CurrRow)
				Post_Event(CtrlEntID, 'CHANGED')
			end		
	End Case
	
end event


Event EDT_LSL_USERS.ROWSELCHANGED(SelRow, SelState)
	
	GoSub EnableControls
	
end event


Event EDT_AD_GROUPS.ROWSELCHANGED(SelRow, SelState)
	
	GoSub EnableControls
	
end event


Event EDL_GROUP_ID.LOSTFOCUS(Flag, FocusID)
	
	If Flag EQ 1 then
		GoSub UnlockRec
		Post_Event(@Window, 'READ')
	end
	
end event


Event EDL_GROUP_ID.OPTIONS()
	
	EventFlow = EVENT_CONTINUE$
	OrigRec   = Get_Property(@Window, '@RECORD')
	EditRec   = Get_Property(@Window, '@EDIT_RECORD')
	If OrigRec NE EditRec then
		Response = Msg(@Window, '', 'YES_NO_CANCEL', '', 'OpenInsight':@FM:'Would you like to save changes to the entry?')
		Begin Case
			Case Response EQ Yes$
				// Save changes and read new record
				Send_Event(@Window, 'WRITE')
			Case Response EQ No$
				// Abandon changes and read new form
				Null
			Case Otherwise$
				// Cancel - Stop Read Event
				EventFlow = EVENT_STOP$
		End Case
	end
	
	If EventFlow EQ EVENT_CONTINUE$ then	
		
		TypeOver           = ''
		TypeOver<PSELECT$> = 1	; // Only allow one group to be selected
		GroupID            = Popup(@WINDOW, TypeOver, 'SEC_GROUPS')
		IF GroupID NE '' then
			Set_Property(@Window, '@RECORD', '')
			Set_Property(@Window, '@EDIT_RECORD', '')
			Set_Property(@Window:'.EDL_GROUP_ID', 'TEXT', GroupID)
			Post_Event(@Window, 'READ')
		end	
	end
	
end event


Event PUB_ADD_USERS.CLICK()
	
	CurrUserIDs = Get_Property(@Window:'.EDT_LSL_USERS','ARRAY')<1>	
	CurrUserIDsTrimmed = ''
	For I = 1 to COUNT(CurrUserIDs,@VM) + (CurrUserIDs NE '')
		If CurrUserIDs<1,I> NE '' then
			CurrUserIDsTrimmed<1,I> = CurrUserIDs<1,I>
		end
	Next I
	CurrUserIDs	= CurrUserIDsTrimmed
	
	NewUserIDs = Popup(@WINDOW,'','SHOW_USERS')
	
	If ( (NewUserIDs NE '') and (NewUserIDs NE CHAR(27)) ) then
	
		For I = 1 TO COUNT(NewUserIDs,@VM) + (NewUserIDs NE '')
			NewUserID = NewUserIDs<1,I>
			Locate NewUserID in CurrUserIDs by 'AL' using @VM setting POS else
				CurrUserIDs = INSERT(CurrUserIDs,1,POS,0,NewUserID)
			end
		Next I
		
		LSLNames = Xlate('LSL_USERS', CurrUserIDs, 'FIRST_LAST', 'X')
		NewArray = CurrUserIDs : @FM : LSLNames
		Set_Property(@Window:'.EDT_LSL_USERS', 'ARRAY', NewArray)
		Post_Event(@Window:'.EDT_LSL_USERS', 'CHANGED')
	end
	
end event


Event PUB_REM_USERS.CLICK()
	
	SelRows = Get_Property(@Window:'.EDT_LSL_USERS', 'SELPOS')
	SelRows = SelRows<2>
	SelData = ''
	NewList = ''
	If SelRows NE '' then 
		DataList = Get_Property(@Window:'.EDT_LSL_USERS', 'LIST')
		For each Row in DataList using @FM setting RowIndex
			If Not(InList(SelRows, RowIndex, @VM)) then NewList<-1> = Row
		Next Row
		Set_Property(@Window:'.EDT_LSL_USERS', 'LIST', NewList)
		GoSub EnableControls
		Post_Event(@Window:'.EDT_LSL_USERS', 'CHANGED')
	end		
	
end event


Event PUB_ADD_GROUPS.CLICK()
	
	GroupArray = Get_Property(@Window:'.EDT_AD_GROUPS', 'ARRAY')
	GroupIds   = GroupArray<1>
	SelGroups  = Dialog_Box('NDW_ACTIVE_DIRECTORY_GROUPS', @Window, GroupIds:@RM:'SECURITY')
	If SelGroups NE '' then 
		Set_Property(@Window:'.EDT_AD_GROUPS', 'LIST', SelGroups)
		
		Def         = ""
		Def<MCOL$>  = -2
		Def<MROW$>  = -2	
		Def<MTEXT$> = "Updating LSL user list..."
		Def<MTYPE$> = "U"
		MsgUp       = Msg(@window, Def)   ;* display the processing message	
		
		GroupArray = Get_Property(@Window:'.EDT_AD_GROUPS', 'ARRAY')
		GroupIds   = GroupArray<1>
		
		LSLUserNames = ''
		LSLNames     = ''
		If GroupIds NE '' then
			For each GroupId in GroupIds using @VM
				MemberList   = Active_Directory_Services('GetADGroupMembersByGroupName', GroupId, 'INFINEON')
				MemberList   = SRP_Array('Rotate', MemberList, @FM, @VM)
				ADUserNames  = MemberList<1>
				
				Open 'DICT.LSL_USERS' to hDict then 
					For each ADUserName in ADUserNames using @VM setting vPos
						Query               = 'DOMAIN_USERNAME':@VM:ADUserName:@FM
						Flag                = ''
						LSLUsername         = ''
						Btree.Extract(Query, 'LSL_USERS', hDict, LSLUsername, '', Flag)
						If LSLUsername NE '' then  LSLUsernames<0, -1> = LSLUsername<0, 1>
					Next ADUserName
				end
			Next GroupId
			If LSLUsernames NE '' then
				LSLUsernames = SRP_Array('Clean', LSLUsernames, 'TrimAndMakeUnique', @VM)
				LSLUsernames = SRP_Array('SortSimpleList', LSLUsernames, 'AscendingText', @VM)
				LSLNames     = Xlate('LSL_USERS', LSLUsernames, 'FIRST_LAST', 'X')
			end
		end
		Array = LSLUsernames : @FM : LSLNames
		Set_Property(@Window:'.EDT_LSL_USERS', 'ARRAY', Array)	
		
		Msg(@window, MsgUp)         ;* take down the processing message
		Post_Event(@Window:'.EDT_AD_GROUPS', 'CHANGED')
	end
	
end event


Event EDL_GROUP_NAME.CHANGED(NewData)
	
	EditRecord                         = Get_Property(@Window, '@EDIT_RECORD')
	EditRecord<SEC_GROUPS_GROUP_NAME$> = NewData
	Set_Property(@Window, '@EDIT_RECORD', EditRecord)
	
end event


Event EDT_AD_GROUPS.CHANGED(NewData)
	
	EditRecord                                      = Get_Property(@Window, '@EDIT_RECORD')
	ADGroupsArray                                   = Get_Property(CtrlEntId, 'ARRAY')
	GroupNames                                      = ADGroupsArray<1>
	EditRecord<SEC_GROUPS_ACTIVE_DIRECTORY_GROUPS$> = GroupNames
	Set_Property(@Window, '@EDIT_RECORD', EditRecord)	
	
end event


Event EDT_TABLE_RIGHTS.CHANGED(NewData)
	
	EditRecord                     = Get_Property(@Window, '@EDIT_RECORD')
	TableRightsArray               = Get_Property(CtrlEntId, 'ARRAY')
	TableNames                     = TableRightsArray<1>
	TableRights                    = TableRightsArray<2>
	EditRecord<SEC_GROUPS_TABLES$> = TableNames
	EditRecord<SEC_GROUPS_RIGHTS$> = TableRights
	Set_Property(@Window, '@EDIT_RECORD', EditRecord)	
	
end event


Event EDT_LSL_USERS.CHANGED(NewData)
	
	EditRecord                   = Get_Property(@Window, '@EDIT_RECORD')
	LSLUsersArray                = Get_Property(CtrlEntId, 'ARRAY')
	LSLUsernames                 = LSLUsersArray<1>
	EditRecord<SEC_GROUPS_USER$> = LSLUsernames
	Set_Property(@Window, '@EDIT_RECORD', EditRecord)	
	
end event


Event PUB_REM_GROUPS.CLICK()
	
	SelRows = Get_Property(@Window:'.EDT_AD_GROUPS', 'SELPOS')
	SelRows = SelRows<2>
	SelData = ''
	NewList = ''
	If SelRows NE '' then 
		DataList = Get_Property(@Window:'.EDT_AD_GROUPS', 'LIST')
		For each Row in DataList using @FM setting RowIndex
			If Not(InList(SelRows, RowIndex, @VM)) then NewList<-1> = Row
		Next Row
		Set_Property(@Window:'.EDT_AD_GROUPS', 'LIST', NewList)
		GoSub EnableControls
		Post_Event(@Window:'.EDT_AD_GROUPS', 'CHANGED')
	end	
	
end event


Event PUB_ADD_TABLE.CLICK()
	
	SelTables           = Popup(@Window, '', 'TABLENAMES')
	If SelTables NE '' then 
		TableRightsList     = Get_Property(@Window:'.EDT_TABLE_RIGHTS', 'LIST')
		TableRightsList<-1> = SelTables
		TableRightsList     = SRP_Array('Clean', TableRightsList, 'TrimAndMakeUnique', @FM)
		TableRightsList     = SRP_Array('SortRows', TableRightsList, 'AL1', 'LIST')
		Set_Property(@Window:'.EDT_TABLE_RIGHTS', 'LIST', TableRightsList)
		Post_Event(@Window:'.EDT_TABLE_RIGHTS', 'CHANGED')
	end
	
end event


Event PUB_EDIT_RIGHTS.CLICK()
	
	CurrRow = Get_Property(@Window:'.EDT_TABLE_RIGHTS', 'ROWDATA')
	RetVal  = Popup(@Window, '', 'SECURITY_RIGHTS')
	If RetVal then
		Convert @VM to '' in RetVal
		RetVal     = OConv(RetVal, '[RIGHTS_CONV]')
		CurrRow<2> = RetVal
		Set_Property(@Window:'.EDT_TABLE_RIGHTS', 'ROWDATA', CurrRow)
		Post_Event(@Window:'.EDT_TABLE_RIGHTS', 'CHANGED')
	end		
	
end event


Event PUB_REMOVE_TABLE.CLICK()
	
	SelRows = Get_Property(@Window:'.EDT_TABLE_RIGHTS', 'SELPOS')
	SelRows = SelRows<2>
	SelData = ''
	NewList = ''
	If SelRows NE '' then 
		DataList = Get_Property(@Window:'.EDT_TABLE_RIGHTS', 'LIST')
		For each Row in DataList using @FM setting RowIndex
			If Not(InList(SelRows, RowIndex, @VM)) then NewList<-1> = Row
		Next Row
		Set_Property(@Window:'.EDT_TABLE_RIGHTS', 'LIST', NewList)
		GoSub EnableControls
		Post_Event(@Window:'.EDT_TABLE_RIGHTS', 'CHANGED')
	end	
	
end event


Event CHK_USE_AD.CLICK()
	
	EditRecord                                   = Get_Property(@Window, '@EDIT_RECORD')
	UseAD                                        = Get_Property(CtrlEntId, 'CHECK')
	EditRecord<SEC_GROUPS_USE_ACTIVE_DIRECTORY$> = UseAD
	Set_Property(@Window, '@EDIT_RECORD', EditRecord)	
	GoSub EnableControls
	
end event


//-----------------------------------------------------------------------------
// Internal GoSubs
//-----------------------------------------------------------------------------


EnableControls:
	
	GroupID = Get_Property(@Window:'.EDL_GROUP_ID', 'TEXT')
	UseAD   = Get_Property(@Window:'.CHK_USE_AD', 'DEFPROP')	
	Enabled = (GroupID NE '')
	
	Set_Property(@Window:'.PUB_CLEAR'   	, 'ENABLED', Enabled)
	Set_Property(@Window:'.EDL_GROUP_NAME'  , 'ENABLED', Enabled)
	Set_Property(@Window:'.CHK_USE_AD'  	, 'ENABLED', Enabled)
	Set_Property(@Window:'.EDT_TABLE_RIGHTS', 'ENABLED', Enabled)
	Set_Property(@Window:'.PUB_REMOVE_TABLE', 'ENABLED', Enabled)
	Set_Property(@Window:'.PUB_ADD_TABLE'   , 'ENABLED', Enabled)
	Set_Property(@Window:'.PUB_EDIT_RIGHTS' , 'ENABLED', Enabled)
	Set_Property(@Window:'.PUB_ADD_USERS'   , 'ENABLED', (UseAD NE True$))
	Set_Property(@Window:'.EDT_LSL_USERS'   , 'ENABLED', Enabled)
	
	If GroupID NE '' then
		Backcolor = WHITE$
	end else
		Backcolor = GREY$
	end
	
	Set_Property(@Window:'.EDT_TABLE_RIGHTS', 'BACKCOLOR', Backcolor)
	Set_Property(@Window:'.EDT_LSL_USERS'   , 'BACKCOLOR', Backcolor)
	Set_Property(@Window:'.GRB_AD_GROUPS'   , 'ENABLED', (UseAD EQ True$))
	Set_Property(@Window:'.PUB_ADD_GROUPS'  , 'ENABLED', (UseAD EQ True$))
	Set_Property(@Window:'.EDT_AD_GROUPS'   , 'ENABLED', (UseAD EQ True$))
	
	If ( Enabled and (UseAD EQ True$) ) then
		Backcolor = WHITE$
	end else
		Backcolor = GREY$
	end
	Set_Property(@Window:'.EDT_AD_GROUPS', 'BACKCOLOR', Backcolor)
	
	DelBtnEnabled = False$
	SelRows       = Get_Property(@Window:'.EDT_LSL_USERS', 'SELPOS')
	SelRows       = SelRows<2>
	SelData       = ''
	If SelRows NE '' then 
		Data    = Get_Property(@Window:'.EDT_LSL_USERS', 'LIST')
		Convert @VM to '' in Data
		For each Row in SelRows using @VM
			If Data<Row> NE '' then 
				SelData<-1> = Data<Row>
			end
		Next Row
		If (SelData NE '') and (UseAD NE True$) then DelBtnEnabled = True$
	end
	Set_Property(@Window:'.PUB_REM_USERS', 'ENABLED', DelBtnEnabled)
	
	DelBtnEnabled = False$
	SelRows       = Get_Property(@Window:'.EDT_AD_GROUPS', 'SELPOS')
	SelRows       = SelRows<2>
	SelData       = ''
	If SelRows NE '' then 
		Data    = Get_Property(@Window:'.EDT_AD_GROUPS', 'LIST')
		Convert @VM to '' in Data
		For each Row in SelRows using @VM
			If Data<Row> NE '' then 
				SelData<-1> = Data<Row>
			end
		Next Row
		If (SelData NE '') and (UseAD EQ True$) then DelBtnEnabled = True$
	end
	Set_Property(@Window:'.PUB_REM_GROUPS', 'ENABLED', DelBtnEnabled)
	
return


UnlockRec:
	
	Key = Get_Property(@Window, '@LOCK_KEY')
	If Key NE '' then 
		HaveLock = Get_Property(@Window, '@HAVE_LOCK')
		If HaveLock then
			Database_Services('ReleaseKeyIDLock', 'SEC_GROUPS', Key)
		end
	end			
	
return


