﻿function doKeypress(e, maxLength)
{
    if(!isNaN(maxLength))
    {
        maxLength = parseInt(maxLength)
        var oTR = e.document.selection.createRange()
        if(oTR.text.length >= 1)
            event.returnValue = true
        else if(e.value.length > maxLength-1)
            event.returnValue = false
    }
}

function doKeydown(e, maxLength)
{
    setTimeout(function()
    {
        maxLength = parseInt(maxLength)
        if(!isNaN(maxLength))
        {
            if(e.value.length > maxLength-1)
            {
                var oTR = window.document.selection.createRange()
                oTR.moveStart("character", -1*(e.value.length-maxLength))
                oTR.text = ""
            }
        }
    },1)
}

function doBeforePaste(e, maxLength)
{
    if(!isNaN(maxLength))
        event.returnValue = false
}

function doPaste(e, maxLength)
{
    if(!isNaN(maxLength))
    {
        event.returnValue = false
        maxLength = parseInt(maxLength)
        var oTR = e.document.selection.createRange()
        var iInsertLength = maxLength - e.value.length + oTR.text.length
        var sData = window.clipboardData.getData("Text").substr(0, iInsertLength)
        oTR.text = sData;
    }
}
