I think it is overly convoluted. You don't have to use cookies and and body onload event. Just keep validate()
called on form submit and changeDocument()
to display your message.
Inside of validate()
if validation failed call changeDocument()
before returning false.