window.onbeforeunload  = showPopupOnExit;
var cookieName = 'questionnaire';
var suppress = false;

 //Intended to be called when page unloads
function showPopupOnExit()
{
    try
    {
        if(!haveCookieValue() && !suppress)
        {
            setCookieValue();
            
            newwindow=window.open('/aspxpages/QuestionnairePopup.aspx?TB_iframe=true&height=310&width=490&modal=true&wasExit=1','QuestionnairePopup', 'height=310,width=500');
            if (window.focus) {newwindow.focus()}
        }
     }
     catch(err)
     {
        //alert('Error:' + err.message);
     }
}

 // Gets the cookie value if it exists.
function getCookieValue()
{
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(cookieName+ "=");
      if (c_start!=-1)
        {
        c_start=c_start + cookieName.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
}

// Sets the cookie value to ensure the user is only prompted once.
function setCookieValue() 
{
    var expire = new Date("January 1, 2030");
    document.cookie = cookieName + "=" + escape('answered') + ";expires=" + expire.toGMTString() + ";path=/";
}

 // Check if user has cookie value already.
function haveCookieValue()
{
    var cookieValue = getCookieValue();
    return (cookieValue != '');
}

//suppress the popup
function suppressPopUp() {
    suppress= true;
}
