Dim sSetText
Dim sOnes
Dim sTens
Dim sTeens
Dim sText
Dim sAmount
Dim sAmt
Dim nPos
Dim nLngth
Dim nNumbSets
Dim nMaxSets
Dim nSetNum

Function vbFormatDate(varDate,NamedFormat)
    vbFormatDate = FormatDateTime(varDate, NamedFormat)
End Function

Function vbDateDiffWrapper(interval, date1, date2)
    vbDateDiffWrapper = DateDiff(interval,date1,date2)
End Function

Function vbFormatNumber(nbr)
    vbFormatNumber = FormatNumber(nbr, 0)
End Function

Function vbFormatDecimal(nbr, decimalPlaces)
    vbFormatDecimal = FormatNumber(nbr, decimalPlaces)
End Function

Function vbFormatMoneyDecimal(nbr, decimalPlaces)
    vbFormatMoneyDecimal = "$" & FormatNumber(nbr, decimalPlaces)
End Function


Function vbFormatMoney(nbr)
    vbFormatMoney = "$" & FormatNumber(nbr, 0)
End Function

' 21.09.99 rch
Function vbValidateEndsWithFiveOrZero(obj)
	InvalidField = false
	s = obj.value
	rightchar = right(s, 1)
	If ( rightchar = "0" ) Then InvalidField = true
	If ( rightchar = "5" ) Then InvalidField = true
	If ( Not InvalidField ) Then
		alert("The value in this field must end with a 0 or 5!")
		obj.focus()
	End If
End Function

' 28.09.99 rch
Function vbValidateEndsWithZero(obj)
	InvalidField = false
	s = obj.value
	rightchar = right(s, 1)
	If ( rightchar = "0" ) Then InvalidField = true
	If ( Not InvalidField ) Then
		alert("The value in this field must end with a 0!")
		obj.focus()
	End If
End Function

Function vbValidateNumber(strCheck)
    if isNumeric(strCheck) then
        vbValidateNumber = 0
    else
        vbValidateNumber = 1
    end if
End Function

Function StringReplace(strOriginal, strFind, strReplaceWith)
    StringReplace = Replace(strOriginal,strFind, strReplaceWith)
end Function

Function vbValidateIsDate(strDate)
    if isDate(strDate) then
        vbValidateIsDate = 0
    else
        vbValidateIsDate = 1
    end if
End Function

Function vbMsgBoxWrapper(strPrompt,intButtonMask,strTitle)
    vbMsgBoxWrapper = msgbox(strPrompt,intButtonMask,strTitle)
end function

Function vbAscWrapper(strChr)
    ' this returns the integer value of a character
    vbAscWrapper = Asc(CStr(strChr))
end function

Function vbCharWrapper(charcode)
    ' this returns the char value of an integer
    vbCharWrapper = Chr(charcode)
end function

Function vbTrimWrapper(strIn)
    vbTrimWrapper = Cstr(trim(strIn))
end Function

'Function vbUCaseWrapper(strIn)
'    vbUCaseWrapper = UCase(cStr(strIn))
'end Function

'Function vbLCaseWrapper(strIn)
'    vbLCaseWrapper = LCase(cStr(strIn))
'end Function

Function vbInStrWrapper(strIn,delimiter)
    vbInStrWrapper = InStr(1,cStr(strIn),cStr(delimiter))
end Function

Function vbMidWrapper(strIn,start,length)
    vbMidWrapper = Mid(cStr(strIn), cInt(start), cInt(length))
end Function

Function vbLeftWrapper(strIn,pos)
    vbLeftWrapper = Left(cStr(strIn), cInt(pos))
end Function

Function vbRightWrapper(strIn,pos)
    vbRightWrapper = Right(cStr(strIn), cInt(pos))
end Function

Function vbDateAddWrapper(interval, number, date)
    vbDateAddWrapper = DateAdd(interval, number, date)
end Function

Function vbAllowanceDateCalculator(interval, number, date)
'Called by the condition book AI allowances routine
    vbAllowanceDateCalculator = cStr(DateAdd(interval, -number, date))
end Function

Function vbProperCase(strIn)
    Dim strTemp
    Dim i
    Dim intWords
    Dim currentWord

    strIn = Replace(strIn, "  ", " ")
    strIn = Replace(strIn, "  ", " ")

    intWords =StrCount(strIn, " ")

    for i = 1 to intWords + 1
        currentWord = Field(strIn, " ", i)
        strTemp = strTemp & UCase(cStr(left(currentWord,1))) & LCase(cStr(right(currentWord,(Len(currentWord) - 1)))) & " "
    next
    vbProperCase = strTemp
end Function

Function NumToText(Number , cnv )

    '====================================================
    '*** CNV = 0 .... Returns FOUR, FORTY-FOUR, ...
    '*** CNV = 1 .... Returns FOURTH, FORTY-FOURTH, ...
    '====================================================
    sSetText = "|THOUSAND|MILLION|BILLION|TRILLION"
    sOnes = "ONE|TWO|THREE|FOUR|FIVE|SIX|SEVEN|EIGHT|NINE|"
    sTens = "|TWENTY|THIRTY|FORTY|FIFTY|SIXTY|SEVENTY|EIGHTY|NINETY|"
    sTeens = "ELEVEN|TWELVE|THIRTEEN|FOURTEEN|FIFTEEN|SIXTEEN|SEVENTEEN|EIGHTEEN|NINETEEN|"
    sText = ""
    sAmount = Trim(Int(Number))
    nLngth = Len(sAmount)
    nNumbSets = Int(nLngth / 3)
    If (nLngth Mod 3) > 0 Then nNumbSets = nNumbSets + 1
    nMaxSets = StrCount(sSetText, "|")
    If nNumbSets > nMaxSets Then
        sText = "?????"
        Return
    End If
    nPos = 1
    For nSetNum = nNumbSets To 1 Step -1
        If nPos = 1 Then
            nLngth = Len(sAmount) Mod 3

        Else
            nLngth = 3
        End If
        If nLngth = 0 Then nLngth = 3
        sAmt = Mid(sAmount, nPos, nLngth)

        calc
        If sAmt <> 0 & nSetNum <> 1 Then
            sText = sText & Field(sSetText, "|", nSetNum) & " "
        End If
        nPos = nPos + nLngth
    Next

    sText = Trim(sText)
    If cnv = 1 Then AddSuffix

    NumToText = sText
    Exit Function

End Function

Function calc()

    if len(sAmt) = 1 then sAmt = "00" & sAmt
    if len(sAmt) = 2 then sAmt = "0" & sAmt

    If Left(sAmt, 1) <> "0" Then
        sText = sText & Field(sOnes, "|", Left(sAmt, 1)) & " HUNDRED "
    End If
    If Mid(sAmt, 2, 1) > 1 Then
        sText = sText & Field(sTens, "|", Mid(sAmt, 2, 1))
        If Mid(sAmt, 3, 1) <> 0 Then
            sText = sText & "-"

        Else
            sText = sText & " "
        End If

    Else
        If Mid(sAmt, 2, 1) = 1 Then
            If Mid(sAmt, 3, 1) = 0 Then
                sText = sText & "TEN "

            Else
                sText = sText & Field(sTeens, "|", Mid(sAmt, 3, 1)) & " "
            End If
        End If
    End If
    If Mid(sAmt, 2, 1) <> 1 And Mid(sAmt, 3, 1) <> 0 Then
        sText = sText & Field(sOnes, "|", Mid(sAmt, 3, 1)) & " "
    End If
End Function

Function AddSuffix()

    If Right(sText, 2) = "TY" Then
        sText = Left(sText, Len(sText) - 1) & "IETH"

    ElseIf Right(sText, 3) = "ONE" Then
        sText = Left(sText, Len(sText) - 3) & "FIRST"

    ElseIf Right(sText, 3) = "TWO" Then
        sText = Left(sText, Len(sText) - 3) & "SECOND"

    ElseIf Right(sText, 5) = "THREE" Then
        sText = Left(sText, Len(sText) - 5) & "THIRD"

    ElseIf Right(sText, 4) = "FIVE" Then
        sText = Left(sText, Len(sText) - 4) & "FIFTH"

    ElseIf Right(sText, 5) = "EIGHT" Then
        sText = sText & "H"

    ElseIf Right(sText, 4) = "NINE" Then
        sText = Left(sText, Len(sText) - 1) & "TH"

    ElseIf Right(sText, 6) = "TWELVE" Then
        sText = Left(sText, Len(sText) - 6) & "TWELFTH"

    Else
        sText = sText & "TH"
    End If

End function

Function StrCount(strng, sub_strng)
    Dim nStrLen
    Dim nCtr
    Dim nSubLen
    Dim nFound
    Dim nCurr

    nStrLen = Len(strng)
    nSubLen = Len(sub_strng)
    nFound = 0
    nCurr = 1
    Do
        If nCurr > nStrLen Then Exit Do
        If Mid(strng, nCurr, nSubLen) = sub_strng Then
            nFound = nFound + 1
            nCurr = nCurr + Len(sub_strng)

        Else
            nCurr = nCurr + 1
        End If
    Loop

    StrCount = nFound
End Function


Function Field(old_string, delim, pos)
    Dim nOldStringLen
    Dim nWordStart
    Dim nDelimLen
    Dim nCount
    Dim nWordStop
    Dim nTempLen
    Dim nOldLen
    Dim bFlag
    Dim sTempstring

    nOldStringLen = Len(old_string)
    nDelimLen = Len(delim)
    nWordStart = 1
    bFlag = False
    For nCount = 1 To pos
        nWordStop = InStr(nWordStart, old_string, delim, 0)  ' Find first space.
        If nWordStop = 0 And nCount = pos Then nWordStop = nOldStringLen + 1
        nTempLen = nWordStop - nWordStart
        If nTempLen < 0 Then
            nTempLen = (nOldStringLen - nWordStart) + 1
            sTempstring = ""
            Exit For
        End If
        sTempstring = Mid(old_string, nWordStart, nTempLen)
        nWordStart = nWordStop + nOldLen + nDelimLen
    Next

    Field = sTempstring
End Function
