Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Stonebroom

  Public Class MaskEdit

    ' specify base class to extend
    Inherits Textbox

    ' ----------------------------------------------

    ' private internal member variables
    Private _font As String = "Courier New"
    Private _mask As String = ""
    Private _fontsize As Integer = 10

    ' ----------------------------------------------

    ' public constructor
    Public Sub New()

      ' call base method first with element type
      MyBase.New()

    End Sub

    ' ----------------------------------------------

    ' public property accessor declarations

    Public Property Mask As String
      Get
        Return _mask
      End Get
      Set
        _mask = value
      End Set
    End Property

    Public Property FontSize As Integer
      Get
        Return _fontsize
      End Get
      Set
        _fontsize = value
      End Set
    End Property

    ' ----------------------------------------------

    OverRides Protected Sub AddAttributesToRender _
                            (writer As HtmlTextWriter)
      ' called when its time to add attributes to control

      ' call base class method to add standard attributes
      MyBase.AddAttributesToRender(writer)

      ' create mask for display as Textbox background
      Dim sQuery As String = _mask
      sQuery = sQuery.Replace("a", "_")
      sQuery = sQuery.Replace("A", "_")
      sQuery = sQuery.Replace("l", "_")
      sQuery = sQuery.Replace("L", "_")
      sQuery = sQuery.Replace("n", "_")
      sQuery = sQuery.Replace("?", "_")

      ' encode it for query string to pass to page
      ' mask-image.aspx that generates the image
      sQuery = Context.Server.UrlEncode(sQuery)

      ' create Style attribute value string
      Dim sStyle As String = "font-family:" & _font _
        & ";font-size:" & _fontsize _
        & "pt;background-image:url(mask-image.aspx?mask=" _
        & sQuery & "&font=" & Context.Server.UrlEncode(_font) _
        & "&size=" & _fontsize.ToString() _
        & "&cols=" & Columns.ToString() & ")"
      writer.AddAttribute(HtmlTextWriterAttribute.Style, sStyle)

      ' declare a carriage return character string
      Dim vbCrlf As String = Convert.ToChar(13) _
                           & Convert.ToChar(10)

      ' create string to use as Tooltip for control
      Dim sTip As String = Mask
      sTip = sTip.Replace("a", "[a]")
      sTip = sTip.Replace("A", "[A]")
      sTip = sTip.Replace("l", "[l]")
      sTip = sTip.Replace("L", "[L]")
      sTip = sTip.Replace("n", "[n]")
      sTip = sTip.Replace("?", "[?]")
      sTip = "Mask: " & sTip & vbCrlf & " where:" _
        & vbCrlf & "[a] = any alphanumeric character" _
        & vbCrlf & "[A] = an uppercase alphanumeric character" _
        & vbCrlf & "[l] = any letter character (A-Z, a-z)" _
        & vbCrlf & "[L] = an uppercase letter character (A-Z)" _
        & vbCrlf & "[n] = any numeric character (0-9)" _
        & vbCrlf & "[?] = any character"
      writer.AddAttribute(HtmlTextWriterAttribute.Title, sTip)

      ' add client-side event handler attributes
      Dim sParams As String = "(event, this, '" & _mask & "')"
      writer.AddAttribute("onkeydown", _
                          "return doKeyDown" & sParams)
      writer.AddAttribute("onkeypress", _
                          "return doKeyPress" & sParams)
      writer.AddAttribute("onkeyup", "return doKeyUp" & sParams)
      writer.AddAttribute("onfocus", "return doFocus" & sParams)


    End Sub

    ' ----------------------------------------------

    OverRides Protected Sub CreateChildControls()
      ' called when its time to create any child controls
      ' just used here to add client-side script section

      ' see if previous instance of this control has already
      ' added the required JavaScript code reference to the page
      If Not Page.IsClientScriptBlockRegistered("StonebroomMaskEdit") Then
        Dim sPath As String = "/aspnet_client/custom/"
        Dim sScript As String = "<script language='javascript' " _
          & "src='" & sPath & "maskedit.js'><" & "/script>"
        ' add this JavaScript code to the page
        Page.RegisterClientScriptBlock("StonebroomMaskEdit", sScript)
      End If

    End Sub

    ' ----------------------------------------------

  End Class

End Namespace