
// Keep user from entering more than maxLength characters
function doKeypress(evt, control, maxlength){
	if(window.event)
	{
		if(control.value.length > (maxlength - 1)){
			if(!IsKeyCodeValid(event.keyCode))
			{
				event.returnValue = false;
			}
		}
	}
	else
	{
		// Check if key pressed is one of the following
		// 8 = Backspace
		// If yes, don't cancel event
		if(evt.which != 0 && evt.which != 8)
		{
			if(control.value.length > (maxlength - 1)){
				if(window.event)
				{
					event.returnValue = false;
				}
				else
				{
					evt.returnValue = false;
					evt.cancelBubble = true;
					evt.preventDefault();
				}
			}
		}
	}
}

function IsKeyCodeValid(key)
{
	if(key == 8)
	{
		return true;
	}
	if(key >= 63232 && key <= 63235)
	{
		return true;
	}
	if(key == 63272)
	{
		return true;
	}
	return false;
}

// Cancel default behavior
function doBeforePaste(control){
    maxLength = control.attributes["maxLength"].value;
     if(maxLength)
     {
          event.returnValue = false;
     }
}
// Cancel default behavior and create a new paste routine
function doPaste(control){
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
     if(maxLength){
          event.returnValue = false;
          maxLength = parseInt(maxLength);
          var oTR = control.document.selection.createRange();
          var iInsertLength = maxLength - value.length + oTR.text.length;
          var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
          oTR.text = sData;
     }
}

function CheckValue(id, maxlength)
{
	control = document.getElementById(id);
	if(control != null)
	{
		if(control.value.length > maxlength)
		{
			control.value = control.value.substring(0, maxlength);
		}
	}
}

