var disablePopup = true;
var disableRedirect = true;
var onExitForm = null;

url = window.location;
if (/^([^#]+?)#/.test(url)) {
	url = RegExp.$1;
}

if (/\?/.test(url)) {
	url += '&_reload=1';
} else {
	url += '?_reload=1';
}

url += '#onexit';
var onExitRedirectUrl = url;

 $(document).ready(function() {
	$("a[@rel!='external']").click(function() { disablePopup = true; disableRedirect = true; return (true); });
	if (/#onexit$/.test(window.location)) {
		disablePopup = true;
		disableRedirect = true;

		$('#onexit').remove().appendTo('body');
		$('#content').empty();
		$('#onexit').remove().appendTo('#content');
		$('#onexit').css('display', 'block');

		if (onExitForm) {
			$('#content').empty();
			$.ajaxSetup({async: false});
			$('#content').load(onExitForm);
			$.ajaxSetup({async: true});
		}
	}

	if (!disableRedirect) window.onunload = exitRedirect;
	if (!disablePopup) window.onbeforeunload = exitPopup;
 });

function exitPopup()
{
	if (!disablePopup) {
		disablePopup = true;
		disableRedirect = true;

		$('#onexit').remove().appendTo('body');
		$('#content').empty();
		$('#onexit').remove().appendTo('#content');
		$('#onexit').css('display', 'block');

		if (onExitForm) {
			$('#content').empty();
			$.ajaxSetup({async: false});
			$('#content').load(onExitForm);
			$.ajaxSetup({async: true});
		}
		return "*****************************************************\n\n" +
			   "We appreciate your feedback, and we need it\n" +
			   "in order to make our service better in the future.\n\n" + 
			   "Please click on the CANCEL button to stay on the current page.\n\n" +
			   "*****************************************************";
	}
}

function exitRedirect()
{
	if (!disableRedirect) {
		window.location.href = onExitRedirectUrl;
	}
}

