I won't say that I don't think there's a way to do this with JavaScript, cause I'm 90 percent sure there is, but I don't know what it is. I suggest checking out [url=http://www.webmonkey.com] and their JS tutorials if you wish to keep looking.
I suggest, however, if you're already writing CGI scripts, just have the job that the JS was going to do done by a CGI. You can print HTML to the browser with Perl just as easily as with JS and it's easy to access the variables in that language. They will be stored as post or get variables from the first form and then you can just do a <input type="hidden" name="varname" value=$variable1>
Any reason you're avoiding that?