'<script Language="vbscript">
'******************************************************************************************
'* cModal
'* Class is intended to instantiate the Meridian system Modal.  
'******************************************************************************************
'******************************************************************************************
'* WHO     WHEN        WHAT
'*========================================================================================
'* RLS     01/23/2001  Original
'* JPD     9/13/2001   Added safeButton parameter, pass in button that should have focus when the modal
'*                     is launched.
'* BOB     08/30/2001  Added Unique naming functionality to the class and public 
'*                     modal function.
'* MCB     04/04/2002  Added ArgumentObject for passing only an object to a modal.
'* SSM     05/14/2002  Changed reference to dialog page since changed to html file.
'*                     Also did some code clean up, renamed Modal to MessageBox - used to be
'*                     ModalMsgBox but conflicting name in validate.vbs
'* jarrett 09.14.2004  Added Resize, WindowHeight, and WindowWidth.  This was done so that
'*                     this class could support modal resizing and return through the
'*                     WindowHeight and WindowWidth properties the modal dimensions when
'*                     the modal was closed.  The dimensions are returned so that we can 
'*                     remember restore the modal to these same dimensions the next time it
'*                     is called.
'******************************************************************************************

Class cModal

  '---| class variable(s) |---
  Private mButtonType
  Private mHeadingMessage
  Private mBodyMessage
  Private mSafeButton
  Private mBodyWidth
  Private mBodyHeight
  Private mScrollBar
  Private mOpacity
  Private mMessageBoxURI
  Private mArgList
  Private mModalName  
  Private mobjModalDialog
  Private bObjectParameter
  Private bStringParameter
  Private bReturnValueIsObject
  Private mDialogPositionMode
  Private mTop
  Private mLeft
  Private mArgObject
  Private mResizeWidth
  Private mResizeHeight
  Private mWindowHeight
  Private mWindowWidth
  Private mIsModeless
  Private mSettingsGUID
  Private mMinimumWidth
  Private mMinimumHeight
  Private mMaximumWidth
  Private mMaximumHeight

' This is the corresponding position elements that are allowed in the meridian shell
' one and six are the most common used. Most are fairly self explanitory.
' typedef enum {
'        dpmCenterInContentWindow	= 1,	'Centered in the shell window minus navigation bar area.
'        dpmCenterInFrameWindow		= 2,	'Centered in the shell window.
'        dpmCenterOnScreen			= 3,	'Centered in the monitor.  
'        dpmAbsolute				= 4,	' not sure how this one works
'        dpmRelativeToContentWindow = 5,	' dito
'        dpmCenterInParentWindow	= 6		' Centered on calling window
'    }


  Private Sub Class_Initialize()
    
    '---| initialize variable(s) |---
    mButtonType		       = vbOKOnly
    mHeadingMessage      = vbNullString
    mBodyMessage	       = vbNullString
    mSafeButton		       = vbNullString
    mBodyWidth		       = 800
    mBodyHeight		       = 500
    mScrollBar		       = False
    mOpacity		         = 100
    mMessageBoxURI       = "/Common/MsgBox.htm"
    mArgList		         = vbNullString	
    mModalName		       = "Modal " & CStr(external.CreateID)
    Set mobjModalDialog	 = Nothing
    mDialogPositionMode  = 1
    mTop			           = 0
    mLeft			           = 0
    bObjectParameter		 = False
    bStringParameter		 = False
    bReturnValueIsObject = False
    Set mArgObject       = Nothing
    mResizeWidth         = False
    mResizeHeight        = False
    mWindowHeight        = mBodyHeight
    mWindowWidth         = mBodyWidth
    mIsModeless          = False
    mSettingsGUID        = "1E6405D3-55CD-4912-95F0-64D85CB47BCD"

    mMinimumWidth = -1
    mMinimumHeight = -1
    mMaximumWidth = -1
    mMaximumHeight = -1
    
  End Sub

  Private Sub Class_Terminate()
  
    If Not mobjModalDialog Is Nothing Then Set mobjModalDialog = Nothing
    If IsObject(mArgList) Then
      If Not mArgList Is Nothing Then Set mArgList = Nothing
  End if
    
  End Sub

  Public Function ShowModal()
  
    Dim vntArgList
  
    On Error Resume Next
    
    If CBool(mIsModeless) Then
      Set mobjModalDialog = external.ActiveDocuments.AddModeless(mHeadingMessage, mDialogPositionMode, mTop, mLeft, mBodyWidth, mBodyHeight, mSettingsGUID, CBool(mResizeWidth), CBool(mResizeHeight))
    End If
    
    Dim DoModal : DoModal = (mobjModalDialog Is Nothing)
    
    If DoModal Then
      Set mobjModalDialog = external.ActiveDocuments.AddDialogEx(mModalName, mDialogPositionMode, mTop, mLeft, mBodyWidth, mBodyHeight, mSettingsGUID, CBool(mResizeWidth), CBool(mResizeHeight))
    End If

    On Error Goto 0
    
    If mobjModalDialog is Nothing Then
      Set mobjModalDialog = external.ActiveDocuments.AddDialog(mModalName, mDialogPositionMode, mTop, mLeft, mBodyWidth, mBodyHeight)
    End If

    If Not mobjModalDialog Is Nothing Then
      mobjModalDialog.URI				= mMessageBoxURI
      mobjModalDialog.Opacity			= mOpacity
      mobjModalDialog.AllowScrollBar	= mScrollBar
      mobjModalDialog.AllowResizeWidth = mResizeWidth
      mobjModalDialog.AllowResizeHeight = mResizeHeight

      If mMinimumWidth > 0 Then
        mobjModalDialog.MinimumWidth = mMinimumWidth
      End If

      If mMinimumHeight > 0 Then
        mobjModalDialog.MinimumHeight = mMinimumHeight
      End If

      If mMaximumWidth > 0 Then
        mobjModalDialog.MaximumWidth = mMaximumWidth
      End If

      If mMaximumHeight > 0 Then
        mobjModalDialog.MaximumHeight = mMaximumHeight
      End If
    
      If bObjectParameter Or bStringParameter Then
        If bReturnValueIsObject Then
          Set ShowModal = mobjModalDialog.ShowDialog(mArgList)
        Else
          ShowModal = mobjModalDialog.ShowDialog(mArgList)
        End if
      Else
        vntArgList = Array(mButtonType, mBodyMessage, mHeadingMessage, mSafeButton)
        If bReturnValueIsObject Then
          Set ShowModal = mobjModalDialog.ShowDialog(vntArgList)
        Else
          ShowModal = mobjModalDialog.ShowDialog(vntArgList)
        End If
      End If

      If mResizeWidth Then
        mWindowWidth  = mobjModalDialog.WindowWidth
      End If

      If mResizeHeight Then
        mWindowHeight = mobjModalDialog.WindowHeight		  		 
      End If
    
      Set mobjModalDialog = Nothing
      external.ActiveDocuments.Remove(mModalName)
    End If

  End Function

'---------------------------------| Property Let Methods |-----------------------------------------

  Public Property Let IsModeless(inData)
    mIsModeless = inData
  End Property
  
  Public Property Let ButtonType(inData)
    mButtonType = inData		
  End Property

  Public Property Let HeadingMessage(inData)
    mHeadingMessage = inData		
  End Property

  Public Property Let BodyMessage(inData)
    mBodyMessage = inData		
  End Property
  
  Public Property Get WindowWidth()
    WindowWidth = mWindowWidth
  End Property

  Public Property Let BodyWidth(inData)
    mBodyWidth = inData		
  End Property
  
  Public Property Get WindowHeight()
    WindowHeight = mWindowHeight
  End Property

  Public Property Let BodyHeight(inData)
    mBodyHeight = inData		
  End Property

  Public Property Let ScrollBar(inData)
    If inData = True Or indata = False Then	mScrollBar = inData		
  End Property

  Public Property Let Opacity(inData)
    If IsNumeric(inData) And inData < 101 Then mOpacity = inData		
  End Property

  Public Property Let MessageBoxURI(inData)
    mMessageBoxURI = inData		
  End Property

  Public Property Let URL(inData)
    mMessageBoxURI = inData		
  End Property

  Public Property Let DialogPositionMode(inData)
    mDialogPositionMode = inData
  End Property

  Public Property Let BodyTop(inData)
    mTop = inData
  End Property

  Public Property Let BodyLeft(inData)
    mLeft = inData
  End Property

  Public Property Let ArgumentList(inData)
    mArgList = inData
    bStringParameter = True
  End Property

  Public Property Let SetArgumentList(inData)
    Set mArgList = inData
    bObjectParameter = True		
  End Property

  Public Property Let ReturnValueIsObject(inData)
    If inData = True Or inData = False then bReturnValueIsObject = inData
  End Property

  Public Property Let ModalName(inData)
    mModalName = inData
  End Property
  
  Public Property Let SafeButton(inData)
    mSafeButton = inData
  End Property

  Public Property Get Resize()
    Resize = mResizeWidth And mResizeHeight
  End Property
  
  Public Property Let Resize(inData)
    mResizeWidth = inData
    mResizeHeight = inData
  End Property
  
  Public Property Get ResizeWidth
    ResizeWidth = mResizeWidth
  End Property
  
  Public Property Let ResizeWidth(vntData)
    mResizeWidth = vntData
  End Property
  
  Public Property Get ResizeHeight
    ResizeHeight = mResizeHeight
  End Property
  
  Public Property Let ResizeHeight(vntData)
    mResizeHeight = vntData
  End Property
  
  Public Property Get SettingsGUID
    SettingsGUID = mSettingsGUID
  End Property
  
  Public Property Let SettingsGUID(inData)
    mSettingsGUID = inData
  End Property

  Public Property Get MinimumWidth()
    MinimumWidth = mMinimumWidth
  End Property

  Public Property Let MinimumWidth(inData)
    mMinimumWidth = inData
  End Property

  Public Property Get MinimumHeight()
    MinimumHeight = mMinimumHeight
  End Property

  Public Property Let MinimumHeight(inData)
    mMinimumHeight = inData
  End Property

  Public Property Get MaximumWidth()
    MaximumWidth = mMaximumWidth
  End Property

  Public Property Let MaximumWidth(inData)
    mMaximumWidth = inData
  End Property

  Public Property Get MaximumHeight()
    MaximumHeight = mMaximumHeight
  End Property

  Public Property Let MaximumHeight(inData)
    mMaximumHeight = inData
  End Property
End Class

Public Function MessageBox(buttonType, headingMessage, bodyMessage, bodyWidth, bodyHeight, safeButton)
  MessageBox = MessageBoxEx(buttonType, headingMessage, bodyMessage, bodyWidth, bodyHeight, safeButton, 0)
End Function

Public Function InfoBox(bodyMessage, headingMessage, types)
  Dim buttonType, iconType, safeButton, remainingTypes, defaultButton

  'Extracting button type and icon type
  If types >= vbDefaultButton2 And types < vbDefaultButton3 Then
    remainingTypes = types - vbDefaultButton2
    defaultButton = vbDefaultButton2
  ElseIf types >= vbDefaultButton3 And types < vbDefaultButton4 Then
    remainingTypes = types - vbDefaultButton3
    defaultButton = vbDefaultButton3
  ElseIf types >= vbDefaultButton4 Then
    remainingTypes = types - vbDefaultButton4
    defaultButton = vbDefaultButton4
  Else
    remainingTypes = types
    defaultButton = vbDefaultButton1
  End If

  buttonType = CInt(Mid(hex(remainingTypes), len(hex(remainingTypes))))
  iconType = remainingTypes - buttonType
  
  Dim bodyWidth
  Dim bodyHeight
  Dim strLength 
  
  'If default button number is greater than the number of buttons, or a default button is not passed in, the safe button defaults to the first button
  'Ex: OK Cancel is two buttons, if vbDefaultButton3 or vbDefaultButton4 is passed in or no default button is passed in, safeButton = vbOK
  Select Case buttonType 
    Case vbOKOnly
      safeButton = vbOK
    Case vbOkCancel
      If defaultButton = vbDefaultButton2 Then
        safeButton = vbCancel
      Else
        safeButton = vbOK
      End If
    Case vbAbortRetryIgnore
      If defaultButton = vbDefaultButton3 Then
        safeButton = vbIgnore
      ElseIf defaultButton = vbDefaultButton2 Then
        safeButton = vbRetry
      Else
        safeButton = vbAbort
      End If
    Case vbRetryCancel
      If defaultButton = vbDefaultButton2 Then
        safeButton = vbCancel
      Else
        safeButton = vbRetry
      End If
    Case vbYesNoCancel
      If defaultButton = vbDefaultButton3 Then
        safeButton = vbCancel
      ElseIf defaultButton = vbDefaultButton2 Then
        safeButton = vbNo
      Else
        safeButton = vbYes
      End If
    Case vbYesNo 
      If defaultButton = vbDefaultButton2 Then
        safeButton = vbNo
      Else
        safeButton = vbYes
      End If
    Case Else
      safeButton = null
  End Select

  Dim maxWidth
  Dim height : height = 100
  Dim width : width = 350
  If iconType = 0 Then
    maxWidth = 520
  Else
    maxWidth = 480
  End if
  
  Dim messageDiv : messageDiv = "<div class=""tyl-ui-announcement-small"" style=""white-space:nowrap; visibility:hidden; position:absolute; left:-500; top:-500;""></div>"
  Dim divElement : Set divElement = document.createElement("div")
  divElement.innerHTML = messageDiv
  Dim docElement : Set docElement = divElement.firstChild
  docElement.innerText = bodyMessage
  document.body.appendChild(docElement)
  
  Dim nodeWidth : nodeWidth = docElement.offsetWidth
  If nodeWidth > width Then
    width = nodeWidth
    If nodeWidth > maxWidth Then
      docElement.style.whiteSpace = ""
      docElement.style.width = maxWidth & "px"
      width = maxWidth
    End If
  End If
  height = height + docElement.offsetHeight
  document.body.removeChild(docElement)
  InfoBox = MessageBoxEx(buttonType, headingMessage, bodyMessage, width, height, safeButton, iconType)
End Function

' Function provided so a msgbox can be created in one line.
' The version in the validate module should no longer be used
Public Function MessageBoxEx(buttonType, headingMessage, bodyMessage, bodyWidth, bodyHeight, safeButton, iconType)

  Dim strModalName
  Dim vntArgList   
  Dim objModalDialog
  
  If CStr(buttonType) = vbNullString Then buttonType = vbOKOnly
  If CStr(bodyWidth)  = vbNullString Then bodyWidth  = 250
  If CStr(bodyHeight) = vbNullString Then bodyHeight = 150
              
    strModalName = "Modal " & CStr(external.CreateID)			    
    vntArgList = Array(buttonType, bodyMessage, headingMessage, safeButton, iconType)
  
  Set objModalDialog = external.ActiveDocuments.AddDialog(strModalName, 6, 0, 0, bodyWidth, bodyHeight)
  If Not (objModalDialog Is Nothing) Then
    
    objModalDialog.URI = "/Common/MsgBox.htm"
    objModalDialog.Opacity = 100
    objModalDialog.AllowScrollBar = False							
    
    MessageBoxEx = objModalDialog.ShowDialog(vntArgList)
    
    external.ActiveDocuments.Remove(strModalName)
    Set objModalDialog = Nothing
    
  End If

End Function

' This function is provided for backward compability (retrofit) only.  Use the above function 
' when ever possible.
Public Function Modal(buttonType, headingMessage, bodyMessage, bodyWidth, bodyHeight, safeButton)

  Modal = MessageBox(buttonType, headingMessage, bodyMessage, bodyWidth, bodyHeight, safeButton)

End Function
