What do you want to know is wrong with your JavaScript? We need more specifics. It works for what your logic is, even though you don't use the cookie portion of the code. I used the latest version...
I cannot remember which is better, but you want to bind your input field to a keydown or keyup event. jQuery works well here and following page has an example utilizing the event:
...