
'Function To Check the Date Pattern 
Function ValidateDatePattern(controlName) 
    Dim dtDate, sPatrn, regEx, Matches
    dtDate = document.GetElementById(controlName).Value
    sPatrn = "(\d{1,2})(\/)(\d{1,2})\2(\d{4})"    
    Set regEx = new RegExp ' Create a regular expression.
    regEx.Pattern = spatrn ' Set pattern.
    regEx.IgnoreCase = True' Set case insensitivity.
    regEx.Global = true' Set global applicability.
    Matches = regEx.Test(dtDate)' Execute search.    
    if Matches = true then
        ValidateDatePattern = true
    else
        ValidateDatePattern = false
    end if
    set Matches = nothing
    set regEx = nothing    
End Function

'Function To Check the Date Range 
Function validateDateRange(controlName) 
    Dim sMonth, sDay, sYear, aDate, dtDate, sErrorText, bIsLeap
    dtDate = Document.GetElementById(controlName).Value
    dtDate = Trim(dtDate)
    validateDateRange = true
    aDate = split(dtDate,"/")

    if (ubound(aDate) = 2) then
            sDay = aDate(0)
            sMonth = aDate(1)
            sYear = aDate(2)
            
            if len(sYear) = 4 and IsNumeric(sYear) and IsNumeric(sMonth) and IsNumeric(sDay) then
                if ((sYear <= 1900) or (sYear >9998)) then 
                    validateDateRange = false 
                elseif (sMonth < 1 or sMonth > 12) then  '// check month range
                        validateDateRange = false 
                elseif (sDay < 1 or sDay > 31) then
                        validateDateRange = false  
                elseif ((sMonth=4 or sMonth=6 or sMonth=9 or sMonth=11) and sDay=31) then
                        validateDateRange = false 
                elseif (sMonth = 2) then '// check for february 29th
                    If (sYear Mod 4 = 0 And sYear Mod 100 <> 0) Or sYear Mod 400 = 0 Then
                        bIsLeap = True
                    Else
                        bIsLeap = False
                    End If
                    if (sDay > 29 or (sDay = 29 and not bIsLeap)) then
                        validateDateRange = false
                    end if
                 End If                      
             else
                validateDateRange = false
             End if 
     End If
End Function

'Function To validate date pattern and Date range
Function ValidateDOB(ControlName)
 dim dtdob,varDate
   
  if ValidateDate(ControlName) = False Then
   Exit Function
  end if
  if Document.GetElementById(ControlName).Value <> "" then
   dtdob =  Document.GetElementById(ControlName).Value
   varDate=Now()
   varDate=FormatDateTime(varDate,2)
 
  If (Datediff("d",dtdob, varDate)< 0) Then
             MsgBox ("Date of Birth cannot be greater than Current date")
              Document.GetElementById(ControlName).Value = ""
              Exit Function
    end if
    
 end if
end function 


'Function To validate date pattern and Date range
Function ValidateDate(ControlName)
    If ValidateDatePattern(ControlName) = False or validateDateRange(ControlName) = False Then
       MsgBox ("Please enter a date between January 1,1900 and December 31,9998")
       Document.getElementById(ControlName).value = "" 
                ValidateDate = False 
       Exit Function
    End If 
   ValidateDate = true
End Function

'Function To validate Expiry date.
Function ValidateExpiryDate(ControlName)
    If (document.getElementById(ControlName).value = "DD/MM/YYYY") Then
        document.getElementById(ControlName).value= ""
    End If
    If (document.getElementById(ControlName).value <> "") Then
        If ValidateDatePattern(ControlName) = False or validateDateRange(ControlName) = False Then
           MsgBox "Enter Date in dd/MM/yyyy format for Date field."
           Document.getElementById(ControlName).value = ""
           Document.getElementById(ControlName).style.color = "gray"
           Document.getElementById(ControlName).style.fontSize="11px"
           Document.getElementById(ControlName).value = "DD/MM/YYYY"
           Exit Function
        End If
    End If
End Function

'Function to validate Name
Function validateName(ControlName)
Dim sName,sCopyName
sName = Trim(document.getElementById(ControlName).value)
If sName <> "" then   
   If ValidateRegExpr("IsAlphaSpaceOH", sName) = False Then
         MsgBox ("Name should not contain Special characters or numeric values")
         document.getElementById(ControlName).value = ""
         document.getElementById(ControlName).focus()
         Exit Function 
   End If 
   If ValidateRegExpr("IsAllHyhen", sName) = True Then
         MsgBox ("Name should not contain all Hyphen as a Special characters")
         document.getElementById(ControlName).value = ""
         document.getElementById(ControlName).focus()
         Exit Function 
   End If   
   If ValidateRegExpr("IsAllApostrophe", sName) = True Then
         MsgBox ("Name should not contain all Apostrophe as a Special characters")
         document.getElementById(ControlName).value = ""
         document.getElementById(ControlName).focus()
         Exit Function 
   End If  
    document.getElementById(controlname).Style.BackGround = "White"      
    sCopyName = ucase(Mid(sName,1,1))
    sCopyName = sCopyName & Mid(sName,2,len(sName))
    document.getElementById(controlname).value = ""
    Do While InStr(1, sCopyName, "  ")
        sCopyName = Replace(sCopyName, "  ", " ")
    Loop
    document.getElementById(controlname).value = sCopyName 
   
End If
    
End Function
'Function to Validate Pincode
Function ValidatePinCode(ControlName)
Dim sPinCode        'To store the pincode

sPinCode = document.getElementById(ControlName).value
    
If sPinCode <> "" Then
    If IsNumeric(sPinCode) = False Then
            MsgBox "PinCode should be a number"
            document.getElementById(ControlName).value = ""
            document.getElementById(ControlName).focus()
            Exit Function
    End If
    If sPinCode < 0 Then
            MsgBox "Please enter correct pin code"
            document.getElementById(ControlName).value = ""
            document.getElementById(ControlName).focus()
            Exit Function
    End If
    If Len(sPinCode) <> 6 Then
            MsgBox "PinCode should be of 6 digits"
            document.getElementById(ControlName).value = ""
            document.getElementById(ControlName).focus()
            Exit Function
    End If
    If sPinCode = "000000" Then
            MsgBox "Pin Code cannot just contain zeroes alone. Please correct it."
            document.getElementById(ControlName).value = ""
            document.getElementById(ControlName).focus()
            Exit Function
    End If
    If Mid(sPinCode, 2, 6) = "00000" Then
            MsgBox "Pin Code cannot contain 5 zeroes from 2nd to 6th digit. Please enter the correct Pin Code"
            document.getElementById(ControlName).value = ""
            document.getElementById(ControlName).focus()
            Exit Function
    End If
    If CInt(Mid(sPinCode, 1, 1)) = 0 Then
            MsgBox "PinCode should not start with 0"
            document.getElementById(ControlName).value = ""
            document.getElementById(ControlName).focus()
            Exit Function
    End If    
End If
End Function


' Function to validate Time
Function ValidateTime(ControlName)
    Dim regExp, match, i, spec, bflag, iValues

    spec = document.getElementById(ControlName).value
    Set regExp = New regExp
    regExp.Global = True
    regExp.IgnoreCase = True
    regExp.Pattern = "^\d{1,2}\:\d{1,2}$"
    match = regExp.Test(spec)
    If (match = False) Then
        MsgBox "Please enter time in hh:mm format"      
        document.getElementById(ControlName).value = ""       
        ValidateTime = false
        Exit Function
    Else 
        iValues = Split(spec, ":")
        If (iValues(0) > 23) Then
              msgbox "Please enter valid hours upto 23"
              document.getElementById(ControlName).value = ""
              ValidateTime = false
              Exit Function
        End If        
        If (iValues(1) > 59) Then
              MsgBox "Please enter valid minutes upto 59"
              document.getElementById(ControlName).value = ""
              ValidateTime = false
              Exit Function
        End If
    End If   
End Function


'Common Function to validate patterns
Function ValidateRegExpr(sRegExp, sControlValue)
    Dim regExp, match, i
    For i = 1 To Len(sControlValue)
        spec = Mid(sControlValue, i, 1)
        Set regExp = New regExp
        regExp.Global = True
        regExp.IgnoreCase = True
        If sRegExp = "IsAlphaWithSpaceM" Then
            regExp.Pattern = "[A-Z]|[a-z]|[ ]|[']|[.]"
        ElseIf sRegExp = "IsAddress" Then
            regExp.Pattern = "[A-Z]|[a-z]|[-]|[/]|[0-9]|[,]|[#]|[ ]|[:]" 
        ElseIf sRegExp = "IsAlphaM" Then
            regExp.Pattern = "[A-Z]|[a-z]|[ ]"  
        ElseIf sRegExp = "IsAlphaNumericValues" Then 
            regExp.Pattern = "[A-Z]|[a-z]|[0-9]|[ ]|[']|[-]|[&]"
        ElseIf sRegExp = "OnlyAlphaValues" Then        
            regExp.Pattern = "[']|[-]|[&]|[0-9]"
        ElseIf sRegExp = "IsAlphaNumeric" Then
            regExp.Pattern = "[A-Z]|[a-z]|[0-9]"
        ElseIf sRegExp = "IsAllAlphabet" Then
            regExp.Pattern = "[A-Z]|[a-z]"
        ElseIf sRegExp = "IsAlphaNumericWithSpace" Then 
            regExp.Pattern = "[A-Z]|[a-z]|[ ]|[0-9]"
        ElseIf sRegExp = "IsAlphaSpaceOH" then
            regExp.Pattern = "[A-Z]|[a-z]|[ ]|[']|[-]"
         ElseIf sRegExp = "IsAlphtNumbericHSCHS" then
            regExp.Pattern = "[A-Z]|[a-z]|[-]|[/]|[0-9]|[,]|[#]|[ ]"
        ElseIf sRegExp = "IsAlphaSpaceCH" then
            regExp.Pattern = "[A-Z]|[a-z]|[ ]|[,]|[-]"
        ElseIf sRegExp = "IsAlphaNumericHS" then
            regExp.Pattern = "[A-Z]|[a-z]|[0-9]|[-]|[/]"  
        ElseIf sRegExp = "IsSelectedNumeric" then
            regExp.Pattern = "[A-Z]|[a-z]|[0-9]|[/]|[ ]|[-]"
        ElseIf sRegExp = "IsAlphaNumericCSHD" then
            regExp.Pattern = "[A-Z]|[a-z]|[0-9]|[,]|[ ]|[-]|[.]"
        ElseIf sRegExp = "IsAlphaWithSpace" then
            regExp.Pattern = "[A-Z,' ']|[a-z,' ']"   
        ElseIf sRegExp = "IsDescriptionCheck" then 
            regExp.Pattern = "[^/^]" 
         ElseIf sRegExp = "MakeModelAccessories" then  
            regExp.Pattern = "[A-Z]|[a-z]|[0-9]|[ ]|[-]"  
         ElseIf sRegExp = "IsAllHyhen" then
            regExp.Pattern = "[-]"
         ElseIf sRegExp = "IsAllApostrophe" then
            regExp.Pattern = "[']"  
         ElseIf sRegExp = "IsAllDot" then
            regExp.Pattern = "[.]" 
         ElseIf sRegExp = "IsAllNumeric" Then
            regExp.Pattern = "[0-9]"         
        End If
        Set match = regExp.Execute(spec)
        If match.Count = 0 Then
            ValidateRegExpr = False
            Exit Function
        End If
        Set regExp = Nothing
    Next
    ValidateRegExpr = True
End Function

'Function To Check Zeros
Function CheckForZeros(Value)
    CheckForZeros = False
    Dim regExp, match
    Set regExp = New regExp
    regExp.Global = True
    regExp.IgnoreCase = True
    regExp.Pattern = "[A-Z]|[a-z]|[1-9]"
    match = regExp.Test(Value)
    If (match = false) Then
        CheckForZeros = True
    End If 
End Function

'Functionto check Two Decimal Point
Function CheckTwoDecimalPoint(ControlName)
Dim sNumber 
sNumber = ControlValue
Dim aList, sChkDecPt
sChkDecPt = InStr(1, sNumber, ".")
If sChkDecPt <> 0 Then
    aList = Split(sNumber, ".")
    If aList(1) <> "" Then
        If Len(aList(1)) > 2 Then
            MsgBox ("Sum Insured cannot be more than 2 decimal points.")
            ControlName.value = ""  
            Exit Function
        End If
    End If
End If
End Function
function Checknumeric(id)

     spec = Document.getElementById(id).Value

       If ((IsNumeric(spec) = False))Then
        MsgBox "Please enter a valid number"
        Document.getElementById(id).Value = ""
        Checknumeric = false
        Exit Function
    End If 
End Function
'Function to check Currency
Function CheckCurrency(id)
    CheckCurrency = true
    spec = Document.getElementById(id).Value
    Set regExp = New regExp
    regExp.Global = True
    regExp.IgnoreCase = True
    regExp.Pattern = "^[0-9]*[\.][0-9]*$"
    match = regExp.Test(spec)
    If ((match = False) and (IsNumeric(spec) = False))Then
        MsgBox "Please enter a valid number"
        Document.getElementById(id).Value = ""
        Document.getElementById(id).focus()
        CheckCurrency = false
        Exit Function
    End If
    If CheckCurrencyFieldLength(id) = false Then
        CheckCurrency = false
    End If   
End Function

'Function to check the currency length
Function CheckCurrencyFieldLength(id)
Dim spec
spec = Document.getElementById(id).Value
CheckCurrencyFieldLength = True
If Document.getElementById(id).value <> "" Then
   If Document.getElementById(id).value > 99999999999999 Then 
        MsgBox ("Value cannot be more than 99999999999999.")
        CheckCurrencyFieldLength = False
        Document.getElementById(id).value = 99999999999999 
        Exit Function
   End If
End If
End Function

'Function to convert to SafeDBL
Function SafeDbl(DblVal)
    SafeDbl = 0
    On Error Resume Next
    SafeDbl = CDbl(DblVal)
    On Error GoTo 0
End Function

'Function to convert to SafeInt
Function safeInt(Uint)
    safeInt = 0
    On Error Resume Next
    safeInt = CInt(Uint)
    On Error GoTo 0
End Function


'// Function for Format (as 2.00 and Number should be numeric)
Function CurrencyAndFormat(controlName)
Dim Value
     If  document.getElementById(controlName).value <> "" Then
        If CheckCurrency1(controlName) = false Then
             document.getElementById(controlName).value = ""
             Exit Function
        End If
    End If
     If  document.getElementById(controlName).value <> "" Then
        Value = document.getElementById(controlName).value
        document.getElementById(controlName).value = FormatNumber(Value,2)
        document.getElementById(controlName).Style.BackGround = "White"
       Exit Function
    End If
End Function

'//Check Currency1 function
Function CheckCurrency1(id)
    Dim spec,match,regExp
        CheckCurrency1 = true
    spec = document.getElementById(id).Value
    Set regExp = New regExp
        regExp.Global = True
        regExp.IgnoreCase = True
        regExp.Pattern = "^[0-9]*[\.][0-9]*$"
    match = regExp.Test(spec)
    If ((match = False) and (IsNumeric(spec) = False))Then
         MsgBox ("Please enter a valid value")
         document.getElementById(id).Value = ""
         document.getElementById(id).focus()
         CheckCurrency1 = false
         Exit Function
    End If
    If CheckCurrencyFieldLength1(id) = false Then
        CheckCurrency1 = false
    End If
 End Function
'//Function to Check Currency Field Length 
Function CheckCurrencyFieldLength1(id)
Dim spec
spec = document.getElementById(id).value
CheckCurrencyFieldLength1 = True
  If document.getElementById(id).value <> "" Then
       If document.getElementById(id).value > 99999999999999 Then 
            MsgBox ("Value cannot be more than 99999999999999.")
            CheckCurrencyFieldLength1 = False
             document.getElementById(id).value = 99999999999999 
             Exit Function
       End If
  End If
End Function


'//Check Safe Dbl
Function SafeDbl(DblVal)
    SafeDbl = 0
    On Error Resume Next
    SafeDbl = CDbl(DblVal)
    On Error GoTo 0
End Function

Function GetCofigurationValue(byval code,HiddenName)
    Dim RecordsArr 
    Dim ValuesArr
    Dim HiddenRecords
    Dim iRecNo
    Dim iColNo

    Hidden = document.getElementById(HiddenName).value
    RecordsArr = Split(Hidden, ";", -1, 1)
    iRecNo  =UBound(RecordsArr)
    While iRecNo >= 0
        ValuesArr = Split(RecordsArr(iRecNo), ",", -1, 1)
        iColNo = UBound(ValuesArr)
        If  iColNo >= 1 Then
            If (ValuesArr(0) = code) Then
                GetCofigurationValue = ValuesArr(1)
                Exit Function
            End If
        End If
        iRecNo = iRecNo - 1
    Wend

    GetCofigurationValue =0 
    Exit Function
End Function

Function AcceptNumeric(controlName)
    Dim sValue
sValue = Trim(document.getElementById(controlName).value)
If(sValue <>"") Then
If IsNumeric(sValue) = False Then
	document.getElementById(controlName).value=""
	document.getElementById(ControlName).focus()
	MsgBox ("Please Enter Numeric Values")
End if    
End if
End Function

Function ClearBox(controlName)
document.getElementById(controlName).value=""
End Function