Compile function NDW_LOAD_UNLOAD_EXTRA_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NDW_LOAD_UNLOAD_EXTRA

Declare subroutine End_Dialog, Post_Event, Database_Services, Error_Services, Msg
Declare subroutine Set_Property, Rds_Services, Set_Status, Rlist, Obj_Post_Log
Declare function Database_Services, Error_Services, Qa_Services, Reactor_Services
Declare function Get_Property, Dialog_Box
$Insert EVENT_SETUP
$Insert LOGICAL
$Insert RDS_EQUATES
$Insert REACT_LL_EQUATES
$Insert REACTOR_EQUATES
$Insert MSG_EQUATES

GoToEvent Event for CtrlEntId else
	// Event not implemented
end

Return EventFlow or 1

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

Event WINDOW.CREATE(CreateParam)
	ResponseValue = Set_Property(@Window, '@responsevalue', 0)
	RDSNo         = CreateParam
	If RowExists('RDS', RDSNo) then
		Set_Property(@Window : '.EDL_RDS', 'TEXT', RDSNo)
		GoSub Refresh
	end else
		ResponseValue = Set_Property(@Window, '@responsevalue', 0)
		Post_Event(@Window, 'CLOSE')
	end
	
End Event

Event WINDOW.CLOSE(CancelFlag)
	ResponseValue = Get_Property(@Window, '@responsevalue')
	End_Dialog(@Window, ResponseValue)
end event

//Signature buttons

Event PUB_SIGN_UNLOAD_EX_1.CLICK()

	RDSNo = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	initParams    = ''
    initParams<4> = 'Sign Unload Extra 1'
    Response      = Dialog_Box('NDW_VERIFY_USER', @WINDOW, initParams)
    Valid         = Response<1>
    
    If Valid then
        Rds_Services('SignUnloadExtra1', RDSNo, @USER4)
        If Error_Services('NoError') then
            Msg(@Window, 'Unload Extra 1 Signed!')
        end else
            Msg(@Window, Error_Services('GetMessage'))
        end
	end
	GoSub Refresh	
end event

Event PUB_SIGN_LOAD_EX_1.CLICK()
	
	RDSNo = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	RDSRec                    = Database_Services('ReadDataRow', 'RDS', RDSNo)
	OldReactorNo              = RDSRec<RDS_REACTOR$>
	NewReactorNo              = Get_Property(@Window : '.EDL_REACTOR1', 'TEXT')
	initParams    = ''
    initParams<4> = 'Sign Load Extra 1'
    Response      = Dialog_Box('NDW_VERIFY_USER', @WINDOW, initParams)
    Valid         = Response<1>
    
    If Valid then
        If Qa_Services('LoadExtra1stReady', RDSNo) AND (OldReactorNo NE NewReactorNo) then
            Gosub ReactorChange
        end
        
        If Error_Services('NoError') then 
            Rds_Services('SignLoadExtra1', RDSNo, @USER4)
            If Error_Services('NoError') then
                Msg(@Window, 'Load Extra 1 Signed!')
            end else
                Msg(@Window, Error_Services('GetMessage'))
            end
        End else
            Msg(@Window, Error_Services('GetMessage'))
        end
    end
	GoSub Refresh
	
end event

Event PUB_SIGN_UNLOAD_EX_2.CLICK()
	RDSNo = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	initParams    = ''
    initParams<4> = 'Sign Unload Extra 2'
    Response      = Dialog_Box('NDW_VERIFY_USER', @WINDOW, initParams)
    Valid         = Response<1>
    
    If Valid then
        Rds_Services('SignUnloadExtra2', RDSNo, @USER4)
        If Error_Services('NoError') then
            Msg(@Window, 'Unload Extra 2 Signed!')
        end else
            Msg(@Window, Error_Services('GetMessage'))
        end
	end
	GoSub Refresh	
end event

Event PUB_SIGN_LOAD_EX_2.CLICK()
	RDSNo = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	RDSRec                    = Database_Services('ReadDataRow', 'RDS', RDSNo)
	OldReactorNo              = RDSRec<RDS_REACTOR$>
	NewReactorNo              = Get_Property(@Window : '.EDL_REACTOR2', 'TEXT')
	initParams    = ''
    initParams<4> = 'Sign Load Extra 2'
    Response      = Dialog_Box('NDW_VERIFY_USER', @WINDOW, initParams)
    Valid         = Response<1>
    
    If Valid then
        If Qa_Services('LoadExtra2ndReady', RDSNo) AND (OldReactorNo NE NewReactorNo) then
            Gosub ReactorChange
        end
        
        If Error_Services('NoError') then
            Rds_Services('SignLoadExtra2', RDSNo, @USER4)
            If Error_Services('NoError') then
                Msg(@Window, 'Load Extra 2 Signed!')
            end else
                Msg(@Window, Error_Services('GetMessage'))
            end
        End else
            Msg(@Window, Error_Services('GetMessage'))
        end
    end
    
	GoSub Refresh
	
end event

//Clear Signature Buttons

Event PUB_CLEAR_UNLOAD_EX_1.CLICK()
	RDSNo = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	Rds_Services('UnsignUnloadExtra1', RDSNo, @User4)
	If Error_Services('NoError') then
		Msg(@Window, 'Unload Extra 1 unsigned!')
	end else
		Msg(@Window, Error_Services('GetMessage'))
	end
	GoSub Refresh	
end event

Event PUB_CLEAR_LOAD_EX_1.CLICK()
	RDSNo = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	Rds_Services('UnsignLoadExtra1', RDSNo, @User4)
	If Error_Services('NoError') then
		Msg(@Window, 'Load Extra 1 unsigned!')
	end else
		Msg(@Window, Error_Services('GetMessage'))
	end
	GoSub Refresh	
end event

Event PUB_CLEAR_UNLOAD_EX_2.CLICK()
	RDSNo = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	Rds_Services('UnsignUnloadExtra2', RDSNo, @User4)
	If Error_Services('NoError') then
		Msg(@Window, 'Unload Extra 2 unsigned!')
	end else
		Msg(@Window, Error_Services('GetMessage'))
	end
	GoSub Refresh	
end event

Event PUB_CLEAR_LOAD_EX_2.CLICK()
	RDSNo = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	Rds_Services('UnsignLoadExtra2', RDSNo, @User4)
	If Error_Services('NoError') then
		Msg(@Window, 'Load Extra 2 unsigned!')
	end else
		Msg(@Window, Error_Services('GetMessage'))
	end
	GoSub Refresh
end event

//-----------------------------------------------------------------------------
// INTERNAL GO-SUBS
//-----------------------------------------------------------------------------


***************
Refresh:
***************
	
	RDSNo = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	If RowExists('RDS', RDSNo) then
		RDSRec                    = Database_Services('ReadDataRow', 'RDS', RDSNo)
		if Error_Services('NoError') then
		    UnloadSigned              = RDSRec<RDS_OPERATOR_OUT$>
            ReactorNo                 = RDSRec<RDS_REACTOR$>
            //Unload Extra 1
            UnloadExtra1Signature     = RDSRec<RDS_OP_OUT_EX1$>
            UnloadExtra1UserFirstLast = xlate( 'LSL_USERS', UnloadExtra1Signature, 'FIRST_LAST', 'X' )
            UnloadExtra1Date          = OConv(RDSRec<RDS_OP_OUT_EX1_DATE$>, 'D')
            UnloadExtra1Time          = Oconv(RDSRec<RDS_OP_OUT_EX1_TIME$>, 'MT')
            //Load Extra 1
            LoadExtra1Signature       = RDSRec<RDS_OP_IN_EX2$>
            LoadExtra1UserFirstLast   = xlate( 'LSL_USERS', LoadExtra1Signature, 'FIRST_LAST', 'X' )
            LoadExtra1Date            = Oconv(RDSRec<RDS_OP_IN_EX2_DATE$>, 'D')
            LoadExtra1Time            = Oconv(RDSRec<RDS_OP_IN_EX2_TIME$>, 'MT')
            LoadExtra1Reactor          = RDSRec<RDS_LOAD_EXTRA_1_REACTOR$>
            //Unload Extra 2
            UnloadExtra2Signature     = RDSRec<RDS_OP_OUT_EX2$>
            UnloadExtra2UserFirstLast = xlate( 'LSL_USERS', UnloadExtra2Signature, 'FIRST_LAST', 'X' )
            UnloadExtra2Date          = Oconv(RDSRec<RDS_OP_OUT_EX2_DATE$>, 'D')
            UnloadExtra2Time          = Oconv(RDSRec<RDS_OP_OUT_EX2_TIME$>, 'MT')
            //Load Extra 2
            LoadExtra2Signature       = RDSRec<RDS_OP_IN_EX3$>
            LoadExtra2UserFirstLast   = xlate( 'LSL_USERS', LoadExtra2Signature, 'FIRST_LAST', 'X' )
            LoadExtra2Date            = Oconv(RDSRec<RDS_OP_IN_EX3_DATE$>, 'D')
            LoadExtra2Time            = Oconv(RDSRec<RDS_OP_IN_EX3_TIME$>, 'MT')
            LoadExtra2Reactor          = RDSRec<RDS_LOAD_EXTRA_2_REACTOR$>
            
            //Populate text fields
            Set_Property(@Window : '.EDL_UNLOAD_EX_1_USER', 'TEXT', UnloadExtra1UserFirstLast)
            Set_Property(@Window : '.EDL_UNLOAD_EX_1_DATE', 'TEXT', UnloadExtra1Date)
            Set_Property(@Window : '.EDL_UNLOAD_EX_1_TIME', 'TEXT', UnloadExtra1Time)
            Set_Property(@Window : '.EDL_LOAD_EX_1_USER', 'TEXT', LoadExtra1UserFirstLast)
            Set_Property(@Window : '.EDL_LOAD_EX_1_DATE', 'TEXT', LoadExtra1Date)
            Set_Property(@Window : '.EDL_LOAD_EX_1_TIME', 'TEXT', LoadExtra1Time)
            Set_Property(@Window : '.EDL_UNLOAD_EX_2_USER', 'TEXT', UnloadExtra2UserFirstLast)
            Set_Property(@Window : '.EDL_UNLOAD_EX_2_DATE', 'TEXT', UnloadExtra2Date)
            Set_Property(@Window : '.EDL_UNLOAD_EX_2_TIME', 'TEXT', UnloadExtra2Time)
            Set_Property(@Window : '.EDL_LOAD_EX_2_USER', 'TEXT', LoadExtra2UserFirstLast)
            Set_Property(@Window : '.EDL_LOAD_EX_2_DATE', 'TEXT', LoadExtra2Date)
            Set_Property(@Window : '.EDL_LOAD_EX_2_TIME', 'TEXT', LoadExtra2Time)
            If LoadExtra1Reactor NE '' then
                Set_Property(@Window : '.EDL_REACTOR1', 'TEXT', LoadExtra1Reactor)
            end else
                Set_Property(@Window : '.EDL_REACTOR1', 'TEXT', ReactorNo)
            end
            If LoadExtra2Reactor NE '' then
                Set_Property(@Window : '.EDL_REACTOR2', 'TEXT', LoadExtra2Reactor)
            end else
                Set_Property(@Window : '.EDL_REACTOR2', 'TEXT', ReactorNo)
            end
            If UnloadSigned EQ '' then
                Set_Property(@Window : '.EDL_REACTOR1', 'ENABLED', True$)
                Set_Property(@Window : '.EDL_REACTOR2', 'ENABLED', True$)
            end else
                Set_Property(@Window : '.EDL_REACTOR1', 'ENABLED', False$)
                Set_Property(@Window : '.EDL_REACTOR2', 'ENABLED', False$)
            end
            If UnloadExtra1Signature NE '' then
                Set_Property(@Window : '.PUB_SIGN_UNLOAD_EX_1', 'ENABLED', False$)
                Set_Property(@Window : '.PUB_CLEAR_UNLOAD_EX_1', 'ENABLED', True$)
            end else
                Set_Property(@Window : '.PUB_SIGN_UNLOAD_EX_1', 'ENABLED', True$)
                Set_Property(@Window : '.PUB_CLEAR_UNLOAD_EX_1', 'ENABLED', False$)
            end
            If UnloadExtra2Signature NE '' then
                Set_Property(@Window : '.PUB_SIGN_UNLOAD_EX_2', 'ENABLED', False$)
                Set_Property(@Window : '.PUB_CLEAR_UNLOAD_EX_2', 'ENABLED', True$)
            end else
                Set_Property(@Window : '.PUB_SIGN_UNLOAD_EX_2', 'ENABLED', True$)
                Set_Property(@Window : '.PUB_CLEAR_UNLOAD_EX_2', 'ENABLED', False$)
            end
            If LoadExtra1Signature NE '' then
                Set_Property(@Window : '.PUB_SIGN_LOAD_EX_1', 'ENABLED', False$)
                Set_Property(@Window : '.PUB_CLEAR_LOAD_EX_1', 'ENABLED', True$)
            end else
                Set_Property(@Window : '.PUB_SIGN_LOAD_EX_1', 'ENABLED', True$)
                Set_Property(@Window : '.PUB_CLEAR_LOAD_EX_1', 'ENABLED', False$)
            end
            If LoadExtra2Signature NE '' then
                Set_Property(@Window : '.PUB_SIGN_LOAD_EX_2', 'ENABLED', False$)
                Set_Property(@Window : '.PUB_CLEAR_LOAD_EX_2', 'ENABLED', True$)
            end else
                Set_Property(@Window : '.PUB_SIGN_LOAD_EX_2', 'ENABLED', True$)
                Set_Property(@Window : '.PUB_CLEAR_LOAD_EX_2', 'ENABLED', False$)
            end
		end else
		    Msg(@Window, 'Error reading RDS record from database.')
		end
		
	end else
		ResponseValue = Set_Property(@Window, '@responsevalue', 0)
		Post_Event(@Window, 'CLOSE')
	end
	
return

ReactorChange:
    
    RDSNo   = Get_Property(@Window : '.EDL_RDS', 'TEXT')
    RDSRec  = Database_Services('ReadDataRow', 'RDS', RDSNo)
    if Error_Services('NoError') then
        WONo        = RDSRec<RDS_WO$>
        ReactorNo = ''
        LLChosen = ''
        ChangePermitted = False$
        Begin Case
            
            Case Control EQ 'PUB_SIGN_LOAD_EX_1'
                 ReactorNo = Get_Property(@Window : '.EDL_REACTOR1', 'TEXT')
                 ReactorField = RDS_LOAD_EXTRA_1_REACTOR$
            Case Control EQ 'PUB_SIGN_LOAD_EX_2'
                 ReactorNo = Get_Property(@Window : '.EDL_REACTOR2', 'TEXT')
                 ReactorField = RDS_LOAD_EXTRA_2_REACTOR$
            Case Otherwise$
            
        End Case
        
        If ReactorNo NE '' then
            PickPlace = Xlate('REACTOR', ReactorNo, 'PICK_PLACE', 'X')
            If (Reactor_Services('GetReactorAvailChamberCount', ReactorNo) NE 0) OR (PickPlace EQ False$) then
                Query         = 'SELECT REACT_STATE WITH SCHED_WO CONTAINING ':WONo
               GoSub ClearCursors
                Set_Status(0)
                RList(Query, TARGET_ACTIVELIST$, '', '' '')
                SchedReactors = ''
                EOF           = False$
                Loop
                    ReadNext SchedReactor else EOF = True$
                Until EOF
                    SchedReactors<0, -1> = SchedReactor
                Repeat
                
                Locate ReactorNo in SchedReactors using @VM setting vPos then
                    If PickPlace EQ True$ then
                        LLDisabled = XLATE('REACTOR', ReactorNo, 'ACTIVE_LL_DISABLED', 'X')
                        IF LLDisabled NE '' then
                            ReactLLRec = Database_Services('ReadDataRow', 'REACT_LL', LLDisabled)
                            SideDisabled = ReactLLRec<REACT_LL_DISABLED$>
                            QualMode = ReactLLRec<REACT_LL_QUAL_MODE$>
                            Begin Case
                                Case SideDisabled EQ 'L'
                                    LLChosen = 'R'
                                Case SideDisabled EQ 'R'
                                    LLChosen = 'L'
                            End Case
                         end else
                             Def = ''
                             Def<MCAPTION$> = 'Select Load Lock'
                             Def<MTYPE$> = 'BLeft,Right'
                             Def<MTEXT$> = 'Please select a load lock.'
                             Def<MMODAL$> = 'A'
                             LLResponse = Msg(@Window, Def)
                             Begin Case
                                Case LLResponse EQ 1
                                    LLChosen = 'L'
                                Case LLResponse EQ 2
                                    LLChosen = 'R'
                            End Case
                         end
                         If LLChosen NE '' then
                            ChangePermitted = True$
                        end else
                            Error_Services('Add', 'You must select a load lock for this lot.')
                        end
                    end else
                        ChangePermitted = True$
                    end
                end else
                    Swap @VM with ',' in SchedReactors
                    Error_Services('Add', 'Assigned reactor, ':ReactorNo:', does not equal any scheduled reactor(s), ':SchedReactors:'.')
                end
            end else
                Error_Services('Add', ReactorNo: 'does not have the capacity necessary for this lot.')
            end
        end else
            Error_Services('Add', 'Invalid Reactor number.')
        end
        If ChangePermitted then
            RDSRec<RDS_REACTOR$> = ReactorNo
            RDSRec<ReactorField> = ReactorNo
            RDSRec<RDS_LOAD_LOCK_SIDE$> = LLChosen
            Database_Services('WriteDataRow', 'RDS', RDSNo, RDSRec, True$, False$, False$)
            If Error_Services('NoError') then
                Msg(@Window, 'Reactor successfully changed!')
            end else
                Msg(@Window, 'Error changing reactor!')
            end
        end
    end else
        Msg(@Window, 'Error reading RDS from database!')
    end
    
    
return

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

ClearCursors:
    For counter = 0 to 8
        ClearSelect counter
    Next counter
return





