i had a look round the net for an example on how to trap a closing window and it seems lots of people have had the same problem.
this link will take you to a msg board with a couple of suggestions - none of them trivial i reckon.
http://216.239.51.100/search?q=cache:
Phx8sc62_n8C:p2p.wrox.com/archive/javascript/
2001-09/46.asp+trap+browser+closing+with+javascript&hl=en
One guy said that you cant trap the user closing the window with the X button (top right hand corner etc) so what you have to do is trap everything else that may be a problem, in your case:
a refresh or moving to a different page within the same directory.
Then, set a flag to true, only if it is none of these events was fired.
hmmmm....
Have a look at that msg board, there is some javascript on it that you could maybe adapt, - javascript being the way to accomplish this probably.
hth
Martin
p.s. sorry about messed up URL. I couldnt get LQ to stop parsing it in funny ways. (i.e inserting smileys or newlines or completely ommiting some of it)