var URL = '/jsps_hmr/common/exit_Popup.jsp';
var windowprops = 'width=550,height=300,menubar=no,toolbar=no,scrollbars=no,resizable=no,status=no';
var domain = 'nutrisystem.com';
var winRef;
var leaving = true;

function PopIt(){
	closePopup();
	winRef = window.open(URL,'ExitPopup',windowprops); 
	if(winRef != null){
		winRef.focus();
	}
}
function closePopup(){ 
	if(winRef && !winRef.closed){winRef.close();} 
}
function UnPopIt(){ /* Do Nothing */ }

var posX = null; var posY = null; var myEvent = null;
function leavingSite(){
	
	/*Don't show exit popup if User has seen it ONCE- 
		1. within a session and 
		2. on a browser close or leaving nutrisystem domain
	*/ 
	var exitPopupSeen = Get_Cookie("NSDomainExitPopupSeen");
	if(exitPopupSeen != null){
		leaving = false;
		var today = new Date();
		var expires = new Date(today.getTime()); // Now
       	Set_Cookie("NSDomainExitPopupSeen", "", expires, "/", "", "");
	}
	/*End*/
	
	/*Don't show exit popup if User has LOGGED-IN and has LOGGED-OUT WITH-IN THE BROWSER SESSION*/ 
	var loggedInUser = Get_Cookie("NSDomainExitPopupLoggedInUser");
	if(loggedInUser != null){
		leaving = false;
	}
	/*End*/
	
	if(leaving){
		if(typeof Event == 'undefined'){ 
			// alert('IE Browser...');
			myEvent = window.event;
			if(myEvent.clientY < 0){
				var today = new Date();
				var expires = new Date(today.getTime()); // Now
       			Set_Cookie("NSDomain", "", expires, "/", "", "");
				PopIt();
			}
		}else{ 
			// alert('Firefox Browser...');
			myEvent = Event; 
			
				var today = new Date();
				var expires = new Date(today.getTime()); // Now
       			Set_Cookie("NSDomain", "", expires, "/", "", "");
				PopIt();
			
		}
	}
}

$(document).ready(function(){ 
	window.name = 'NSHome';
	Set_Cookie("NSDomain", "1", "", "/", "", "");
		
	window.onbeforeunload = leavingSite;
	
	$("a").click(function(){
		// alert("List of Click Events Registered for this Element are::" + this.onclick);
		window.onbeforeunload = UnPopIt; 
	});
	$("form").submit(function(){
		// alert("List of Events Registered for this Form Submission are::" + this.onsubmit);
		window.onbeforeunload = UnPopIt; 
	});
});