/*
 * Written by tlhunter January 2010
 * Purpose: Provide a popup window when a user exits a page
 * Usage:
	<script src="/scripts/jquery-1.4.1.js" type="text/javascript"></script>
	<script src="/scripts/exit_popup.js" type="text/javascript"></script>
	<script language="JavaScript">
		surveyUrl = "http://www.dow.com";
		surveyUnder = true;
	</script>
 */

exitPageViaClick = false;	// Leave as false
surveyUnder = false;		// Set in calling document
surveyUrl = "";				// Set in calling document
function pageExitPopupCheck() {

	if (!exitPageViaClick && (getCookie("x1DowTakenOrDeclined") != "true")) {
		if (surveyUrl != "") {
			window.open(surveyUrl, "_blank", "toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=700,height=550,top=20,left=50");
			if (surveyUnder) {
				window.focus();
			}
		} else {
			// oops, our calling document didn't set surveyUrl!
		}
	}
}
window.onunload = pageExitPopupCheck;

function normalLeave(destUrl) {
	exitPageViaClick = true;
	window.location = destUrl;
}

$(document).ready(function() {
	$("a").each(function() 
    {
    
		var destUrl = $(this).attr("href");
        if (destUrl.indexOf("mailto") == -1) 
        {
            if (destUrl.indexOf("http://") == -1) 
            {
                    $(this).click(function() {
                        normalLeave($(this).attr("href"));
                        return false;
                    });
     
                $(this).attr("href", "javascript:normalLeave('" + destUrl + "');");
                $(this).addClass("nosurvey"); // This can be removed
            } 
            else {
                $(this).addClass("survey");   // This can be removed
            }
        }
	});
$("map").each(function() {
	$("area").each(function() {
		var destUrl = $(this).attr("href");
		if (destUrl.indexOf("http://") == -1) {
			$(this).attr("href", "javascript:normalLeave('" + destUrl + "');");
			$(this).addClass("nosurvey"); // This can be removed
		} else {
			$(this).addClass("survey");   // This can be removed
		}
	});
});
});

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.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 "";
}

function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value)+	((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}

/* This needs to be executed to prevent popups from happening again. */
function SetNoInviteCookie() {
	var exp = new Date();
	exp.setTime(exp.getTime() + 365 * 24 * 60 * 60 * 1000);
	document.cookie = "x1DowTakenOrDeclined=" + "true" + ";path=/;Expires=" +exp.toGMTString();
	parent.document.getElementById('surveyarea').style.display = 'none';
	parent.document.getElementById('fade').style.display = 'none';
}

