Calling JS function using ExternalInterface

I’m very pressed for time and I hope someone can help me in lieu of extensive archive searching…

DISCLAIMER: I don’t know JavaScript. The first code block I show below comes from someone who helped me, and he’s not available now.

My swf is a quiz that the user should not close until they’re finished because their progress will be lost. So I’ve added the following javascript in the html page:

<script language = "javascript">
	window.onbeforeunload = function (evt) {
		var message = 'Are you sure? Progress will be lost.';
		if (typeof evt == 'undefine') {
			evt = window.event;
		}
		if (evt) {
			evt.returnValue = message;
		}
		return message;
	}
</script>

So far so good. HOWEVER, once the user finishes the quiz and hits the Results page, this alert is unnecessary (and actually inaccurate) because their results have been saved to a database.

So I’m thinking I want to include this in the javascript:

var okToClose = false;

…and then call a function from Flash that sets okToClose to true, which will then presumably cause the alert to not show up. So I’ve added to the javascript so now it looks like this:

 < script language = "javascript" >
	var okToClose = false;
	window.onbeforeunload = function (evt) {
		if (okToClose == false) {
			var message = 'Are you sure? Progress will be lost.';
			if (typeof evt == 'undefine') {
				evt = window.event;
			}
			if (evt) {
				evt.returnValue = message;
			}
			return message;
		} else {
			var message = 'OK to close now.'; <!-- just to verify it is working -->
			if (typeof evt == 'undefine') {
				evt = window.event;
			}
			if (evt) {
				evt.returnValue = message;
			}
			return message;
		}
	}
	function okToClose() {
		okToClose = true;
	}
</script>

Then I call okToClose from Flash when I hit the Results page:

ExternalInterface.call("setOKtoClose");

This doesn’t work. No errors, I just don’t get the behavior I need. What am I doing wrong?