mi dice che str e nullo o non e un oggetto

var maxchars=500;

function CheckLength()
{
with (document.form1)
{
chars=appunti.value
if (chars.length > maxchars)
{
alert('Nel campo appunti sono consentiti massimo 500 caratteri!')
str = chars.value
chars.value = str.substr(0,500)

}

}
}
//-->