This is a Javascript question. But what you want is an onSubmit handler in your <form> tag. And if you don't want to go to another page, an action of "#". But then the bit that document.write() did would be lost.
So you want <input type="button" onclick="test()"> instead.
And if you do want to submit it after all, have form.submit() in the function.