﻿/* NOTE: If you experience an error it is most likely due to the strict AJAX security, make sure that you are accessing the correct URL, for example, if you have http://domain.com in your browser, and http://www.domain.com in the 'ExitPopURL' then there will be a conflict. they must both match. .htaccess to ensure that your visitors are visiting www. is good practice here.*/
var AlertBox = "*****************************************************\n\nWait! Don't pass on this offer to lower your Auto Insurance Rate!\n\nSelect 'Cancel' below to stay.\n\n*****************************************************";

var PoptimerID = '';
var done = false;


window.onload = function() {
    // this is where we start our journey...
    createExitPop();
} // end function onunload

function ajaxGET(divId, page, effect) {
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari 
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer 
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            if (effect == 'collapse') { document.getElementById(divId).style.display = 'none'; }
            else { document.getElementById(divId).innerHTML = xmlHttp.responseText; }
        }
    }
    xmlHttp.open("GET", page, true);
    xmlHttp.send(null);
}

function createExitPop() {
    var theBody = document.getElementsByTagName('body')[0];
    var popUp = document.createElement('div');
    popUp.setAttribute('id', 'ExitDiv');

    popUp.style.width = ExitPopWidth + "px";
    popUp.style.height = (parseInt(ExitPopHeight) + 16) + "px";

    var popNav = document.createElement('div');
    popNav.setAttribute('id', 'PopupNav');
    popNav.innerHTML = "<div align='left' style='float: left; width: 70%; background-color:#003399; color :White; font-weight:bold';valign='top' >Special Offer!</div><div style='background-color:#003399; color :White; text-align:right;'><a href='javascript:void(0);' onclick='javascript:hidePopup();' style ='color :White;'>Close - [X]</a></div>";


    var popContent = document.createElement('div');
    popContent.setAttribute('id', 'PopupContent');

    popUp.appendChild(popNav);
    popUp.appendChild(popContent);
    // put div on page
    theBody.appendChild(popUp);

    //add exit pop to page (contents are from your exit.php(or whatever you named it) page)
    document.getElementById('ExitDiv').value = ajaxGET('PopupContent', ExitPopURL);

    // style exit pop to resemble its own page
    document.getElementById('ExitDiv').style.display = "none";
    document.getElementById('ExitDiv').style.top = '150px';
    document.getElementById('ExitDiv').style.left = '100px';
    document.getElementById('ExitDiv').style.position = 'absolute';
    document.getElementById('ExitDiv').style.backgroundColor = '#FFFFFF';

} // end createExitPop

isProperExit = false;

function ExitPop(isProperExit) {
    if (isProperExit != true) {
        isProperExit = true;
        isPop = true;

        var popUp = document.getElementById("ExitDiv");
        popUp.style.display = "";

        return AlertBox;
    } // end if
    else {
        document.getElementById('ExitDiv').style.display = "none";
    }
} // end function

function hidePopup() {
    var popUp = document.getElementById("ExitDiv");
    popUp.style.display = "none";
}

window.onbeforeunload = function() {

    // Lay down an exit pop!!
    return ExitPop(isProperExit);

} // end function onunload

function HitTest() {
    if (PoptimerID != '') clearTimeout(PoptimerID);
    isProperExit = true;
    PoptimerID = setTimeout("hcInterval();", 1000);
}

function hcInterval() {
    if (!done) {
        isProperExit = false;
    }
    PoptimerID = '';
}
document.documentElement.onmousedown = HitTest;
document.documentElement.onclick = HitTest;
document.documentElement.onkeypress = HitTest;


