'-----------------------------------------------------------------------------
'To implement:
' place this VBScript in the body of the page, NOT the head
'
' add the attribute "validated" to the form.
'
' to each text field you wish to be validated:
' if the field is required, the attribute "required"
' if the field should be validated for a specific datatype, add the attribute "datatype" containing the type of data to be checked for.
'
'
'-----------------------------------------------------------------------------

If ScriptEngineMajorVersion >= 5 Then document.body.onload = GetRef("prepare_all")

Set submitDict = CreateObject("Scripting.Dictionary")

Set reDict = CreateObject("Scripting.Dictionary")
Set expDict = CreateObject("Scripting.Dictionary")
reDict.Add "required", "^[\S\s]+$"
expDict.Add "required", "This field is required."

reDict.Add "name", "^[a-zA-Z '-\.αινσϊρΡΊ]+$"
expDict.Add "name", "A name must contain only alpha characters. (a-z)"

reDict.Add "integer", "^\d+$"
expDict.Add "integer", "This should be an integer."

reDict.Add "decimal", "^-?(\d+)?(\.\d+)?$"
expDict.Add "decimal", "Not a valid decimal value."

reDict.Add "currency", "^-?\$?\d{0,3}((\,?\d{3})*)?(\.\d+)?$"
expDict.Add "currency", "A currency amount must be a numeric value."

reDict.Add "ssn", "^\d{3}\W?\d{2}\W?\d{4}$"
expDict.Add "ssn", "Social Security Number must have 9 digits, optionally separated by hyphens.  (xxx-xx-xxxx)"

reDict.Add "phone", "^\(?\d{3}\)?\W?\d{3}\W?\d{4}(x\d+)?$"
expDict.Add "phone", "Phone number must have 10 digits, including area code. (xxx)xxx-xxxx"

reDict.Add "usazip", "^\d{5}(\W?\d{4})?$"
expDict.Add "usazip", "A U.S. zipcode is 5 digits long, with an optional 4 digit extension."

reDict.Add "canadazip", "^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$"
expDict.Add "canadazip", "A Canadian zipcode is 6 characters long with alternating letters and numbers<br>Ex. N6A 4L9"


reDict.Add "email", "^[-\w]+(\.[-\w]+)*@([-\w]+\.)*[-\w]+(\.[-\w]+)$"
expDict.Add "email", "Not a valid email address"

reDict.Add "date", "^$" ' Special Circumstances
expDict.Add "date", "Not a recognizable date.  Use one of the following formats:<br>mm/dd/yy<br>mm/dd/yyyy<br>mmm dd, yyyy<br>dd mmm yyyy"

Function CheckDate(date_string)
	CheckDate = True
	If Not IsDate(date_string) Then CheckDate = False
End Function

Sub validate_this()
	call validate_field(window.event.srcElement)
End Sub

Sub validate_field(byRef obj)
	valtype = obj.getAttribute("datatype")
	required = Not IsNull(obj.getAttribute("required"))
	If IsNull(valtype) And required Then valtype = "required"
	
	' if zip/county, assign objects, and get correct valtype to check against
	If valtype = "zipcode" Or valtype = "country" Then
		If valtype = "zipcode" Then
			' find the country field
			For each thing in obj.form
				If thing.getAttribute("datatype") = "country" And thing.name = obj.getAttribute("country") Then
					Set countryobj = thing
				End If
			Next
		ElseIf valtype = "country" Then
			' find the zipcode field
			Set countryobj = obj
			For each thing in obj.form
				If thing.getAttribute("datatype") = "zip" And thing.getAttribute("country") = obj.name Then
					Set obj = thing
				End If
			Next
			valtype = obj.getAttribute("datatype")
			required = Not IsNull(obj.getAttribute("required"))
		End If
		country = countryobj.options(countryobj.selectedIndex).text
		If country = "USA" Then
			valtype = "usazip"
		ElseIf country = "Canada" Then
			valtype = "canadazip"
		End If	
	End If

	' Set Pattern
	pattern = reDict(valtype)
	
	' Check to see if it is required
	If Not required Then pattern = "("&pattern&")|(^$)"
		Set regEx = New RegExp
	regEx.Pattern = pattern
	regEx.IgnoreCase = True
	matches = regEx.Test(obj.value)
	If valtype = "date" Then
		If required then
			matches = CheckDate(obj.value)
		Else
			matches = (CheckDate(obj.value) Or obj.value = "")
		End If
	End If
	badnow = submitDict.Exists(obj.name)
	If matches Then	
		obj.style.border = "thin inset white"
		obj.style.backgroundcolor = "white"
		If badnow Then submitDict.Remove obj.name	
	ElseIf Not matches Then
		obj.style.border = "thin solid red"
		If Not badnow Then submitDict.Add obj.name, False
	End If
	If badnow Then
		hide_helpwin()
		obj.style.backgroundcolor = "white"
	End If
End Sub

Sub validate_all(byVal formObj)
	For Each thing in formObj
		If thing.getAttribute("datatype") <> "" Then validate_field(thing)
	Next	
End Sub

Sub prepare_all()
	document.body.appendChild(helpwin)	
	For Each thing in document.body.all
		If Not IsNull(thing.getAttribute("validated")) Then
			thing.onSubmit = GetRef("submit_form")
			For each otherthing in thing
				If Not IsNull(otherthing.getAttribute("datatype")) Then
					otherthing.language = "VBScript"
					otherthing.onBlur = GetRef("validate_this")
					If otherthing.getAttribute("datatype") <> "country" Then otherthing.onKeyDown = GetRef("get_help")
				ElseIf Not IsNull(otherthing.getAttribute("required")) Then
					otherthing.language = "VBScript"
					otherthing.onBlur = GetRef("validate_this")
					otherthing.onKeyDown = GetRef("get_help")
				End If
			Next
		End If
	Next	
End Sub

Sub submit_form()
	validate_all(window.event.srcElement)
	If submitDict.Count > 0 Then
		alert("The form cannot be submitted until all fields contain valid data."&VbCrLf&"Fields with invalid data are outlined in red."&VbCrLf&"Press F2 while in a field for help fixing the information.")
		window.event.returnValue = false
	End If
End Sub

'Create Help Window
Set helpwin = document.createElement("div")
helpwin.innerText = ""
helpwin.id = "helpwin"
helpwin.style.backgroundcolor = "#eeeeee"
helpwin.style.left = 300
helpwin.style.top = 50
helpwin.style.width = 150
helpwin.style.padding = "5px"
helpwin.style.fontFamily = "Verdana, Arial, Helvetica"
helpwin.style.fontSize = "10pt"
helpwin.style.border = "thin solid"
helpwin.style.borderColor = "black"
helpwin.style.position = "absolute"
helpwin.style.visibility = "hidden"

Sub help_user(help_text)
	helpwin.innerHTML = help_text
	helpwin.style.left = window.event.x + document.body.scrollLeft + 50
	helpwin.style.top = window.event.y + document.body.scrollTop
	helpwin.style.visibility = "visible"
End Sub

Sub hide_helpwin()
	helpwin.style.visibility = "hidden"
End Sub

Sub get_help()
	If window.event.keyCode = 113 Then
		Set obj = window.event.srcElement
		If submitDict.Exists(obj.name) Then
			valtype = obj.getAttribute("datatype")
			required = Not IsNull(obj.getAttribute("required"))
			If IsNull(valtype) And required Then valtype = "required"
		
			If valtype = "zipcode" Then
				For each thing in obj.form
					If thing.getAttribute("datatype") = "country" And thing.name = obj.getAttribute("country") Then
						Set countryobj = thing
					End If
				Next
				country = countryobj.options(countryobj.selectedIndex).text
				If country = "USA" Then
					valtype = "usazip"
				ElseIf country = "Canada" Then
					valtype = "canadazip"
				End If	
			End If
			
			help_user(expDict(valtype))
			obj.style.backgroundcolor = "#eeeeee"
		End If
	End If
End Sub

