Detecting flash/html window closed

Hi

is it possible for flash to detect when a user closes an html window with a swf embeded in it?

I want to record a user session time and need to be able to call a function and serverside script from within flash when a user closes the window…is there a way?

(I thought I might be able to do it with onUnload, but that will only work for unloading movieclips, not closing the main swf.)

thanks for your help