<%@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 ModalDialog As Boolean = True
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

  If (ModalDialog = True) And (fVer >= 4) Then

    ' browser is Internet Explorer 4 or higher and want dialog
    ' 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 + '?arg=' + escape(sArgs), '', sFeatures)" & vbCrlf _
      & "  return bSubmit;" & vbCrlf _
      & "}" & vbCrlf _
      & "//-->" & vbCrlf _
      & "<" & "/script>" & vbCrlf
      Page.RegisterClientScriptBlock("AHHIEDlg", sScript)
    End If

  Else

    ' browser is not IE4 or above, or want new window
    ' use window.open method to open a new browser window
    ' create client-side script if not already registered
    If Not Page.IsClientScriptBlockRegistered("AHHIEDlg") Then
      sFeatures = "top=" & Top.ToString() _
                & ",left=" & Left.ToString() _
                & ",height=" & Height.ToString() _
                & ",width=" & Width.ToString() _
                & ",scrollbars=" & sScroll _
                & ",resizable=" & sResize _
                & ",status=" & sStatus _
                & ",titlebar=yes,menubar=no,location=no," _
                & "fullscrceen=no,toolbar=no,directories=no"
      sScript = "<script language='javascript'>" & vbCrlf _
      & "<!--" & vbCrlf _
      & "function IEDlgEvent(sURL, sArgs, sFeatures, sField," _
      & "                    bSubmit) {" & vbCrlf _
      & "  window.open(sURL + '?arg=' + escape(sArgs), '_blank'," _
      & "                    sFeatures);" & vbCrlf _
      & "  return false;" & vbCrlf _
      & "}" & vbCrlf _
      & "//-->" & vbCrlf _
      & "<" & "/script>" & vbCrlf
      Page.RegisterClientScriptBlock("AHHIEDlg", sScript)
    End If

  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

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

' use to get argument passed from opening page into window
Function GetWindowArgument(ControlID As String) As String

 ' get posted value from Request collection
  Return Server.UrlDecode(Request.QueryString("arg"))

End Function

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

' use in browser window to set arg to pass back to opening page
Sub SetWindowResult(ControlID As String, ReturnValue As String)

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

  ' create client-side script
  Dim sScript As String
  sScript = "<script language='javascript'>" & vbCrlf _
    & "<!--" & vbCrlf _
    & "if (opener != null) {" & vbCrlf _
    & "  var oHidden = window.opener.document.forms[0]" _
    & "  .elements['" & sHidFieldName & "'];" & vbCrlf _
    & "  if (oHidden != null)" & vbCrlf _
    & "    oHidden.value = '" & ReturnValue & "';" & vbCrlf _
    & "  }" & vbCrlf _
    & "else" & vbCrlf _
    & "  window.returnValue = '" & ReturnValue & "';" & vbCrlf _
    & "window.close();" & vbCrlf _
    & "//-->" & vbCrlf _
    & "<" & "/script>" & vbCrlf
  Page.RegisterStartupScript("AHHDlgReturn", sScript)

End Sub

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

</script>