' File: Logon_lo.vbs
' Date: PN20021205 PN20021204 PN20021119 PN20021030 PN20020520 PN20020429 PN20020424 
'
' To include this file in your .asp, you must use the #include statement:
'	<SCRIPT LANGUAGE=VBScript SRC="/Logon/Scripts/Logon_lo.vbs"></SCRIPT>
'
' SUMMARY
'
' This is a set of Visual Basic Script functions for Logon related fields
' and form validations.
'
' SPECIAL NOTE:
' The contents of the file Validate.js is required for the operation of the 
' functions in this file.
' 
' GLOBAL VARIABLES CAN BE SET IN THE CALLING ASP:
' lo_DisplayMessage						Display the error message dialog when validation 
'															failed. The default is set to True.
'
' FIELD VALIDATION FUNCTIONS:
'
' lo_fldValidate(fld)					Logon database field validation cover function.
'
' lo_checkClientId(fld)				Validate the field "ClientId".
' lo_checkGroupId(fld)				Validate the field "GroupId".
' lo_checkUserId(fld)					Validate the field "UserId".
' lo_checkUserName(fld)				Validate the field "UserName".
' lo_checkSignInName(fld)			Validate the field "SignInName".
' lo_checkPassword(fld)				Validate the field "Password".
' lo_checkUserStatus(fld)			Validate the field "UserStatus".
' lo_checkEmail(fld)					Validate the field "Email".
'	lo_checkContactInfo(fld)		Validate the field "ContactInfo".
'	lo_checkGroupLogoURL(fld)		Validate the field "GroupLogoURL".
' lo_wrnFldValidate(fld, msg)	Show error dialog box depends on the global 
'															variable "lo_DisplayMessage" and set field 
'															focus if failed.
'
' FORM VALIDATION FUNCTIONS:
'
' lo_frmValidate(frm)					Logon database form validation cover function.
'
' lo_checkfrmLogon(frm)				Validate the form "frmLogon".
' lo_wrnFrmValidate(msg)			Show error dialog box depends on the global 
'															variable "lo_DisplayMessage".
'
' MISC. FORM FUNCTIONS:
'
'	lo_ValidPassword(fldValue)	Validate Password value.
'	lo_IsURL(URL)								Validate URL value.
'
'------------------------------------------------------------------------------	
' GLOBAL VARIABLES:
'------------------------------------------------------------------------------	
	Dim lo_DisplayMessage
	lo_DisplayMessage = True
	
'------------------------------------------------------------------------------	
' FIELD VALIDATION FUNCTIONS:
'------------------------------------------------------------------------------	
Function lo_fldValidate(fld)
	Dim IsValid
	Select Case fld.name
		Case "ClientId":		IsValid = lo_checkClientId(fld)
		Case "GroupId":			IsValid = lo_checkGroupId(fld)
		Case "UserId":			IsValid = lo_checkUserId(fld)
		Case "UserName":		IsValid = lo_checkUserName(fld)
		Case "SignInName":	IsValid = lo_checkSignInName(fld)
		Case "Password":		IsValid = lo_checkPassword(fld)
		Case "UserStatus":	IsValid = lo_checkUserStatus(fld)
		Case "Email":				IsValid = lo_checkEmail(fld)
		Case "ContactInfo":	IsValid = lo_checkContactInfo(fld)
		Case "GroupLogoURL":IsValid = lo_checkGroupLogoURL(fld)
		Case Else:					IsValid = False
	End Select
	lo_fldValidate = IsValid	
End Function
'------------------------------------------------------------------------------
Function lo_checkClientId(fld)
	Dim IsValid, fldValue
	fldValue = fld.value
	IsValid = isNonnegativeInteger(fldValue)
	If (Not IsValid) Then
		lo_wrnFldValidate fld, "Client Id must be a non-negative integer."
	End If
	lo_checkClientId = IsValid		
End Function	
'------------------------------------------------------------------------------
Function lo_checkGroupId(fld)
	Dim IsValid, fldValue
	fldValue = fld.value
	IsValid = isNonnegativeInteger(fldValue)
	If (Not IsValid) Then
		lo_wrnFldValidate fld, "Customer Id must be a non-negative integer."
	End If
	lo_checkGroupId = IsValid		
End Function	
'------------------------------------------------------------------------------
Function lo_checkUserId(fld)
	Dim IsValid, fldValue
	fldValue = fld.value
	fldValue = Replace(fldValue, "-", "", 1, 1)
	IsValid = isAlphanumeric(fldValue)
	If (Not IsValid) Then
		lo_wrnFldValidate fld, "User Id must be alphanumeric in 'XX-XXXX' format up to 30 characters."
	End If
	lo_checkUserId = IsValid		
End Function	
'------------------------------------------------------------------------------
Function lo_checkUserName(fld)
	Dim IsValid, fldValue
	fldValue = stripWhitespace(fld.value)
	IsValid = isAlphanumeric(fldValue)
	If (Not IsValid) Then
		lo_wrnFldValidate fld, "User name must be alphanumeric(including blanks) up to 30 characters."
	End If
	lo_checkUserName = IsValid		
End Function	
'------------------------------------------------------------------------------
Function lo_checkSignInName(fld)
	Dim IsValid, fldValue
	fldValue = stripWhitespace(fld.value)
	IsValid = isAlphanumeric(fldValue)
	If (Not IsValid) Then
		lo_wrnFldValidate fld, "Sign in name must be alphanumeric(including blanks) up to 30 characters."
	End If		
	lo_checkSignInName = IsValid
End Function	
'------------------------------------------------------------------------------
Function lo_checkPassword(fld)
	Dim IsValid, fldValue
	fldValue = fld.value
	IsValid = lo_ValidPassword(fldValue)
	If (Not IsValid) Then
		lo_wrnFldValidate fld, "Password must be alphanumeric(excluding blanks) up to 15 characters."
	End If
	lo_checkPassword = IsValid		
End Function	
'------------------------------------------------------------------------------
Function lo_checkUserStatus(fld)
	Dim IsValid, fldValue
	fldValue = fld.value
	IsValid = isPositiveInteger(fldValue)
	If (Not IsValid) Then
		lo_wrnFldValidate fld, "User status must be positive integer."
	End If
	lo_checkUserStatus = IsValid		
End Function	
'------------------------------------------------------------------------------
Function lo_checkEmail(fld)
	Dim IsValid, fldValue, emptyOK
	fldValue = fld.value
	emptyOK = defaultEmptyOK
	defaultEmptyOK = True
	IsValid = isEmail(fldValue)
	defaultEmptyOK = emptyOK
	If (Not IsValid) Then
		lo_wrnFldValidate fld, "Email entry is invalid."
	End If
	lo_checkEmail = IsValid		
End Function	
'------------------------------------------------------------------------------
Function lo_checkContactInfo(fld)
	Dim IsValid, fldValue
	fldValue = fld.value
	If Len(fldValue) > 500 Then
		fld.value = Left(fldValue, 500)
	Else
		IsValid = True
	End If
	If (Not IsValid) Then
		lo_wrnFldValidate fld, "Contact Information is limited to 500 characters."
	End If
	lo_checkContactInfo = IsValid		
End Function	
'------------------------------------------------------------------------------
Function lo_checkGroupLogoURL(fld)
	Dim IsValid, fldValue
	fldValue = fld.value
	IsValid = lo_IsURL(fldValue)
	If (Not IsValid) Then
		lo_wrnFldValidate fld, "Group Logo URL entry is invalid."
	End If
	lo_checkGroupLogoURL = IsValid		
End Function	
'------------------------------------------------------------------------------
Sub lo_wrnFldValidate(fld, msg)
'Global variable: lo_DisplayMessage 
	If lo_DisplayMessage Then
		window.alert(msg)
		fld.focus
		If fld.type = "text" Then
			fld.select
		End If
	End If
End Sub
'------------------------------------------------------------------------------
'FORM VALIDATION FUNCTIONS
'------------------------------------------------------------------------------
Function lo_frmValidate(frm)
	Dim IsValid
	Select Case frm.name
		Case "frmLogon":			IsValid = lo_checkfrmLogon(frm)
		Case Else:						IsValid = False
	End Select
	lo_frmValidate = IsValid	
End Function
'------------------------------------------------------------------------------
Function lo_checkfrmLogon(frm)
'PN20021030
	Dim IsValid, blnMsg
	blnMsg = lo_DisplayMessage
	IsValid = False
	If (frm.GroupId.value = "") OR (frm.SignInName.value = "") OR (frm.Password.value = "") Then
		lo_wrnFrmValidate "All fields must be entered."
	Else
		lo_DisplayMessage = False
		IsValid = lo_checkGroupId(frm.GroupId)
		IsValid = IsValid AND lo_checkSignInName(frm.SignInName)
		IsValid = IsValid AND lo_checkPassword(frm.Password)
		lo_DisplayMessage = blnMsg
		If Not IsValid Then
			lo_wrnFrmValidate "Invalid Group Id, Sign In Name, and/or Password."
		End If
	End If
	lo_checkfrmLogon = IsValid
End Function
'------------------------------------------------------------------------------	
Sub lo_wrnFrmValidate(msg)
'Global variable: lo_DisplayMessage 
	If lo_DisplayMessage Then
		window.alert(msg)
	End If
End Sub
'------------------------------------------------------------------------------
'MISC. FUNCTIONS
'------------------------------------------------------------------------------
Function lo_ValidPassword(fldvalue)
	Dim IsValid
	IsValid = isAlphanumeric(fldValue)
	lo_ValidPassword = IsValid		
End Function	
'------------------------------------------------------------------------------
Function lo_IsURL(URL)
	Dim IsValid, intI, intFound, strPrefix
	Dim ValidChars
	Dim Digits, LowercaseLetters, UppercaseLetters, FileNameDelimiter, OtherValidchars
	
	IsValid = True
	If Len(URL) <> 0 Then
		Digits = "0123456789"
		LowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
		UppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		FileNameDelimiter = "/"
		OtherValidChars = ":._-&?%;"
		ValidChars = Digits & LowercaseLetters & UppercaseLetters & FileNameDelimiter & OtherValidChars

		'Check for invalid characters
		For intI = 1 To Len(URL)
			If (InStr(1, ValidChars, Mid(URL, intI, 1)) = 0) Then
				IsValid = False
			End If
		Next

		If IsValid Then
			intFound = InStr(1, URL, "://")
			If intFound <> 0 Then
				'URL with http prefix
				strPrefix = Left(URL, intFound-1)
				If NOT (strPrefix = "http") OR (strPrefix = "https") Then
					IsValid  = False
				End If
			End If
		End If
	End If
	lo_IsURL = IsValid
End Function
