Restore will always download and install the latest OS version from Apples server which at the moment is iOS4.0.
If you want to restore to a previous version like 3.1.2\3.1.3 you need to download a copy of that version .ipsw file to your computer and then hold the shift key (PC) or command key (mac) when pressing the restore button.
This will open a browser window for you to locate your downloaded .ipsw file and iTunes will use that to restore.