home *** CD-ROM | disk | FTP | other *** search
/ 66.142.0.142 / 66.142.0.142.tar / 66.142.0.142 / App_Code / aspxfn7.vb < prev    next >
Text File  |  2014-01-25  |  153KB  |  4,652 lines

  1. Imports System.Data
  2. Imports System.Data.Common
  3. Imports System.IO
  4. Imports System.Security.Cryptography
  5. Imports System.Drawing
  6. Imports System.Drawing.Imaging
  7. Imports System.Drawing.Drawing2D
  8. Imports System.Reflection
  9. Imports System.Data.OleDb
  10.  
  11. '
  12. ' ASP.NET Maker 7 Project Class (Shared Functions)
  13. ' (C)2009 e.World Technology Limited. All rights reserved.
  14. '
  15. Public Partial Class AspNetMaker7_tfpssnet
  16.     Inherits System.Web.UI.Page
  17.  
  18.     ' Compare object as string
  19.     Public Shared Function ew_SameStr(v1 As Object, v2 As Object) As Boolean
  20.         Return String.Equals(Convert.ToString(v1).Trim(), Convert.ToString(v2).Trim())
  21.     End Function
  22.  
  23.     ' Compare object as string (case insensitive)
  24.     Public Shared Function ew_SameText(v1 As Object, v2 As Object) As Boolean
  25.         Return String.Equals(Convert.ToString(v1).Trim().ToLower(), Convert.ToString(v2).Trim().ToLower())
  26.     End Function
  27.  
  28.     ' Check if empty string
  29.     Public Shared Function ew_Empty(value As Object) As Boolean
  30.         Return String.Equals(Convert.ToString(value).Trim(), String.Empty)
  31.     End Function
  32.  
  33.     ' Check if not empty string
  34.     Public Shared Function ew_NotEmpty(value As Object) As Boolean
  35.         Return Not ew_Empty(value)
  36.     End Function
  37.  
  38.     ' Convert object to integer
  39.     Public Shared Function ew_ConvertToInt(value As Object) As Integer
  40.         Try
  41.             Return Convert.ToInt32(value)
  42.         Catch
  43.             Return 0
  44.         End Try
  45.     End Function
  46.  
  47.     ' Convert object to double
  48.     Public Shared Function ew_ConvertToDouble(value As Object) As Double
  49.         Try
  50.             Return Convert.ToDouble(value)
  51.         Catch
  52.             Return 0
  53.         End Try
  54.     End Function
  55.  
  56.     ' Convert object to bool
  57.     Public Shared Function ew_ConvertToBool(ByVal value As Object) As Boolean
  58.         Try
  59.             If Information.IsNumeric(value) Then
  60.                 Return Convert.ToBoolean(ew_ConvertToDouble(value))
  61.             Else
  62.                 Return Convert.ToBoolean(value)
  63.             End If
  64.         Catch
  65.             Return False
  66.         End Try
  67.     End Function
  68.  
  69.     ' Get user IP
  70.     Public Shared Function ew_CurrentUserIP() As String
  71.         Return HttpContext.Current.Request.ServerVariables("REMOTE_HOST")
  72.     End Function    
  73.  
  74.     ' Get current host name, e.g. "www.mycompany.com"
  75.     Public Shared Function ew_CurrentHost() As String
  76.         Return HttpContext.Current.Request.ServerVariables("HTTP_HOST")
  77.     End Function    
  78.  
  79.     ' Get current date in default date format
  80.     Public Shared Function ew_CurrentDate() As String
  81.         If EW_DEFAULT_DATE_FORMAT = 6 OrElse EW_DEFAULT_DATE_FORMAT = 7 Then
  82.             Return ew_FormatDateTime(Today, EW_DEFAULT_DATE_FORMAT)
  83.         Else
  84.             Return ew_FormatDateTime(Today, 5)
  85.         End If
  86.     End Function    
  87.  
  88.     ' Get current time in hh:mm:ss format
  89.     Public Shared Function ew_CurrentTime() As String
  90.         Dim DT As DateTime = Now()
  91.         Return DT.ToString("HH:mm:ss")
  92.     End Function    
  93.  
  94.     ' Get current date in default date format with
  95.     ' Current time in hh:mm:ss format
  96.     Public Shared Function ew_CurrentDateTime() As String
  97.         Dim DT As DateTime = Now()
  98.         If EW_DEFAULT_DATE_FORMAT = 6 OrElse EW_DEFAULT_DATE_FORMAT = 7 Then
  99.             ew_CurrentDateTime = ew_FormatDateTime(DT, EW_DEFAULT_DATE_FORMAT)
  100.         Else
  101.             ew_CurrentDateTime = ew_FormatDateTime(DT, 5)
  102.         End If
  103.         ew_CurrentDateTime = ew_CurrentDateTime & " " & DT.ToString("HH:mm:ss")
  104.     End Function    
  105.  
  106.     ' Remove XSS
  107.     Public Shared Function ew_RemoveXSS(val As Object) As Object         
  108.         Dim val_before As String, pattern As String, replacement As String
  109.  
  110.         ' Handle null value
  111.         If IsDBNull(val) Then Return val
  112.  
  113.         ' Remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed 
  114.         ' This prevents some character re-spacing such as <java\0script> 
  115.         ' Note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
  116.  
  117.         Dim regEx As Regex = New Regex("([\x00-\x08][\x0b-\x0c][\x0e-\x20])", RegexOptions.IgnoreCase) ' Create regular expression.
  118.         val = regEx.Replace(Convert.ToString(val), "")
  119.  
  120.         ' Straight replacements, the user should never need these since they're normal characters 
  121.         ' This prevents like <IMG SRC=@avascript:alert('XSS')> 
  122.  
  123.         Dim search As String = "abcdefghijklmnopqrstuvwxyz"
  124.         search = search & "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
  125.         search = search & "1234567890!@#$%^&*()" 
  126.         search = search & "~`"";:?+/={}[]-_|'\"
  127.         For i As Integer = 0 To search.Length - 1
  128.  
  129.             ' ;? matches the ;, which is optional 
  130.             ' 0{0,7} matches any padded zeros, which are optional and go up to 8 chars    
  131.             ' @ @ search for the hex values
  132.  
  133.             regEx = New Regex("(&#[x|X]0{0,8}" & Hex(Asc(search(i))) & ";?)") ' With a ;
  134.             val = regEx.Replace(val, search(i))
  135.  
  136.             ' @ @ 0{0,7} matches '0' zero to seven times
  137.             regEx = New Regex("(�{0,8}" & Asc(search(i)) & ";?)") ' With a ;
  138.             val = regEx.Replace(val, search(i))
  139.         Next
  140.  
  141.         ' Now the only remaining whitespace attacks are \t, \n, and \r    
  142.         Dim Found As Boolean = True ' Keep replacing as long as the previous round replaced something 
  143.         Do While Found
  144.             val_before = val
  145.             For i As Integer = 0 To EW_REMOVE_XSS_KEYWORDS.GetUpperBound(0)
  146.                 pattern = ""
  147.                 For j As Integer = 0 To EW_REMOVE_XSS_KEYWORDS(i).Length - 1
  148.                     If j > 0 Then
  149.                         pattern = pattern & "("
  150.                         pattern = pattern & "(&#[x|X]0{0,8}([9][a][b]);?)?"
  151.                         pattern = pattern & "|(�{0,8}([9][10][13]);?)?"
  152.                         pattern = pattern & ")?"
  153.                     End If
  154.                     pattern = pattern & EW_REMOVE_XSS_KEYWORDS(i)(j)
  155.                 Next
  156.                 replacement = EW_REMOVE_XSS_KEYWORDS(i).Substring(0, 2) & "<x>" & EW_REMOVE_XSS_KEYWORDS(i).Substring(2) ' Add in <> to nerf the tag
  157.                 regEx = New Regex(pattern)
  158.                 val = regEx.Replace(val, replacement) ' Filter out the hex tags
  159.                 If val_before = val Then                    
  160.                     Found = False ' No replacements were made, so exit the loop
  161.                 End If
  162.             Next
  163.         Loop
  164.         Return val
  165.     End Function
  166.  
  167.     ' Highlight keywords
  168.     Public Shared Function ew_Highlight(name As String, src As Object, bkw As String, bkwtype As Object, akw As String) As String
  169.         Dim kw As String, kwlist() As String, kwstr As String = "", wrksrc As String, outstr As String = ""
  170.         Dim x As Integer, y As Integer, xx As Integer, yy As Integer
  171.         If ew_NotEmpty(src) AndAlso (ew_NotEmpty(bkw) OrElse ew_NotEmpty(akw)) Then
  172.             src = Convert.ToString(src)
  173.             xx = 0
  174.             yy = src.IndexOf("<", xx)
  175.             If yy < 0 Then yy = src.Length
  176.             Do While yy > 0
  177.                 If yy > xx Then
  178.                     wrksrc = src.Substring(xx, yy - xx)
  179.                     If ew_NotEmpty(bkw) Then kwstr = bkw.Trim()
  180.                     If ew_NotEmpty(akw) Then
  181.                         If kwstr.Length > 0 Then kwstr = kwstr & " "
  182.                         kwstr = kwstr & akw.Trim()
  183.                     End If
  184.                     kwlist = kwstr.Split(New Char() {" "})
  185.                     x = 0
  186.                     ew_GetKeyword(wrksrc, kwlist, x, y, kw)
  187.                     Do While y >= 0
  188.                         outstr = outstr & wrksrc.Substring(x, y - x) & _
  189.                             "<span id=""" & name & """ name=""" & name & """ class=""ewHighlightSearch"">" & _
  190.                             wrksrc.Substring(y, kw.Length) & "</span>"
  191.                         x = y + kw.Length
  192.                         ew_GetKeyword(wrksrc, kwlist, x, y, kw)
  193.                     Loop 
  194.                     outstr = outstr & wrksrc.Substring(x)
  195.                     xx = xx + wrksrc.Length
  196.                 End If
  197.                 If xx < src.Length Then
  198.                     yy = src.IndexOf(">", xx)
  199.                     If yy >= 0 Then
  200.                         outstr = outstr & src.Substring(xx, yy - xx + 1)
  201.                         xx = yy + 1
  202.                         yy = src.IndexOf("<", xx)
  203.                         If yy < 0 Then yy = src.Length + 1
  204.                     Else
  205.                         outstr = outstr & src.Substring(xx)
  206.                         yy = -1
  207.                     End If
  208.                 Else
  209.                     yy = -1
  210.                 End If
  211.             Loop
  212.         Else
  213.             outstr = Convert.ToString(src)
  214.         End If
  215.         Return outstr
  216.     End Function
  217.  
  218.     ' Get keyword
  219.     Public Shared Sub ew_GetKeyword(src As String, kwlist As String(), x As Integer, ByRef y As Integer, ByRef kw As String)
  220.         Dim wrky As Integer, thisy As Integer = -1, thiskw As String = "", wrkkw As String
  221.         For i As Integer = 0 To kwlist.GetUpperBound(0)
  222.             wrkkw = kwlist(i).Trim()
  223.             If wrkkw <> "" Then
  224.                 If EW_HIGHLIGHT_COMPARE Then ' Case-insensitive
  225.                     wrky = src.IndexOf(wrkkw, x, StringComparison.CurrentCultureIgnoreCase)
  226.                 Else
  227.                     wrky = src.IndexOf(wrkkw, x)
  228.                 End If
  229.                 If wrky > -1 Then
  230.                     If thisy = -1 Then
  231.                         thisy = wrky
  232.                         thiskw = wrkkw
  233.                     ElseIf wrky < thisy Then 
  234.                         thisy = wrky
  235.                         thiskw = wrkkw
  236.                     End If
  237.                 End If
  238.             End If
  239.         Next 
  240.         y = thisy
  241.         kw = thiskw
  242.     End Sub
  243.  
  244.     '
  245.     ' Security shortcut functions
  246.     '
  247.     ' Get current user name
  248.     Public Shared Function CurrentUserName() As String        
  249.         Return Convert.ToString(ew_Session(EW_SESSION_USER_NAME))
  250.     End Function    
  251.  
  252.     ' Get current user ID
  253.     Public Shared Function CurrentUserID() As Object
  254.         Return Convert.ToString(ew_Session(EW_SESSION_USER_ID))
  255.     End Function    
  256.  
  257.     ' Get current parent user ID
  258.     Public Shared Function CurrentParentUserID() As Object
  259.         Return Convert.ToString(ew_Session(EW_SESSION_PARENT_USER_ID))
  260.     End Function    
  261.  
  262.     ' Get current user level
  263.     Public Shared Function CurrentUserLevel() As Integer
  264.         Return ew_Session(EW_SESSION_USER_LEVEL_ID)
  265.     End Function
  266.  
  267.     ' Is Logged In
  268.     Public Shared Function IsLoggedIn() As Boolean
  269.         Return ew_SameStr(ew_Session(EW_SESSION_STATUS), "login")
  270.     End Function    
  271.  
  272.     ' Is System Admin
  273.     Public Shared Function IsSysAdmin() As Boolean
  274.         Return (ew_Session(EW_SESSION_SYS_ADMIN) = 1)
  275.     End Function
  276.  
  277.     ' Execute SQL
  278.     Public Shared Function ew_Execute(Sql As String) As Integer
  279.         Dim c As New cConnection()
  280.         Try
  281.             Return c.Execute(Sql)
  282.         Finally
  283.             c.Dispose()
  284.         End Try
  285.     End Function
  286.  
  287.     ' Execute SQL and return first value of first row
  288.     Public Shared Function ew_ExecuteScalar(Sql As String) As Object
  289.         Dim c As New cConnection()
  290.         Try
  291.             Return c.ExecuteScalar(Sql)
  292.         Finally
  293.             c.Dispose()
  294.         End Try
  295.     End Function
  296.  
  297.     ' Execute SQL and return first rowr
  298.     Public Shared Function ew_ExecuteRow(Sql As String) As OrderedDictionary
  299.         Dim dr As OleDbDataReader = Nothing
  300.         Dim c As New cConnection()
  301.         Try
  302.             dr = c.GetDataReader(Sql)
  303.             If dr.Read() Then
  304.                 Return c.GetRow(dr)
  305.             Else
  306.                 Return Nothing
  307.             End If
  308.         Finally
  309.             If dr IsNot Nothing Then
  310.                 dr.Close()
  311.                 dr.Dispose()
  312.             End If
  313.             c.Dispose()
  314.         End Try
  315.     End Function
  316.  
  317.     '
  318.     ' TEA encrypt/decrypt class
  319.     '
  320.     Public Class cTEA
  321.  
  322.     Public Shared Function Encrypt(ByVal Data As String, ByVal Key As String) As String
  323.             Try
  324.                 If Data.Length = 0 Then
  325.                     Throw New ArgumentException("Data must be at least 1 character in length.")
  326.                 End If
  327.                 Dim formattedKey As UInteger() = FormatKey(Key)
  328.                 If Data.Length Mod 2 <> 0 Then Data += Chr(0) ' Make sure array is even in length
  329.                 Dim dataBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
  330.                 Dim cipher As String = String.Empty
  331.                 Dim tempData As UInteger() = New UInteger(1) {}
  332.                 For i As Integer = 0 To dataBytes.Length - 1 Step 2
  333.                     tempData(0) = dataBytes(i)
  334.                     tempData(1) = dataBytes(i + 1)
  335.                     Code(tempData, formattedKey)
  336.                     cipher += ConvertUIntToString(tempData(0)) + ConvertUIntToString(tempData(1))
  337.                 Next
  338.                 Return UrlEncode(cipher)
  339.             Catch
  340.                 Return Data    
  341.             End Try
  342.     End Function
  343.  
  344.     Public Shared Function Decrypt(ByVal Data As String, ByVal Key As String) As String
  345.             Try
  346.                 Data = UrlDecode(Data)
  347.                 Dim formattedKey As UInteger() = FormatKey(Key)
  348.                 Dim x As Integer = 0
  349.                 Dim tempData As UInteger() = New UInteger(1) {}
  350.                 Dim dataBytes As Byte() = New Byte(Data.Length / 8 * 2 - 1) {}
  351.                 For i As Integer = 0 To Data.Length - 1 Step 8
  352.                     tempData(0) = ConvertStringToUInt(Data.Substring(i, 4))
  353.                     tempData(1) = ConvertStringToUInt(Data.Substring(i + 4, 4))
  354.                     Decode(tempData, formattedKey)
  355.                     dataBytes(x) = CByte(tempData(0))
  356.                     x += 1
  357.                     dataBytes(x) = CByte(tempData(1))
  358.                     x += 1
  359.                 Next
  360.                 Dim decipheredString As String = System.Text.ASCIIEncoding.ASCII.GetString(dataBytes, 0, dataBytes.Length)
  361.                 If decipheredString(decipheredString.Length - 1) = Chr(0) Then ' Strip the null char if it was added
  362.                     decipheredString = decipheredString.Substring(0, decipheredString.Length - 1)
  363.                 End If
  364.                 Return decipheredString
  365.             Catch
  366.                 Return Data
  367.             End Try
  368.     End Function
  369.  
  370.     Private Shared Function FormatKey(ByVal Key As String) As UInteger()
  371.             If Key.Length = 0 Then
  372.                 Throw New ArgumentException("Key must be between 1 and 16 characters in length")
  373.             End If
  374.             Key = Key.PadRight(16, " "c).Substring(0, 16) ' Ensure that the key is 16 chars in length
  375.             Dim formattedKey As UInteger() = New UInteger(3) {}
  376.  
  377.             ' Get the key into the correct format for TEA usage
  378.             Dim j As Integer = 0
  379.             For i As Integer = 0 To Key.Length - 1 Step 4
  380.                 formattedKey(j) = ConvertStringToUInt(Key.Substring(i, 4))
  381.                 j += 1
  382.             Next
  383.             Return formattedKey
  384.     End Function
  385.  
  386.     Private Shared Function Add(ByVal v1 As ULong, ByVal v2 As ULong) As UInteger
  387.             Dim t As ULong
  388.             If v1 = 4294967295 And v2 = 4294967295 Then
  389.                 t = 0
  390.             Else
  391.               t = v1 + v2
  392.             End If
  393.             If t > 2 ^ 32 Then t = t - 2 ^ 32
  394.             Return t
  395.     End Function
  396.  
  397.     Private Shared Function Minus(ByVal v1 As Long, ByVal v2 As Long) As UInteger
  398.             Dim t As Long
  399.             t = v1 - v2
  400.             If t > 2 ^ 32 Then
  401.                 t = t - 2 ^ 32
  402.             ElseIf t < 0 Then
  403.                 t = t + 2 ^ 32
  404.             End If
  405.             Return t
  406.     End Function
  407.  
  408.     Private Shared Sub Code(ByVal v As UInteger(), ByVal k As UInteger())
  409.             Dim y As UInteger = v(0)
  410.             Dim z As UInteger = v(1)
  411.             Dim sum As UInteger = 0
  412.             Dim delta As UInteger = 2654435769
  413.             Dim n As UInteger = 32
  414.             While n > 0
  415.                 y = Add(y, Add(z << 4 Xor z >> 5, z) Xor Add(sum, k(sum And 3)))
  416.                 sum = Add(sum, delta)
  417.                 z = Add(z, Add(y << 4 Xor y >> 5, y) Xor (Add(sum, k((sum >> 11) And 3))))
  418.                 n -= 1
  419.             End While
  420.             v(0) = y
  421.             v(1) = z
  422.     End Sub
  423.  
  424.     Private Shared Sub Decode(ByVal v As UInteger(), ByVal k As UInteger())
  425.             Dim y As UInteger = v(0)
  426.             Dim z As UInteger = v(1)
  427.             Dim sum As UInteger = 3337565984
  428.             Dim delta As UInteger = 2654435769
  429.             Dim n As UInteger = 32
  430.             While n > 0
  431.                 z = Minus(z, Add(y << 4 Xor y >> 5, y) Xor Add(sum, k(sum >> 11 And 3)))
  432.                 sum = Minus(sum, delta)
  433.                 y = Minus(y, Add(z << 4 Xor z >> 5, z) Xor Add(sum, k(sum And 3)))
  434.                 n -= 1
  435.             End While
  436.             v(0) = y
  437.             v(1) = z
  438.     End Sub
  439.  
  440.     Private Shared Function ConvertStringToUInt(ByVal Input As String) As UInteger
  441.             Dim output As UInteger
  442.             output = Convert.ToUInt32(Input(0))
  443.             output += (Convert.ToUInt32(Input(1)) << 8)
  444.             output += (Convert.ToUInt32(Input(2)) << 16)
  445.             output += (Convert.ToUInt32(Input(3)) << 24)
  446.             Return output
  447.     End Function
  448.  
  449.     Private Shared Function ConvertUIntToString(ByVal Input As UInteger) As String
  450.             Dim output As New System.Text.StringBuilder()
  451.             output.Append(Convert.ToChar(Input And 255))
  452.             output.Append(Convert.ToChar((Input >> 8) And 255))
  453.             output.Append(Convert.ToChar((Input >> 16) And 255))
  454.             output.Append(Convert.ToChar((Input >> 24) And 255))
  455.             Return output.ToString()
  456.     End Function
  457.  
  458.     Private Shared Function UrlEncode(ByVal str As String) As String
  459.             Dim encoding As New System.Text.UnicodeEncoding()
  460.             str = Convert.ToBase64String(encoding.GetBytes(str))
  461.             str = str.Replace("+"c, "-"c)
  462.             str = str.Replace("/"c, "_"c)
  463.             str = str.Replace("="c, "."c)
  464.             Return str
  465.     End Function
  466.  
  467.     Private Shared Function UrlDecode(ByVal str As String) As String
  468.             str = str.Replace("-"c, "+"c)
  469.             str = str.Replace("_"c, "/"c)
  470.             str = str.Replace("."c, "="c)
  471.             Dim dataBytes As Byte() = Convert.FromBase64String(str)
  472.             Dim encoding As New System.Text.UnicodeEncoding()
  473.             Return encoding.GetString(dataBytes)
  474.     End Function
  475.     End Class
  476.  
  477.     ' Save binary to file
  478.     Public Shared Function ew_SaveFile(folder As String, fn As String, ByRef filedata As Byte()) As Boolean
  479.         If ew_CreateFolder(folder) Then
  480.             Try
  481.                 Dim fs As FileStream
  482.                 fs = New FileStream(folder & fn, FileMode.Create)
  483.                 fs.Write(filedata, 0, filedata.Length)
  484.                 fs.Close()
  485.                 Return True
  486.             Catch
  487.                 If EW_DEBUG_ENABLED Then Throw
  488.                 Return False
  489.             End Try            
  490.         End If
  491.     End Function
  492.  
  493.     '
  494.     ' Common base class
  495.     '
  496.     Class AspNetMakerBase
  497.  
  498.         ' Parent page (The ASP.NET page inherited from System.Web.UI.Page)
  499.         Protected m_ParentPage As AspNetMaker7_tfpssnet
  500.  
  501.         ' Page (ASP.NET Maker page, e.g. List/View/Add/Edit/Delete)
  502.         Protected m_Page As AspNetMakerPage
  503.  
  504.         ' Parent page
  505.         Public Property ParentPage() As AspNetMaker7_tfpssnet
  506.             Get                
  507.                 Return m_ParentPage
  508.             End Get
  509.             Set(ByVal v As AspNetMaker7_tfpssnet)
  510.                 m_ParentPage = v    
  511.             End Set                    
  512.         End Property
  513.  
  514.         ' Page
  515.         Public Property Page() As AspNetMakerPage
  516.             Get                
  517.                 Return m_Page
  518.             End Get
  519.             Set(ByVal v As AspNetMakerPage)
  520.                 m_Page = v    
  521.             End Set                        
  522.         End Property
  523.  
  524.         ' Connection
  525.         Public Property Conn() As cConnection
  526.             Get                
  527.                 Return ParentPage.Conn
  528.             End Get
  529.             Set(ByVal v As cConnection)
  530.                 ParentPage.Conn = v    
  531.             End Set            
  532.         End Property
  533.  
  534.         ' Security
  535.         Public Property Security() As cAdvancedSecurity
  536.             Get                
  537.                 Return ParentPage.Security
  538.             End Get
  539.             Set(ByVal v As cAdvancedSecurity)
  540.                 ParentPage.Security = v    
  541.             End Set    
  542.         End Property
  543.  
  544.         ' Form
  545.         Public Property ObjForm() As cFormObj
  546.             Get                
  547.                 Return ParentPage.ObjForm
  548.             End Get
  549.             Set(ByVal v As cFormObj)
  550.                 ParentPage.ObjForm = v    
  551.             End Set    
  552.         End Property        
  553.     End Class
  554.  
  555.     '
  556.     ' Common page class
  557.     '
  558.     Class AspNetMakerPage
  559.         Inherits AspNetMakerBase        
  560.  
  561.         ' Page ID
  562.         Protected m_PageID As String = ""
  563.  
  564.         Public ReadOnly Property PageID() As String
  565.             Get
  566.                 Return m_PageID
  567.             End Get
  568.         End Property
  569.  
  570.         ' Table name
  571.         Protected m_TableName As String = ""
  572.  
  573.         Public ReadOnly Property TableName() As String
  574.             Get
  575.                 Return m_TableName
  576.             End Get
  577.         End Property
  578.  
  579.         ' Page object name
  580.         Protected m_PageObjName As String = ""
  581.  
  582.         Public ReadOnly Property PageObjName() As String
  583.             Get
  584.                 Return m_PageObjName
  585.             End Get
  586.         End Property
  587.  
  588.         ' Page object type name
  589.         Protected m_PageObjTypeName As String = ""
  590.  
  591.         Public ReadOnly Property PageObjTypeName() As String
  592.             Get
  593.                 Return m_PageObjTypeName
  594.             End Get
  595.         End Property
  596.  
  597.         ' Page Name
  598.         Public ReadOnly Property PageName() As String
  599.             Get
  600.                 Return ew_CurrentPage()
  601.             End Get
  602.         End Property
  603.  
  604.         ' Message
  605.         Public Property Message() As String
  606.             Get
  607.                 Return ew_Session(EW_SESSION_MESSAGE)
  608.             End Get
  609.             Set(ByVal v As String)
  610.                 If ew_NotEmpty(ew_Session(EW_SESSION_MESSAGE)) Then
  611.                     If Not ew_SameStr(ew_Session(EW_SESSION_MESSAGE), v) Then ' Append
  612.                         ew_Session(EW_SESSION_MESSAGE) = ew_Session(EW_SESSION_MESSAGE) & "<br>" & v
  613.                     End If
  614.                 Else
  615.                     ew_Session(EW_SESSION_MESSAGE) = v
  616.                 End If
  617.             End Set    
  618.         End Property
  619.  
  620.         ' Show Message
  621.         Public Sub ShowMessage()
  622.             If Message <> "" Then ' Message in Session, display
  623.                 ew_Write("<p><span class=""ewMessage"">" & Message & "</span></p>")
  624.                 ew_Session(EW_SESSION_MESSAGE) = "" ' Clear message in Session
  625.             End If
  626.         End Sub
  627.  
  628.         Public Sub ShowTLCMessage()
  629.             Dim tlcsub As New AspNetMaker7_tfpssnet
  630.             Dim tmpMessage As String = ""
  631.             tlcsub.tlcGetPSSMessage(tmpMessage)
  632.             If tmpMessage <> "" Then
  633.                 ew_Write("<table width='80%'><tr><td>")
  634.                 ew_Write("<font size='large' color='red'><b>Message from the Production Office: </b><em>" & tmpMessage & "</em></font>")
  635.                 ew_Write("</td></tr></table>")
  636.             End If
  637.         End Sub
  638.     End Class
  639.  
  640.     '
  641.     ' Email class
  642.     '
  643.     Class cEmail
  644.  
  645.         Public Sender As String ' Sender
  646.  
  647.         Public Recipient As String ' Recipient
  648.  
  649.         Public Cc As String = "" ' Cc
  650.  
  651.         Public Bcc As String = "" ' Bcc
  652.  
  653.         Public Subject As String ' Subject
  654.  
  655.         Public Format As String ' Format
  656.  
  657.         Public Content As String ' Content
  658.  
  659.         ' Load email from template
  660.         Public Sub Load(fn As String)
  661.             Dim sHeader As String, sWrk As String, arrHeader() As String
  662.             Dim sName As String, sValue As String
  663.             Dim i, j As Integer
  664.             sWrk = ew_LoadTxt(fn) ' Load text file content
  665.             sWrk = sWrk.Replace(vbCrLf, vbLf) ' Convert to Lf
  666.             sWrk = sWrk.Replace(vbCr, vbLf) ' Convert to Lf
  667.             If sWrk <> "" Then 
  668.                 i = sWrk.IndexOf(vbLf & vbLf) ' Locate header and mail content
  669.                 If i > 0 Then
  670.                     sHeader = sWrk.Substring(0, i + 1)
  671.                     Content = sWrk.Substring(i + 2)
  672.                     arrHeader = sHeader.Split(New Char() {vbLf})
  673.                     For j = 0 To arrHeader.GetUpperBound(0)
  674.                         i = arrHeader(j).IndexOf(":")
  675.                         If i > 0 Then
  676.                             sName = arrHeader(j).Substring(0, i).Trim()
  677.                             sValue = arrHeader(j).Substring(i + 1).Trim()
  678.                             Select Case sName.ToLower()
  679.                                 Case "subject"
  680.                                     Subject = sValue
  681.                                 Case "from"
  682.                                     Sender = sValue
  683.                                 Case "to"
  684.                                     Recipient = sValue
  685.                                 Case "cc"
  686.                                     Cc = sValue
  687.                                 Case "bcc"
  688.                                     Bcc = sValue
  689.                                 Case "format"
  690.                                     Format = sValue
  691.                             End Select
  692.                         End If
  693.                     Next 
  694.                 End If
  695.             End If
  696.         End Sub
  697.  
  698.         ' Replace sender
  699.         Public Sub ReplaceSender(ASender As String)
  700.             Sender = Sender.Replace("<!--$From-->", ASender)
  701.         End Sub
  702.  
  703.         ' Replace recipient
  704.         Public Sub ReplaceRecipient(ARecipient As String)
  705.             Recipient = Recipient.Replace("<!--$To-->", ARecipient)
  706.         End Sub
  707.  
  708.         ' Add cc email
  709.         Public Sub AddCc(ACc As String)
  710.             If ACc <> "" Then
  711.                 If Cc <> "" Then Cc = Cc & ";"
  712.                 Cc = Cc & ACc
  713.             End If
  714.         End Sub
  715.  
  716.         ' Add bcc email
  717.         Public Sub AddBcc(ABcc As String)
  718.             If ABcc <> "" Then
  719.                 If Bcc <> "" Then Bcc = Bcc & ";"
  720.                 Bcc = Bcc & ABcc
  721.             End If
  722.         End Sub
  723.  
  724.         ' Replace subject
  725.         Public Sub ReplaceSubject(ASubject As String)
  726.             Subject = Subject.Replace("<!--$Subject-->", ASubject)
  727.         End Sub
  728.  
  729.         ' Replace content
  730.         Public Sub ReplaceContent(Find As String, ReplaceWith As String)
  731.             Content = Content.Replace(Find, ReplaceWith)
  732.         End Sub
  733.  
  734.         ' Send email
  735.         Public Function Send() As Boolean
  736.             Return ew_SendEmail(Sender, Recipient, Cc, Bcc, Subject, Content, Format)
  737.         End Function
  738.  
  739.         ' Display as string
  740.         Public Function AsString() As String
  741.             Return "{" & "Sender: " & Sender & ", " & "Recipient: " & Recipient & ", " & "Cc: " & Cc & ", " & "Bcc: " & Bcc & ", " & "Subject: " & Subject & ", " & "Format: " & Format & ", " & "Content: " & Content & "}"
  742.         End Function
  743.     End Class    
  744.  
  745.     '
  746.     ' Class for Pager item
  747.     '    
  748.     Class cPagerItem
  749.  
  750.         Public Text As String
  751.  
  752.         Public Start As Integer
  753.  
  754.         Public Enabled As Boolean
  755.  
  756.         ' Constructor
  757.         Public Sub New(AStart As Integer, AText As String, AEnabled As Boolean)
  758.             Text = AText
  759.             Start = AStart
  760.             Enabled = AEnabled
  761.         End Sub
  762.  
  763.         ' Constructor
  764.         Public Sub New()
  765.  
  766.             ' Do nothing
  767.         End Sub    
  768.     End Class    
  769.  
  770.     '
  771.     ' Class for Numeric pager
  772.     '    
  773.     Class cNumericPager
  774.  
  775.         Public Items As New ArrayList
  776.  
  777.         Public PageSize As Integer, ToIndex As Integer, Count As Integer, FromIndex As Integer, RecordCount As Integer, Range As Integer
  778.  
  779.         Public LastButton As cPagerItem, PrevButton As cPagerItem, FirstButton As cPagerItem, NextButton As cPagerItem
  780.  
  781.         Public ButtonCount As Integer
  782.  
  783.         Public Visible As Boolean
  784.  
  785.         ' Constructor
  786.         Public Sub New(AFromIndex As Integer, APageSize As Integer, ARecordCount As Integer, ARange As Integer)
  787.             FromIndex = AFromIndex
  788.             PageSize = APageSize
  789.             RecordCount = ARecordCount
  790.             Range = ARange
  791.             FirstButton = New cPagerItem
  792.             PrevButton = New cPagerItem
  793.             NextButton = New cPagerItem
  794.             LastButton = New cPagerItem
  795.             Visible = True
  796.             Init()
  797.         End Sub
  798.  
  799.         ' Init pager
  800.         Public Sub Init()
  801.             If FromIndex > RecordCount Then FromIndex = RecordCount
  802.             ToIndex = FromIndex + PageSize - 1
  803.             If ToIndex > RecordCount Then ToIndex = RecordCount
  804.             Count = 0
  805.             SetupNumericPager()
  806.  
  807.             ' Update button count
  808.             ButtonCount = Count + 1
  809.             If FirstButton.Enabled Then ButtonCount = ButtonCount + 1
  810.             If PrevButton.Enabled Then ButtonCount = ButtonCount + 1
  811.             If NextButton.Enabled Then ButtonCount = ButtonCount + 1
  812.             If LastButton.Enabled Then ButtonCount = ButtonCount + 1
  813.         End Sub
  814.  
  815.         ' Add pager item
  816.         Private Sub AddPagerItem(StartIndex As Integer, Text As String, Enabled As Boolean)
  817.             Items.Add(New cPagerItem(StartIndex, Text, Enabled))
  818.             Count = Items.Count
  819.         End Sub
  820.  
  821.         ' Setup pager items
  822.         Private Sub SetupNumericPager()
  823.             Dim HasPrev As Boolean, NoNext As Boolean
  824.             Dim dy2 As Integer, dx2 As Integer, y As Integer, x As Integer, dx1 As Integer, dy1 As Integer, ny As Integer, TempIndex As Integer
  825.             If RecordCount > PageSize Then
  826.                 NoNext = (RecordCount < (FromIndex + PageSize))
  827.                 HasPrev = (FromIndex > 1)
  828.  
  829.                 ' First Button
  830.                 TempIndex = 1
  831.                 FirstButton.Start = TempIndex
  832.                 FirstButton.Enabled = (FromIndex > TempIndex)
  833.  
  834.                 ' Prev Button
  835.                 TempIndex = FromIndex - PageSize
  836.                 If TempIndex < 1 Then TempIndex = 1
  837.                 PrevButton.Start = TempIndex
  838.                 PrevButton.Enabled = HasPrev
  839.  
  840.                 ' Page links
  841.                 If HasPrev Or Not NoNext Then
  842.                     x = 1
  843.                     y = 1
  844.                     dx1 = ((FromIndex - 1) \ (PageSize * Range)) * PageSize * Range + 1
  845.                     dy1 = ((FromIndex - 1) \ (PageSize * Range)) * Range + 1
  846.                     If (dx1 + PageSize * Range - 1) > RecordCount Then
  847.                         dx2 = (RecordCount \ PageSize) * PageSize + 1
  848.                         dy2 = (RecordCount \ PageSize) + 1
  849.                     Else
  850.                         dx2 = dx1 + PageSize * Range - 1
  851.                         dy2 = dy1 + Range - 1
  852.                     End If
  853.                     While x <= RecordCount
  854.                         If x >= dx1 And x <= dx2 Then
  855.                             AddPagerItem(x, y, FromIndex <> x)
  856.                             x = x + PageSize
  857.                             y = y + 1
  858.                         ElseIf x >= (dx1 - PageSize * Range) And x <= (dx2 + PageSize * Range) Then 
  859.                             If x + Range * PageSize < RecordCount Then
  860.                                 AddPagerItem(x, y & "-" & (y + Range - 1), True)
  861.                             Else
  862.                                 ny = (RecordCount - 1) \ PageSize + 1
  863.                                 If ny = y Then
  864.                                     AddPagerItem(x, y, True)
  865.                                 Else
  866.                                     AddPagerItem(x, y & "-" & ny, True)
  867.                                 End If
  868.                             End If
  869.                             x = x + Range * PageSize
  870.                             y = y + Range
  871.                         Else
  872.                             x = x + Range * PageSize
  873.                             y = y + Range
  874.                         End If
  875.                     End While
  876.                 End If
  877.  
  878.                 ' Next Button
  879.                 NextButton.Start = FromIndex + PageSize
  880.                 TempIndex = FromIndex + PageSize
  881.                 NextButton.Start = TempIndex
  882.                 NextButton.Enabled = Not NoNext
  883.  
  884.                 ' Last Button
  885.                 TempIndex = ((RecordCount - 1) \ PageSize) * PageSize + 1
  886.                 LastButton.Start = TempIndex
  887.                 LastButton.Enabled = (FromIndex < TempIndex)
  888.             End If
  889.         End Sub
  890.     End Class    
  891.  
  892.     '
  893.     ' Class for PrevNext pager
  894.     '    
  895.     Class cPrevNextPager
  896.  
  897.         Public NextButton As cPagerItem, FirstButton As cPagerItem, PrevButton As cPagerItem, LastButton As cPagerItem
  898.  
  899.         Public ToIndex As Integer, PageCount As Integer, CurrentPage As Integer, PageSize As Integer, FromIndex As Integer, RecordCount As Integer
  900.  
  901.         Public Visible As Boolean
  902.  
  903.         ' Constructor
  904.         Public Sub New(AFromIndex As Integer, APageSize As Integer, ARecordCount As Integer)
  905.             FromIndex = AFromIndex
  906.             PageSize = APageSize
  907.             RecordCount = ARecordCount
  908.             FirstButton = New cPagerItem
  909.             PrevButton = New cPagerItem
  910.             NextButton = New cPagerItem
  911.             LastButton = New cPagerItem
  912.             Visible = True
  913.             Init()
  914.         End Sub
  915.  
  916.         ' Method to init pager
  917.         Public Sub Init()
  918.             Dim TempIndex As Integer
  919.             If PageSize > 0 Then
  920.                 CurrentPage = (FromIndex - 1) \ PageSize + 1
  921.                 PageCount = (RecordCount - 1) \ PageSize + 1
  922.                 If FromIndex > RecordCount Then FromIndex = RecordCount
  923.                 ToIndex = FromIndex + PageSize - 1
  924.                 If ToIndex > RecordCount Then ToIndex = RecordCount
  925.  
  926.                 ' First Button
  927.                 TempIndex = 1
  928.                 FirstButton.Start = TempIndex
  929.                 FirstButton.Enabled = (TempIndex <> FromIndex)
  930.  
  931.                 ' Prev Button
  932.                 TempIndex = FromIndex - PageSize
  933.                 If TempIndex < 1 Then TempIndex = 1
  934.                 PrevButton.Start = TempIndex
  935.                 PrevButton.Enabled = (TempIndex <> FromIndex)
  936.  
  937.                 ' Next Button
  938.                 TempIndex = FromIndex + PageSize
  939.                 If TempIndex > RecordCount Then TempIndex = FromIndex
  940.                 NextButton.Start = TempIndex
  941.                 NextButton.Enabled = (TempIndex <> FromIndex)
  942.  
  943.                 ' Last Button
  944.                 TempIndex = ((RecordCount - 1) \ PageSize) * PageSize + 1
  945.                 LastButton.Start = TempIndex
  946.                 LastButton.Enabled = (TempIndex <> FromIndex)
  947.             End If
  948.         End Sub
  949.     End Class    
  950.  
  951.     '
  952.     '  Field class
  953.     '
  954.     Class cField
  955.         Implements IDisposable
  956.  
  957.         Public TblVar As String ' Table var
  958.  
  959.         Public FldName As String ' Field name
  960.  
  961.         Public FldVar As String ' Field variable name
  962.  
  963.         Public FldExpression As String ' Field expression (used in SQL)
  964.  
  965.         Public FldType As Integer ' Field type (ADO data type)
  966.  
  967.         Public FldDbType As OleDbType ' Field type (.NET data type)
  968.  
  969.         Public FldDataType As Integer ' Field type (ASP.NET Maker data type)
  970.  
  971.         Public Visible As Boolean ' Visible
  972.  
  973.         Public FldDateTimeFormat As Integer ' Date time format
  974.  
  975.         Public CssStyle As String ' CSS style
  976.  
  977.         Public CssClass As String ' CSS class
  978.  
  979.         Public ImageAlt As String ' Image alt
  980.  
  981.         Public ImageWidth As Integer ' Image width
  982.  
  983.         Public ImageHeight As Integer ' Image height
  984.  
  985.         Public ViewCustomAttributes As String
  986.  
  987.         Public EditCustomAttributes As String
  988.  
  989.         Public CustomMsg As String ' Custom message
  990.  
  991.         Public RowAttributes As String ' Row attributes
  992.  
  993.         Public CellCssClass As String ' Cell CSS class
  994.  
  995.         Public CellCssStyle As String ' Cell CSS style
  996.  
  997.         Public CellCustomAttributes As String
  998.  
  999.         Public MultiUpdate As Object ' Multi update
  1000.  
  1001.         Public OldValue As Object ' Old Value
  1002.  
  1003.         Public ConfirmValue As Object ' Confirm Value
  1004.  
  1005.         Public CurrentValue As Object ' Current value
  1006.  
  1007.         Public ViewValue As Object ' View value
  1008.  
  1009.         Public EditValue As Object ' Edit value
  1010.  
  1011.         Public EditValue2 As Object ' Edit value 2 (search)
  1012.  
  1013.         Public HrefValue As Object ' Href value
  1014.  
  1015.         Public HrefValue2 As Object
  1016.  
  1017.         Private m_FormValue As Object ' Form value
  1018.  
  1019.         Private m_QueryStringValue As Object ' QueryString value
  1020.  
  1021.         Private m_DbValue As Object ' Database Value
  1022.  
  1023.         ' Create new field object
  1024.         Public Sub New(atblvar As String, afldvar As String, afldname As String, afldexpression As String, afldtype As Integer, aflddbtype As OleDbType, aflddatatype As Integer, aflddtformat As Integer)
  1025.             TblVar = atblvar
  1026.             FldVar = afldvar
  1027.             FldName = afldname
  1028.             FldExpression = afldexpression
  1029.             FldType = afldtype
  1030.             FldDbType = aflddbtype
  1031.             FldDataType = aflddatatype
  1032.             FldDateTimeFormat = aflddtformat
  1033.             ImageWidth = 0
  1034.             ImageHeight = 0
  1035.             Visible = True
  1036.         End Sub        
  1037.  
  1038.         ' View Attributes
  1039.         Public ReadOnly Property ViewAttributes() As Object
  1040.             Get
  1041.                 Dim sAtt As String = ""
  1042.                 If ew_NotEmpty(CssStyle) Then
  1043.                     sAtt = sAtt & " style=""" & CssStyle.Trim() & """"
  1044.                 End If
  1045.                 If ew_NotEmpty(CssClass) Then
  1046.                     sAtt = sAtt & " class=""" & CssClass.Trim() & """"
  1047.                 End If
  1048.                 If ew_NotEmpty(ImageAlt) Then
  1049.                     sAtt = sAtt & " alt=""" & ImageAlt.Trim() & """"
  1050.                 End If
  1051.                 If ImageWidth > 0 Then
  1052.                     sAtt = sAtt & " width=""" & ImageWidth & """"
  1053.                 End If
  1054.                 If ImageHeight > 0 Then
  1055.                     sAtt = sAtt & " height=""" & ImageHeight & """"
  1056.                 End If
  1057.                 If ew_NotEmpty(ViewCustomAttributes) Then
  1058.                     sAtt = sAtt & " " & ViewCustomAttributes.Trim()
  1059.                 End If
  1060.                 Return sAtt
  1061.             End Get
  1062.         End Property
  1063.  
  1064.         ' Edit Attributes
  1065.         Public ReadOnly Property EditAttributes() As Object
  1066.             Get
  1067.                 Dim sAtt As String = ""
  1068.                 If ew_NotEmpty(CssStyle) Then
  1069.                     sAtt = sAtt & " style=""" & CssStyle.Trim() & """"
  1070.                 End If
  1071.                 If ew_NotEmpty(CssClass) Then
  1072.                     sAtt = sAtt & " class=""" & CssClass.Trim() & """"
  1073.                 End If
  1074.                 If ew_NotEmpty(EditCustomAttributes) Then
  1075.                     sAtt = sAtt & " " & EditCustomAttributes.Trim()
  1076.                 End If
  1077.                 Return sAtt
  1078.             End Get
  1079.         End Property
  1080.  
  1081.         ' Cell Attributes
  1082.         Public ReadOnly Property CellAttributes() As Object
  1083.             Get
  1084.                 Dim sAtt As String = ""
  1085.                 If ew_NotEmpty(CellCssStyle) Then
  1086.                     sAtt = sAtt & " style=""" & CellCssStyle.Trim() & """"
  1087.                 End If
  1088.                 If ew_NotEmpty(CellCssClass) Then
  1089.                     sAtt = sAtt & " class=""" & CellCssClass.Trim() & """"
  1090.                 End If
  1091.                 If ew_NotEmpty(CellCustomAttributes) Then
  1092.                     sAtt = sAtt & " " & CellCustomAttributes.Trim() ' Cell custom attributes
  1093.                 End If
  1094.                 Return sAtt
  1095.             End Get
  1096.         End Property
  1097.  
  1098.         ' Sort Attributes        
  1099.         Public Property Sort() As Object
  1100.             Get
  1101.                 Return ew_Session(EW_PROJECT_NAME & "_" & TblVar & "_" & EW_TABLE_SORT & "_" & FldVar)
  1102.             End Get
  1103.             Set(ByVal Value As Object)
  1104.                 If ew_Session(EW_PROJECT_NAME & "_" & TblVar & "_" & EW_TABLE_SORT & "_" & FldVar) <> Value Then
  1105.                     ew_Session(EW_PROJECT_NAME & "_" & TblVar & "_" & EW_TABLE_SORT & "_" & FldVar) = Value
  1106.                 End If
  1107.             End Set
  1108.         End Property
  1109.  
  1110.         ' List View value
  1111.         Public ReadOnly Property ListViewValue() As String
  1112.             Get
  1113.                 If ew_Empty(ViewValue) Then
  1114.                     Return " "
  1115.                 Else
  1116.                     Dim Result As String = Convert.ToString(ViewValue)
  1117.                     Dim Result2 As String = Regex.Replace(Result, "<[^>]*>", String.Empty) ' Remove HTML tags
  1118.                     Return IIf(Result2.Trim().Equals(String.Empty), " ", Result)    
  1119.                 End If
  1120.             End Get
  1121.         End Property
  1122.  
  1123.         ' Export Value
  1124.         Public Function ExportValue(ByVal Export As Object, ByVal Original As Object) As Object
  1125.             Dim ExpVal As Object
  1126.             ExpVal = IIf(Original, CurrentValue, ViewValue)                
  1127.             If Export = "xml" AndAlso IsDbNull(ExpVal) Then ExpVal = "<Null>"
  1128.             Return ExpVal
  1129.         End Function        
  1130.  
  1131.         Public Property FormValue() As Object
  1132.             Get
  1133.                 Return m_FormValue
  1134.             End Get
  1135.             Set(ByVal Value As Object)
  1136.                 m_FormValue = Value
  1137.                 CurrentValue = m_FormValue
  1138.             End Set
  1139.         End Property
  1140.  
  1141.         Public Property QueryStringValue() As Object
  1142.             Get
  1143.                 Return m_QueryStringValue
  1144.             End Get
  1145.             Set(ByVal Value As Object)
  1146.                 m_QueryStringValue = Value
  1147.                 CurrentValue = m_QueryStringValue
  1148.             End Set
  1149.         End Property
  1150.  
  1151.         Public Property DbValue() As Object
  1152.             Get
  1153.                 Return m_DbValue
  1154.             End Get
  1155.             Set(ByVal Value As Object)
  1156.                 m_DbValue = Value
  1157.                 CurrentValue = m_DbValue
  1158.             End Set
  1159.         End Property
  1160.  
  1161.         ' Session Value        
  1162.         Public Property SessionValue() As Object
  1163.             Get
  1164.                 Return ew_Session(EW_PROJECT_NAME & "_" & TblVar & "_" & FldVar & "_SessionValue")
  1165.             End Get
  1166.             Set(ByVal Value As Object)
  1167.                 ew_Session(EW_PROJECT_NAME & "_" & TblVar & "_" & FldVar & "_SessionValue") = Value
  1168.             End Set
  1169.         End Property
  1170.  
  1171.         Public ReadOnly Property AdvancedSearch() As cAdvancedSearch
  1172.             Get
  1173.                 If m_AdvancedSearch Is Nothing Then m_AdvancedSearch = New cAdvancedSearch
  1174.                 Return m_AdvancedSearch
  1175.             End Get
  1176.         End Property
  1177.  
  1178.         Public ReadOnly Property Upload() As cUpload
  1179.             Get
  1180.                 If m_Upload Is Nothing Then
  1181.                     m_Upload = New cUpload(TblVar, FldVar)
  1182.                 End If
  1183.                 Return m_Upload
  1184.             End Get
  1185.         End Property        
  1186.  
  1187.         Public Function ReverseSort() As String
  1188.             Return IIf(Sort = "ASC", "DESC", "ASC")            
  1189.         End Function        
  1190.  
  1191.         ' Set up database value
  1192.         Public Sub SetDbValue(value As Object, def As Object)
  1193.             Select Case FldType
  1194.                 Case 2, 3, 16, 17, 18, 19, 20, 21 ' Int
  1195.                     If IsNumeric(value) Then
  1196.                         m_DbValue = ew_Conv(value, FldType)
  1197.                     Else
  1198.                         m_DbValue = def
  1199.                     End If
  1200.                 Case 5, 6, 14, 131 ' Double
  1201.                     If IsNumeric(value) Then
  1202.                         m_DbValue = ew_Conv(value, FldType)
  1203.                     Else
  1204.                         m_DbValue = def
  1205.                     End If
  1206.                 Case 4 ' Single
  1207.                     If IsNumeric(value) Then
  1208.                         m_DbValue = ew_Conv(value, FldType)
  1209.                     Else
  1210.                         m_DbValue = def
  1211.                     End If
  1212.                 Case 7, 133, 134, 135 ' Date
  1213.                     If IsDate(value) Then
  1214.                         m_DbValue = Convert.ToDateTime(value)
  1215.                     Else
  1216.                         m_DbValue = def
  1217.                     End If
  1218.                 Case 201, 203, 129, 130, 200, 202 ' String
  1219.                     If EW_REMOVE_XSS Then
  1220.                         m_DbValue = ew_RemoveXSS(Convert.ToString(value))
  1221.                     Else
  1222.                         m_DbValue = Convert.ToString(value)
  1223.                     End If
  1224.                     If Convert.ToString(m_DbValue) = "" Then m_DbValue = def
  1225.                 Case 128, 204, 205 ' Binary
  1226.                     If IsDbNull(value) Then
  1227.                         m_DbValue = def
  1228.                     Else
  1229.                         m_DbValue = value
  1230.                     End If
  1231.                 Case 72 ' GUID
  1232.                     If ew_NotEmpty(value) AndAlso ew_CheckGUID(value.Trim()) Then
  1233.                         m_DbValue = value
  1234.                     Else
  1235.                         m_DbValue = def
  1236.                     End If
  1237.                 Case Else
  1238.                     m_DbValue = value
  1239.             End Select
  1240.         End Sub
  1241.  
  1242.         Public Count As Integer ' Count
  1243.  
  1244.         Public Total As Object ' Total
  1245.  
  1246.         ' AdvancedSearch Object
  1247.         Private m_AdvancedSearch As Object
  1248.  
  1249.         ' Upload Object
  1250.         Private m_Upload As Object
  1251.  
  1252.         ' Show object as string
  1253.         Public Function AsString() As Object
  1254.             Dim AdvancedSearchAsString As String, UploadAsString As String
  1255.             If m_AdvancedSearch IsNot Nothing Then
  1256.                 AdvancedSearchAsString = m_AdvancedSearch.AsString
  1257.             Else
  1258.                 AdvancedSearchAsString = "{Null}"
  1259.             End If
  1260.             If m_Upload IsNot Nothing Then
  1261.                 UploadAsString = m_Upload.AsString
  1262.             Else
  1263.                 UploadAsString = "{Null}"
  1264.             End If
  1265.             Return "{" & "FldName: " & FldName & ", " & "FldVar: " & FldVar & ", " & "FldExpression: " & FldExpression & ", " & "FldType: " & FldType & ", " & "FldDateTimeFormat: " & FldDateTimeFormat & ", " & "CssStyle: " & CssStyle & ", " & "CssClass: " & CssClass & ", " & "ImageAlt: " & ImageAlt & ", " & "ImageWidth: " & ImageWidth & ", " & "ImageHeight: " & ImageHeight & ", " & "ViewCustomAttributes: " & ViewCustomAttributes & ", " & "EditCustomAttributes: " & EditCustomAttributes & ", " & "CellCssStyle: " & CellCssStyle & ", " & "CellCssClass: " & CellCssClass & ", " & "Sort: " & Sort & ", " & "MultiUpdate: " & MultiUpdate & ", " & "CurrentValue: " & CurrentValue & ", " & "ViewValue: " & ViewValue & ", " & "EditValue: " & Convert.ToString(EditValue) & ", " & "EditValue2: " & Convert.ToString(EditValue2) & ", " & "HrefValue: " & HrefValue & ", " & "HrefValue2: " & HrefValue2 & ", " & "FormValue: " & m_FormValue & ", " & "QueryStringValue: " & m_QueryStringValue & ", " & "DbValue: " & m_DbValue & ", " & "SessionValue: " & SessionValue & ", " & "Count: " & Count & ", " & "Total: " & Total & ", " & "AdvancedSearch: " & AdvancedSearchAsString & ", " & "Upload: " & UploadAsString & "}"
  1266.         End Function
  1267.  
  1268.         ' Class terminate
  1269.         Public Sub Dispose() Implements IDisposable.Dispose
  1270.             If m_AdvancedSearch IsNot Nothing Then
  1271.                 m_AdvancedSearch = Nothing
  1272.             End If
  1273.             If m_Upload IsNot Nothing Then
  1274.                 m_Upload = Nothing
  1275.             End If
  1276.         End Sub
  1277.     End Class
  1278.  
  1279.     '
  1280.     '  List option collection class
  1281.     '    
  1282.     Class cListOptions
  1283.  
  1284.         Public Items As ArrayList
  1285.  
  1286.         Public Sub New()
  1287.             Items = New ArrayList
  1288.         End Sub
  1289.  
  1290.         ' Add and return a new option
  1291.         Public Function Add() As cListOption
  1292.             Add = New cListOption
  1293.             Items.Add(Add)
  1294.         End Function
  1295.     End Class
  1296.  
  1297.     '
  1298.     '  List option class
  1299.     '    
  1300.     Class cListOption
  1301.  
  1302.         Public Visible As Boolean
  1303.  
  1304.         Public HeaderCellHtml As String
  1305.  
  1306.         Public FooterCellHtml As String
  1307.  
  1308.         Public BodyCellHtml As String
  1309.  
  1310.         Public MultiColumnLinkHtml As String
  1311.  
  1312.         ' Constructor
  1313.         Public Sub New()
  1314.             Visible = True
  1315.         End Sub
  1316.  
  1317.         ' Convert to string
  1318.         Public Function AsString() As String
  1319.             Return "{" & "Visible: " & Visible & ", " & "HeaderCellHtml: " & ew_HtmlEncode(HeaderCellHtml) & ", " & "FooterCellHtml: " & ew_HtmlEncode(FooterCellHtml) & ", " & "BodyCellHtml: " & ew_HtmlEncode(BodyCellHtml) & ", " & "MultiColumnLinkHtml: " & ew_HtmlEncode(MultiColumnLinkHtml) & "}"
  1320.         End Function
  1321.     End Class
  1322.  
  1323.     '
  1324.     ' Connection object
  1325.     '
  1326.     Public Class cConnection
  1327.         Implements IDisposable
  1328.  
  1329.         Public ConnectionString As String = EW_DB_CONNECTION_STRING
  1330.  
  1331.         Public Conn As OleDbConnection
  1332.  
  1333.         Public Trans As OleDbTransaction
  1334.  
  1335.         Private TempConn As OleDbConnection
  1336.  
  1337.         Private TempCommand As OleDbCommand
  1338.  
  1339.         Private TempDataReader As OleDbDataReader
  1340.  
  1341.         ' Constructor
  1342.         Public Sub New(ConnStr As String)
  1343.             ConnectionString = ConnStr
  1344.             Conn = New OleDbConnection(ConnectionString)
  1345.             Conn.Open()
  1346.         End Sub
  1347.  
  1348.         ' Constructor
  1349.         Public Sub New()
  1350.             Conn = New OleDbConnection(ConnectionString)
  1351.             Conn.Open()
  1352.         End Sub
  1353.  
  1354.         ' Execute SQL
  1355.         Public Function Execute(Sql As String) As Integer
  1356.             Dim Cmd As OleDbCommand = GetCommand(Sql)                
  1357.             Return Cmd.ExecuteNonQuery()            
  1358.         End Function
  1359.  
  1360.         ' Execute SQL and return first value of first row
  1361.         Public Function ExecuteScalar(Sql As String) As Object
  1362.             Try
  1363.                 Dim Cmd As OleDbCommand = GetCommand(Sql)
  1364.                 Return Cmd.ExecuteScalar()
  1365.             Catch
  1366.                 If EW_DEBUG_ENABLED Then Throw
  1367.                 Return Nothing 
  1368.             End Try                
  1369.         End Function
  1370.  
  1371.         ' Get last insert ID
  1372.         Public Function GetLastInsertId() As Object
  1373.             Dim Id As Object = System.DBNull.Value 
  1374.             Id = ExecuteScalar("SELECT @@Identity")            
  1375.             Return Id
  1376.         End Function
  1377.  
  1378.         ' Get data reader
  1379.         Public Function GetDataReader(Sql As String) As OleDbDataReader
  1380.             Try
  1381.                 Dim Cmd As OleDbCommand = GetCommand(Sql)
  1382.                 Return Cmd.ExecuteReader()
  1383.             Catch
  1384.                 If EW_DEBUG_ENABLED Then Throw
  1385.                 Return Nothing 
  1386.             End Try    
  1387.         End Function
  1388.  
  1389.         ' Get temporary data reader
  1390.         Public Function GetTempDataReader(Sql As String) As OleDbDataReader
  1391.             Try
  1392.                 If TempConn Is Nothing Then
  1393.                     TempConn = New OleDbConnection(ConnectionString)
  1394.                     TempConn.Open()
  1395.                 End If
  1396.                 If TempCommand Is Nothing Then
  1397.                     TempCommand = New OleDbCommand(Sql, TempConn)
  1398.                 End If
  1399.                 CloseTempDataReader()
  1400.                 TempCommand.CommandText = Sql
  1401.                 TempDataReader = TempCommand.ExecuteReader()            
  1402.                 Return TempDataReader
  1403.             Catch
  1404.                 If EW_DEBUG_ENABLED Then Throw
  1405.                 Return Nothing 
  1406.             End Try    
  1407.         End Function
  1408.  
  1409.         ' Close temporary data reader
  1410.         Public Sub CloseTempDataReader()
  1411.             If TempDataReader IsNot Nothing    Then
  1412.                 TempDataReader.Close()
  1413.                 TempDataReader.Dispose()
  1414.             End If            
  1415.         End Sub
  1416.  
  1417.         ' Get OrderedDictionary from data reader
  1418.         Public Function GetRow(ByRef dr As OleDbDataReader) As OrderedDictionary
  1419.             Dim od As New OrderedDictionary
  1420.             For i As Integer = 0 to dr.FieldCount - 1 
  1421.                 od(dr.GetName(i)) = dr(i)
  1422.             Next
  1423.             Return od
  1424.         End Function
  1425.  
  1426.         ' Get rows
  1427.         Public Overloads Function GetRows(ByRef dr As OleDbDataReader) As ArrayList
  1428.             Dim Rows As New ArrayList() 
  1429.             While dr.Read() 
  1430.                 Rows.Add(GetRow(dr)) 
  1431.             End While
  1432.             Return Rows 
  1433.         End Function
  1434.  
  1435.         ' Get rows by SQL
  1436.         Public Overloads Function GetRows(Sql As String) As ArrayList
  1437.             Dim dr As OleDbDataReader = GetTempDataReader(Sql)
  1438.             Try
  1439.                 Return GetRows(dr)
  1440.             Finally
  1441.                 CloseTempDataReader()
  1442.             End Try         
  1443.         End Function    
  1444.  
  1445.         ' Get dataset
  1446.         Public Function GetDataSet(Sql As String) As DataSet
  1447.             Try
  1448.                 Dim Adapter As New OleDbDataAdapter(Sql, Conn)
  1449.                 Dim DS As DataSet = new DataSet()
  1450.                 Adapter.Fill(DS)
  1451.                 Return DS
  1452.             Catch
  1453.                 If EW_DEBUG_ENABLED Then Throw
  1454.                 Return Nothing
  1455.             End Try    
  1456.         End Function
  1457.  
  1458.         ' Get command
  1459.         Public Function GetCommand(Sql As String) As OleDbCommand
  1460.             Dim Cmd As New OleDbCommand(Sql, Conn)
  1461.             If Trans IsNot Nothing Then Cmd.Transaction = Trans
  1462.             Return Cmd
  1463.         End Function
  1464.  
  1465.         ' Begin transaction
  1466.         Public Sub BeginTrans()
  1467.             Try
  1468.                 Trans = Conn.BeginTransaction()
  1469.             Catch
  1470.                 If EW_DEBUG_ENABLED Then Throw 
  1471.             End Try
  1472.         End Sub
  1473.  
  1474.         ' Commit transaction
  1475.         Public Sub CommitTrans()
  1476.             If Trans IsNot Nothing Then Trans.Commit()
  1477.         End Sub
  1478.  
  1479.         ' Rollback transaction
  1480.         Public Sub RollbackTrans()
  1481.             If Trans IsNot Nothing Then Trans.Rollback()
  1482.         End Sub
  1483.  
  1484.         ' Dispose    
  1485.         Public Sub Dispose() Implements IDisposable.Dispose
  1486.             If Trans IsNot Nothing Then Trans.Dispose()
  1487.             Conn.Close()
  1488.             Conn.Dispose()
  1489.             If TempCommand IsNot Nothing Then
  1490.                 TempCommand.Dispose()
  1491.             End If
  1492.             If TempConn IsNot Nothing Then
  1493.                 TempConn.Close()
  1494.                 TempConn.Dispose()
  1495.             End If
  1496.         End Sub
  1497.     End Class    
  1498.  
  1499.     '
  1500.     '  Advanced Search class
  1501.     '    
  1502.     Class cAdvancedSearch
  1503.  
  1504.         Public SearchValue As Object ' Search value
  1505.  
  1506.         Public SearchOperator As String = "=" ' Search operator
  1507.  
  1508.         Public SearchCondition As String = "AND" ' Search condition
  1509.  
  1510.         Public SearchValue2 As Object ' Search value 2
  1511.  
  1512.         Public SearchOperator2 As String = "" ' Search operator 2
  1513.  
  1514.         ' Show object as string
  1515.         Public Function AsString() As Object
  1516.             AsString = "{" & "SearchValue: " & SearchValue & ", " & "SearchOperator: " & SearchOperator & ", " & "SearchCondition: " & SearchCondition & ", " & "SearchValue2: " & SearchValue2 & ", " & "SearchOperator2: " & SearchOperator2 & "}"
  1517.         End Function
  1518.     End Class
  1519.  
  1520.     '
  1521.     '  Upload class
  1522.     '
  1523.     Class cUpload
  1524.  
  1525.         Public Index As Integer = 0 ' Index to handle multiple form elements
  1526.  
  1527.         Public TblVar As String ' Table variable
  1528.  
  1529.         Public FldVar As String ' Field variable
  1530.  
  1531.         Public DbValue As Object ' Value from database
  1532.  
  1533.         Public UploadPath As String = "" ' Upload path        
  1534.  
  1535.         Private m_Message As String = "" ' Error message        
  1536.  
  1537.         Private m_Value As Object ' Upload value        
  1538.  
  1539.         Private m_Action As String = "" ' Upload action        
  1540.  
  1541.         Private m_FileName As String = "" ' Upload file name        
  1542.  
  1543.         Private m_FileSize As Long = -1 ' Upload file size        
  1544.  
  1545.         Private m_ContentType As String = "" ' File content type        
  1546.  
  1547.         Private m_ImageWidth As Integer = -1 ' Image width        
  1548.  
  1549.         Private m_ImageHeight As Integer = -1 ' Image height
  1550.  
  1551.         ' Reference to page form object
  1552.         Private m_FormObj As cFormObj        
  1553.  
  1554.         ' Contructor
  1555.         Public Sub New(ATblVar As String, AFldVar As String)
  1556.             TblVar = ATblVar
  1557.             FldVar = AFldVar
  1558.         End Sub
  1559.  
  1560.         ' Form object
  1561.         Public ReadOnly Property ObjForm() As cFormObj
  1562.             Get                
  1563.                 Return m_FormObj
  1564.             End Get            
  1565.         End Property
  1566.  
  1567.         Public ReadOnly Property Message() As Object
  1568.             Get
  1569.                 Return m_Message
  1570.             End Get
  1571.         End Property        
  1572.  
  1573.         Public Property Value() As Object
  1574.             Get
  1575.                 Return m_Value
  1576.             End Get
  1577.             Set(ByVal Value As Object)
  1578.                 m_Value = Value
  1579.             End Set
  1580.         End Property
  1581.  
  1582.         Public ReadOnly Property Action() As String
  1583.             Get
  1584.                 Return m_Action
  1585.             End Get
  1586.         End Property
  1587.  
  1588.         Public ReadOnly Property FileName() As String
  1589.             Get
  1590.                 Return m_FileName
  1591.             End Get
  1592.         End Property
  1593.  
  1594.         Public ReadOnly Property FileSize() As Long
  1595.             Get
  1596.                 Return m_FileSize
  1597.             End Get
  1598.         End Property
  1599.  
  1600.         Public ReadOnly Property ContentType() As String
  1601.             Get
  1602.                 Return m_ContentType
  1603.             End Get
  1604.         End Property
  1605.  
  1606.         Public ReadOnly Property ImageWidth() As Integer
  1607.             Get
  1608.                 Return m_ImageWidth
  1609.             End Get
  1610.         End Property
  1611.  
  1612.         Public ReadOnly Property ImageHeight() As Integer
  1613.             Get
  1614.                 Return m_ImageHeight
  1615.             End Get
  1616.         End Property
  1617.  
  1618.         ' Set form object
  1619.         Public Sub SetForm(ByRef Obj As cFormObj)            
  1620.             m_FormObj = Obj
  1621.             Index = Obj.Index    
  1622.         End Sub        
  1623.  
  1624.         ' Save Db value to Session
  1625.         Public Sub SaveDbToSession()            
  1626.             Dim sSessionID As String = EW_PROJECT_NAME & "_" & TblVar & "_" & FldVar & "_" & Index
  1627.             ew_Session(sSessionID & "_DbValue") = DbValue
  1628.         End Sub
  1629.  
  1630.         ' Restore Db value from Session
  1631.         Public Sub RestoreDbFromSession()            
  1632.             Dim sSessionID As String = EW_PROJECT_NAME & "_" & TblVar & "_" & FldVar & "_" & Index
  1633.             DbValue = ew_Session(sSessionID & "_DbValue")
  1634.         End Sub
  1635.  
  1636.         ' Remove Db value from Session
  1637.         Public Sub RemoveDbFromSession()            
  1638.             Dim sSessionID As String = EW_PROJECT_NAME & "_" & TblVar & "_" & FldVar & "_" & Index
  1639.             HttpContext.Current.Session.Contents.Remove((sSessionID & "_DbValue"))
  1640.         End Sub
  1641.  
  1642.         ' Save Upload values to Session
  1643.         Public Sub SaveToSession()            
  1644.             Dim sSessionID As String = EW_PROJECT_NAME & "_" & TblVar & "_" & FldVar & "_" & Index
  1645.             ew_Session(sSessionID & "_Action") = m_Action
  1646.             ew_Session(sSessionID & "_FileSize") = m_FileSize
  1647.             ew_Session(sSessionID & "_FileName") = m_FileName
  1648.             ew_Session(sSessionID & "_ContentType") = m_ContentType
  1649.             ew_Session(sSessionID & "_ImageWidth") = m_ImageWidth
  1650.             ew_Session(sSessionID & "_ImageHeight") = m_ImageHeight
  1651.             ew_Session(sSessionID & "_Value") = m_Value
  1652.         End Sub
  1653.  
  1654.         ' Restore Upload values from Session
  1655.         Public Sub RestoreFromSession()            
  1656.             Dim sSessionID As String = EW_PROJECT_NAME & "_" & TblVar & "_" & FldVar & "_" & Index
  1657.             m_Action = ew_Session(sSessionID & "_Action")
  1658.             m_FileSize = ew_Session(sSessionID & "_FileSize")
  1659.             m_FileName = ew_Session(sSessionID & "_FileName")
  1660.             m_ContentType = ew_Session(sSessionID & "_ContentType")
  1661.             m_ImageWidth = ew_Session(sSessionID & "_ImageWidth")
  1662.             m_ImageHeight = ew_Session(sSessionID & "_ImageHeight")
  1663.             m_Value = ew_Session(sSessionID & "_Value")
  1664.         End Sub
  1665.  
  1666.         ' Remove Upload values from Session
  1667.         Public Sub RemoveFromSession()
  1668.             Dim sSessionID As String = EW_PROJECT_NAME & "_" & TblVar & "_" & FldVar & "_" & Index
  1669.             HttpContext.Current.Session.Contents.Remove((sSessionID & "_Action"))
  1670.             HttpContext.Current.Session.Contents.Remove((sSessionID & "_FileSize"))
  1671.             HttpContext.Current.Session.Contents.Remove((sSessionID & "_FileName"))
  1672.             HttpContext.Current.Session.Contents.Remove((sSessionID & "_ContentType"))
  1673.             HttpContext.Current.Session.Contents.Remove((sSessionID & "_ImageWidth"))
  1674.             HttpContext.Current.Session.Contents.Remove((sSessionID & "_ImageHeight"))
  1675.             HttpContext.Current.Session.Contents.Remove((sSessionID & "_Value"))
  1676.         End Sub
  1677.  
  1678.         ' Check the file type of the uploaded file
  1679.         Private Function UploadAllowedFileExt(FileName As String) As Boolean
  1680.             Return ew_CheckFileType(FileName)
  1681.         End Function
  1682.  
  1683.         ' Get upload file
  1684.         Public Function UploadFile() As Boolean
  1685.             Try
  1686.                 Dim gsFldVar As String = FldVar
  1687.                 Dim gsFldVarAction As String = "a" & gsFldVar.Substring(1)
  1688.                 Dim gsFldVarWidth As String = "wd" & gsFldVar.Substring(1)
  1689.                 Dim gsFldVarHeight As String = "ht" & gsFldVar.Substring(1)
  1690.  
  1691.                 ' Initialize upload value
  1692.                 m_Value = System.DBNull.Value
  1693.  
  1694.                 ' Get action
  1695.                 m_Action = ObjForm.GetValue(gsFldVarAction)
  1696.  
  1697.                 ' Get and check the upload file size
  1698.                 m_FileSize = ObjForm.GetUploadFileSize(gsFldVar)
  1699.  
  1700.                 ' Get and check the upload file type
  1701.                 m_FileName = ObjForm.GetUploadFileName(gsFldVar)
  1702.  
  1703.                 ' Get upload file content type
  1704.                 m_ContentType = ObjForm.GetUploadFileContentType(gsFldVar)
  1705.  
  1706.                 ' Get upload value
  1707.                 m_Value = ObjForm.GetUploadFileData(gsFldVar)
  1708.  
  1709.                 ' Get image width and height
  1710.                 m_ImageWidth = ObjForm.GetUploadImageWidth(gsFldVar)
  1711.                 m_ImageHeight = ObjForm.GetUploadImageHeight(gsFldVar)
  1712.                 If m_ImageWidth < 0 OrElse m_ImageHeight < 0 Then
  1713.                     If IsNumeric(ObjForm.GetValue(gsFldVarWidth)) Then m_ImageWidth = ObjForm.GetValue(gsFldVarWidth)
  1714.                     If IsNumeric(ObjForm.GetValue(gsFldVarHeight)) Then m_ImageHeight = ObjForm.GetValue(gsFldVarHeight)
  1715.                 End If
  1716.                 Return True
  1717.             Catch
  1718.                 Return False
  1719.             End Try
  1720.         End Function
  1721.  
  1722.         ' Resize image
  1723.         Public Function Resize(Width As Integer, Height As Integer, Interpolation As Integer) As Boolean
  1724.             Dim wrkWidth As Integer, wrkHeight As Integer
  1725.             If Not IsDbNull(m_Value) Then
  1726.                 wrkWidth = Width
  1727.                 wrkHeight = Height
  1728.                 Resize = ew_ResizeBinary(m_Value, wrkWidth, wrkHeight, Interpolation)
  1729.                 If Resize Then
  1730.                     m_ImageWidth = wrkWidth
  1731.                     m_ImageHeight = wrkHeight
  1732.                     m_FileSize = m_Value.Length
  1733.                 End If
  1734.             End If
  1735.         End Function
  1736.  
  1737.         ' Save uploaded data to file (Path relative to application root)
  1738.         Public Function SaveToFile(Path As String, NewFileName As String, Overwrite As Boolean) As Boolean
  1739.             If Not IsDbNull(m_Value) Then
  1740.                 Path = ew_UploadPathEx(True, Path)
  1741.                 If ew_Empty(NewFileName) Then NewFileName = m_FileName
  1742.                 If Overwrite Then
  1743.                     Return ew_SaveFile(Path, NewFileName, m_Value)
  1744.                 Else
  1745.                     Return ew_SaveFile(Path, ew_UploadFileNameEx(Path, NewFileName), m_Value)
  1746.                 End If
  1747.             End If
  1748.             Return False
  1749.         End Function
  1750.  
  1751.         ' Resize and save uploaded data to file (Path relative to application root)
  1752.         Public Function ResizeAndSaveToFile(Width As Integer, Height As Integer, Interpolation As Integer, Path As String, NewFileName As String, Overwrite As Boolean) As Boolean                        
  1753.             If Not IsDbNull(m_Value) Then
  1754.                 Dim OldValue As Object = m_Value ' Save old values
  1755.                 Dim OldWidth As Integer = m_ImageWidth
  1756.                 Dim OldHeight As Integer = m_ImageHeight
  1757.                 Dim OldFileSize As Long = m_FileSize
  1758.                 Try
  1759.                     Resize(Width, Height, Interpolation)
  1760.                     Return SaveToFile(Path, NewFileName, OverWrite)
  1761.                 Finally
  1762.                     m_Value = OldValue  ' Restore old values
  1763.                     m_ImageWidth = OldWidth
  1764.                     m_ImageHeight = OldHeight
  1765.                     m_FileSize = OldFileSize
  1766.                 End Try
  1767.             End If
  1768.             Return False
  1769.         End Function        
  1770.  
  1771.         ' Show object as string
  1772.         Public Function AsString() As String
  1773.             AsString = "{" & "Index: " & Index & ", " & "Message: " & m_Message & ", " & "Action: " & m_Action & ", " & "UploadPath: " & UploadPath & ", " & "FileName: " & m_FileName & ", " & "FileSize: " & m_FileSize & ", " & "ContentType: " & m_ContentType & ", " & "ImageWidth: " & m_ImageWidth & ", " & "ImageHeight: " & m_ImageHeight & "}"
  1774.         End Function
  1775.     End Class
  1776.  
  1777.     '
  1778.     ' Advanced Security class
  1779.     '
  1780.     Class cAdvancedSecurity
  1781.         Inherits AspNetMakerBase
  1782.  
  1783. '        Private m_Page As AspNetMaker7_tfpssnet
  1784.         Private m_ArUserLevel As ArrayList
  1785.  
  1786.         Private m_ArUserLevelPriv As ArrayList
  1787.  
  1788.         Private m_ArUserLevelID() As Integer
  1789.  
  1790.         ' Current User Level ID / User Level
  1791.         Public CurrentUserLevelID As Integer
  1792.  
  1793.         Public CurrentUserLevel As Integer
  1794.  
  1795.         ' Current User ID / Parent User ID / User ID array
  1796.         Public CurrentUserID As Object
  1797.  
  1798.         Public CurrentParentUserID As Object
  1799.  
  1800.         Private m_ArUserID() As Object
  1801.  
  1802.         ' Init
  1803.         Public Sub New(ByRef APage As AspNetMakerPage)
  1804.             m_Page = APage
  1805.             m_ParentPage = APage.ParentPage
  1806.             m_ArUserLevel = New ArrayList()
  1807.             m_ArUserLevelPriv = New ArrayList()
  1808.  
  1809.             ' Init User Level
  1810.             CurrentUserLevelID = SessionUserLevelID
  1811.             If IsNumeric(CurrentUserLevelID) Then
  1812.                 If CurrentUserLevelID >= -1 Then                    
  1813.                     Array.Resize(m_ArUserLevelID, 1)
  1814.                     m_ArUserLevelID(0) = CurrentUserLevelID
  1815.                 End If
  1816.             End If
  1817.  
  1818.             ' Init User ID
  1819.             CurrentUserID = SessionUserID
  1820.             CurrentParentUserID = SessionParentUserID
  1821.  
  1822.             ' Load user level (for TablePermission_Loading event)
  1823.             LoadUserLevel()
  1824.         End Sub
  1825.  
  1826.         ' User table
  1827.         Public ReadOnly Property tblEmployees() As ctblEmployees
  1828.             Get
  1829.                 Return ParentPage.tblEmployees
  1830.             End Get
  1831.         End Property
  1832.  
  1833.         ' Session User ID        
  1834.         Public Property SessionUserID() As Object
  1835.             Get
  1836.                 Return Convert.ToString(ew_Session(EW_SESSION_USER_ID))
  1837.             End Get
  1838.             Set(ByVal Value As Object)
  1839.                 ew_Session(EW_SESSION_USER_ID) = Value
  1840.                 CurrentUserID = Value
  1841.             End Set
  1842.         End Property
  1843.  
  1844.         ' Session parent User ID        
  1845.         Public Property SessionParentUserID() As Object
  1846.             Get
  1847.                 Return Convert.ToString(ew_Session(EW_SESSION_PARENT_USER_ID))
  1848.             End Get
  1849.             Set(ByVal Value As Object)
  1850.                 ew_Session(EW_SESSION_PARENT_USER_ID) = Value
  1851.                 CurrentParentUserID = Value
  1852.             End Set
  1853.         End Property
  1854.  
  1855.         ' Current user name        
  1856.         Public Property CurrentUserName() As Object
  1857.             Get
  1858.                 Return Convert.ToString(ew_Session(EW_SESSION_USER_NAME))
  1859.             End Get
  1860.             Set(ByVal Value As Object)
  1861.                 ew_Session(EW_SESSION_USER_NAME) = Value
  1862.             End Set
  1863.         End Property
  1864.  
  1865.         ' Session User Level ID        
  1866.         Public Property SessionUserLevelID() As Object
  1867.             Get
  1868.                 Return ew_Session(EW_SESSION_USER_LEVEL_ID)
  1869.             End Get
  1870.             Set(ByVal Value As Object)
  1871.                 ew_Session(EW_SESSION_USER_LEVEL_ID) = Value
  1872.                 CurrentUserLevelID = Value
  1873.             End Set
  1874.         End Property
  1875.  
  1876.         ' Session User Level value    
  1877.         Public Property SessionUserLevel() As Object
  1878.             Get
  1879.                 Return ew_Session(EW_SESSION_USER_LEVEL)
  1880.             End Get
  1881.             Set(ByVal Value As Object)
  1882.                 ew_Session(EW_SESSION_USER_LEVEL) = Value
  1883.                 CurrentUserLevel = Value
  1884.             End Set
  1885.         End Property
  1886.  
  1887.         ' Can add        
  1888.         Public Property CanAdd() As Boolean
  1889.             Get
  1890.                 Return ((CurrentUserLevel And EW_ALLOW_ADD) = EW_ALLOW_ADD)
  1891.             End Get
  1892.             Set(ByVal Value As Boolean)
  1893.                 If (Value) Then
  1894.                     CurrentUserLevel = (CurrentUserLevel Or EW_ALLOW_ADD)
  1895.                 Else
  1896.                     CurrentUserLevel = (CurrentUserLevel And (Not EW_ALLOW_ADD))
  1897.                 End If
  1898.             End Set
  1899.         End Property
  1900.  
  1901.         ' Can delete        
  1902.         Public Property CanDelete() As Boolean
  1903.             Get
  1904.                 Return ((CurrentUserLevel And EW_ALLOW_DELETE) = EW_ALLOW_DELETE)
  1905.             End Get
  1906.             Set(ByVal Value As Boolean)
  1907.                 If (Value) Then
  1908.                     CurrentUserLevel = (CurrentUserLevel Or EW_ALLOW_DELETE)
  1909.                 Else
  1910.                     CurrentUserLevel = (CurrentUserLevel And (Not EW_ALLOW_DELETE))
  1911.                 End If
  1912.             End Set
  1913.         End Property
  1914.  
  1915.         ' Can edit        
  1916.         Public Property CanEdit() As Boolean
  1917.             Get
  1918.                 Return ((CurrentUserLevel And EW_ALLOW_EDIT) = EW_ALLOW_EDIT)
  1919.             End Get
  1920.             Set(ByVal Value As Boolean)
  1921.                 If (Value) Then
  1922.                     CurrentUserLevel = (CurrentUserLevel Or EW_ALLOW_EDIT)
  1923.                 Else
  1924.                     CurrentUserLevel = (CurrentUserLevel And (Not EW_ALLOW_EDIT))
  1925.                 End If
  1926.             End Set
  1927.         End Property
  1928.  
  1929.         ' Can view        
  1930.         Public Property CanView() As Boolean
  1931.             Get
  1932.                 Return ((CurrentUserLevel And EW_ALLOW_VIEW) = EW_ALLOW_VIEW)
  1933.             End Get
  1934.             Set(ByVal Value As Boolean)
  1935.                 If (Value) Then
  1936.                     CurrentUserLevel = (CurrentUserLevel Or EW_ALLOW_VIEW)
  1937.                 Else
  1938.                     CurrentUserLevel = (CurrentUserLevel And (Not EW_ALLOW_VIEW))
  1939.                 End If
  1940.             End Set
  1941.         End Property
  1942.  
  1943.         ' Can list        
  1944.         Public Property CanList() As Boolean
  1945.             Get
  1946.                 Return ((CurrentUserLevel And EW_ALLOW_LIST) = EW_ALLOW_LIST)
  1947.             End Get
  1948.             Set(ByVal Value As Boolean)
  1949.                 If (Value) Then
  1950.                     CurrentUserLevel = (CurrentUserLevel Or EW_ALLOW_LIST)
  1951.                 Else
  1952.                     CurrentUserLevel = (CurrentUserLevel And (Not EW_ALLOW_LIST))
  1953.                 End If
  1954.             End Set
  1955.         End Property
  1956.  
  1957.         ' Can report        
  1958.         Public Property CanReport() As Boolean
  1959.             Get
  1960.                 Return ((CurrentUserLevel And EW_ALLOW_REPORT) = EW_ALLOW_REPORT)
  1961.             End Get
  1962.             Set(ByVal Value As Boolean)
  1963.                 If (Value) Then
  1964.                     CurrentUserLevel = (CurrentUserLevel Or EW_ALLOW_REPORT)
  1965.                 Else
  1966.                     CurrentUserLevel = (CurrentUserLevel And (Not EW_ALLOW_REPORT))
  1967.                 End If
  1968.             End Set
  1969.         End Property
  1970.  
  1971.         ' Can search        
  1972.         Public Property CanSearch() As Boolean
  1973.             Get
  1974.                 Return ((CurrentUserLevel And EW_ALLOW_SEARCH) = EW_ALLOW_SEARCH)
  1975.             End Get
  1976.             Set(ByVal Value As Boolean)
  1977.                 If (Value) Then
  1978.                     CurrentUserLevel = (CurrentUserLevel Or EW_ALLOW_SEARCH)
  1979.                 Else
  1980.                     CurrentUserLevel = (CurrentUserLevel And (Not EW_ALLOW_SEARCH))
  1981.                 End If
  1982.             End Set
  1983.         End Property
  1984.  
  1985.         ' Can admin        
  1986.         Public Property CanAdmin() As Boolean
  1987.             Get
  1988.                 Return ((CurrentUserLevel And EW_ALLOW_ADMIN) = EW_ALLOW_ADMIN)
  1989.             End Get
  1990.             Set(ByVal Value As Boolean)
  1991.                 If (Value) Then
  1992.                     CurrentUserLevel = (CurrentUserLevel Or EW_ALLOW_ADMIN)
  1993.                 Else
  1994.                     CurrentUserLevel = (CurrentUserLevel And (Not EW_ALLOW_ADMIN))
  1995.                 End If
  1996.             End Set
  1997.         End Property
  1998.  
  1999.         ' Last URL
  2000.         Public ReadOnly Property LastUrl() As String
  2001.             Get
  2002.                 Return ew_Cookie("lasturl")
  2003.             End Get
  2004.         End Property
  2005.  
  2006.         ' Save last URL
  2007.         Public Sub SaveLastUrl()
  2008.             Dim s As String = HttpContext.Current.Request.ServerVariables("SCRIPT_NAME")
  2009.             Dim q As String = HttpContext.Current.Request.ServerVariables("QUERY_STRING")            
  2010.             If q <> "" Then s = s & "?" & q
  2011.             If LastUrl = s Then s = ""
  2012.             ew_Cookie("lasturl") = s
  2013.         End Sub
  2014.  
  2015.         ' Auto login
  2016.         Public Function AutoLogin() As Boolean
  2017.             If ew_SameStr(ew_Cookie("autologin"), "autologin") Then
  2018.                 Dim sUsr As String = ew_Cookie("username")
  2019.                 Dim sPwd As String = ew_Cookie("password")
  2020.                 sPwd = cTEA.Decrypt(sPwd, EW_RANDOM_KEY)
  2021.                 Dim bValid As Boolean = ValidateUser(sUsr, sPwd)    
  2022.                 Return bValid
  2023.             Else
  2024.                 Return False
  2025.             End If
  2026.         End Function        
  2027.  
  2028.         ' Validate user
  2029.         Public Function ValidateUser(usr As String, pwd As String) As Boolean
  2030.             Dim sFilter As String, sSql As String, RsUser As OleDbDataReader
  2031.             ValidateUser = False
  2032.             If EW_CASE_SENSITIVE_PASSWORD Then
  2033.                 ValidateUser = (EW_ADMIN_USER_NAME = usr And EW_ADMIN_PASSWORD = pwd)
  2034.             Else
  2035.                 ValidateUser = (ew_SameText(EW_ADMIN_USER_NAME, usr) And ew_SameText(EW_ADMIN_PASSWORD, pwd))
  2036.             End If
  2037.             If ValidateUser Then
  2038.                 ew_Session(EW_SESSION_STATUS) = "login"
  2039.                 ew_Session(EW_SESSION_SYS_ADMIN) = 1 ' System Administrator
  2040.                 CurrentUserName = usr ' Load user name 
  2041.                 SessionUserID = -1 ' System Administrator
  2042.                 SessionUserLevelID = -1 ' System Administrator
  2043.                 SetUpUserLevel()
  2044.             End If
  2045.  
  2046.             ' Check other users
  2047.             If Not ValidateUser Then
  2048.                 sFilter = "([empUsername] = '" & ew_AdjustSql(usr) & "')"
  2049.  
  2050.                 ' Get SQL from GetSql function in <UserTable> class, <UserTable>info.aspx
  2051.                 '                sSql = tblEmployees.GetSQL(sFilter, "")
  2052.                 sSql = "Select * from tblEmployees WHERE " & sFilter
  2053.                 RsUser = Conn.GetDataReader(sSql)
  2054.                 If RsUser.Read() Then
  2055.                     If EW_CASE_SENSITIVE_PASSWORD Then
  2056.                         If EW_MD5_PASSWORD Then
  2057.                             ValidateUser = ew_SameStr(RsUser("empUserPass"), MD5(pwd))
  2058.                         Else
  2059.                             ValidateUser = ew_SameStr(RsUser("empUserPass"), pwd)
  2060.                         End If
  2061.                     Else
  2062.                         If EW_MD5_PASSWORD Then
  2063.                             ValidateUser = ew_SameStr(RsUser("empUserPass"), MD5(pwd.ToLower()))
  2064.                         Else
  2065.                             ValidateUser = ew_SameText(RsUser("empUserPass"), pwd)
  2066.                         End If
  2067.                     End If
  2068.                     If ValidateUser Then
  2069.                         ew_Session(EW_SESSION_STATUS) = "login"
  2070.                         ew_Session(EW_SESSION_SYS_ADMIN) = 0 ' Non System Administrator
  2071.                         CurrentUserName = RsUser("empUsername") ' Load user name
  2072.                         SessionUserID = RsUser("empID") ' Load user id
  2073.                         SessionUserLevelID = ew_ConvertToInt(RsUser("empUserLevel")) ' Load user level
  2074.                         SetUpUserLevel()
  2075.                         ew_Session("tfpssnet_Status_UserEmail") = RsUser("empEmail")
  2076.  
  2077.                         ' User Validated event
  2078.                         User_Validated(RsUser)
  2079.                     End If
  2080.                 End If
  2081.                 RsUser.Close()
  2082.                 RsUser.Dispose()
  2083.             End If
  2084.         End Function
  2085.  
  2086.         ' Static user level security
  2087.         Public Sub SetUpUserLevel()
  2088.             m_ArUserLevel.Clear()
  2089.             m_ArUserLevelPriv.Clear()
  2090.             Dim od As OrderedDictionary
  2091.  
  2092.             ' User level definitions
  2093.             od = New OrderedDictionary
  2094.             od.Add("UserLevelId", 0)
  2095.             od.Add("UserLevelName", "Default")
  2096.             m_ArUserLevel.Add(od)
  2097.             od = New OrderedDictionary
  2098.             od.Add("UserLevelId", 1)
  2099.             od.Add("UserLevelName", "Standard")
  2100.             m_ArUserLevel.Add(od)
  2101.             od = New OrderedDictionary
  2102.             od.Add("UserLevelId", 2)
  2103.             od.Add("UserLevelName", "Standard Lighting")
  2104.             m_ArUserLevel.Add(od)
  2105.             od = New OrderedDictionary
  2106.             od.Add("UserLevelId", 3)
  2107.             od.Add("UserLevelName", "Admin Lighting")
  2108.             m_ArUserLevel.Add(od)
  2109.             od = New OrderedDictionary
  2110.             od.Add("UserLevelId", 4)
  2111.             od.Add("UserLevelName", "Full Time")
  2112.             m_ArUserLevel.Add(od)
  2113.  
  2114.             ' User level privileges                    
  2115.             od = New OrderedDictionary
  2116.             od.Add("TableName", "tblDepartments")
  2117.             od.Add("UserLevelId", 0)
  2118.             od.Add("UserLevel", 0)
  2119.             m_ArUserLevelPriv.Add(od)
  2120.             od = New OrderedDictionary
  2121.             od.Add("TableName", "tblDepartments")
  2122.             od.Add("UserLevelId", 1)
  2123.             od.Add("UserLevel", 0)
  2124.             m_ArUserLevelPriv.Add(od)
  2125.             od = New OrderedDictionary
  2126.             od.Add("TableName", "tblDepartments")
  2127.             od.Add("UserLevelId", 2)
  2128.             od.Add("UserLevel", 0)
  2129.             m_ArUserLevelPriv.Add(od)
  2130.             od = New OrderedDictionary
  2131.             od.Add("TableName", "tblDepartments")
  2132.             od.Add("UserLevelId", 3)
  2133.             od.Add("UserLevel", 0)
  2134.             m_ArUserLevelPriv.Add(od)
  2135.             od = New OrderedDictionary
  2136.             od.Add("TableName", "tblDepartments")
  2137.             od.Add("UserLevelId", 4)
  2138.             od.Add("UserLevel", 0)
  2139.             m_ArUserLevelPriv.Add(od)
  2140.             od = New OrderedDictionary
  2141.             od.Add("TableName", "tblEmployees")
  2142.             od.Add("UserLevelId", 0)
  2143.             od.Add("UserLevel", 0)
  2144.             m_ArUserLevelPriv.Add(od)
  2145.             od = New OrderedDictionary
  2146.             od.Add("TableName", "tblEmployees")
  2147.             od.Add("UserLevelId", 1)
  2148.             od.Add("UserLevel", 0)
  2149.             m_ArUserLevelPriv.Add(od)
  2150.             od = New OrderedDictionary
  2151.             od.Add("TableName", "tblEmployees")
  2152.             od.Add("UserLevelId", 2)
  2153.             od.Add("UserLevel", 0)
  2154.             m_ArUserLevelPriv.Add(od)
  2155.             od = New OrderedDictionary
  2156.             od.Add("TableName", "tblEmployees")
  2157.             od.Add("UserLevelId", 3)
  2158.             od.Add("UserLevel", 0)
  2159.             m_ArUserLevelPriv.Add(od)
  2160.             od = New OrderedDictionary
  2161.             od.Add("TableName", "tblEmployees")
  2162.             od.Add("UserLevelId", 4)
  2163.             od.Add("UserLevel", 0)
  2164.             m_ArUserLevelPriv.Add(od)
  2165.             od = New OrderedDictionary
  2166.             od.Add("TableName", "tblEmployeeStatus")
  2167.             od.Add("UserLevelId", 0)
  2168.             od.Add("UserLevel", 0)
  2169.             m_ArUserLevelPriv.Add(od)
  2170.             od = New OrderedDictionary
  2171.             od.Add("TableName", "tblEmployeeStatus")
  2172.             od.Add("UserLevelId", 1)
  2173.             od.Add("UserLevel", 0)
  2174.             m_ArUserLevelPriv.Add(od)
  2175.             od = New OrderedDictionary
  2176.             od.Add("TableName", "tblEmployeeStatus")
  2177.             od.Add("UserLevelId", 2)
  2178.             od.Add("UserLevel", 0)
  2179.             m_ArUserLevelPriv.Add(od)
  2180.             od = New OrderedDictionary
  2181.             od.Add("TableName", "tblEmployeeStatus")
  2182.             od.Add("UserLevelId", 3)
  2183.             od.Add("UserLevel", 0)
  2184.             m_ArUserLevelPriv.Add(od)
  2185.             od = New OrderedDictionary
  2186.             od.Add("TableName", "tblEmployeeStatus")
  2187.             od.Add("UserLevelId", 4)
  2188.             od.Add("UserLevel", 0)
  2189.             m_ArUserLevelPriv.Add(od)
  2190.             od = New OrderedDictionary
  2191.             od.Add("TableName", "tblEvents")
  2192.             od.Add("UserLevelId", 0)
  2193.             od.Add("UserLevel", 0)
  2194.             m_ArUserLevelPriv.Add(od)
  2195.             od = New OrderedDictionary
  2196.             od.Add("TableName", "tblEvents")
  2197.             od.Add("UserLevelId", 1)
  2198.             od.Add("UserLevel", 0)
  2199.             m_ArUserLevelPriv.Add(od)
  2200.             od = New OrderedDictionary
  2201.             od.Add("TableName", "tblEvents")
  2202.             od.Add("UserLevelId", 2)
  2203.             od.Add("UserLevel", 0)
  2204.             m_ArUserLevelPriv.Add(od)
  2205.             od = New OrderedDictionary
  2206.             od.Add("TableName", "tblEvents")
  2207.             od.Add("UserLevelId", 3)
  2208.             od.Add("UserLevel", 0)
  2209.             m_ArUserLevelPriv.Add(od)
  2210.             od = New OrderedDictionary
  2211.             od.Add("TableName", "tblEvents")
  2212.             od.Add("UserLevelId", 4)
  2213.             od.Add("UserLevel", 0)
  2214.             m_ArUserLevelPriv.Add(od)
  2215.             od = New OrderedDictionary
  2216.             od.Add("TableName", "tblFixtureMaster")
  2217.             od.Add("UserLevelId", 0)
  2218.             od.Add("UserLevel", 0)
  2219.             m_ArUserLevelPriv.Add(od)
  2220.             od = New OrderedDictionary
  2221.             od.Add("TableName", "tblFixtureMaster")
  2222.             od.Add("UserLevelId", 1)
  2223.             od.Add("UserLevel", 0)
  2224.             m_ArUserLevelPriv.Add(od)
  2225.             od = New OrderedDictionary
  2226.             od.Add("TableName", "tblFixtureMaster")
  2227.             od.Add("UserLevelId", 2)
  2228.             od.Add("UserLevel", 0)
  2229.             m_ArUserLevelPriv.Add(od)
  2230.             od = New OrderedDictionary
  2231.             od.Add("TableName", "tblFixtureMaster")
  2232.             od.Add("UserLevelId", 3)
  2233.             od.Add("UserLevel", 0)
  2234.             m_ArUserLevelPriv.Add(od)
  2235.             od = New OrderedDictionary
  2236.             od.Add("TableName", "tblFixtureMaster")
  2237.             od.Add("UserLevelId", 4)
  2238.             od.Add("UserLevel", 0)
  2239.             m_ArUserLevelPriv.Add(od)
  2240.             od = New OrderedDictionary
  2241.             od.Add("TableName", "tblLocations")
  2242.             od.Add("UserLevelId", 0)
  2243.             od.Add("UserLevel", 0)
  2244.             m_ArUserLevelPriv.Add(od)
  2245.             od = New OrderedDictionary
  2246.             od.Add("TableName", "tblLocations")
  2247.             od.Add("UserLevelId", 1)
  2248.             od.Add("UserLevel", 0)
  2249.             m_ArUserLevelPriv.Add(od)
  2250.             od = New OrderedDictionary
  2251.             od.Add("TableName", "tblLocations")
  2252.             od.Add("UserLevelId", 2)
  2253.             od.Add("UserLevel", 0)
  2254.             m_ArUserLevelPriv.Add(od)
  2255.             od = New OrderedDictionary
  2256.             od.Add("TableName", "tblLocations")
  2257.             od.Add("UserLevelId", 3)
  2258.             od.Add("UserLevel", 0)
  2259.             m_ArUserLevelPriv.Add(od)
  2260.             od = New OrderedDictionary
  2261.             od.Add("TableName", "tblLocations")
  2262.             od.Add("UserLevelId", 4)
  2263.             od.Add("UserLevel", 0)
  2264.             m_ArUserLevelPriv.Add(od)
  2265.             od = New OrderedDictionary
  2266.             od.Add("TableName", "tblLPR")
  2267.             od.Add("UserLevelId", 0)
  2268.             od.Add("UserLevel", 0)
  2269.             m_ArUserLevelPriv.Add(od)
  2270.             od = New OrderedDictionary
  2271.             od.Add("TableName", "tblLPR")
  2272.             od.Add("UserLevelId", 1)
  2273.             od.Add("UserLevel", 0)
  2274.             m_ArUserLevelPriv.Add(od)
  2275.             od = New OrderedDictionary
  2276.             od.Add("TableName", "tblLPR")
  2277.             od.Add("UserLevelId", 2)
  2278.             od.Add("UserLevel", 13)
  2279.             m_ArUserLevelPriv.Add(od)
  2280.             od = New OrderedDictionary
  2281.             od.Add("TableName", "tblLPR")
  2282.             od.Add("UserLevelId", 3)
  2283.             od.Add("UserLevel", 15)
  2284.             m_ArUserLevelPriv.Add(od)
  2285.             od = New OrderedDictionary
  2286.             od.Add("TableName", "tblLPR")
  2287.             od.Add("UserLevelId", 4)
  2288.             od.Add("UserLevel", 15)
  2289.             m_ArUserLevelPriv.Add(od)
  2290.             od = New OrderedDictionary
  2291.             od.Add("TableName", "tblLPRStatus")
  2292.             od.Add("UserLevelId", 0)
  2293.             od.Add("UserLevel", 0)
  2294.             m_ArUserLevelPriv.Add(od)
  2295.             od = New OrderedDictionary
  2296.             od.Add("TableName", "tblLPRStatus")
  2297.             od.Add("UserLevelId", 1)
  2298.             od.Add("UserLevel", 0)
  2299.             m_ArUserLevelPriv.Add(od)
  2300.             od = New OrderedDictionary
  2301.             od.Add("TableName", "tblLPRStatus")
  2302.             od.Add("UserLevelId", 2)
  2303.             od.Add("UserLevel", 0)
  2304.             m_ArUserLevelPriv.Add(od)
  2305.             od = New OrderedDictionary
  2306.             od.Add("TableName", "tblLPRStatus")
  2307.             od.Add("UserLevelId", 3)
  2308.             od.Add("UserLevel", 15)
  2309.             m_ArUserLevelPriv.Add(od)
  2310.             od = New OrderedDictionary
  2311.             od.Add("TableName", "tblLPRStatus")
  2312.             od.Add("UserLevelId", 4)
  2313.             od.Add("UserLevel", 15)
  2314.             m_ArUserLevelPriv.Add(od)
  2315.             od = New OrderedDictionary
  2316.             od.Add("TableName", "tblMessage")
  2317.             od.Add("UserLevelId", 0)
  2318.             od.Add("UserLevel", 0)
  2319.             m_ArUserLevelPriv.Add(od)
  2320.             od = New OrderedDictionary
  2321.             od.Add("TableName", "tblMessage")
  2322.             od.Add("UserLevelId", 1)
  2323.             od.Add("UserLevel", 0)
  2324.             m_ArUserLevelPriv.Add(od)
  2325.             od = New OrderedDictionary
  2326.             od.Add("TableName", "tblMessage")
  2327.             od.Add("UserLevelId", 2)
  2328.             od.Add("UserLevel", 0)
  2329.             m_ArUserLevelPriv.Add(od)
  2330.             od = New OrderedDictionary
  2331.             od.Add("TableName", "tblMessage")
  2332.             od.Add("UserLevelId", 3)
  2333.             od.Add("UserLevel", 0)
  2334.             m_ArUserLevelPriv.Add(od)
  2335.             od = New OrderedDictionary
  2336.             od.Add("TableName", "tblMessage")
  2337.             od.Add("UserLevelId", 4)
  2338.             od.Add("UserLevel", 0)
  2339.             m_ArUserLevelPriv.Add(od)
  2340.             od = New OrderedDictionary
  2341.             od.Add("TableName", "tblParts")
  2342.             od.Add("UserLevelId", 0)
  2343.             od.Add("UserLevel", 0)
  2344.             m_ArUserLevelPriv.Add(od)
  2345.             od = New OrderedDictionary
  2346.             od.Add("TableName", "tblParts")
  2347.             od.Add("UserLevelId", 1)
  2348.             od.Add("UserLevel", 0)
  2349.             m_ArUserLevelPriv.Add(od)
  2350.             od = New OrderedDictionary
  2351.             od.Add("TableName", "tblParts")
  2352.             od.Add("UserLevelId", 2)
  2353.             od.Add("UserLevel", 0)
  2354.             m_ArUserLevelPriv.Add(od)
  2355.             od = New OrderedDictionary
  2356.             od.Add("TableName", "tblParts")
  2357.             od.Add("UserLevelId", 3)
  2358.             od.Add("UserLevel", 0)
  2359.             m_ArUserLevelPriv.Add(od)
  2360.             od = New OrderedDictionary
  2361.             od.Add("TableName", "tblParts")
  2362.             od.Add("UserLevelId", 4)
  2363.             od.Add("UserLevel", 0)
  2364.             m_ArUserLevelPriv.Add(od)
  2365.             od = New OrderedDictionary
  2366.             od.Add("TableName", "tblPositions")
  2367.             od.Add("UserLevelId", 0)
  2368.             od.Add("UserLevel", 0)
  2369.             m_ArUserLevelPriv.Add(od)
  2370.             od = New OrderedDictionary
  2371.             od.Add("TableName", "tblPositions")
  2372.             od.Add("UserLevelId", 1)
  2373.             od.Add("UserLevel", 0)
  2374.             m_ArUserLevelPriv.Add(od)
  2375.             od = New OrderedDictionary
  2376.             od.Add("TableName", "tblPositions")
  2377.             od.Add("UserLevelId", 2)
  2378.             od.Add("UserLevel", 0)
  2379.             m_ArUserLevelPriv.Add(od)
  2380.             od = New OrderedDictionary
  2381.             od.Add("TableName", "tblPositions")
  2382.             od.Add("UserLevelId", 3)
  2383.             od.Add("UserLevel", 0)
  2384.             m_ArUserLevelPriv.Add(od)
  2385.             od = New OrderedDictionary
  2386.             od.Add("TableName", "tblPositions")
  2387.             od.Add("UserLevelId", 4)
  2388.             od.Add("UserLevel", 0)
  2389.             m_ArUserLevelPriv.Add(od)
  2390.             od = New OrderedDictionary
  2391.             od.Add("TableName", "tblSchedule")
  2392.             od.Add("UserLevelId", 0)
  2393.             od.Add("UserLevel", 0)
  2394.             m_ArUserLevelPriv.Add(od)
  2395.             od = New OrderedDictionary
  2396.             od.Add("TableName", "tblSchedule")
  2397.             od.Add("UserLevelId", 1)
  2398.             od.Add("UserLevel", 12)
  2399.             m_ArUserLevelPriv.Add(od)
  2400.             od = New OrderedDictionary
  2401.             od.Add("TableName", "tblSchedule")
  2402.             od.Add("UserLevelId", 2)
  2403.             od.Add("UserLevel", 12)
  2404.             m_ArUserLevelPriv.Add(od)
  2405.             od = New OrderedDictionary
  2406.             od.Add("TableName", "tblSchedule")
  2407.             od.Add("UserLevelId", 3)
  2408.             od.Add("UserLevel", 12)
  2409.             m_ArUserLevelPriv.Add(od)
  2410.             od = New OrderedDictionary
  2411.             od.Add("TableName", "tblSchedule")
  2412.             od.Add("UserLevelId", 4)
  2413.             od.Add("UserLevel", 13)
  2414.             m_ArUserLevelPriv.Add(od)
  2415.             od = New OrderedDictionary
  2416.             od.Add("TableName", "tblScheduleStatus")
  2417.             od.Add("UserLevelId", 0)
  2418.             od.Add("UserLevel", 0)
  2419.             m_ArUserLevelPriv.Add(od)
  2420.             od = New OrderedDictionary
  2421.             od.Add("TableName", "tblScheduleStatus")
  2422.             od.Add("UserLevelId", 1)
  2423.             od.Add("UserLevel", 0)
  2424.             m_ArUserLevelPriv.Add(od)
  2425.             od = New OrderedDictionary
  2426.             od.Add("TableName", "tblScheduleStatus")
  2427.             od.Add("UserLevelId", 2)
  2428.             od.Add("UserLevel", 0)
  2429.             m_ArUserLevelPriv.Add(od)
  2430.             od = New OrderedDictionary
  2431.             od.Add("TableName", "tblScheduleStatus")
  2432.             od.Add("UserLevelId", 3)
  2433.             od.Add("UserLevel", 0)
  2434.             m_ArUserLevelPriv.Add(od)
  2435.             od = New OrderedDictionary
  2436.             od.Add("TableName", "tblScheduleStatus")
  2437.             od.Add("UserLevelId", 4)
  2438.             od.Add("UserLevel", 0)
  2439.             m_ArUserLevelPriv.Add(od)
  2440.             od = New OrderedDictionary
  2441.             od.Add("TableName", "tblTypes")
  2442.             od.Add("UserLevelId", 0)
  2443.             od.Add("UserLevel", 0)
  2444.             m_ArUserLevelPriv.Add(od)
  2445.             od = New OrderedDictionary
  2446.             od.Add("TableName", "tblTypes")
  2447.             od.Add("UserLevelId", 1)
  2448.             od.Add("UserLevel", 0)
  2449.             m_ArUserLevelPriv.Add(od)
  2450.             od = New OrderedDictionary
  2451.             od.Add("TableName", "tblTypes")
  2452.             od.Add("UserLevelId", 2)
  2453.             od.Add("UserLevel", 0)
  2454.             m_ArUserLevelPriv.Add(od)
  2455.             od = New OrderedDictionary
  2456.             od.Add("TableName", "tblTypes")
  2457.             od.Add("UserLevelId", 3)
  2458.             od.Add("UserLevel", 0)
  2459.             m_ArUserLevelPriv.Add(od)
  2460.             od = New OrderedDictionary
  2461.             od.Add("TableName", "tblTypes")
  2462.             od.Add("UserLevelId", 4)
  2463.             od.Add("UserLevel", 0)
  2464.             m_ArUserLevelPriv.Add(od)
  2465.             od = New OrderedDictionary
  2466.             od.Add("TableName", "tblLPRFixtureLocation")
  2467.             od.Add("UserLevelId", 0)
  2468.             od.Add("UserLevel", 0)
  2469.             m_ArUserLevelPriv.Add(od)
  2470.             od = New OrderedDictionary
  2471.             od.Add("TableName", "tblLPRFixtureLocation")
  2472.             od.Add("UserLevelId", 1)
  2473.             od.Add("UserLevel", 0)
  2474.             m_ArUserLevelPriv.Add(od)
  2475.             od = New OrderedDictionary
  2476.             od.Add("TableName", "tblLPRFixtureLocation")
  2477.             od.Add("UserLevelId", 2)
  2478.             od.Add("UserLevel", 0)
  2479.             m_ArUserLevelPriv.Add(od)
  2480.             od = New OrderedDictionary
  2481.             od.Add("TableName", "tblLPRFixtureLocation")
  2482.             od.Add("UserLevelId", 3)
  2483.             od.Add("UserLevel", 15)
  2484.             m_ArUserLevelPriv.Add(od)
  2485.             od = New OrderedDictionary
  2486.             od.Add("TableName", "tblLPRFixtureLocation")
  2487.             od.Add("UserLevelId", 4)
  2488.             od.Add("UserLevel", 15)
  2489.             m_ArUserLevelPriv.Add(od)
  2490.             od = New OrderedDictionary
  2491.             od.Add("TableName", "tblLPRFixtureType")
  2492.             od.Add("UserLevelId", 0)
  2493.             od.Add("UserLevel", 0)
  2494.             m_ArUserLevelPriv.Add(od)
  2495.             od = New OrderedDictionary
  2496.             od.Add("TableName", "tblLPRFixtureType")
  2497.             od.Add("UserLevelId", 1)
  2498.             od.Add("UserLevel", 0)
  2499.             m_ArUserLevelPriv.Add(od)
  2500.             od = New OrderedDictionary
  2501.             od.Add("TableName", "tblLPRFixtureType")
  2502.             od.Add("UserLevelId", 2)
  2503.             od.Add("UserLevel", 0)
  2504.             m_ArUserLevelPriv.Add(od)
  2505.             od = New OrderedDictionary
  2506.             od.Add("TableName", "tblLPRFixtureType")
  2507.             od.Add("UserLevelId", 3)
  2508.             od.Add("UserLevel", 15)
  2509.             m_ArUserLevelPriv.Add(od)
  2510.             od = New OrderedDictionary
  2511.             od.Add("TableName", "tblLPRFixtureType")
  2512.             od.Add("UserLevelId", 4)
  2513.             od.Add("UserLevel", 15)
  2514.             m_ArUserLevelPriv.Add(od)
  2515.             od = New OrderedDictionary
  2516.             od.Add("TableName", "viewEmployees")
  2517.             od.Add("UserLevelId", 0)
  2518.             od.Add("UserLevel", 0)
  2519.             m_ArUserLevelPriv.Add(od)
  2520.             od = New OrderedDictionary
  2521.             od.Add("TableName", "viewEmployees")
  2522.             od.Add("UserLevelId", 1)
  2523.             od.Add("UserLevel", 12)
  2524.             m_ArUserLevelPriv.Add(od)
  2525.             od = New OrderedDictionary
  2526.             od.Add("TableName", "viewEmployees")
  2527.             od.Add("UserLevelId", 2)
  2528.             od.Add("UserLevel", 12)
  2529.             m_ArUserLevelPriv.Add(od)
  2530.             od = New OrderedDictionary
  2531.             od.Add("TableName", "viewEmployees")
  2532.             od.Add("UserLevelId", 3)
  2533.             od.Add("UserLevel", 12)
  2534.             m_ArUserLevelPriv.Add(od)
  2535.             od = New OrderedDictionary
  2536.             od.Add("TableName", "viewEmployees")
  2537.             od.Add("UserLevelId", 4)
  2538.             od.Add("UserLevel", 12)
  2539.             m_ArUserLevelPriv.Add(od)
  2540.             od = New OrderedDictionary
  2541.             od.Add("TableName", "viewClockInOut")
  2542.             od.Add("UserLevelId", 0)
  2543.             od.Add("UserLevel", 0)
  2544.             m_ArUserLevelPriv.Add(od)
  2545.             od = New OrderedDictionary
  2546.             od.Add("TableName", "viewClockInOut")
  2547.             od.Add("UserLevelId", 1)
  2548.             od.Add("UserLevel", 9)
  2549.             m_ArUserLevelPriv.Add(od)
  2550.             od = New OrderedDictionary
  2551.             od.Add("TableName", "viewClockInOut")
  2552.             od.Add("UserLevelId", 2)
  2553.             od.Add("UserLevel", 9)
  2554.             m_ArUserLevelPriv.Add(od)
  2555.             od = New OrderedDictionary
  2556.             od.Add("TableName", "viewClockInOut")
  2557.             od.Add("UserLevelId", 3)
  2558.             od.Add("UserLevel", 9)
  2559.             m_ArUserLevelPriv.Add(od)
  2560.             od = New OrderedDictionary
  2561.             od.Add("TableName", "viewClockInOut")
  2562.             od.Add("UserLevelId", 4)
  2563.             od.Add("UserLevel", 9)
  2564.             m_ArUserLevelPriv.Add(od)
  2565.             od = New OrderedDictionary
  2566.             od.Add("TableName", "viewSchedule")
  2567.             od.Add("UserLevelId", 0)
  2568.             od.Add("UserLevel", 0)
  2569.             m_ArUserLevelPriv.Add(od)
  2570.             od = New OrderedDictionary
  2571.             od.Add("TableName", "viewSchedule")
  2572.             od.Add("UserLevelId", 1)
  2573.             od.Add("UserLevel", 12)
  2574.             m_ArUserLevelPriv.Add(od)
  2575.             od = New OrderedDictionary
  2576.             od.Add("TableName", "viewSchedule")
  2577.             od.Add("UserLevelId", 2)
  2578.             od.Add("UserLevel", 12)
  2579.             m_ArUserLevelPriv.Add(od)
  2580.             od = New OrderedDictionary
  2581.             od.Add("TableName", "viewSchedule")
  2582.             od.Add("UserLevelId", 3)
  2583.             od.Add("UserLevel", 12)
  2584.             m_ArUserLevelPriv.Add(od)
  2585.             od = New OrderedDictionary
  2586.             od.Add("TableName", "viewSchedule")
  2587.             od.Add("UserLevelId", 4)
  2588.             od.Add("UserLevel", 12)
  2589.             m_ArUserLevelPriv.Add(od)
  2590.             od = New OrderedDictionary
  2591.             od.Add("TableName", "askDate")
  2592.             od.Add("UserLevelId", 0)
  2593.             od.Add("UserLevel", 0)
  2594.             m_ArUserLevelPriv.Add(od)
  2595.             od = New OrderedDictionary
  2596.             od.Add("TableName", "askDate")
  2597.             od.Add("UserLevelId", 1)
  2598.             od.Add("UserLevel", 0)
  2599.             m_ArUserLevelPriv.Add(od)
  2600.             od = New OrderedDictionary
  2601.             od.Add("TableName", "askDate")
  2602.             od.Add("UserLevelId", 2)
  2603.             od.Add("UserLevel", 0)
  2604.             m_ArUserLevelPriv.Add(od)
  2605.             od = New OrderedDictionary
  2606.             od.Add("TableName", "askDate")
  2607.             od.Add("UserLevelId", 3)
  2608.             od.Add("UserLevel", 0)
  2609.             m_ArUserLevelPriv.Add(od)
  2610.             od = New OrderedDictionary
  2611.             od.Add("TableName", "askDate")
  2612.             od.Add("UserLevelId", 4)
  2613.             od.Add("UserLevel", 0)
  2614.             m_ArUserLevelPriv.Add(od)
  2615.  
  2616.             od = New OrderedDictionary
  2617.             od.Add("TableName", "ltgFixtureLocation")
  2618.             od.Add("UserLevelId", 0)
  2619.             od.Add("UserLevel", 0)
  2620.             m_ArUserLevelPriv.Add(od)
  2621.             od = New OrderedDictionary
  2622.             od.Add("TableName", "ltgFixtureLocation")
  2623.             od.Add("UserLevelId", 1)
  2624.             od.Add("UserLevel", 0)
  2625.             m_ArUserLevelPriv.Add(od)
  2626.             od = New OrderedDictionary
  2627.             od.Add("TableName", "ltgFixtureLocation")
  2628.             od.Add("UserLevelId", 2)
  2629.             od.Add("UserLevel", 0)
  2630.             m_ArUserLevelPriv.Add(od)
  2631.             od = New OrderedDictionary
  2632.             od.Add("TableName", "ltgFixtureLocation")
  2633.             od.Add("UserLevelId", 3)
  2634.             od.Add("UserLevel", 15)
  2635.             m_ArUserLevelPriv.Add(od)
  2636.             od = New OrderedDictionary
  2637.             od.Add("TableName", "ltgFixtureMaster")
  2638.             od.Add("UserLevelId", 0)
  2639.             od.Add("UserLevel", 0)
  2640.             m_ArUserLevelPriv.Add(od)
  2641.             od = New OrderedDictionary
  2642.             od.Add("TableName", "ltgFixtureMaster")
  2643.             od.Add("UserLevelId", 1)
  2644.             od.Add("UserLevel", 0)
  2645.             m_ArUserLevelPriv.Add(od)
  2646.             od = New OrderedDictionary
  2647.             od.Add("TableName", "ltgFixtureMaster")
  2648.             od.Add("UserLevelId", 2)
  2649.             od.Add("UserLevel", 8)
  2650.             m_ArUserLevelPriv.Add(od)
  2651.             od = New OrderedDictionary
  2652.             od.Add("TableName", "ltgFixtureMaster")
  2653.             od.Add("UserLevelId", 3)
  2654.             od.Add("UserLevel", 15)
  2655.             m_ArUserLevelPriv.Add(od)
  2656.             od = New OrderedDictionary
  2657.             od.Add("TableName", "ltgFixtureNote")
  2658.             od.Add("UserLevelId", 0)
  2659.             od.Add("UserLevel", 0)
  2660.             m_ArUserLevelPriv.Add(od)
  2661.             od = New OrderedDictionary
  2662.             od.Add("TableName", "ltgFixtureNote")
  2663.             od.Add("UserLevelId", 1)
  2664.             od.Add("UserLevel", 0)
  2665.             m_ArUserLevelPriv.Add(od)
  2666.             od = New OrderedDictionary
  2667.             od.Add("TableName", "ltgFixtureNote")
  2668.             od.Add("UserLevelId", 2)
  2669.             od.Add("UserLevel", 13)
  2670.             m_ArUserLevelPriv.Add(od)
  2671.             od = New OrderedDictionary
  2672.             od.Add("TableName", "ltgFixtureNote")
  2673.             od.Add("UserLevelId", 3)
  2674.             od.Add("UserLevel", 15)
  2675.             m_ArUserLevelPriv.Add(od)
  2676.             od = New OrderedDictionary
  2677.             od.Add("TableName", "ltgFixtureNoteType")
  2678.             od.Add("UserLevelId", 0)
  2679.             od.Add("UserLevel", 0)
  2680.             m_ArUserLevelPriv.Add(od)
  2681.             od = New OrderedDictionary
  2682.             od.Add("TableName", "ltgFixtureNoteType")
  2683.             od.Add("UserLevelId", 1)
  2684.             od.Add("UserLevel", 0)
  2685.             m_ArUserLevelPriv.Add(od)
  2686.             od = New OrderedDictionary
  2687.             od.Add("TableName", "ltgFixtureNoteType")
  2688.             od.Add("UserLevelId", 2)
  2689.             od.Add("UserLevel", 0)
  2690.             m_ArUserLevelPriv.Add(od)
  2691.             od = New OrderedDictionary
  2692.             od.Add("TableName", "ltgFixtureNoteType")
  2693.             od.Add("UserLevelId", 3)
  2694.             od.Add("UserLevel", 15)
  2695.             m_ArUserLevelPriv.Add(od)
  2696.             od = New OrderedDictionary
  2697.             od.Add("TableName", "ltgFixtureType")
  2698.             od.Add("UserLevelId", 0)
  2699.             od.Add("UserLevel", 0)
  2700.             m_ArUserLevelPriv.Add(od)
  2701.             od = New OrderedDictionary
  2702.             od.Add("TableName", "ltgFixtureType")
  2703.             od.Add("UserLevelId", 1)
  2704.             od.Add("UserLevel", 0)
  2705.             m_ArUserLevelPriv.Add(od)
  2706.             od = New OrderedDictionary
  2707.             od.Add("TableName", "ltgFixtureType")
  2708.             od.Add("UserLevelId", 2)
  2709.             od.Add("UserLevel", 0)
  2710.             m_ArUserLevelPriv.Add(od)
  2711.             od = New OrderedDictionary
  2712.             od.Add("TableName", "ltgFixtureType")
  2713.             od.Add("UserLevelId", 3)
  2714.             od.Add("UserLevel", 15)
  2715.             m_ArUserLevelPriv.Add(od)
  2716.  
  2717.             ParentPage.ew_InvokeMethod("SetUpReportUserLevel", New Object() {m_ArUserLevelPriv})
  2718.  
  2719.             ' UserLevel_Loaded event
  2720.             UserLevel_Loaded()
  2721.  
  2722.             ' Save the user level to session variable
  2723.             SaveUserLevel()
  2724.         End Sub
  2725.  
  2726.         ' Add user permission
  2727.         Public Sub AddUserPermission(UserLevelName As String, TableName As String, UserPermission As Integer)
  2728.             Dim UserLevelID As String = ""
  2729.  
  2730.             ' Get user level id from user name
  2731.             If ew_IsArrayList(m_ArUserLevel) Then
  2732.                 For Each Row As OrderedDictionary In m_ArUserLevel
  2733.                     If ew_SameStr(UserLevelName, Row(1)) Then
  2734.                         UserLevelID = Row(0)
  2735.                         Exit For
  2736.                     End If
  2737.                 Next 
  2738.             End If
  2739.             If ew_IsArrayList(m_ArUserLevelPriv) AndAlso UserLevelID <> "" Then
  2740.                 For Each Row As OrderedDictionary In m_ArUserLevelPriv
  2741.                     If ew_SameStr(Row(0), TableName) AndAlso ew_SameStr(Row(1), UserLevelID) Then
  2742.                         Row(2) = Row(2) Or UserPermission ' Add permission
  2743.                         Exit For
  2744.                     End If
  2745.                 Next 
  2746.             End If
  2747.         End Sub
  2748.  
  2749.         ' Delete user permission
  2750.         Public Sub DeleteUserPermission(UserLevelName As String, TableName As String, UserPermission As Integer)
  2751.             Dim UserLevelID As String = ""
  2752.  
  2753.             ' Get user level id from user name
  2754.             If ew_IsArrayList(m_ArUserLevel) Then
  2755.                 For Each Row As OrderedDictionary In m_ArUserLevel
  2756.                     If ew_SameStr(UserLevelName, Row(1)) Then
  2757.                         UserLevelID = Row(0)
  2758.                         Exit For
  2759.                     End If
  2760.                 Next 
  2761.             End If
  2762.             If ew_IsArrayList(m_ArUserLevelPriv) AndAlso UserLevelID <> "" Then
  2763.                 For Each Row As OrderedDictionary In m_ArUserLevelPriv
  2764.                     If ew_SameStr(Row(0), TableName) AndAlso ew_SameStr(Row(1), UserLevelID) Then
  2765.                         Row(2) = Row(2) And (127 - UserPermission) ' Remove permission
  2766.                         Exit For
  2767.                     End If
  2768.                 Next 
  2769.             End If
  2770.         End Sub
  2771.  
  2772.         ' Load current user level
  2773.         Public Sub LoadCurrentUserLevel(Table As String)
  2774.             LoadUserLevel()
  2775.             SessionUserLevel = CurrentUserLevelPriv(Table)
  2776.         End Sub
  2777.  
  2778.         ' Get current user privilege
  2779.         Private Function CurrentUserLevelPriv(TableName As String) As Integer
  2780.             If IsLoggedIn() Then
  2781.                 CurrentUserLevelPriv = 0
  2782.                 For i As Integer = 0 To m_ArUserLevelID.GetUpperBound(0)
  2783.                     CurrentUserLevelPriv = CurrentUserLevelPriv Or GetUserLevelPrivEx(TableName, m_ArUserLevelID(i))
  2784.                 Next 
  2785.             Else
  2786.                 Return 0
  2787.             End If
  2788.         End Function
  2789.  
  2790.         ' Get user level ID by user level name
  2791.         Public Function GetUserLevelID(UserLevelName As String) As Integer
  2792.             If ew_SameStr(UserLevelName, "Administrator") Then
  2793.                 Return -1
  2794.             ElseIf UserLevelName <> "" Then 
  2795.                 If ew_IsArrayList(m_ArUserLevel) Then
  2796.                     For Each Row As OrderedDictionary In m_ArUserLevel
  2797.                         If ew_SameStr(Row(1), UserLevelName) Then
  2798.                             Return Row(0)
  2799.                         End If
  2800.                     Next 
  2801.                 End If
  2802.             End If
  2803.             Return -2 ' Unknown
  2804.         End Function
  2805.  
  2806.         ' Add user level (for use with UserLevel_Loading event)
  2807.         Public Sub AddUserLevel(UserLevelName As String)
  2808.             Dim bFound As Boolean = False
  2809.             If ew_Empty(UserLevelName) Then Exit Sub
  2810.             Dim UserLevelID As Integer = GetUserLevelID(UserLevelName)
  2811.             If Not IsNumeric(UserLevelID) Then Exit Sub
  2812.             If UserLevelID < -1 Then Exit Sub
  2813.             If Not IsArray(m_ArUserLevelID) Then
  2814.                 Array.Resize(m_ArUserLevelID, 1)
  2815.             Else
  2816.                 For i As Integer = 0 To m_ArUserLevelID.GetUpperBound(0)
  2817.                     If m_ArUserLevelID(i) = UserLevelID Then
  2818.                         bFound = True
  2819.                         Exit For
  2820.                     End If
  2821.                 Next 
  2822.                 If Not bFound Then
  2823.                     Array.Resize(m_ArUserLevelID, m_ArUserLevelID.Length + 1)
  2824.                 End If
  2825.             End If
  2826.             If Not bFound Then
  2827.                 m_ArUserLevelID(m_ArUserLevelID.GetUpperBound(0)) = UserLevelID
  2828.             End If
  2829.         End Sub
  2830.  
  2831.         ' Delete user level (for use with UserLevel_Loading event)
  2832.         Public Sub DeleteUserLevel(UserLevelName As String)            
  2833.             If UserLevelName = "" OrElse IsDbNull(UserLevelName) Then Exit Sub
  2834.             Dim UserLevelID As Integer = GetUserLevelID(UserLevelName)
  2835.             If Not IsNumeric(UserLevelID) Then Exit Sub
  2836.             If UserLevelID < -1 Then Exit Sub
  2837.             If IsArray(m_ArUserLevelID) Then
  2838.                 For i As Integer = 0 To m_ArUserLevelID.GetUpperBound(0)
  2839.                     If m_ArUserLevelID(i) = UserLevelID Then
  2840.                         For j As Integer = i + 1 To m_ArUserLevelID.GetUpperBound(0)
  2841.                             m_ArUserLevelID(j - 1) = m_ArUserLevelID(j)
  2842.                         Next 
  2843.                         If m_ArUserLevelID.Length > 0 Then
  2844.  
  2845.                             'm_ArUserLevelID = ""
  2846.                         'Else
  2847.  
  2848.                             Array.Resize(m_ArUserLevelID, m_ArUserLevelID.Length - 1)
  2849.                         End If
  2850.                         Exit Sub
  2851.                     End If
  2852.                 Next 
  2853.             End If
  2854.         End Sub
  2855.  
  2856.         ' User level list
  2857.         Public Function UserLevelList() As String
  2858.             Dim List As String = ""
  2859.             If IsArray(m_ArUserLevelID) Then
  2860.                 For i As Integer = 0 To m_ArUserLevelID.GetUpperBound(0)
  2861.                     If List <> "" Then List = List & ", "
  2862.                     List = List & m_ArUserLevelID(i)
  2863.                 Next 
  2864.             End If
  2865.             Return List
  2866.         End Function
  2867.  
  2868.         ' User level name list
  2869.         Public Function UserLevelNameList() As String
  2870.             Dim List As String = ""
  2871.             If IsArray(m_ArUserLevelID) Then
  2872.                 For i As Integer = 0 To m_ArUserLevelID.GetUpperBound(0)
  2873.                     If List <> "" Then List = List & ", "
  2874.                     List = List & ew_QuotedValue(GetUserLevelName(m_ArUserLevelID(i)), EW_DATATYPE_STRING)
  2875.                 Next 
  2876.             End If
  2877.             Return List
  2878.         End Function
  2879.  
  2880.         ' Get user privilege based on table name and user level
  2881.         Public Function GetUserLevelPrivEx(TableName As String, UserLevelID As Integer) As Integer
  2882.             If ew_SameStr(UserLevelID, "-1") Then ' System Administrator
  2883.                 If EW_USER_LEVEL_COMPAT Then
  2884.                     Return 31 ' Use old user level values
  2885.                 Else
  2886.                     Return 127 ' Use new user level values (separate View/Search)
  2887.                 End If
  2888.             ElseIf UserLevelID >= 0 Then 
  2889.                 If ew_IsArrayList(m_ArUserLevelPriv) Then
  2890.                     For Each Row As OrderedDictionary In m_ArUserLevelPriv
  2891.                         If ew_SameStr(Row(0), TableName) AndAlso ew_SameStr(Row(1), UserLevelID) Then
  2892.                             Return ew_ConvertToInt(Row(2))
  2893.                         End If
  2894.                     Next
  2895.                 End If
  2896.                 Return 0
  2897.             End If            
  2898.         End Function
  2899.  
  2900.         ' Get current user level name
  2901.         Public Function CurrentUserLevelName() As String
  2902.             Return GetUserLevelName(CurrentUserLevelID)
  2903.         End Function
  2904.  
  2905.         ' Get user level name based on user level
  2906.         Public Function GetUserLevelName(UserLevelID As Object) As String
  2907.             If ew_SameStr(UserLevelID, "-1") Then
  2908.                 Return "Administrator"
  2909.             ElseIf UserLevelID >= 0 Then 
  2910.                 If ew_IsArrayList(m_ArUserLevel) Then
  2911.                     For Each Row As OrderedDictionary In m_ArUserLevel
  2912.                         If ew_SameStr(Row(0), UserLevelID) Then
  2913.                             Return Row(1)
  2914.                         End If
  2915.                     Next 
  2916.                 End If
  2917.             End If
  2918.             Return ""
  2919.         End Function
  2920.  
  2921.         ' Display all the User Level settings (for debug only)
  2922.         Public Sub ShowUserLevelInfo()
  2923.             If ew_IsArrayList(m_ArUserLevel) Then
  2924.                 ew_Write("User Levels:<br>")
  2925.                 ew_Write("UserLevelId, UserLevelName<br>")
  2926.                 For Each Row As OrderedDictionary In m_ArUserLevel
  2927.                     ew_Write("  " & Row(0) & ", " & Row(1) & "<br>")
  2928.                 Next 
  2929.             Else
  2930.                 ew_Write("No User Level definitions." & "<br>")
  2931.             End If
  2932.             If ew_IsArrayList(m_ArUserLevelPriv) Then
  2933.                 ew_Write("User Level Privs:<br>")
  2934.                 ew_Write("TableName, UserLevelId, UserLevelPriv<br>")
  2935.                 For Each Row As OrderedDictionary In m_ArUserLevelPriv
  2936.                     ew_Write("  " & Row(0) & ", " & Row(1) & ", " & Row(2) & "<br>")
  2937.                 Next 
  2938.             Else
  2939.                 ew_Write("No User Level privilege settings." & "<br>")
  2940.             End If
  2941.             ew_Write("CurrentUserLevel = " & CurrentUserLevel & "<br>")
  2942.             ew_Write("User Levels = " & UserLevelList() & "<br>")
  2943.         End Sub
  2944.  
  2945.         ' Check privilege for List page (for menu items)
  2946.         Public Function AllowList(TableName As String) As Boolean
  2947.             Return ew_ConvertToBool(CurrentUserLevelPriv(TableName) And EW_ALLOW_LIST)
  2948.         End Function
  2949.  
  2950.         ' Check privilege for Add
  2951.         Public Function AllowAdd(TableName As String) As Boolean
  2952.             Return ew_ConvertToBool(CurrentUserLevelPriv(TableName) And EW_ALLOW_ADD)
  2953.         End Function
  2954.  
  2955.         ' Check if user is logged in
  2956.         Public Function IsLoggedIn() As Boolean
  2957.             Return ew_SameStr(ew_Session(EW_SESSION_STATUS), "login")
  2958.         End Function
  2959.  
  2960.         ' Check if user is system administrator
  2961.         Public Function IsSysAdmin() As Boolean
  2962.             Return (ew_Session(EW_SESSION_SYS_ADMIN) = 1)
  2963.         End Function
  2964.  
  2965.         ' Check if user is administrator
  2966.         Public Function IsAdmin() As Boolean
  2967.             Dim Result As Boolean = IsSysAdmin()
  2968.             If Not Result AndAlso Information.IsArray(m_ArUserLevelID) Then
  2969.                 Result = Array.IndexOf(m_ArUserLevelID, -1) > -1
  2970.             End If
  2971.             If Not Result Then
  2972.                 Result = ew_SameStr(CurrentUserID, "-1")
  2973.             End If
  2974.             If Not Result AndAlso Information.IsArray(m_ArUserID) Then            
  2975.                 Result = Array.IndexOf(m_ArUserID, -1) > -1
  2976.             End If
  2977.             Return Result
  2978.         End Function
  2979.  
  2980.         ' Save user level to session
  2981.         Public Sub SaveUserLevel()
  2982.             ew_Session(EW_SESSION_AR_USER_LEVEL) = m_ArUserLevel
  2983.             ew_Session(EW_SESSION_AR_USER_LEVEL_PRIV) = m_ArUserLevelPriv            
  2984.         End Sub
  2985.  
  2986.         ' Load user level from session
  2987.         Public Sub LoadUserLevel()
  2988.             If Not ew_IsArrayList(ew_Session(EW_SESSION_AR_USER_LEVEL)) Then
  2989.                 SetUpUserLevel()
  2990.                 SaveUserLevel()
  2991.             Else
  2992.                 m_ArUserLevel = ew_Session(EW_SESSION_AR_USER_LEVEL)
  2993.                 m_ArUserLevelPriv = ew_Session(EW_SESSION_AR_USER_LEVEL_PRIV)
  2994.             End If
  2995.         End Sub
  2996.  
  2997.         ' Get user email
  2998.         Public Function CurrentUserEmail() As String
  2999.             Return CurrentUserInfo("empEmail")
  3000.         End Function
  3001.  
  3002.         ' Get user info
  3003.         Public Function CurrentUserInfo(FieldName As String) As Object
  3004.             Dim Info As Object = Nothing
  3005.             Try                
  3006.                 Info = GetUserInfo(FieldName, CurrentUserID)
  3007.                 Return Info
  3008.             Catch
  3009.                 If EW_DEBUG_ENABLED Then Throw
  3010.                 Return Info
  3011.             End Try
  3012.         End Function
  3013.  
  3014.         ' Get user info
  3015.         Public Function GetUserInfo(FieldName As String, userid As Object) As Object
  3016.             Dim Info As Object = Nothing
  3017.             If ew_Empty(userid) Then Return Info
  3018.             Try        
  3019.  
  3020.                 ' Get SQL from GetSql function in <UserTable> class, <UserTable>info.aspx
  3021.                 Dim sFilter As String = "([empID] = " & ew_AdjustSql(UserID) & ")"
  3022.                 Dim sSql As String = tblEmployees.GetSQL(sFilter, "")                        
  3023.                 Dim od As OrderedDictionary = ew_ExecuteRow(sSql)                
  3024.                 If od IsNot Nothing Then
  3025.                     Info = od(FieldName)
  3026.                 End If
  3027.                 Return Info
  3028.             Catch
  3029.                 If EW_DEBUG_ENABLED Then Throw
  3030.                 Return Info
  3031.             End Try
  3032.       End Function 
  3033.  
  3034.         ' Get user id value by user login name
  3035.         Public Function GetUserIDByUserName(UserName As String) As Object
  3036.             Dim UserID As Object = ""    
  3037.             If UserName <> "" Then
  3038.                 Dim sFilter As String = "[empUsername] = '" & ew_AdjustSql(UserName) & "'"
  3039.                 Dim sSql As String = tblEmployees.GetSQL(sFilter, "")
  3040.                 Dim od As OrderedDictionary = ew_ExecuteRow(sSql)
  3041.                 If od IsNot Nothing Then
  3042.                     UserID = od("empID")
  3043.                 End If
  3044.             End If            
  3045.             Return UserID                
  3046.         End Function
  3047.  
  3048.         ' Load user id
  3049.         Public Sub LoadUserID()            
  3050.             Dim sUserIDList As String, sCurUserIDList As String
  3051.             Array.Resize(m_ArUserID, 0)
  3052.             If Not ew_SameStr(CurrentUserID, "-1") Then ' Get first level
  3053.                 AddUserID(CurrentUserID)
  3054.                 Dim sFilter As String = tblEmployees.UserIDFilter(CurrentUserID)
  3055.                 Dim sSql As String = tblEmployees.GetSQL(sFilter, "")
  3056.                 Dim RsUser As OleDbDataReader = Conn.GetDataReader(sSql)
  3057.                 Do While RsUser.Read()
  3058.                     AddUserID(RsUser("empID"))
  3059.                 Loop
  3060.                 RsUser.Close()
  3061.                 RsUser.Dispose()
  3062.             End If
  3063.         End Sub
  3064.  
  3065.         ' Add user name
  3066.         Public Sub AddUserName(UserName As String)
  3067.             AddUserID(GetUserIDByUserName(UserName))
  3068.         End Sub
  3069.  
  3070.         ' Add user ID
  3071.         Public Sub AddUserID(userid As Object)
  3072.             Dim bFound As Boolean = False
  3073.             If ew_Empty(userid) Then Exit Sub
  3074.             If Not IsNumeric(userid) Then Exit Sub
  3075.             If Not IsArray(m_ArUserID) Then
  3076.                 Array.Resize(m_ArUserID, 1)
  3077.             Else
  3078.                 For i As Integer = 0 to m_ArUserID.GetUpperBound(0)
  3079.                     If ew_SameStr(m_ArUserID(i), userid) Then
  3080.                         bFound = True
  3081.                         Exit For
  3082.                     End If
  3083.                 Next
  3084.                 If Not bFound Then
  3085.                     Array.Resize(m_ArUserID, m_ArUserID.Length + 1)
  3086.                 End If
  3087.             End If
  3088.             If Not bFound Then
  3089.                 m_ArUserID(m_ArUserID.GetUpperBound(0)) = userid
  3090.             End If
  3091.         End Sub
  3092.  
  3093.         ' Delete user name
  3094.         Public Sub DeleteUserName(UserName As String)
  3095.             DeleteUserID(GetUserIDByUserName(UserName))
  3096.         End Sub
  3097.  
  3098.         ' Delete user ID
  3099.         Public Sub DeleteUserID(userid As Object)
  3100.             If ew_Empty(userid) Then Exit Sub
  3101.             If Not IsNumeric(userid) Then Exit Sub
  3102.             If IsArray(m_ArUserID) Then
  3103.                 For i As Integer = 0 to m_ArUserID.GetUpperBound(0)
  3104.                     If ew_SameStr(m_ArUserID(i), userid) Then
  3105.                         For j As Integer = i+1 to m_ArUserID.GetUpperBound(0)
  3106.                             m_ArUserID(j-1) = m_ArUserID(j)
  3107.                         Next
  3108.                         If m_ArUserID.Length > 0 Then
  3109.  
  3110.                             'm_ArUserID = ""
  3111.                         'Else
  3112.  
  3113.                             Array.Resize(m_ArUserID, m_ArUserID.Length - 1)
  3114.                         End If
  3115.                         Exit Sub
  3116.                     End If
  3117.                 Next
  3118.             End If
  3119.         End Sub
  3120.  
  3121.         ' User ID list
  3122.         Public Function UserIDList() As String
  3123.             Dim List As String = ""
  3124.             If IsArray(m_ArUserID) Then
  3125.                 For i As Integer = 0 to m_ArUserID.GetUpperBound(0)
  3126.                     If List <> "" Then List = List & ", "
  3127.                     List = List & ew_QuotedValue(m_ArUserID(i), EW_DATATYPE_NUMBER)
  3128.                 Next
  3129.             End If
  3130.             Return List
  3131.         End Function
  3132.  
  3133.         ' List of allowed user ids for this user
  3134.         Function IsValidUserID(userid As Object) As Boolean
  3135.             If IsLoggedIn() Then
  3136.                 If IsArray(m_ArUserID) Then
  3137.                     For i As Integer = 0 to m_ArUserID.GetUpperBound(0)
  3138.                         If ew_SameStr(m_ArUserID(i), userid) Then
  3139.                             Return True
  3140.                         End If
  3141.                     Next
  3142.                 End If
  3143.             End If
  3144.             Return False    
  3145.         End Function
  3146.  
  3147.         ' UserID Loading event
  3148.         Public Sub UserID_Loading()
  3149.  
  3150.             'HttpContext.Current.Response.Write("UserID Loading: " & CurrentUserID & "<br>")
  3151.         End Sub
  3152.  
  3153.         ' UserID Loaded event
  3154.         Public Sub UserID_Loaded()
  3155.  
  3156.             'HttpContext.Current.Response.Write("UserID Loaded: " & UserIDList & "<br>")
  3157.         End Sub
  3158.  
  3159.         ' User Level Loaded event
  3160.         Public Sub UserLevel_Loaded()
  3161.  
  3162.             'AddUserPermission(<UserLevelName>, <TableName>, <UserPermission>)
  3163.             'DeleteUserPermission(<UserLevelName>, <TableName>, <UserPermission>)
  3164.  
  3165.         End Sub
  3166.  
  3167.         ' Table Permission Loading event
  3168.         Public Sub TablePermission_Loading()
  3169.  
  3170.             'HttpContext.Current.Response.Write("Table Permission Loading: " & CurrentUserLevelID & "<br>")
  3171.         End Sub
  3172.  
  3173.         ' Table Permission Loaded event
  3174.         Public Sub TablePermission_Loaded()
  3175.  
  3176.             'HttpContext.Current.Response.Write("Table Permission Loaded: " & CurrentUserLevel & "<br>")
  3177.         End Sub
  3178.  
  3179.         ' User Validated event
  3180.         Public Sub User_Validated(rs As DbDataReader)
  3181.  
  3182.             'HttpContext.Current.Session("UserEmail") = rs("Email")
  3183.         End Sub
  3184.     End Class
  3185.  
  3186.     ' Return multi-value search SQL
  3187.     Public Shared Function ew_GetMultiSearchSql(ByRef Fld As Object, FldVal As String) As String
  3188.         Dim sSql As String, sVal As String, sWrk As String = ""
  3189.         Dim arVal() As String = FldVal.Split(New Char() {","c})
  3190.         For i As Integer = 0 To arVal.GetUpperBound(0)
  3191.             sVal = arVal(i).Trim()
  3192.             If arVal.GetUpperBound(0) = 0 OrElse EW_SEARCH_MULTI_VALUE_OPTION = 3 Then
  3193.                 sSql = Fld.FldExpression & " = '" & ew_AdjustSql(sVal) & "' OR " & ew_GetMultiSearchSqlPart(Fld, sVal)
  3194.             Else
  3195.                 sSql = ew_GetMultiSearchSqlPart(Fld, sVal)
  3196.             End If
  3197.             If sWrk <> "" Then
  3198.                 If EW_SEARCH_MULTI_VALUE_OPTION = 2 Then
  3199.                     sWrk = sWrk & " AND "
  3200.                 ElseIf EW_SEARCH_MULTI_VALUE_OPTION = 3 Then 
  3201.                     sWrk = sWrk & " OR "
  3202.                 End If
  3203.             End If
  3204.             sWrk = sWrk & "(" & sSql & ")"
  3205.         Next 
  3206.         Return sWrk
  3207.     End Function
  3208.  
  3209.     ' Get multi search SQL part
  3210.     Public Shared Function ew_GetMultiSearchSqlPart(ByRef Fld As Object, FldVal As String) As String
  3211.         Return Fld.FldExpression & " LIKE '" & ew_AdjustSql(FldVal) & ",%' OR " & Fld.FldExpression & " LIKE '%," & FldVal & ",%' OR " & Fld.FldExpression & " LIKE '%," & FldVal & "'"
  3212.     End Function
  3213.  
  3214.     ' Get search sql
  3215.     Public Shared Function ew_GetSearchSql(ByRef Fld As Object, FldVal As String, FldOpr As String, FldCond As String, FldVal2 As String, FldOpr2 As String) As String
  3216.         Dim IsValidValue As Boolean
  3217.         Dim sSql As String = ""
  3218.         If FldOpr = "BETWEEN" Then
  3219.             IsValidValue = (Fld.FldDataType <> EW_DATATYPE_NUMBER) OrElse (Fld.FldDataType = EW_DATATYPE_NUMBER AndAlso IsNumeric(FldVal) AndAlso IsNumeric(FldVal2))
  3220.             If FldVal <> "" AndAlso FldVal2 <> "" AndAlso IsValidValue Then
  3221.                 sSql = Fld.FldExpression & " BETWEEN " & ew_QuotedValue(FldVal, Fld.FldDataType) & " AND " & ew_QuotedValue(FldVal2, Fld.FldDataType)
  3222.             End If
  3223.         ElseIf FldOpr = "IS NULL" OrElse FldOpr = "IS NOT NULL" Then 
  3224.             sSql = Fld.FldExpression & " " & FldOpr
  3225.         Else
  3226.             IsValidValue = (Fld.FldDataType <> EW_DATATYPE_NUMBER) OrElse (Fld.FldDataType = EW_DATATYPE_NUMBER AndAlso IsNumeric(FldVal))
  3227.             If FldVal <> "" AndAlso IsValidValue AndAlso ew_IsValidOpr(FldOpr, Fld.FldDataType) Then
  3228.                 sSql = Fld.FldExpression & ew_SearchString(FldOpr, FldVal, Fld.FldDataType)
  3229.             End If
  3230.             IsValidValue = (Fld.FldDataType <> EW_DATATYPE_NUMBER) OrElse (Fld.FldDataType = EW_DATATYPE_NUMBER AndAlso IsNumeric(FldVal2))
  3231.             If FldVal2 <> "" AndAlso IsValidValue AndAlso ew_IsValidOpr(FldOpr2, Fld.FldDataType) Then
  3232.                 If sSql <> "" Then
  3233.                     sSql = sSql & " " & IIf(FldCond = "OR", "OR", "AND") & " "
  3234.                 End If
  3235.                 sSql = "(" & sSql & Fld.FldExpression & ew_SearchString(FldOpr2, FldVal2, Fld.FldDataType) & ")"
  3236.             End If
  3237.         End If
  3238.         Return sSql
  3239.     End Function
  3240.  
  3241.     ' Return search string
  3242.     Public Shared Function ew_SearchString(FldOpr As String, FldVal As String, FldType As Integer) As String
  3243.         If FldOpr = "LIKE" OrElse FldOpr = "NOT LIKE" Then
  3244.             Return " " & FldOpr & " " & ew_QuotedValue("%" & FldVal & "%", FldType)
  3245.         ElseIf FldOpr = "STARTS WITH" Then 
  3246.             Return " LIKE " & ew_QuotedValue(FldVal & "%", FldType)
  3247.         Else
  3248.             Return " " & FldOpr & " " & ew_QuotedValue(FldVal, FldType)
  3249.         End If
  3250.     End Function
  3251.  
  3252.     ' Check if valid operator
  3253.     Public Shared Function ew_IsValidOpr(Opr As String, FldType As Integer) As Boolean
  3254.         Dim Valid As Boolean = (Opr = "=" OrElse Opr = "<" OrElse Opr = "<=" OrElse Opr = ">" OrElse Opr = ">=" OrElse Opr = "<>")
  3255.         If FldType = EW_DATATYPE_STRING OrElse FldType = EW_DATATYPE_MEMO Then
  3256.             Valid = Valid OrElse Opr = "LIKE" OrElse Opr = "NOT LIKE" OrElse Opr = "STARTS WITH"
  3257.         End If
  3258.         Return Valid
  3259.     End Function    
  3260.  
  3261.     ' Quoted value for field type
  3262.     Public Shared Function ew_QuotedValue(Value As Object, FldType As Integer) As String
  3263.         Value = Convert.ToString(Value)
  3264.         Select Case FldType
  3265.             Case EW_DATATYPE_STRING, EW_DATATYPE_MEMO
  3266.                 Return "'" & ew_AdjustSql(Value) & "'"
  3267.             Case EW_DATATYPE_GUID
  3268.                 If EW_IS_MSACCESS Then
  3269.                     If Value.StartsWith("{") Then
  3270.                         Return Value
  3271.                     Else
  3272.                         Return "{" & ew_AdjustSql(Value) & "}"
  3273.                     End If
  3274.                 Else
  3275.                     Return "'" & ew_AdjustSql(Value) & "'"
  3276.                 End If
  3277.             Case EW_DATATYPE_DATE, EW_DATATYPE_TIME
  3278.                 If EW_IS_MSACCESS Then
  3279.                     Return "#" & ew_AdjustSql(Value) & "#"
  3280.                 Else
  3281.                     Return "'" & ew_AdjustSql(Value) & "'"
  3282.                 End If
  3283.             Case Else
  3284.                 Return Value
  3285.         End Select
  3286.     End Function    
  3287.  
  3288.     ' Pad zeros before number
  3289.     Public Shared Function ew_ZeroPad(m As Object, t As Integer) As String
  3290.         Return Convert.ToString(m).PadLeft(t, "0"c)
  3291.     End Function    
  3292.  
  3293.     ' Convert numeric value
  3294.     Public Shared Function ew_Conv(v As Object, t As Integer) As Object    
  3295.         If IsDbNull(v) Then Return System.DBNull.Value
  3296.         Select Case t            
  3297.             Case 20 ' adBigInt
  3298.                 Return Convert.ToInt64(v)
  3299.             Case 21 ' adUnsignedBigInt
  3300.                 Return Convert.ToUInt64(v)                
  3301.             Case 2, 16 ' adSmallInt/adTinyInt
  3302.                 Return Convert.ToInt16(v)
  3303.             Case 3 ' adInteger
  3304.                 Return Convert.ToInt32(v)
  3305.             Case 17, 18 ' adUnsignedTinyInt/adUnsignedSmallInt
  3306.                 Return Convert.ToUInt16(v)
  3307.             Case 19 ' adUnsignedInt
  3308.                 Return Convert.ToUInt32(v)
  3309.             Case 4 ' adSingle
  3310.                 Return Convert.ToSingle(v)                
  3311.             Case 5, 6, 131 ' adDouble/adCurrency/adNumeric
  3312.                 Return Convert.ToDouble(v)
  3313.             Case Else
  3314.                 Return v
  3315.         End Select
  3316.     End Function    
  3317.  
  3318.     ' Public Shared Function for debug
  3319.     Public Shared Sub ew_Trace(Msg As Object)
  3320.         Try
  3321.             Dim FileName as String = HttpContext.Current.Server.MapPath("debug.txt")   
  3322.         Dim sw as StreamWriter = File.AppendText(FileName)   
  3323.         sw.WriteLine(Convert.ToString(Msg))   
  3324.         sw.Close()
  3325.         Catch
  3326.             If EW_DEBUG_ENABLED Then Throw        
  3327.         End Try       
  3328.     End Sub    
  3329.  
  3330.     ' Compare values with special handling for null values
  3331.     Public Shared Function ew_CompareValue(v1 As Object, v2 As Object) As Boolean
  3332.         If IsDbNull(v1) AndAlso IsDbNull(v2) Then
  3333.             Return True
  3334.         ElseIf IsDbNull(v1) OrElse IsDbNull(v2) Then 
  3335.             Return False
  3336.         Else
  3337.             Return ew_SameStr(v1, v2)
  3338.         End If
  3339.     End Function    
  3340.  
  3341.     ' Adjust SQL for special characters
  3342.     Public Shared Function ew_AdjustSql(value As Object) As String
  3343.         Dim sWrk As String = Convert.ToString(value).Trim()
  3344.         sWrk = sWrk.Replace("'", "''") ' Adjust for Single Quote
  3345.         sWrk = sWrk.Replace("[", "[[]") ' Adjust for Open Square Bracket
  3346.         Return sWrk
  3347.     End Function    
  3348.  
  3349.     ' Build select SQL based on different SQL part
  3350.     Public Shared Function ew_BuildSelectSql(sSelect As String, sWhere As String, sGroupBy As String, sHaving As String, sOrderBy As String, sFilter As String, sSort As String) As String
  3351.         Dim sSql As String, sDbOrderBy As String
  3352.         Dim sDbWhere As String = sWhere
  3353.         If sDbWhere <> "" Then
  3354.             If sFilter <> "" Then sDbWhere = "(" & sDbWhere & ") AND (" & sFilter & ")"
  3355.         Else
  3356.             sDbWhere = sFilter
  3357.         End If
  3358.         sDbOrderBy = sOrderBy
  3359.         If sSort <> "" Then
  3360.             sDbOrderBy = sSort
  3361.         End If
  3362.         sSql = sSelect
  3363.         If sDbWhere <> "" Then
  3364.             sSql = sSql & " WHERE " & sDbWhere
  3365.         End If
  3366.         If sGroupBy <> "" Then
  3367.             sSql = sSql & " GROUP BY " & sGroupBy
  3368.         End If
  3369.         If sHaving <> "" Then
  3370.             sSql = sSql & " HAVING " & sHaving
  3371.         End If
  3372.         If sDbOrderBy <> "" Then
  3373.             sSql = sSql & " ORDER BY " & sDbOrderBy
  3374.         End If
  3375.         Return sSql
  3376.     End Function    
  3377.  
  3378.     ' Load a text file
  3379.     Public Shared Function ew_LoadTxt(fn As String) As String
  3380.         Dim sTxt As String = ""
  3381.         If ew_NotEmpty(fn) Then
  3382.             Dim sw as StreamReader = File.OpenText(fn)   
  3383.         sTxt = sw.ReadToEnd()      
  3384.         sw.Close()
  3385.             End If
  3386.         Return sTxt 
  3387.     End Function    
  3388.  
  3389.     ' Write audit trail (login/logout)
  3390.     Public Shared Sub ew_WriteAuditTrailOnLogInOut(logtype As String, username As String)
  3391.         Try
  3392.             Dim field As String = "", oldvalue As String = "", keyvalue As String = "", newvalue As String = ""
  3393.             Dim filePfx As String = "log"
  3394.             Dim dt As DateTime = DateTime.Now()
  3395.             Dim curDate As String = dt.ToString("yyyy/MM/dd")
  3396.             Dim curTime As String = dt.ToString("HH:mm:ss")
  3397.             Dim table As String = logtype    
  3398.             Dim id As String = HttpContext.Current.Request.ServerVariables("SCRIPT_NAME")            
  3399.             ew_WriteAuditTrail(filePfx, curDate, curTime, id, username, logtype, table, field, keyvalue, oldvalue, newvalue)
  3400.         Catch
  3401.             If EW_DEBUG_ENABLED Then Throw    
  3402.         End Try
  3403.     End Sub    
  3404.  
  3405.     ' Write audit trail (insert/update/delete)
  3406.     Public Shared Sub ew_WriteAuditTrail(pfx As String, curDate As String, curTime As String, id As String, user As String, action As String, table As String, field As String, keyvalue As Object, oldvalue As Object, newvalue As Object)
  3407.         Try            
  3408.             Dim userwrk As String = user
  3409.             If userwrk = "" Then userwrk = "-1" ' assume Administrator if no user
  3410.  
  3411.             ' Write audit trail to log file
  3412.             Dim sHeader As String = "date" & vbTab & "time" & vbTab & "id" & vbTab & "user" & vbTab & "action" & vbTab & "table" & vbTab & "field" & vbTab & "key value" & vbTab & "old value" & vbTab & "new value"
  3413.             Dim sMsg As String = curDate & vbTab & curTime & vbTab & id & vbTab & userwrk & vbTab & action & vbTab & table & vbTab & field & vbTab & Convert.ToString(keyvalue) & vbTab & Convert.ToString(oldvalue) & vbTab & Convert.ToString(newvalue)
  3414.             Dim sFolder As String = EW_AUDIT_TRAIL_PATH            
  3415.             Dim sFn As String = pfx & "_" & DateTime.Now.ToString("yyyyMMdd") & ".txt"            
  3416.             Dim bWriteHeader As Boolean = Not File.Exists(ew_UploadPathEx(True, sFolder) & sFn)
  3417.             Dim sw as StreamWriter = File.AppendText(ew_UploadPathEx(True, sFolder) & sFn)
  3418.             If bWriteHeader Then sw.WriteLine(sHeader) 
  3419.             sw.WriteLine(sMsg)
  3420.             sw.Close()
  3421.  
  3422.             ' Sample code to write audit trail to database  (e.g. MS Access)
  3423.             ' Dim sAuditSql As String = "INSERT INTO AuditTrailTable" & _
  3424.             '    " ([date], [time], [id], [user], [action], [table], [field], [keyvalue], [oldvalue], [newvalue])" & _
  3425.             '    " VALUES (" & _
  3426.             '     "#" & ew_AdjustSql(curDate) & "#, " & _
  3427.             '    "#" & ew_AdjustSql(curTime) & "#, " & _
  3428.             '    """" & ew_AdjustSql(id) & """, " & _
  3429.             '     """" & ew_AdjustSql(userwrk) & """, " & _
  3430.             '    """" & ew_AdjustSql(action) & """, " & _
  3431.             '    """" & ew_AdjustSql(table) & """, " & _
  3432.             '    """" & ew_AdjustSql(field) & """, " & _
  3433.             '    """" & ew_AdjustSql(keyvalue) & """, " & _
  3434.             '    """" & ew_AdjustSql(oldvalue) & """, " & _
  3435.             '    """" & ew_AdjustSql(newvalue) & """)"
  3436.             '    ' ew_Write(sAuditSql) ' uncomment to debug
  3437.             '    ' ew_End()
  3438.             ' ew_Execute(sAuditSql) 
  3439.  
  3440.         Catch
  3441.             If EW_DEBUG_ENABLED Then Throw    
  3442.         End Try        
  3443.     End Sub
  3444.  
  3445.     ' Functions for default date format
  3446.     ' ANamedFormat = 0-8, where 0-4 same as VBScript
  3447.     ' 5 = "yyyymmdd"
  3448.     ' 6 = "mmddyyyy"
  3449.     ' 7 = "ddmmyyyy"
  3450.     ' 8 = Short Date + Short Time
  3451.     ' 9 = "yyyymmdd HH:MM:SS"
  3452.     ' 10 = "mmddyyyy HH:MM:SS"
  3453.     ' 11 = "ddmmyyyy HH:MM:SS"
  3454.     ' 12 = "HH:MM:SS"
  3455.     ' 14 = "HH::MM AM/PM"
  3456.     ' Format date time based on format type
  3457.     Public Shared Function ew_FormatDateTime(ADate As Object, ANamedFormat As Integer) As String
  3458.         Dim sDT As String
  3459.         If IsDate(ADate) Then
  3460.             Dim DT As DateTime = Convert.ToDateTime(ADate)
  3461.             If ANamedFormat >= 0 AndAlso ANamedFormat <= 4 Then
  3462.                 sDT = FormatDateTime(ADate, ANamedFormat)
  3463.             ElseIf ANamedFormat = 5 OrElse ANamedFormat = 9 Then 
  3464.                 sDT = DT.Year & EW_DATE_SEPARATOR & DT.Month & EW_DATE_SEPARATOR & DT.Day
  3465.             ElseIf ANamedFormat = 6 OrElse ANamedFormat = 10 Then 
  3466.                 sDT = DT.Month & EW_DATE_SEPARATOR & DT.Day & EW_DATE_SEPARATOR & DT.Year
  3467.             ElseIf ANamedFormat = 7 OrElse ANamedFormat = 11 Then 
  3468.                 sDT = DT.Day & EW_DATE_SEPARATOR & DT.Month & EW_DATE_SEPARATOR & DT.Year
  3469.             ElseIf ANamedFormat = 8 Then 
  3470.                 sDT = FormatDateTime(ADate, 2)
  3471.                 If DT.Hour <> 0 OrElse DT.Minute <> 0 OrElse DT.Second <> 0 Then
  3472.                     sDT = sDT & " " & DT.ToString("HH:mm:ss")
  3473.                 End If
  3474.             ElseIf ANamedFormat = 12 Then 
  3475.                 sDT = DT.ToString("HH:mm:ss")
  3476.             ElseIf ANamedFormat = 14 Then
  3477.                 Dim Time1 As String, Time1Part As String(), Time1Split As String()
  3478.                 Time1 = FormatDateTime(ADate, 3)
  3479.                 Time1Part = Split(Time1, " ")
  3480.                 Time1Split = Split(Time1Part(0), ":")
  3481.                 sDT = Time1Split(0) & ":" & Time1Split(1) & " " & Time1Part(1)
  3482.             Else
  3483.                 Return Convert.ToString(DT)
  3484.             End If
  3485.             If ANamedFormat >= 9 AndAlso ANamedFormat <= 11 Then
  3486.                 sDT = sDT & " " & DT.ToString("HH:mm:ss")
  3487.             End If
  3488.             Return sDT
  3489.         Else
  3490.             Return Convert.ToString(ADate)
  3491.         End If
  3492.     End Function    
  3493.  
  3494.     ' Unformat date time based on format type
  3495.     Public Shared Function ew_UnFormatDateTime(ADate As Object, ANamedFormat As Integer) As String
  3496.         Dim arDate() As String, arDateTime() As String
  3497.         Dim d As DateTime
  3498.         Dim sDT As String
  3499.         ADate = Convert.ToString(ADate).Trim()
  3500.         While ADate.Contains("  ")
  3501.             ADate = ADate.Replace("  ", " ")
  3502.         End While
  3503.         arDateTime = ADate.Split(New Char() {" "c})
  3504.         If ANamedFormat = 0 AndAlso IsDate(ADate) Then
  3505.             d = Convert.ToDateTime(arDateTime(0))
  3506.             sDT = d.ToString("yyyy/MM/dd")
  3507.             If arDateTime.GetUpperBound(0) > 0 Then
  3508.                 For i As Integer = 1 To arDateTime.GetUpperBound(0)
  3509.                     sDT = sDT & " " & arDateTime(i)
  3510.                 Next 
  3511.             End If
  3512.             Return sDT
  3513.         Else
  3514.             arDate = arDateTime(0).Split(New Char() {Convert.ToChar(EW_DATE_SEPARATOR)})
  3515.             If arDate.GetUpperBound(0) = 2 Then
  3516.                 sDT = arDateTime(0)
  3517.                 If ANamedFormat = 6 OrElse ANamedFormat = 10 Then ' mmddyyyy
  3518.                     If arDate(0).Length <= 2 AndAlso arDate(1).Length <= 2 AndAlso arDate(2).Length <= 4 Then
  3519.                         sDT = arDate(2) & "/" & arDate(0) & "/" & arDate(1)
  3520.                     End If
  3521.                 ElseIf ANamedFormat = 7 OrElse ANamedFormat = 11 Then  ' ddmmyyyy
  3522.                     If arDate(0).Length <= 2 AndAlso arDate(1).Length <= 2 AndAlso arDate(2).Length <= 4 Then
  3523.                         sDT = arDate(2) & "/" & arDate(1) & "/" & arDate(0)
  3524.                     End If
  3525.                 ElseIf ANamedFormat = 5 OrElse ANamedFormat = 9 Then  ' yyyymmdd
  3526.                     If arDate(0).Length <= 4 AndAlso arDate(1).Length <= 2 AndAlso arDate(2).Length <= 2 Then
  3527.                         sDT = arDate(0) & "/" & arDate(1) & "/" & arDate(2)
  3528.                     End If
  3529.                 End If
  3530.                 If arDateTime.GetUpperBound(0) > 0 Then
  3531.                     If IsDate(arDateTime(1)) Then ' Is time
  3532.                         sDT = sDT & " " & arDateTime(1)
  3533.                     End If
  3534.                 End If
  3535.                 Return sDT
  3536.             Else
  3537.                 Return ADate.ToString()
  3538.             End If
  3539.         End If
  3540.     End Function    
  3541.  
  3542.     ' Format currency
  3543.     Public Shared Function ew_FormatCurrency(Expression As Object, NumDigitsAfterDecimal As Integer, IncludeLeadingDigit As Integer, UseParensForNegativeNumbers As Integer, GroupDigits As Integer) As String
  3544.         If Not Information.IsNumeric(Expression) Then Return Convert.ToString(Expression)
  3545.         If IsDbNull(Expression) Then Return String.Empty         
  3546.         Return Strings.FormatCurrency(Expression, NumDigitsAfterDecimal, IncludeLeadingDigit, UseParensForNegativeNumbers, GroupDigits)
  3547.     End Function    
  3548.  
  3549.     ' Format number
  3550.     Public Shared Function ew_FormatNumber(Expression As Object, NumDigitsAfterDecimal As Integer, IncludeLeadingDigit As Integer, UseParensForNegativeNumbers As Integer, GroupDigits As Integer) As String
  3551.         If Not Information.IsNumeric(Expression) Then Return Convert.ToString(Expression)
  3552.         If IsDbNull(Expression) Then Return String.Empty
  3553.         Return Strings.FormatNumber(Expression, NumDigitsAfterDecimal, IncludeLeadingDigit, UseParensForNegativeNumbers, GroupDigits)
  3554.     End Function    
  3555.  
  3556.     ' Format percent
  3557.     Public Shared Function ew_FormatPercent(Expression As Object, NumDigitsAfterDecimal As Integer, IncludeLeadingDigit As Integer, UseParensForNegativeNumbers As Integer, GroupDigits As Integer) As String
  3558.         If Not Information.IsNumeric(Expression) Then Return Convert.ToString(Expression)
  3559.         If IsDbNull(Expression) Then Return String.Empty
  3560.         Return Strings.FormatPercent(Expression, NumDigitsAfterDecimal, IncludeLeadingDigit, UseParensForNegativeNumbers, GroupDigits)        
  3561.     End Function    
  3562.  
  3563.     ' Encode HTML
  3564.     Public Shared Function ew_HtmlEncode(Expression As Object) As String
  3565.         Return HttpContext.Current.Server.HtmlEncode(Convert.ToString(Expression))
  3566.     End Function    
  3567.  
  3568.     ' Encode value for single-quoted JavaScript string
  3569.     Public Shared Function ew_JsEncode(val As Object) As String
  3570.         Return Convert.ToString(val).Replace("'", "\'")
  3571.     End Function    
  3572.  
  3573.     ' Generate Value Separator based on current row count
  3574.     ' rowcnt - zero based row count
  3575.     Public Shared Function ew_ValueSeparator(rowcnt As Integer) As String
  3576.         Return ", "
  3577.     End Function    
  3578.  
  3579.     ' Generate View Option Separator based on current row count (Multi-Select / CheckBox)
  3580.     ' rowcnt - zero based row count
  3581.     Public Shared Function ew_ViewOptionSeparator(rowcnt As Integer) As String
  3582.         Dim Sep As String = ", "
  3583.  
  3584.         ' Sample code to adjust 2 options per row
  3585.         'If ((rowcnt + 1) Mod 2 = 0) Then ' 2 options per row
  3586.         '    Sep = Sep & "<br>"
  3587.         'End If
  3588.  
  3589.         Return Sep        
  3590.     End Function    
  3591.  
  3592.     ' Render repeat column table
  3593.     ' rowcnt - zero based row count
  3594.     Public Shared Function ew_RepeatColumnTable(totcnt As Integer, rowcnt As Integer, repeatcnt As Integer, rendertype As Integer) As String
  3595.         Dim sWrk As String = ""
  3596.         If rendertype = 1 Then ' Render control start
  3597.             If rowcnt = 0 Then sWrk = sWrk & "<table class=""" & EW_ITEM_TABLE_CLASSNAME & """>"
  3598.             If (rowcnt Mod repeatcnt = 0) Then sWrk = sWrk & "<tr>"
  3599.             sWrk = sWrk & "<td>"            
  3600.         ElseIf rendertype = 2 Then ' Render control end
  3601.             sWrk = sWrk & "</td>"            
  3602.             If (rowcnt Mod repeatcnt = repeatcnt - 1) Then
  3603.                 sWrk = sWrk & "</tr>"
  3604.             ElseIf rowcnt = totcnt Then 
  3605.                 For i As Integer = ((rowcnt Mod repeatcnt) + 1) To repeatcnt - 1
  3606.                     sWrk = sWrk & "<td> </td>"
  3607.                 Next 
  3608.                 sWrk = sWrk & "</tr>"
  3609.             End If
  3610.             If rowcnt = totcnt Then sWrk = sWrk & "</table>"
  3611.         End If
  3612.         Return sWrk
  3613.     End Function
  3614.  
  3615.     ' Truncate Memo Field based on specified length, string truncated to nearest space or CrLf
  3616.     Public Shared Function ew_TruncateMemo(str As String, ln As Integer) As String
  3617.         Dim j As Integer, i As Integer, k As Integer
  3618.         If str.Length > 0 AndAlso str.Length > ln Then
  3619.             k = 0
  3620.             Do While k >= 0 AndAlso k < str.Length
  3621.                 i = str.IndexOf(" ", k)
  3622.                 j = str.IndexOf(vbCrLf, k)
  3623.                 If i < 0 AndAlso j < 0 Then ' Unable to truncate
  3624.                     Return str
  3625.                 Else                    
  3626.  
  3627.                     ' Get nearest space or CrLf
  3628.                     If i > 0 AndAlso j > 0 Then
  3629.                         If i < j Then    k = i    Else k = j
  3630.                     ElseIf i > 0 Then 
  3631.                         k = i
  3632.                     ElseIf j > 0 Then 
  3633.                         k = j
  3634.                     End If                    
  3635.  
  3636.                     ' Get truncated text
  3637.                     If k >= ln Then
  3638.                         Return str.Substring(0, k) & "..."
  3639.                     Else
  3640.                         k = k + 1
  3641.                     End If
  3642.                 End If
  3643.             Loop 
  3644.         End If
  3645.         Return str
  3646.     End Function    
  3647.  
  3648.     ' Send notify email
  3649.     Public Shared Sub ew_SendNotifyEmail(sFn As String, sSubject As String, sTable As String, sKey As String, sAction As String)
  3650.         Try
  3651.             Dim bEmailSent As Boolean
  3652.             If EW_SENDER_EMAIL <> "" And EW_RECIPIENT_EMAIL <> "" Then
  3653.                 bEmailSent = ew_SendTemplateEmail(sFn, EW_SENDER_EMAIL, EW_RECIPIENT_EMAIL, "", "", sSubject, New String(){"<!--table-->", sTable, "<!--key-->", sKey, "<!--action-->", sAction})
  3654.             End If
  3655.         Catch
  3656.             If EW_DEBUG_ENABLED Then Throw
  3657.         End Try
  3658.     End Sub    
  3659.  
  3660.     ' Send email by template
  3661.     Public Shared Function ew_SendTemplateEmail(sTemplate As String, sSender As String, sRecipient As String, sCcEmail As String, sBccEmail As String, sSubject As String, arContent As String()) As Boolean
  3662.         Try
  3663.             Dim cnt As Integer
  3664.             Dim Email As cEmail
  3665.             If sSender <> "" AndAlso sRecipient <> "" Then
  3666.                 Email = New cEmail
  3667.                 Email.Load(sTemplate)
  3668.                 Email.ReplaceSender(sSender) ' Replace Sender
  3669.                 Email.ReplaceRecipient(sRecipient) ' Replace Recipient
  3670.                 If sCcEmail <> "" Then Email.AddCC(sCcEmail) ' Add Cc
  3671.                 If sBccEmail <> "" Then Email.AddBcc(sBccEmail) ' Add Bcc
  3672.                 If sSubject <> "" Then Email.ReplaceSubject(sSubject) ' Replace subject
  3673.                 If IsArray(arContent) Then
  3674.                     cnt = arContent.Length
  3675.                     If cnt Mod 2 = 1 Then cnt = cnt - 1
  3676.                     For i As Integer = 0 To cnt Step 2
  3677.                         Email.ReplaceContent(arContent(i), arContent(i + 1))
  3678.                     Next 
  3679.                 End If
  3680.                 Return Email.Send()
  3681.             Else
  3682.                 Return False
  3683.             End If
  3684.         Catch
  3685.             If EW_DEBUG_ENABLED Then Throw
  3686.             Return False
  3687.         End Try
  3688.     End Function    
  3689.  
  3690.     ' Send email
  3691.     Public Shared Function ew_SendEmail(ByVal sFrEmail As String, ByVal sToEmail As String, ByVal sCcEmail As String, ByVal sBccEmail As String, ByVal sSubject As String, ByVal sMail As String, ByVal sFormat As String) As Boolean
  3692.         Dim mail As New System.Net.Mail.MailMessage()
  3693.         If sFrEmail <> "" Then
  3694.             mail.From = New System.Net.Mail.MailAddress(sFrEmail)
  3695.         End If
  3696.         If sToEmail <> "" Then
  3697.             sToEmail = sToEmail.Replace(","c, ";"c)
  3698.             Dim arTo() As String = sToEmail.Split(New Char() {";"c})
  3699.             For Each strTo As String In arTo
  3700.                 mail.To.Add(strTo)
  3701.             Next
  3702.         End If
  3703.         If sCcEmail <> "" Then
  3704.             sCcEmail = sCcEmail.Replace(","c, ";"c)
  3705.             Dim arCC() As String = sCcEmail.Split(New Char() {";"c})
  3706.             For Each strCC As String In arCC
  3707.                 mail.CC.Add(strCC)
  3708.             Next
  3709.         End If
  3710.         If sBccEmail <> "" Then
  3711.             sBccEmail = sBccEmail.Replace(","c, ";"c)
  3712.             Dim arBcc() As String = sBccEmail.Split(New Char() {";"c})
  3713.             For Each strBcc As String In arBcc
  3714.                 mail.Bcc.Add(strBcc)
  3715.             Next
  3716.         End If
  3717.         mail.Subject = sSubject
  3718.         mail.Body = sMail
  3719.         mail.IsBodyHtml = ew_SameText(sFormat, "html")
  3720.         Dim smtp As New System.Net.Mail.SmtpClient()
  3721.         If EW_SMTP_SERVER <> "" Then
  3722.             smtp.Host = EW_SMTP_SERVER
  3723.         Else
  3724.             smtp.Host = "localhost"
  3725.         End If
  3726.         If EW_SMTP_SERVER_PORT > 0 Then
  3727.             smtp.Port = EW_SMTP_SERVER_PORT
  3728.         End If
  3729.         If EW_SMTP_SERVER_USERNAME <> "" AndAlso EW_SMTP_SERVER_PASSWORD <> "" Then
  3730.             Dim smtpuser As New System.Net.NetworkCredential()
  3731.             smtpuser.UserName = EW_SMTP_SERVER_USERNAME
  3732.             smtpuser.Password = EW_SMTP_SERVER_PASSWORD
  3733.             smtp.UseDefaultCredentials = False
  3734.             smtp.Credentials = smtpuser
  3735.         End If
  3736.         Try
  3737.             smtp.Send(mail)
  3738.             Return True
  3739.         Catch ex As Exception
  3740.             ew_Session("ew_SendMail_result") = ex.Message
  3741.             If EW_DEBUG_ENABLED Then Throw
  3742.             Return False
  3743.         End Try
  3744.     End Function
  3745.  
  3746.     ' Return path of the uploaded file
  3747.     '    Parameter: If PhyPath is true(1), return physical path on the server
  3748.     '               If PhyPath is false(0), return relative URL
  3749.     Public Shared Function ew_UploadPathEx(PhyPath As String, DestPath As String) As String
  3750.         Dim pos As Integer, Path As String
  3751.         If DestPath.StartsWith("~/") Then DestPath = DestPath.Substring(2)
  3752.         If PhyPath Then                
  3753.             Path = HttpContext.Current.Request.ServerVariables("APPL_PHYSICAL_PATH")
  3754.             Path = ew_IncludeTrailingDelimiter(Path, PhyPath)
  3755.             Path = Path & DestPath.Replace("/", "\")
  3756.         Else
  3757.             Path = HttpContext.Current.Request.ServerVariables("APPL_MD_PATH")
  3758.             pos = Path.IndexOf("Root", StringComparison.InvariantCultureIgnoreCase)
  3759.             If pos > 0 Then Path = Path.Substring(pos + 4)
  3760.             Path = ew_IncludeTrailingDelimiter(Path, PhyPath)
  3761.             Path = Path & DestPath
  3762.         End If
  3763.         Return ew_IncludeTrailingDelimiter(Path, PhyPath)
  3764.     End Function    
  3765.  
  3766.     ' Change the file name of the uploaded file
  3767.     Public Shared Function ew_UploadFileNameEx(folder As String, FileName As String) As String
  3768.         Dim OutFileName As String
  3769.  
  3770.         ' By default, ewUniqueFileName() is used to get an unique file name.
  3771.         ' Amend your logic here
  3772.  
  3773.         OutFileName = ew_UniqueFileName(folder, FileName)
  3774.  
  3775.         ' Return computed output file name
  3776.         Return OutFileName
  3777.     End Function    
  3778.  
  3779.     ' Return path of the uploaded file
  3780.     ' returns global upload folder, for backward compatibility only
  3781.     Public Shared Function ew_UploadPath(PhyPath As Boolean) As String
  3782.         Return ew_UploadPathEx(PhyPath, EW_UPLOAD_DEST_PATH)
  3783.     End Function    
  3784.  
  3785.     ' Change the file name of the uploaded file
  3786.     ' use global upload folder, for backward compatibility only
  3787.     Public Shared Function ew_UploadFileName(FileName As String) As String
  3788.         Return ew_UploadFileNameEx(ew_UploadPath(True), FileName)
  3789.     End Function    
  3790.  
  3791.     ' Generate an unique file name (filename(n).ext)
  3792.     Public Shared Function ew_UniqueFileName(folder As String, FileName As String) As String
  3793.         If FileName = "" Then FileName = ew_DefaultFileName()
  3794.         If FileName = "." Then Throw New Exception("Invalid file name: " & FileName)
  3795.         If folder = "" Then Throw New Exception("Unspecified folder")
  3796.         Dim Name As String = Path.GetFileNameWithoutExtension(FileName)
  3797.         Dim Ext As String = Path.GetExtension(FileName)
  3798.         folder = ew_IncludeTrailingDelimiter(folder, True)
  3799.         If Not Directory.Exists(folder) AndAlso Not ew_CreateFolder(folder) Then
  3800.             Throw New Exception("Folder does not exist: " & folder)
  3801.         End If
  3802.         Dim Index As Integer = 0
  3803.         Dim Suffix As String = ""        
  3804.  
  3805.         ' Check to see if filename exists
  3806.         While File.Exists(folder & Name & Suffix & Ext)
  3807.             Index = Index + 1
  3808.             Suffix = "(" & Index & ")"            
  3809.         End While
  3810.  
  3811.         ' Return unique file name
  3812.         Return Name & Suffix & Ext
  3813.     End Function    
  3814.  
  3815.     ' Create a default file name (yyyymmddhhmmss.bin)
  3816.     Public Shared Function ew_DefaultFileName() As Object
  3817.         Dim DT As DateTime = DateTime.Now()
  3818.         Return DT.ToString("yyyyMMddHHmmss") & ".bin"
  3819.     End Function    
  3820.  
  3821.     ' Get path relative to application root
  3822.     Public Shared Function ew_ServerMapPath(Path As String) As String
  3823.         Return ew_PathCombine(HttpContext.Current.Request.ServerVariables("APPL_PHYSICAL_PATH"), Path, True)
  3824.     End Function
  3825.  
  3826.     ' Get path relative to a base path
  3827.     Public Shared Function ew_PathCombine(BasePath As String, RelPath As String, PhyPath As Boolean) As String
  3828.         Dim p2 As Integer, p1 As Integer
  3829.         Dim Path2 As String, Path As String, Delimiter As String
  3830.         BasePath = ew_RemoveTrailingDelimiter(BasePath, True)
  3831.         If PhyPath Then
  3832.             Delimiter = "\"
  3833.             RelPath = RelPath.Replace("/", "\")
  3834.         Else
  3835.             Delimiter = "/"
  3836.             RelPath = RelPath.Replace("\", "/")
  3837.         End If
  3838.         If RelPath = "." Or RelPath = ".." Then RelPath = RelPath & Delimiter
  3839.         p1 = RelPath.IndexOf(Delimiter)
  3840.         Path2 = ""
  3841.         While p1 > -1
  3842.             Path = RelPath.Substring(0, p1 + 1)
  3843.             If Path = Delimiter OrElse Path = "." & Delimiter Then                
  3844.  
  3845.                 ' Skip
  3846.             ElseIf Path = ".." & Delimiter Then 
  3847.                 p2 = BasePath.LastIndexOf(Delimiter)
  3848.                 If p2 > -1 Then BasePath = BasePath.Substring(0, p2)
  3849.             Else
  3850.                 Path2 = Path2 & Path
  3851.             End If
  3852.             RelPath = RelPath.Substring(p1 + 1)
  3853.             p1 = RelPath.IndexOf(Delimiter)
  3854.         End While
  3855.         Return ew_IncludeTrailingDelimiter(BasePath, True) & Path2 & RelPath
  3856.     End Function    
  3857.  
  3858.     ' Remove the last delimiter for a path
  3859.     Public Shared Function ew_RemoveTrailingDelimiter(Path As String, ByVal PhyPath As Boolean) As String
  3860.         Dim Delimiter As String
  3861.         If PhyPath Then Delimiter = "\" Else Delimiter = "/"
  3862.         While Path.EndsWith(Delimiter)
  3863.             Path = Path.Substring(0, Path.Length - 1)
  3864.         End While
  3865.         Return Path
  3866.     End Function    
  3867.  
  3868.     ' Include the last delimiter for a path
  3869.     Public Shared Function ew_IncludeTrailingDelimiter(Path As String, PhyPath As Boolean) As String
  3870.         Dim Delimiter As String
  3871.         Path = ew_RemoveTrailingDelimiter(Path, PhyPath)
  3872.         If PhyPath Then Delimiter = "\" Else Delimiter = "/"
  3873.         Return Path & Delimiter
  3874.     End Function    
  3875.  
  3876.     ' Write the paths for config/debug only
  3877.     Public Shared Sub ew_WriteUploadPaths()
  3878.         ew_Write("APPL_PHYSICAL_PATH = " & HttpContext.Current.Request.ServerVariables("APPL_PHYSICAL_PATH") & "<br>")
  3879.         ew_Write("APPL_MD_PATH = " & HttpContext.Current.Request.ServerVariables("APPL_MD_PATH") & "<br>")
  3880.     End Sub    
  3881.  
  3882.     ' Get current page name
  3883.     Public Shared Function ew_CurrentPage() As String
  3884.         Return ew_GetPageName(HttpContext.Current.Request.ServerVariables("SCRIPT_NAME"))
  3885.     End Function    
  3886.  
  3887.     ' Get refer page name
  3888.     Public Shared Function ew_ReferPage() As String
  3889.         Return ew_GetPageName(HttpContext.Current.Request.ServerVariables("HTTP_REFERER"))
  3890.     End Function    
  3891.  
  3892.     ' Get page name
  3893.     Public Shared Function ew_GetPageName(url As String) As String
  3894.         If url <> "" Then
  3895.             If url.Contains("?") Then
  3896.                 url = url.Substring(0, url.LastIndexOf("?")) ' Remove querystring first
  3897.             End If
  3898.             Return url.Substring(url.LastIndexOf("/") + 1) ' Remove path
  3899.         Else
  3900.             Return ""
  3901.         End If
  3902.     End Function    
  3903.  
  3904.     ' Get full URL
  3905.     Public Shared Function ew_FullUrl() As String
  3906.         Dim sUrl As String = "http"
  3907.         Dim bSSL As Boolean = Not ew_SameText(HttpContext.Current.Request.ServerVariables("HTTPS"), "off")
  3908.         Dim sPort As String = HttpContext.Current.Request.ServerVariables("SERVER_PORT")
  3909.         Dim defPort As String = IIf(bSSL, "443", "80")
  3910.         If sPort = defPort Then sPort = "" Else sPort = ":" & sPort
  3911.         If bSSL Then sUrl = sUrl & "s"
  3912.         Return sUrl & "://" & HttpContext.Current.Request.ServerVariables("SERVER_NAME") & sPort & HttpContext.Current.Request.ServerVariables("SCRIPT_NAME")
  3913.     End Function    
  3914.  
  3915.     ' Convert to full URL
  3916.     Public Shared Function ew_ConvertFullUrl(url As String) As String        
  3917.         If url = "" Then
  3918.             Return ""
  3919.         ElseIf url.Contains("://") Then 
  3920.             Return url
  3921.         Else
  3922.             Dim sUrl As String = ew_FullUrl()
  3923.             Return sUrl.Substring(0, sUrl.LastIndexOf("/")+1) & url
  3924.         End If
  3925.     End Function    
  3926.  
  3927.     ' Check if folder exists
  3928.     Public Shared Function ew_FolderExists(folder As String) As Boolean
  3929.         Return Directory.Exists(folder)    
  3930.     End Function    
  3931.  
  3932.     ' Check if file exists
  3933.     Public Shared Function ew_FileExists(folder As String, fn As String) As Boolean
  3934.         Return File.Exists(folder & fn)
  3935.     End Function    
  3936.  
  3937.     ' Delete file
  3938.     Public Shared Sub ew_DeleteFile(FilePath As String)
  3939.         File.Delete(FilePath)
  3940.     End Sub    
  3941.  
  3942.     ' Rename file
  3943.     Sub ew_RenameFile(OldFilePath As String, NewFilePath As String)
  3944.         File.Move(OldFilePath, NewFilePath)    
  3945.     End Sub    
  3946.  
  3947.     ' Create folder
  3948.     Public Shared Function ew_CreateFolder(folder As String) As Boolean
  3949.         Try
  3950.             Dim di As DirectoryInfo = Directory.CreateDirectory(folder)
  3951.             Return (di IsNot Nothing)
  3952.         Catch
  3953.             Return False
  3954.         End Try
  3955.     End Function
  3956.  
  3957.     ' Export header
  3958.     Public Shared Function ew_ExportHeader(ExpType As String) As String
  3959.         Select Case ExpType
  3960.             Case "html"
  3961.                 Return "<link rel=""stylesheet"" type=""text/css"" href=""" & EW_PROJECT_CSSFILE & """>" & _
  3962.                     "<table class=""ewExportTable"">"
  3963.             Case "word", "excel"
  3964.                 Return "<table>"
  3965.             Case Else ' "csv"
  3966.                 Return ""
  3967.         End Select
  3968.     End Function    
  3969.  
  3970.     ' Export footer
  3971.     Public Shared Function ew_ExportFooter(ExpType As String) As String
  3972.         Select Case ExpType
  3973.             Case "html", "word", "excel"
  3974.                 Return "</table>"
  3975.             Case Else ' "csv"
  3976.                 Return ""
  3977.         End Select
  3978.     End Function    
  3979.  
  3980.     ' Export value
  3981.     Public Shared Sub ew_ExportAddValue(ByRef str As String, val As Object, ExpType As String)
  3982.         Select Case ExpType
  3983.             Case "html", "word", "excel"
  3984.                 str = str & "<td>" & Convert.ToString(val) & "</td>"
  3985.             Case "csv"
  3986.                 If str <> "" Then str = str & ","
  3987.                 str = str & """" & Convert.ToString(val).Replace("""", """""") & """"
  3988.         End Select
  3989.     End Sub    
  3990.  
  3991.     ' Export line
  3992.     Public Shared Function ew_ExportLine(str As String, ExpType As String) As String
  3993.         Select Case ExpType
  3994.             Case "html", "word", "excel"
  3995.                 Return "<tr>" & str & "</tr>"
  3996.             Case Else ' "csv"
  3997.                 Return str & vbCrLf
  3998.         End Select
  3999.     End Function    
  4000.  
  4001.     ' Export field
  4002.     Public Shared Function ew_ExportField(cap As String, val As Object, ExpType As String) As String
  4003.         Return "<tr><td>" & cap & "</td><td>" & Convert.ToString(val) & "</td></tr>"
  4004.     End Function        
  4005.  
  4006.     '
  4007.     '  Form object class
  4008.     '
  4009.     Class cFormObj
  4010.  
  4011.         Public Index As Integer = 0 ' Index to handle multiple form elements
  4012.  
  4013.         ' Get form element name based on index
  4014.         Public Function GetIndexedName(name As String) As String
  4015.             Dim Pos As Integer
  4016.             If Index <= 0 Then
  4017.                 Return name
  4018.             Else
  4019.                 Pos = name.IndexOf("_")
  4020.                 If Pos = 1 OrElse Pos = 2 Then
  4021.                     Return name.Substring(0, Pos) & Index & name.Substring(Pos)
  4022.                 Else
  4023.                     Return name
  4024.                 End If
  4025.             End If
  4026.         End Function
  4027.  
  4028.         ' Get value for form element
  4029.         Public Function GetValue(name As String) As Object
  4030.             Dim wrkname As String = GetIndexedName(name)
  4031.             Return ew_Post(wrkname)            
  4032.         End Function
  4033.  
  4034.         ' Get upload file size
  4035.         Function GetUploadFileSize(name As String) As Long
  4036.             Dim wrkname As String = GetIndexedName(name)
  4037.             Dim file As HttpPostedFile = HttpContext.Current.Request.Files(wrkname)
  4038.             If file IsNot Nothing AndAlso file.ContentLength > 0 Then
  4039.                 Return HttpContext.Current.Request.Files(wrkname).ContentLength
  4040.             Else
  4041.                 Return -1
  4042.             End If
  4043.         End Function
  4044.  
  4045.         ' Get upload file name
  4046.         Function GetUploadFileName(name As String) As String            
  4047.             Dim wrkname As String = GetIndexedName(name)
  4048.             Dim file As HttpPostedFile = HttpContext.Current.Request.Files(wrkname)
  4049.             If file IsNot Nothing AndAlso file.ContentLength > 0 Then
  4050.                 Dim FileName As String = HttpContext.Current.Request.Files(wrkname).FileName
  4051.                 Return Path.GetFileName(FileName)
  4052.             Else
  4053.                 Return ""
  4054.             End If
  4055.         End Function
  4056.  
  4057.         ' Get file content type
  4058.         Function GetUploadFileContentType(name As String) As String
  4059.             Dim wrkname As String = GetIndexedName(name)
  4060.             Dim file As HttpPostedFile = HttpContext.Current.Request.Files(wrkname)
  4061.             If file IsNot Nothing AndAlso file.ContentLength > 0 Then
  4062.                 Return HttpContext.Current.Request.Files(wrkname).ContentType
  4063.             Else
  4064.                 Return ""
  4065.             End If
  4066.         End Function
  4067.  
  4068.         ' Get upload file data
  4069.         Function GetUploadFileData(name As String) As Object
  4070.             Dim wrkname As String = GetIndexedName(name)
  4071.             Dim file As HttpPostedFile = HttpContext.Current.Request.Files(wrkname)
  4072.             If file IsNot Nothing AndAlso file.ContentLength > 0 Then
  4073.                 Dim filelength As Integer = file.ContentLength            
  4074.                 Dim bindata(filelength) As Byte
  4075.                 Dim fs As Stream = file.InputStream
  4076.                 fs.Read(bindata, 0, filelength)                
  4077.                 Return bindata
  4078.             Else
  4079.                 Return System.DBNull.Value
  4080.             End If
  4081.         End Function
  4082.  
  4083.         ' Get file image width
  4084.         Function GetUploadImageWidth(name As String) As Integer
  4085.             Dim wrkname As String = GetIndexedName(name)
  4086.             Dim file As HttpPostedFile = HttpContext.Current.Request.Files(wrkname)
  4087.             If file IsNot Nothing AndAlso file.ContentLength > 0 Then
  4088.                 Try
  4089.                     Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(file.InputStream)
  4090.                     Return img.PhysicalDimension.Width
  4091.                 Catch
  4092.                     Return -1    
  4093.                 End Try            
  4094.             Else
  4095.                 Return -1
  4096.             End If
  4097.         End Function
  4098.  
  4099.         ' Get file image height
  4100.         Function GetUploadImageHeight(name As String) As Integer
  4101.             Dim wrkname As String = GetIndexedName(name)
  4102.             Dim file As HttpPostedFile = HttpContext.Current.Request.Files(wrkname)
  4103.             If file IsNot Nothing AndAlso file.ContentLength > 0 Then
  4104.                 Try
  4105.                     Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(file.InputStream)
  4106.                     Return img.PhysicalDimension.Height
  4107.                 Catch
  4108.                     Return -1    
  4109.                 End Try
  4110.             Else
  4111.                 Return -1
  4112.             End If
  4113.         End Function
  4114.     End Class
  4115.  
  4116.     ' Resize binary to thumbnail
  4117.     Public Shared Function ew_ResizeBinary(ByRef filedata As Byte(), ByRef width As Integer, ByRef height As Integer, interpolation As Integer) As Boolean
  4118.         Return True ' No resize
  4119.     End Function    
  4120.  
  4121.     ' Resize file to thumbnail file
  4122.     Public Shared Function ew_ResizeFile(fn As String, tn As String, ByRef width As Integer, ByRef height As Integer, interpolation As Integer) As Boolean
  4123.         Try
  4124.             If File.Exists(fn) Then
  4125.                 File.Copy(fn, tn) ' Copy only
  4126.                 Return True
  4127.             End If
  4128.         Catch
  4129.             If EW_DEBUG_ENABLED Then Throw
  4130.             Return False
  4131.         End Try        
  4132.     End Function    
  4133.  
  4134.     ' Resize file to binary
  4135.     Public Shared Function ew_ResizeFileToBinary(fn As String, ByRef width As Integer, ByRef height As Integer, interpolation As Integer) As Object
  4136.         Try
  4137.             If File.Exists(fn) Then
  4138.                 Dim oFile As FileInfo
  4139.             oFile = New FileInfo(fn)    
  4140.             Dim fs As FileStream = oFile.OpenRead()
  4141.             Dim lBytes As Long = fs.Length
  4142.                 If lBytes > 0 Then
  4143.                     Dim fileData(lBytes-1) As Byte                
  4144.                     fs.Read(fileData, 0, lBytes) ' Read the file into a byte array
  4145.                     fs.Close()
  4146.                     fs.Dispose()
  4147.                     Return fileData
  4148.                 End If              
  4149.           End If
  4150.             Return System.DBNull.Value
  4151.         Catch
  4152.             If EW_DEBUG_ENABLED Then Throw
  4153.             Return System.DBNull.Value
  4154.         End Try        
  4155.     End Function
  4156.  
  4157.     ' Menu class    
  4158.     Class cMenu
  4159.         Inherits AspNetMakerBase
  4160.  
  4161.         Public Id As Object        
  4162.  
  4163.         Public IsRoot As Boolean        
  4164.  
  4165.         Public ItemData As New ArrayList ' ArrayList of cMenuItem
  4166.  
  4167.         ' Init
  4168.         Public Sub New(AId As Object, ARoot As Boolean)
  4169.             Id = AId
  4170.             IsRoot = ARoot            
  4171.         End Sub            
  4172.  
  4173.         ' Add a menu item
  4174.         Sub AddMenuItem(id As Integer, text As String, url As String, parentid As Integer, src As String, allowed As Boolean)
  4175.             Dim oParentMenu As cMenuItem
  4176.             Dim item As New cMenuItem(id, text, url, parentid, src, allowed)
  4177.             item.ParentPage = Me.ParentPage
  4178.             If Not ParentPage.MenuItem_Adding(item) Then
  4179.                 Exit Sub
  4180.             End If
  4181.             If item.ParentId < 0 Then
  4182.                 AddItem(item)
  4183.             Else
  4184.                 If FindItem(item.ParentId, oParentMenu) Then
  4185.                     oParentMenu.AddItem(item)
  4186.                 End If
  4187.             End If
  4188.         End Sub
  4189.  
  4190.         ' Add item to internal ArrayList
  4191.         Sub AddItem(ByRef item As cMenuItem)
  4192.             ItemData.Add(item)
  4193.         End Sub
  4194.  
  4195.         ' Find item
  4196.         Function FindItem(id As Integer, ByRef out As Object) As Boolean
  4197.             Dim item As cMenuItem
  4198.             FindItem = False
  4199.             For i As Integer = 0 To ItemData.Count - 1
  4200.                 item = CType(ItemData(i), cMenuItem)
  4201.                 If item.Id = id Then
  4202.                     out = item
  4203.                     Return True
  4204.                 ElseIf Not IsDbNull(item.SubMenu) Then 
  4205.                     If item.SubMenu.FindItem(id, out) Then Return True
  4206.                 End If
  4207.             Next 
  4208.         End Function
  4209.  
  4210.         ' Check if a menu item should be shown
  4211.         Public Function RenderItem(ByVal item As cMenuItem) As Boolean
  4212.             If Not IsDbNull(item.SubMenu) Then
  4213.                 For Each subitem As cMenuItem In item.SubMenu.ItemData
  4214.                     If item.SubMenu.RenderItem(subitem) Then
  4215.                         Return True
  4216.                     End If
  4217.                 Next
  4218.             End If
  4219.             Return (item.Allowed AndAlso ew_NotEmpty(item.Url))
  4220.         End Function
  4221.  
  4222.         ' Check if this menu should be rendered
  4223.         Public Function RenderMenu() As Boolean
  4224.             For Each item As cMenuItem In ItemData
  4225.                 If RenderItem(item) Then
  4226.                     Return True
  4227.                 End If
  4228.             Next
  4229.             Return False
  4230.         End Function
  4231.  
  4232.         ' Render the menu
  4233.         Sub Render()
  4234.             If Not RenderMenu() Then Return
  4235.             Dim i As Integer
  4236.             Dim item As cMenuItem
  4237.             Dim itemcnt As Integer = ItemData.Count
  4238.             ew_Write("<ul")
  4239.             If ew_NotEmpty(Id) Then
  4240.                 If IsNumeric(Id) Then
  4241.                     ew_Write(" id=""menu_" & Id & """")
  4242.                 Else
  4243.                     ew_Write(" id=""" & Id & """")
  4244.                 End If
  4245.             End If
  4246.             If IsRoot Then
  4247.                 ew_Write(" class=""" & EW_MENUBAR_VERTICAL_CLASSNAME & """")
  4248.             End If
  4249.             ew_Write(">" & vbCrLf)
  4250.             For i = 0 To itemcnt - 1
  4251.                 If RenderItem(ItemData(i)) Then
  4252.                     ew_Write("<li><a")
  4253.                     If Not IsDbNull(ItemData(i).SubMenu) Then
  4254.                         ew_Write(" class=""" & EW_MENUBAR_SUBMENU_CLASSNAME & """")
  4255.                     End If
  4256.                     If ItemData(i).Url <> "" Then
  4257.                         ew_Write(" href=""" & ew_HtmlEncode(ItemData(i).Url) & """")
  4258.                     End If
  4259.                     ew_Write(">" & ItemData(i).Text & "</a>" & vbCrLf)
  4260.                     If Not IsDbNull(CType(ItemData(i), cMenuItem).SubMenu) Then
  4261.                         item = CType(ItemData(i), cMenuItem)
  4262.                         item.SubMenu.Render()
  4263.                     End If
  4264.                     ew_Write("</li>" & vbCrLf)
  4265.                 End If
  4266.             Next 
  4267.             ew_Write("</ul>" & vbCrLf)
  4268.         End Sub        
  4269.     End Class    
  4270.  
  4271.     ' Menu item class
  4272.     Class cMenuItem
  4273.         Inherits AspNetMakerBase
  4274.  
  4275.         Public Id As Integer
  4276.  
  4277.         Public Text As String
  4278.  
  4279.         Public Url As String
  4280.  
  4281.         Public ParentId As Integer
  4282.  
  4283.         Public SubMenu As Object
  4284.  
  4285.         Public Source As String
  4286.  
  4287.         Public Allowed As Boolean = True
  4288.  
  4289.         Public Sub New(AId As Integer, AText As String, AUrl As String, AParentId As Integer, ASource As String, AAllowed As Boolean)
  4290.             Id = AId
  4291.             Text = AText
  4292.             Url = AUrl
  4293.             ParentId = AParentId
  4294.             SubMenu = System.DBNull.Value
  4295.             Source = ASource
  4296.             Allowed = AAllowed
  4297.         End Sub
  4298.  
  4299.         Sub AddItem(ByRef item As cMenuItem) ' Add submenu item
  4300.             If IsDBNull(SubMenu) Then
  4301.                 SubMenu = New cMenu(Id, False)
  4302.                 SubMenu.ParentPage = Me.ParentPage
  4303.             End If
  4304.             SubMenu.AddItem(item)
  4305.         End Sub
  4306.  
  4307.         Function AsString() As String
  4308.             Dim OutStr As String = "{ Id: " & Id & ", Text: " & Text & ", Url: " & Url & ", ParentId: " & ParentId
  4309.             If IsDBNull(SubMenu) Then
  4310.                 OutStr = OutStr & ", SubMenu: (Null)"
  4311.             Else
  4312.                 OutStr = OutStr & ", SubMenu: (Object)"
  4313.             End If
  4314.             OutStr = OutStr & ", Source: " & Source
  4315.             OutStr = OutStr & ", Allowed: " & Allowed
  4316.             Return OutStr & " }" & "<br />"
  4317.         End Function
  4318.     End Class
  4319.  
  4320.     ' Is Admin
  4321.     Public Function IsAdmin() As Boolean
  4322.         If Security IsNot Nothing Then
  4323.             Return Security.IsAdmin()
  4324.         Else
  4325.             Return (ew_ConvertToInt(ew_Session(EW_SESSION_USER_LEVEL_ID)) = -1 OrElse ew_ConvertToInt(ew_Session(EW_SESSION_SYS_ADMIN)) = 1)
  4326.         End If
  4327.     End Function
  4328.  
  4329.     ' Allow list
  4330.     Public Function AllowList(TableName As String) As Boolean
  4331.         If Security IsNot Nothing Then
  4332.             Return Security.AllowList(TableName)
  4333.         Else
  4334.             Return True
  4335.         End If
  4336.     End Function    
  4337.  
  4338.     ' Allow add
  4339.     Public Function AllowAdd(TableName As String) As Boolean
  4340.         If Security IsNot Nothing Then
  4341.             Return Security.AllowAdd(TableName)
  4342.         Else
  4343.             Return True
  4344.         End If
  4345.     End Function
  4346.  
  4347.     ' Validation functions
  4348.     ' Check date format
  4349.     ' format: std/us/euro
  4350.     Public Shared Function ew_CheckDateEx(value As String, format As String, sep As String) As Boolean
  4351.         If value = ""    Then Return True
  4352.         While value.Contains("  ")
  4353.             value = value.Replace("  ", " ")
  4354.         End While
  4355.         value = value.Trim()
  4356.         Dim arDT() As String, arD() As String, pattern As String
  4357.         Dim sYear As String, sMonth As String, sDay As String
  4358.         arDT = value.Split(New Char() {" "c})
  4359.         If arDT.Length > 0 Then
  4360.             sep = "\" & sep
  4361.             Select Case format
  4362.                 Case "std"
  4363.                     pattern = "^([0-9]{4})" & sep & "([0]?[1-9]|[1][0-2])" & sep & "([0]?[1-9]|[1|2][0-9]|[3][0|1])"
  4364.                 Case "us"
  4365.                     pattern = "^([0]?[1-9]|[1][0-2])" & sep & "([0]?[1-9]|[1|2][0-9]|[3][0|1])" & sep & "([0-9]{4})"
  4366.                 Case "euro"
  4367.                     pattern = "^([0]?[1-9]|[1|2][0-9]|[3][0|1])" & sep & "([0]?[1-9]|[1][0-2])" & sep & "([0-9]{4})"
  4368.             End Select
  4369.             Dim re As New Regex(pattern) 
  4370.             If Not re.IsMatch(arDT(0)) Then Return False
  4371.             arD = arDT(0).split(New Char() {Convert.ToChar(EW_DATE_SEPARATOR)})
  4372.             Select Case format
  4373.                 Case "std"
  4374.                     sYear = arD(0)
  4375.                     sMonth = arD(1)
  4376.                     sDay = arD(2)                
  4377.                 Case "us"
  4378.                     sYear = arD(2)
  4379.                     sMonth = arD(0)
  4380.                     sDay = arD(1)
  4381.                 Case "euro"
  4382.                     sYear = arD(2)
  4383.                     sMonth = arD(1)
  4384.                     sDay = arD(0)                    
  4385.             End Select
  4386.             If Not ew_CheckDay(ew_ConvertToInt(sYear), ew_ConvertToInt(sMonth), ew_ConvertToInt(sDay)) Then Return False
  4387.         End If
  4388.         If arDT.Length > 1 AndAlso Not ew_CheckTime(arDT(1)) Then Return False
  4389.         Return True
  4390.     End Function
  4391.  
  4392.     ' Check Date format (yyyy/mm/dd)
  4393.     Public Shared Function ew_CheckDate(value As String) As Boolean
  4394.         Return ew_CheckDateEx(value, "std", EW_DATE_SEPARATOR)
  4395.     End Function
  4396.  
  4397.     ' Check US Date format (mm/dd/yyyy)
  4398.     Public Shared Function ew_CheckUSDate(value As String) As Boolean
  4399.         Return ew_CheckDateEx(value, "us", EW_DATE_SEPARATOR)
  4400.     End Function
  4401.  
  4402.     ' Check Euro Date format (dd/mm/yyyy)
  4403.     Public Shared Function ew_CheckEuroDate(value As String) As Boolean
  4404.         Return ew_CheckDateEx(value, "euro", EW_DATE_SEPARATOR)
  4405.     End Function
  4406.  
  4407.     ' Check day
  4408.     Public Shared Function ew_CheckDay(checkYear As Integer, checkMonth As Integer, checkDay As Integer) As Boolean
  4409.         Dim maxDay As Integer = 31
  4410.         If checkMonth = 4 OrElse checkMonth = 6 OrElse checkMonth = 9 OrElse checkMonth = 11 Then
  4411.             maxDay = 30
  4412.         ElseIf checkMonth = 2 Then
  4413.             If checkYear Mod 4 > 0 Then
  4414.                 maxDay = 28
  4415.             ElseIf checkYear Mod 100 = 0 AndAlso checkYear Mod 400 > 0 Then
  4416.                 maxDay = 28
  4417.             Else
  4418.                 maxDay = 29
  4419.             End If
  4420.         End If
  4421.         Return ew_CheckRange(checkDay, 1, maxDay)
  4422.     End Function
  4423.  
  4424.     ' Check integer
  4425.     Public Shared Function ew_CheckInteger(value As String) As Boolean
  4426.         If value = ""    Then Return True
  4427.         Dim re As New Regex("^\-?\+?[0-9]+") 
  4428.         Return re.IsMatch(value)
  4429.     End Function
  4430.  
  4431.     ' Check number range
  4432.     Public Shared Function ew_NumberRange(value As String, min As Object, max As Object) As Boolean
  4433.         If (min IsNot Nothing AndAlso Convert.ToDouble(value) < Convert.ToDouble(min)) OrElse (max IsNot Nothing AndAlso Convert.ToDouble(value) > Convert.ToDouble(max)) Then
  4434.             Return False
  4435.         End If
  4436.         Return True
  4437.     End Function
  4438.  
  4439.     ' Check number
  4440.     Public Shared Function ew_CheckNumber(value As String) As Boolean
  4441.         If value = ""    Then Return True
  4442.         Return IsNumeric(Trim(value))
  4443.     End Function
  4444.  
  4445.     ' Check range
  4446.     Public Shared Function ew_CheckRange(value As String, min As Object, max As Object) As Boolean
  4447.         If value = ""    Then Return True
  4448.         If Not ew_CheckNumber(value) Then Return False
  4449.         Return ew_NumberRange(value, min, max)
  4450.     End Function
  4451.  
  4452.     ' Check time
  4453.     Public Shared Function ew_CheckTime(value As String) As Boolean
  4454.         If value = ""    Then Return True
  4455.         '        Dim re As New Regex("^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]")
  4456.         Dim re As New Regex("^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm|a|A|p|P))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$")
  4457.         Return re.IsMatch(value)
  4458.     End Function
  4459.  
  4460.     ' Check US phone number
  4461.     Public Shared Function ew_CheckPhone(value As String) As Boolean
  4462.         If value = ""    Then Return True
  4463.         Dim re As New Regex("^\(\d{3}\) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}") 
  4464.         Return re.IsMatch(value)
  4465.     End Function
  4466.  
  4467.     ' Check US zip code
  4468.     Public Shared Function ew_CheckZip(value As String) As Boolean
  4469.         If value = ""    Then Return True
  4470.         Dim re As New Regex("^\d{5}|^\d{5}-\d{4}") 
  4471.         Return re.IsMatch(value)
  4472.     End Function
  4473.  
  4474.     ' Check credit card
  4475.     Public Shared Function ew_CheckCreditCard(value As String) As Boolean
  4476.         If value = ""    Then Return True
  4477.         Dim creditcard As New Hashtable
  4478.         Dim match As Boolean = False
  4479.         creditcard.Add("visa", "^4\d{3}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}")
  4480.         creditcard.Add("mastercard", "^5[1-5]\d{2}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}")
  4481.         creditcard.Add("discover", "^6011[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}")
  4482.         creditcard.Add("amex", "^3[4,7]\d{13}")
  4483.         creditcard.Add("diners", "^3[0,6,8]\d{12}")
  4484.         creditcard.Add("bankcard", "^5610[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}")
  4485.         creditcard.Add("jcb", "^[3088|3096|3112|3158|3337|3528]\d{12}")
  4486.         creditcard.Add("enroute", "^[2014|2149]\d{11}")
  4487.         creditcard.Add("switch", "^[4903|4911|4936|5641|6333|6759|6334|6767]\d{12}")
  4488.         Dim re As Regex 
  4489.         For Each de As DictionaryEntry In creditcard
  4490.             re = New Regex(de.Value)
  4491.             If re.IsMatch(value) Then
  4492.                 Return ew_CheckSum(value)
  4493.             End If
  4494.         Next
  4495.         Return False
  4496.     End Function
  4497.  
  4498.     ' Check sum
  4499.     Public Shared Function ew_CheckSum(value As String) As Boolean
  4500.         Dim checksum As Integer, digit As Byte
  4501.         value = value.Replace("-", "")
  4502.         value = value.Replace(" ", "")
  4503.         checksum = 0
  4504.         For i As Integer = 2-(value.Length Mod 2) To value.Length Step 2
  4505.             checksum = checksum + Convert.ToByte(value.Chars(i-1))
  4506.         Next
  4507.       For i As Integer = (value.Length Mod 2)+1 To value.Length Step 2
  4508.           digit = Convert.ToByte(value.Chars(i-1)) * 2
  4509.             checksum = checksum + IIf(digit < 10, digit, digit-9)
  4510.       Next
  4511.         Return (checksum Mod 10 = 0)
  4512.     End Function
  4513.  
  4514.     ' Check US social security number
  4515.     Public Shared Function ew_CheckSSC(value As String) As Boolean
  4516.         If value = ""    Then Return True
  4517.         Dim re As New Regex("^(?!000)([0-6]\d{2}|7([0-6]\d|7[012]))([ -]?)(?!00)\d\d\3(?!0000)\d{4}") 
  4518.         Return re.IsMatch(value)
  4519.     End Function
  4520.  
  4521.     ' Check email
  4522.     Public Shared Function ew_CheckEmail(value As String) As Boolean
  4523.         If value = ""    Then Return True
  4524.         Dim re As New Regex("^[A-Za-z0-9\._\-+]+@[A-Za-z0-9_\-+]+(\.[A-Za-z0-9_\-+]+)+") 
  4525.         Return re.IsMatch(value)
  4526.     End Function
  4527.  
  4528.     ' Check GUID
  4529.     Public Shared Function ew_CheckGUID(value As String) As Boolean
  4530.         If value = ""    Then Return True
  4531.         Dim re1 As New Regex("^{{1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}}{1}")
  4532.         Dim re2 As New Regex("^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}")
  4533.         Return re1.IsMatch(value) OrElse re2.IsMatch(value)
  4534.     End Function
  4535.  
  4536.     ' Check file extension
  4537.     Public Shared Function ew_CheckFileType(value As String) As Boolean
  4538.         If value = ""    OrElse ew_Empty(EW_UPLOAD_ALLOWED_FILE_EXT)    Then Return True        
  4539.         Dim extension As String = Path.GetExtension(value).Substring(1)
  4540.         Dim allowExt() As String = EW_UPLOAD_ALLOWED_FILE_EXT.Split(New Char() {","c})
  4541.         For Each ext As String In allowExt
  4542.             If ew_SameText(ext, extension) Then Return True
  4543.         Next
  4544.         Return False
  4545.     End Function
  4546.  
  4547.     ' MD5
  4548.     Public Shared Function MD5(InputStr As String) As String
  4549.         Dim Md5Hasher As New MD5CryptoServiceProvider()
  4550.         Dim Data As Byte() = Md5Hasher.ComputeHash(Encoding.Default.GetBytes(InputStr))
  4551.         Dim sBuilder As New StringBuilder()
  4552.         For i As Integer = 0 To Data.Length - 1
  4553.             sBuilder.Append(Data(i).ToString("x2"))
  4554.         Next i
  4555.         Return sBuilder.ToString()    
  4556.     End Function
  4557.  
  4558.     ' Invoke method with no parameter
  4559.     Public Function ew_InvokeMethod(ByVal name As String, ByVal parameters As Object()) As Object
  4560.         Dim mi As MethodInfo = GetType(AspNetMaker7_tfpssnet).GetMethod(name)
  4561.         If mi IsNot Nothing Then
  4562.             Return mi.Invoke(Me, parameters)
  4563.         Else
  4564.             Return False
  4565.         End If
  4566.     End Function
  4567.  
  4568.     ' Get field value
  4569.     Public Shared Function ew_GetFieldValue(ByVal name As String) As Object
  4570.         Dim fi As FieldInfo = GetType(AspNetMaker7_tfpssnet).GetField(name)
  4571.         If fi IsNot Nothing Then Return fi.GetValue(Nothing)
  4572.         Return Nothing
  4573.     End Function
  4574.  
  4575.     ' Check if object is ArrayList
  4576.     Public Shared Function ew_IsArrayList(ByVal obj As Object) As Boolean
  4577.         Return (obj IsNot Nothing) AndAlso (obj.GetType().ToString() = "System.Collections.ArrayList")
  4578.     End Function
  4579.  
  4580.     ' Global random
  4581.     Private Shared GlobalRandom As New Random()
  4582.  
  4583.     ' Get a random number
  4584.     Public Shared Function ew_Random() As Integer
  4585.         SyncLock GlobalRandom
  4586.             Dim NewRandom As New Random(GlobalRandom.Next())
  4587.             Return NewRandom.Next()
  4588.         End SyncLock
  4589.     End Function
  4590.  
  4591.     ' Get query string value
  4592.     Public Shared Function ew_Get(name As String) As String
  4593.         If HttpContext.Current.Request.QueryString(name) IsNot Nothing Then
  4594.             Return HttpContext.Current.Request.QueryString(name)
  4595.         Else
  4596.             Return ""
  4597.         End If
  4598.     End Function
  4599.  
  4600.     ' Get form value
  4601.     Public Shared Function ew_Post(name As String) As String
  4602.         If HttpContext.Current.Request.Form(name) IsNot Nothing Then
  4603.             Return HttpContext.Current.Request.Form(name)
  4604.         Else
  4605.             Return ""
  4606.         End If
  4607.     End Function
  4608.  
  4609.     ' Get/set session values
  4610.     Public Shared Property ew_Session(name As String) As Object
  4611.         Get
  4612.             Return HttpContext.Current.Session(name)
  4613.         End Get
  4614.         Set(ByVal Value As Object)
  4615.             HttpContext.Current.Session(name) = Value
  4616.         End Set
  4617.     End Property
  4618.  
  4619.     ' Get project cookie
  4620.     Public Shared Property ew_Cookie(name As String) As String
  4621.         Get
  4622.             If HttpContext.Current.Request.Cookies(EW_PROJECT_NAME) IsNot Nothing Then
  4623.                 Return HttpContext.Current.Request.Cookies(EW_PROJECT_NAME)(name)
  4624.             Else
  4625.                 Return ""
  4626.             End If
  4627.         End Get
  4628.         Set(ByVal value As String)
  4629.             Dim c As HttpCookie
  4630.             If HttpContext.Current.Request.Cookies(EW_PROJECT_NAME) IsNot Nothing Then
  4631.                 c = HttpContext.Current.Request.Cookies(EW_PROJECT_NAME)
  4632.                 c.Values(name) = value
  4633.             Else
  4634.                 c = New HttpCookie(EW_PROJECT_NAME)
  4635.             End If
  4636.             c.Values(name) = value
  4637.             c.Expires = DateAdd("d", 365, Today()) ' Change the expiry date of the cookies here
  4638.             HttpContext.Current.Response.Cookies.Add(c)    
  4639.         End Set
  4640.     End Property
  4641.  
  4642.     ' Response.Write
  4643.     Public Shared Sub ew_Write(value As Object)
  4644.         HttpContext.Current.Response.Write(value)
  4645.     End Sub
  4646.  
  4647.     ' Response.End
  4648.     Public Shared Sub ew_End()
  4649.         HttpContext.Current.Response.End()
  4650.     End Sub
  4651. End Class
  4652.