PopUp On Exit


I would like to add a popup when the user leaves my site.
I would like to gather feedback from the user about the website, as it is due to be launched and will be in testing phase.
I would like to have a popup box initially that asks them if they are willing to complete a questionnaire at the end of the session, and if yes, when they exit the site they will get another popup asking them to complete the questionnaire.

I am not sure how I would approach this - any ideas?