<%@Control Language="VB" Debug="True" %>

<script runat="server">

<%------------------------------------------------------------%>

' enumeration of dialog types
Public Enum DialogType
  Alert = 0
  VBInfoMessage = 1
  VBWarningMessage = 2
  VBCriticalMessage = 3
  Confirm = 4
  VBOKCancel = 5
  VBYesNo = 6
  VBRetryCancel = 7
  Prompt = 8
  VBInput = 9
End Enum

<%------------------------------------------------------------%>

Sub AttachDialog(DlgType As DialogType, _
                 ControlID As String, _
                 EventName As String, _
                 MessageText As String, _
                 Optional Title As String = "", _
                 Optional CancelEvent As Boolean = False, _
                 Optional SubmitForm As Boolean = False)

  ' create hidden field in page for any return value
  Dim sHidFieldName As String = "AHHClientDlg$" & ControlID
  Page.RegisterHiddenField(sHidFieldName, "")

  ' get reference to BrowserCapabilities object
  Dim oBrowser As HttpBrowserCapabilities = Request.Browser

  ' see if it supports VBScript
  Dim bUseVBS As Boolean = oBrowser("VBScript")

  ' variables to hold language-specific details
  Dim sLang As String = "javascript"
  Dim sExt As String = "js"
  If bUseVBS = True Then
    ' set language specific variables
    sLang = "VBScript"
    sExt = "vbs"
  Else
    ' can only use JavaScript dialogs
    Select Case DlgType
      Case 1,2,3: DlgType = 0
      Case 5,6,7: DlgType = 4
      Case 9: DlgType = 8
    End Select
  End If

  ' set dialog type details
  Dim sFunction, sButtons As String
  Select Case DlgType
    Case DialogType.Alert:
      sFunction = "return AlertDlgEvent"
      sButtons = "0"
    Case DialogType.VBInfoMessage:
      sFunction = "return VBInfoDlgEvent"
      sButtons = "64"
    Case DialogType.VBWarningMessage:
      sFunction = "return VBInfoDlgEvent"
      sButtons = "48"
    Case DialogType.VBCriticalMessage:
      sFunction = "return VBInfoDlgEvent"
      sButtons = "16"
    Case DialogType.Confirm:
      sFunction = "return ConfirmDlgEvent"
      sButtons = "0"
    Case DialogType.VBOKCancel:
      sFunction = "return VBQuestionDlgEvent"
      sButtons = "33"
    Case DialogType.VBYesNo:
      sFunction = "return VBQuestionDlgEvent"
      sButtons = "292"
    Case DialogType.VBRetryCancel:
      sFunction = "return VBQuestionDlgEvent"
      sButtons = "309"
    Case DialogType.Prompt:
      sFunction = "return PromptDlgEvent"
      sButtons = "0"
    Case DialogType.VBInput:
      sFunction = "return VBInputDlgEvent"
      sButtons = "0"
  End Select

  ' create function name to attach to control
  sFunction &= "('" & sHidFieldName & "', '" _
    & MessageText.Replace("'", "\'") & "', '" _
    & Title.Replace("'", "\'") & "', " _
    & sButtons & ", " _
    & (Not CancelEvent).ToString().toLower() & ", " _
    & SubmitForm.ToString().toLower() & ");"

  ' attach client-side event handler to element
  ' need to determine base control type and cast to the
  ' appropriate type that has an Attributes collection
  Dim oCtrl As Control = Parent.FindControl(ControlID)
  If TypeOf oCtrl Is HtmlControl Then
    CType(oCtrl, HtmlControl).Attributes.Add(EventName, sFunction)
  ElseIf TypeOf oCtrl Is WebControl Then
    CType(oCtrl, WebControl).Attributes.Add(EventName, sFunction)
  Else
    Throw New Exception("Control Type Not Supported")
  End If

  ' create client-side script if not already registered
  If Not Page.IsClientScriptBlockRegistered("AHHClientDlg") Then
   Dim sPath As String = "/aspnet_client/custom/"
   Dim sScript As String = "<script language='" & sLang & "' " _
     & "src='" & sPath & "adaptive-dialog." & sExt & "'><" & "/script>"
   ' add this code to the page
   Page.RegisterClientScriptBlock("AHHClientDlg", sScript)

  End If

End Sub

<%------------------------------------------------------------%>

Function GetDialogResult(ControlID As String) As String

  ' build hidden field name
  Dim sHidFieldName As String = "AHHClientDlg$" & ControlID

  ' get posted value from Request collection
  Return Request.Form(sHidFieldName)

End Function

<%------------------------------------------------------------%>

</script>