<PUBLIC:COMPONENT id="bhvmaxLength" urn="sUrnmaxLength">
<PUBLIC:PROPERTY name="maxLength" />
<PUBLIC:ATTACH EVENT="onload" FOR=window ONEVENT="InitMaxLength()" />
<PUBLIC:ATTACH EVENT="onkeypress" FOR=element ONEVENT="KeyPress()" />
<PUBLIC:ATTACH EVENT="onpaste" FOR=element ONEVENT="Paste()" />

<SCRIPT language="VBScript">
	Dim NOTSET
	NOTSET = -1

	'Initialize maxLength (ie. check to see if maxLenght property is set)
	Sub InitMaxLength
		If IsNull(maxLength) Then
			'maxLength is not defined, therefore there is no max length.
			'NOTSET (-1) will be our flag to denote no max length.
			maxLength = NOTSET
		End If
	End Sub

	Sub KeyPress
		Dim objTR

		Set objTR = element.document.selection.createRange()
		If Len(objTR.text) >= 1 Then						'Allows a user to type over selected text
											'even if maxLength has been reached.
			window.event.returnvalue = TRUE
		ElseIf (Not (maxLength = NOTSET)) AND (Not IsNuLL(maxLength)) Then	'Do not impose maxLength if property not set.
			If Len(value) >= CInt(maxLength) Then
				window.event.returnvalue = FALSE			'Do not allow user to exceed maxLength characters
			End If
		End If
	End Sub

	Sub Paste
		Dim objTR
		Dim intInsertLength
		Dim strPasteData

		'Redefine the paste behavior to only paste as much as is allowed without
		'exceeding the maxLength
		If (Not (maxLength = NOTSET)) AND (Not IsNull(maxLength)) Then
			window.event.returnvalue = FALSE				'"Turns Off" the default event action
			Set objTR = element.document.selection.CreateRange()
			intInsertLength = CInt(maxLength) - Len(value) + Len(objTR.text)
			if intInsertLength < 0 then intInsertLength = 0		'If the element contains the maximum number of characters, then paste nothing.
			strPasteData = Left(window.clipboardData.getData("Text"), intInsertLength)
			objTR.text = strPasteData
		End If
	End Sub
</SCRIPT>
</PUBLIC:COMPONENT>