All the code is in the document head, when it should be in the body. Or, even better, define it in functions in the head, which you then call from the body. I'm not sure it's possible to call document.writeln after the document has been laid out, though; probably not.