Compile function WEB_OI_WIZARD_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window WEB_OI_WIZARD

$Insert EVENT_SETUP
$Insert APP_INSERTS
$Insert PS_WebView_Equates

EQU ONE_DAY_SECS$ to 84600

Declare function   OI_Wizard_Services, Exec_Method, Environment_Services
Declare subroutine PlaceDialog

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

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

Event WINDOW.CREATE(CreateParam)

	OIWizardBaseUrl = Environment_Services('GetOIWizardBaseUrl')
	Set_Property(@Window, '@BASE_URL', OIWizardBaseUrl)
	InitNavPage     = CreateParam
	Set_Property(@Window, '@INIT_NAV_PAGE', InitNavPage)
	Set_Property(@Window, '@FIRST_RENDER', True$)
	Set_Property(@Window:'.WEBVIEW_MAIN', 'AUTOSIZEHEIGHT', True$)
	Set_Property(@Window:'.WEBVIEW_MAIN', 'AUTOSIZEWIDTH', True$)
	ScreenSize      = Get_Property('SYSTEM', 'SIZE')
	ClientWidth     = ScreenSize<3>
	ClientHeight    = ScreenSize<4>
	FormSize        = Get_Property(@Window, 'SIZE')
	FormWidth       = Int(ClientWidth * (3/4))
	FormHeight      = Int(ClientHeight * (3/4))
	FormSize<1>     = Int( (ClientWidth - FormWidth ) / 2 )
	FormSize<2>     = Int( (ClientHeight - FormHeight ) / 2 )
	FormSize<3>     = FormWidth
	FormSize<4>     = FormHeight
	FormSize<5>     = True$
	Set_Property(@Window, 'SIZE', FormSize)	
    
End Event


Event WEBVIEW_MAIN.WEBVIEWCREATED()

    OIWizardURL = Get_Property(@Window, '@BASE_URL')
    NavPage = Get_Property(@Window, '@INIT_NAV_PAGE')
    NavTo = OIWizardURL:NavPage
    SuccessFlag = Exec_Method( CtrlEntID, "NAVIGATE", NavTo)
    If Not(SuccessFlag) then
    	Msg(@Window, '', 'OK', '', 'Application Error':@FM:'Error navigating to ':NavTo)
    end
    
end event


Event WEBVIEW_MAIN.WEBNAVIGATED(NavID, URI, Redirected, UserInitiated, StatusInfo, FrameID)

	FirstRender = Get_Property(@Window, '@FIRST_RENDER')
	If FirstRender then 
		SuccessFlag = Exec_Method(CtrlEntID, "DELETECOOKIES", '*')
		If SuccessFlag then
			LSLUserId  = @User4
			OIWizardID = OI_Wizard_Services('CreateWizardID', LSLUserID)
			If Error_Services('NoError') then
				Expiry    = Xlate('OI_WIZARD', OIWizardID, 'EXPIRY', 'X')
				Domain    = Get_Property(@Window, '@BASE_URL')
				Swap 'http://'  with '' in Domain
				Swap 'https://' with '' in Domain
				PortIndex = Index(Domain, ':', 1)
				If PortIndex then Domain = Domain[1, PortIndex - 1]
				Cookie                      	   = ""
				Cookie<0, WBV_COOKIE_POS_NAME$>    = "sessionID"
				Cookie<0, WBV_COOKIE_POS_VALUE$>   = OIWizardID
				Cookie<0, WBV_COOKIE_POS_DOMAIN$>  = Domain
				Cookie<0, WBV_COOKIE_POS_EXPIRES$> = Expiry
				SuccessFlag                        = Exec_Method( CtrlEntID, "SETCOOKIE", Cookie )
				If SuccessFlag then
					Cookies                            = Exec_Method( CtrlEntID, "GETCOOKIES")	
					Cookie<0, WBV_COOKIE_POS_NAME$>    = "userID"
					Cookie<0, WBV_COOKIE_POS_VALUE$>   = LSLUserID
					Cookie<0, WBV_COOKIE_POS_DOMAIN$>  = Domain
					Cookie<0, WBV_COOKIE_POS_EXPIRES$> = Expiry
					SuccessFlag                        = Exec_Method( CtrlEntID, "SETCOOKIE", Cookie )
					If SuccessFlag then
						// Set a timer to execute our session service javascript hook once the page is ready
						Set_Property(@Window, "TIMER", 0:@FM:1000)
						Set_Property(@Window, '@FIRST_RENDER', False$)	
					end else
						Msg(@Window, '', 'OK', '', 'Application Error':@FM:'Error setting userID cookie')
					end
				end else
					Msg(@Window, '', 'OK', '', 'Application Error':@FM:'Error setting sessionID cookie')
				end
			end else
				Msg(@Window, '', 'OK', '', 'Application Error':@FM:'Error creating OI Wizard session')
			end			
		end else
			Msg(@Window, '', 'OK', '', 'Application Error':@FM:'Error deleting cookies')
		end
	end
	
end event


Event WINDOW.TIMER()
	
	Response = Exec_Method(@Window:'.WEBVIEW_MAIN', 'EXECUTESCRIPT', 'SessionServiceHelper.SetSessionFromCookies()')
	
end event

