
exitPopupMessage = "";              // no default message
function ExitPopup() {
    // NOTE: the cookie 'exitPopup' is always removed by "global.php" when a new VisitSales record is created
    if (ReadCookie('exitPopup') == 'sleep') { // if the user has already tried to close the window ...
        ExitPopup_Disable();
    }

    if (exitPopupMessage != "") {
        CreateCookie('exitPopup','sleep');

        if (document.location.href.indexOf(".php") == -1)
            separator = "index.php?";
        else if (document.location.href.indexOf("?") == -1)
            separator = "?";
        else
            separator = "&";

        // redirection / refreshes the page that contains the coupon code entry field
        //if (document.location.href.indexOf('payment_s4s') > 0)
            document.location.href += separator + "cc=" + ReadCookie('exitPopup');    //document.location.reload(true);

        return exitPopupMessage;    // show the exit pop up dialog box with the OK/Cancel options
    }
}

exitPopupMessage_old = "";
function ExitPopup_Disable() {
    exitPopupMessage_old = exitPopupMessage;
    exitPopupMessage = "";      // clean up the exitPopup message so it won't be seen again
}

function ExitPopup_Enable() {
    exitPopupMessage = exitPopupMessage_old;
}

window.onbeforeunload = ExitPopup;


//==============================================================================
// COOKIE OPERATIONS  (ref: http://www.quirksmode.org/js/cookies.html)
function CreateCookie(name,value,days) {
    var expires = "";
    if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	expires = "; expires="+date.toGMTString();
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function ReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function RemoveCookie(cookieName) {
    //document.cookie = cookieName + '=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT';
    CreateCookie(cookieName,"",-1);
}

