Actually the right answer is for you to download
YUI,
Scriptaculous,
Prototype,
Mootools,
JQuery, or one of the other
Javascript frameworks, and use their pre-made functions to do this stuff. They have been tested, they are free/open source, they have great cross-browser compatibility built in, and you don't have to re-invent the wheel.