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

<script runat="server">

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

Public Arguments As String = ""
Public BorderRaised As Boolean = False
Public CancelEvent As Boolean = False
Public Center As Boolean = True   'Top and Left must be empty
Public Height As Integer = 400
Public HelpButton As Boolean = False
Public Left As Integer = 150
Public Resizable As Boolean = False    'IE 5.5 and above only
Public ScrollBars As Boolean = True
Public StatusBar As Boolean = False    'IE 5.5 and above only
Public Top As Integer = 150
Public Width As Integer = 600

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

' use in page that opens dialog to attach dialog to control
Public Sub AttachDialog(ControlID As String, _
                        EventName As String, _
                        SourceURL As String)

  ' check values are provided for parameters
  If ControlID = "" Then
    Throw New Exception("Must specify ID of target control")
  End If
  If EventName = "" Then
    Throw New Exception("Must specify name of event to handle")
  End If
  If SourceURL = "" Then
    Throw New Exception("Must specify URL of page to display")
  End If

  ' variables used to build client-side script
  Dim sFeatures, sScript As String
  Dim sResize As String = "no"
  If (Resizable = True) Then
    sResize = "yes"
  End If
  Dim sStatus As String = "no"
  If (StatusBar = True) Then
    sStatus = "yes"
  End If
  Dim sBorder As String = "Sunken"
  If (BorderRaised = True) Then
    sBorder = "Raised"
  End If
  Dim sScroll As String = "no"
  If (ScrollBars = True) Then
    sScroll = "yes"
  End If
  Dim sHelp As String = "no"
  If (HelpButton = True) Then
    sHelp = "yes"
  End If

  ' get browser version, but only if it's Internet Explorer
  Dim fVer As Decimal = 0
  If Request.Browser.Browser = "IE" Then
    Try
      Dim iMajor As Integer = Request.Browser.MajorVersion
      Dim iMinor As Integer = Request.Browser.MinorVersion
      fVer = Decimal.Parse(iMajor.ToString() & "." _
                         & iMinor.ToString())
    Catch
    End Try
  End If

  ' create client-side script if not already registered
  If Not Page.IsClientScriptBlockRegistered("AHHIEDlg") Then
    ' decide whether position is specified or centered
    If (Center = True) Then
      sFeatures = "center:yes;"
    Else
      sFeatures = "dialogTop:" & Top.ToString() _
                & "px;dialogLeft:" & Left.ToString() & "px;"
    End If
    sFeatures &= "dialogHeight:" & Height.ToString() _
              & "px;dialogWidth:" & Width.ToString() _
              & "px;edge:" & sBorder & ";scroll:" _
              & sScroll & ";help:" & sHelp & ";"
    'see if it's IE 5.5 or higher
    If fVer >= 5.5 Then
      sFeatures &= "resizable:" & sResize _
                & ";status:" & sStatus & ";"
    End If
    sScript = "<script language='javascript'>" & vbCrlf _
    & "<!--" & vbCrlf _
    & "function IEDlgEvent(sURL, sArgs, sFeatures, sField," _
    & "                    bSubmit) {" & vbCrlf _
    & "  var oHidden = document.getElementById(sField);" & vbCrlf _
    & "  oHidden.value = window.showModalDialog(sURL, sArgs," _
    & "                    sFeatures)" & vbCrlf _
    & "  return bSubmit;" & vbCrlf _
    & "}" & vbCrlf _
    & "//-->" & vbCrlf _
    & "<" & "/script>" & vbCrlf
    Page.RegisterClientScriptBlock("AHHIEDlg", sScript)
  End If

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

  ' create function name to attach to control
  ' must escape any single quotes in agruments string
  Dim sArgs As String = Arguments.Replace("'", "\'")
  Dim sFunctionName As String = "return IEDlgEvent('" _
    & SourceURL & "', '" & sArgs & "', '" _
    & sFeatures & "', '"  & sHidFieldName & "', " _
    & (Not CancelEvent).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, sFunctionName)
  ElseIf TypeOf oCtrl Is WebControl Then
    CType(oCtrl, WebControl).Attributes.Add(EventName, sFunctionName)
  Else
    Throw New Exception("Control Type Not Supported")
  End If

End Sub

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

' use in page that opens dialog to get result afterwards
Function GetDialogResult(ControlID As String) As String

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

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

End Function

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

</script>