My first question is why use code? I ask because this can all be accomplished using some simple rules within infopath. Though I do find it easier sometimes to use code myself.
Also the incrementing number would have to be saved in a list or database field. A new form would be opened, query that field, add 1 to it, then save it back. I just did that in an access database but saved the number as an integer, and then did format(FIELD, "00000") to get the correct format for use in the filename.
I don't know javascript, but I might be able to find some in vb.net