// set leaving flag to true
// used by exitTraffic() function
var leaving = true;

function getCookieData(labelName){
	var labelLen = labelName.length;
	var cookieData = document.cookie;
	var cLen = cookieData.length;
	var i = 0;
	var cEnd;
	while (i < cLen){
		var j = i + labelLen;
		if (cookieData.substring(i,j) == labelName){
			cEnd = cookieData.indexOf(";",j)
			if(cEnd == -1){
				cEnd = cookieData.length;
			}
			return unescape(cookieData.substring(j+1, cEnd));
		}
		i++;
	}
	return "";		
}

// called from window.onunload event
function exitTraffic(){
	// leaving is true when page loads
	// leaving is set to false by every link on the site and when any form is submitted
	// if leaving is still true then user is leaving the site so launch popunder
	if (leaving){
		// set redirect cookie flag so that user only gets popunder once per month
		var redirectCookie = getCookieData("redirectCookie");
		// check for existance noPopUp cookie
		// if cookie exists, then user has already downloaded
		// if cookie does not exist, prompt to launch popDownload.asp popup
		var exitCookie = getCookieData("noPopUp");
		/* This part is for download pop-up
		if(exitCookie != 'true'){
			if(confirm("Click OK to download our FREE Software while browsing the site")){
				popDownload(the_language);
			}
		}
		*/
		// if cookie not found (equal to true) then launch popover
		if(redirectCookie != 'true'){
			var expDate = new Date();
			var oneMonthFromNow = expDate.getTime() + (30 * 24 * 60 * 60 * 1000);
			expDate.setTime(oneMonthFromNow);
			document.cookie = "redirectCookie=true; expires=" + expDate.toGMTString();
			//exitWin=window.open("https://www.bingocard.com/amsBGC/campaignhit.action?trackingId=116182&redirecturl=http://www.bingocard.com/paasBGC/home.action","BML_exit","");
			//exitWin.blur();  // pop "under"
			//exitWin.focus();  // pop "over"
		}
	}
}

// call exitTraffic() function when window unloads
window.onunload=exitTraffic;