Imports System.io
Module MainModule
  Public OleDbString As String
  Public db As DBConnection
  Public g_intUserLevel As Integer
  Public User As DataRow = Nothing

  Public Function StartSystem() As Integer
    Dim blnReturn As Integer = 0
    Dim iniRead As New StreamReader(System.Windows.Forms.Application.StartupPath & "\DB.ini")
    Dim strPath As String = iniRead.ReadLine
    iniRead.Close()
    Dim iniRead2 As New StreamReader(System.Windows.Forms.Application.StartupPath & "\DB2.ini")
    Dim strPathWorkgroup As String = iniRead2.ReadLine
    iniRead2.Close()

    g_intUserLevel = 0
    If System.IO.File.Exists(strPath) Then
      db = New DBConnection(strPath, strPathWorkgroup)
      db.Connect()
      If db.connState <> ConnectionState.Open Then
        blnReturn = 1
      Else
      End If
    Else
      blnReturn = 2
    End If
    Return blnReturn
  End Function

  Public Function Nz(ByVal obj As Object, ByVal strReturn As String) As String
    If IsNothing(obj) Then
      Return strReturn
    ElseIf IsDBNull(obj) Then
      Return strReturn
    Else
      'If strReturn = "" Then
      '20070209 added by Gareth to strip out " in a string
      'Dim s As String
      's = obj.ToString
      's = s.Replace(Chr(34), "")
      'Return s
      'Else
      Return obj.ToString
      'End If
    End If
  End Function

  Public Function StripNonDigits(ByVal Text As String) As String
    Dim sb As New System.Text.StringBuilder(Text.Length)
    Dim chr As Char
    For Each chr In Text
      If Char.IsDigit(chr) Or chr.Equals(".") Then
        sb.Append(chr)
      End If
    Next chr
    If sb.Length = 0 Then
      sb.Append("0")
    End If
    Return sb.ToString()
  End Function

  Public Sub loadCombo(ByVal cmb As ComboBox, ByVal dt As DataTable, ByVal strDescription As String, ByVal valueField As String, ByVal displayField As String)
    Dim dr As DataRow
    Dim strTmp As String
    dr = dt.NewRow()
    dr.Item(valueField) = 0
    strTmp = Left(strDescription, 1)
    If (strTmp = "a") Or (strTmp = "e") Or (strTmp = "i") Or (strTmp = "o") Or (strTmp = "u") Then
      dr.Item(displayField) = "Select an " & strDescription
    Else
      dr.Item(displayField) = "Select a " & strDescription
    End If
    dt.Rows.InsertAt(dr, 0)
    cmb.DisplayMember = displayField
    cmb.ValueMember = valueField
    cmb.DataSource = dt
    dt = Nothing
  End Sub

  Public Sub setCombo(ByVal cmb As ComboBox, ByVal strValue As String)
    Dim i As Integer
    For i = 0 To cmb.Items.Count - 1
      If CStr(cmb.Items(i)(0)) = strValue Then
        cmb.SelectedIndex = i
        Exit For
      End If
    Next
    If i > cmb.Items.Count - 1 Then
      cmb.SelectedIndex = -1
    End If
  End Sub

  Public Sub loadConsoleCombo(ByVal cmb As ComboBox, ByVal dt As DataTable, ByVal strDescription As String, ByVal valueField As String, ByVal displayField As String)
    Dim dr As DataRow
    Dim strTmp As String
    dr = dt.NewRow()
    dr.Item(valueField) = 0
    strTmp = Left(strDescription, 1)
    dr.Item(displayField) = "All " & strDescription
    dt.Rows.InsertAt(dr, 0)
    cmb.DisplayMember = displayField
    cmb.ValueMember = valueField
    cmb.DataSource = dt
    dt = Nothing
  End Sub

  Public Sub loadComboWithoutDesc(ByVal cmb As ComboBox, ByVal dt As DataTable, ByVal strDescription As String, ByVal valueField As String, ByVal displayField As String)
    cmb.DisplayMember = displayField
    cmb.ValueMember = valueField
    cmb.DataSource = dt
    dt = Nothing
  End Sub

  Public Function makeDBdate(ByVal strDate As String) As String
    Return Left(strDate, 4) & Mid(strDate, 6, 2) & Right(strDate, 2)
  End Function

  Public Function makeAPPdate(ByVal strDate As String) As String
    If Len(strDate) = 0 Then
      Return Year(Now) & "-" & CStr(Month(Now)).PadLeft(2, "0") & "-" & CStr(Microsoft.VisualBasic.Day(Now)).PadLeft(2, "0")
    Else
      Return Left(strDate, 4) & "-" & Mid(strDate, 5, 2) & "-" & Right(strDate, 2)
    End If
  End Function

End Module
