Compile function NDW_REACTORS_MODE_CHG_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NDW_REACTORS_MODE_CHG

Declare function Get_Property, Reactor_Services, Database_Services
Declare subroutine Set_Property, RList, Msg , Reactor_Services, End_Window, Post_Event

$Insert APP_INSERTS
$Insert EVENT_SETUP
$Insert REACTOR_EQUATES
$insert logical
$Insert RLIST_EQUATES
$Insert REACTOR_MODES_EQUATES
$Insert REACT_MODE_NG_EQUATES
$Insert MSG_EQUATES

// 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 else
    // Event not implemented
end

Return EventFlow or 1

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

Event WINDOW.CREATE(CreateParam)
    EventFlow = 1
    GoSub   Setup_OLE_Controls
    GoSub   PopulateReactors
    GoSub   PopulateModes
    GoSub   CheckCanSubmit
End Event

Event PUB_SELECT_ALL.CLICK()
    ReactorTableCtrlID = @Window : '.OLE_REACTOR_SEL_TABLE'
    Send_Message(ReactorTableCtrlID, "OLE.SelectAll")
    GoSub UpdateSelectedReactorsText
    GoSub CheckCanSubmit
end event

Event PUB_RESET_SELECTION.CLICK()
    ReactorTableCtrlID = @Window : '.OLE_REACTOR_SEL_TABLE'
    Send_Message(ReactorTableCtrlID, "OLE.UnselectAll")
    GoSub UpdateSelectedReactorsText
    GoSub CheckCanSubmit
end event

Event CMB_MODES.CHANGED(NewModeData)()
    CatDesc         = Xlate('REACTOR_MODES', NewModeData, REACTOR_MODES_SERVICE_CATEGORIES$, 'X')
    Swap @VM with @FM in CatDesc
    Set_Property(@Window : '.CMB_REACT_MODE_CAT', 'LIST', CatDesc)
    GoSub CheckCanSubmit
end event

Event CMB_REACT_MODE_CAT.CHANGED(NewData)()
    GoSub CheckCanSubmit
end event

Event PUB_SUBMIT.CLICK()
    SelectedMode     = Get_Property(@Window : '.CMB_MODES', "TEXT")
    SelectedSubCat   = Get_Property(@Window : '.CMB_REACT_MODE_CAT', 'TEXT')
    Comment          = Get_Property(@Window : '.TXT_COMMENTS', 'TEXT')
    SelectedReactors = Get_Property(@Window : '.OLE_REACTOR_SEL_TABLE', "OLE.SelList")
    Response         = Dialog_Box('NDW_VERIFY_USER', @WINDOW, @USER4 : @FM : 'LEAD' : @VM : 'SUPERVISOR' : @VM : 'MAINTENANCE')
    if Response AND DCOUNT(SelectedReactors, @FM) GT 1 then
        Def              = ""
        Def<MCAPTION$>   = "Please wait... Changing Modes..."
        Def<MTYPE$ >     = "GC"
        Def<MEXTENT$>    = DCOUNT(SelectedReactors, @FM)
        Def<MTEXTWIDTH$> = 400
        MsgUp = Msg(@WINDOW, Def)
        for each Reactor in SelectedReactors using @FM setting iPos
            Void = Msg(@WINDOW, MsgUp, iPos, MSGINSTUPDATE$)
            Reactor_Services('CreateReactModeChange', @USER4, Reactor<1,1>, SelectedMode, SelectedSubCat, Comment, True$)
        Next Reactor
        Msg(@WINDOW, MsgUp) 		;* take down the gauge
        GoSub ResetForm
    end else
        
    end
end event

Event PUB_CANCEL.CLICK()
    Post_Event(@Window, 'CLOSE')
end event

Event WINDOW.CLOSE(CancelFlag)
    End_Window(@Window)
end event

Event OLE_REACTOR_SEL_TABLE.OnItemClick(Row, Button, Col, Point)
    GoSub UpdateSelectedReactorsText
    GoSub CheckCanSubmit
end event

* * * * * * * * * * * * * * * * * * *
* * * * * * * * GO-SUBS * * * * * * * 
* * * * * * * * * * * * * * * * * * *

Setup_OLE_Controls:
 
    ReactorTableCtrlID = @Window : '.OLE_REACTOR_SEL_TABLE'
    Set_Property(ReactorTableCtrlID, "OLE.AlwaysShowSelection", True$)
    rv                 = Send_Message(ReactorTableCtrlID, "QUALIFY_EVENT", "OnItemClick", 1)
    rv                 = Send_Message(ReactorTableCtrlID, "QUALIFY_EVENT", "OnSelChange", 1)

    ColArray       = ''
    //Col 1(Reactor No)
    ColArray<1,1>  = 'Reactor';//Col Title - Reqd
    ColArray<1,2>  = 'T';//Col Title - required
    ColArray<1,3>  = 50;//Col Width - required
    ColArray<1,4>  = False$;//Autosize
    ColArray<1,7>  = True$;//Sortable
    ColArray<1,10> = 'Center';//Alignment
    //Col 2(Reactor Type)
    ColArray<2,1>  = 'Type';//Col Title - Reqd
    ColArray<2,2>  = 'T';//Col Title - required
    ColArray<2,3>  = 35;//Col Width - required
    ColArray<2,4>  = False$;//Autosize
    ColArray<2,7>  = True$;//Sortable
    ColArray<2,10> = 'Center';//Alignment
    //Col 3(Current Mode)
    ColArray<3,1>  = 'Curr Mode';//Col Title - Reqd
    ColArray<3,2>  = 'T';//Col Title - required
    ColArray<3,3>  = 200;//Col Width - required
    ColArray<3,4>  = False$;//Autosize
    ColArray<3,7>  = True$;//Sortable
    ColArray<3,10> = 'Center';//Alignment
    //Col 4(Current Sub Mode)
    ColArray<4,1>  = 'Sub Mode';//Col Title - Reqd
    ColArray<4,2>  = 'T';//Col Title - required
    ColArray<4,3>  = 150;//Col Width - required
    ColArray<4,4>  = False$;//Autosize
    ColArray<4,7>  = True$;//Sortable
    ColArray<4,10> = 'Center';//Alignment
    
    //Set Table Columns
    Set_Property(ReactorTableCtrlID, "OLE.ColumnList", ColArray)
    Set_Property(ReactorTableCtrlID, "OLE.MultiSelect", 2)
return

PopulateReactors:
    AllReactors    = Reactor_Services('GetReactorNumbers')
    ActiveReactors = ''
    for each reactorNo in AllReactors using @FM setting rPos
        arrIndex      = 1
        ReactorRec    = Database_Services('ReadDataRow', 'REACTOR', reactorNo)
        ReactorStatus = ReactorRec<REACTOR_REACT_ASSIGNMENT$>
        IF ReactorStatus NE 'O' then
            ReactType          = ReactorRec<REACTOR_REACT_TYPE$>
            ReactCurrModeKey   = Xlate('REACTOR_CHILD_KEY_IDS_NG', reactorNo, 1, 'X')<1,1>
            ReactCurrModeRec   = Database_Services('ReadDataRow', 'REACT_MODE_NG', ReactCurrModeKey)
            ReactCurrMode      = ReactCurrModeRec<REACT_MODE_NG_MODE$>
            ReactCurrSubMode   = ReactCurrModeRec<REACT_MODE_NG_SERVICE_DESC$>
            ActiveReactors<-1> = reactorNo : @VM : ReactType : @VM : ReactCurrMode : @VM : ReactCurrSubMode
        end
    Next reactorNo
    Set_Property(ReactorTableCtrlID, "OLE.RecordList", ActiveReactors)
    Send_Message(@Window:".OLE_REACTOR_SEL_TABLE", "OLE.UnselectAll")
return

PopulateModes:
    Statement = 'SELECT REACTOR_MODES BY DESCRIPTION'
    rlist( Statement, target_activelist$, '', '', '' )
    ModeList  = ''
    IF @RecCount then
        EoF = 0
        NumKeys = @RecCount
        Cnt = 0
        Loop
            ReadNext ReactorModeID Else EoF = 1
        until EoF
        	//Take out RESPONSE TIME modes
        	IF NOT(Indexc(ReactorModeID, "RESPONSE", 1)) then
        		ModeList<-1> = ReactorModeID
        	end

        Repeat
    end
    Set_Property(@Window : '.CMB_MODES', 'LIST', ModeList)
return

UpdateSelectedReactorsText:
    ReactorTableCtrlID   = @Window : '.OLE_REACTOR_SEL_TABLE'
    SelectedReactorsText = 'Selected Reactors: '
    SelectedReactors     = Get_Property(ReactorTableCtrlID, "OLE.SelList")
    for each Reactor in SelectedReactors using @Fm setting iPos
        SelectedReactorsText := Reactor<1,1> : ', '
    Next Reactor
    Set_Property(@Window : '.LBL_SELECTED_REACTORS', 'TEXT', SelectedReactorsText)
return

CheckCanSubmit:
    SelectedReactors = Get_Property(@Window : '.OLE_REACTOR_SEL_TABLE', "OLE.SelList")
    NewMode          = Get_Property(@Window : '.CMB_MODES', 'TEXT')
    NewSubCat        = Get_Property(@Window : '.CMB_REACT_MODE_CAT', 'TEXT')
    //Step1: Check that there are more than one reactors selected
    if DCount(SelectedReactors, @FM) > 1 then
        //Step2: Check that a mode is selected
        if NewMode NE '' then
            //Step3: Check that a mode category is selected
            if NewSubCat NE '' then
                Set_Property(@Window : '.PUB_SUBMIT', 'ENABLED', 1)
            end else
                Set_Property(@Window : '.PUB_SUBMIT', 'ENABLED', 0)
            end
        end else
            Set_Property(@Window : '.PUB_SUBMIT', 'ENABLED', 0)
        end
    end else
        Set_Property(@Window : '.PUB_SUBMIT', 'ENABLED', 0)
    end
return

ResetForm:
    //Unselect all reactors
    Send_Message(@Window:".OLE_REACTOR_SEL_TABLE", "OLE.UnselectAll")
    //Unselect Mode
    Set_Property(@Window : '.CMB_MODES', 'TEXT', '')
    //Unselect Sub Category
    Set_Property(@Window : '.CMB_REACT_MODE_CAT', 'TEXT', '')
    //Clear Comments
    Set_Property(@Window : '.TXT_COMMENTS', 'TEXT', '')
    //Reset Selected Reactors Text
    GoSub UpdateSelectedReactorsText
    //Reset Submit Button
    Set_Property(@Window : '.PUB_SUBMIT', 'ENABLED', False$)
    //Refresh the select box
    Set_Property(ReactorTableCtrlID, "OLE.RecordList", '')
    GoSub PopulateReactors
return



