<div class="modal inmodal" id="inactive-timeout" tabindex="-1" role="dialog">
	<div class="modal-dialog">
		<div class="modal-content animated fadeIn">
			<div class="modal-header">
				<h4 class="modal-title">Session Timeout Warning</h4>
			</div>
			<div class="modal-body">
				<div>
					<p>
						You've been inactive for a while. For your security, we'll automatically log you off in approximately two minutes unless you click "Stay Online" to continue.
					</p>
					<p>
						Or you may click "Log Off" now if you're done.
					</p>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-white"><a href="/logout">Log off</a></button>
				<button type="button" class="btn btn-primary" onclick="closeInactivePopup();">Stay Online</button>
			</div>
		</div>
	</div>
</div>

<script>
 
	$(document).ready(function (e) {
		
		var currentTime = 0;
		var date = new Date();
	 	var currentTime = date.getTime(); 

		// popup time set in cookie
	 	var minutesforPopup = 60 * 60000;
	 	var popupTime = parseInt(currentTime) + parseInt(minutesforPopup);
	 	$.cookie('popupTime', popupTime, {  path: '/' });
	 	popupTime = $.cookie('popupTime');
	 	
	 	// expiry time set in cookie
	 	var minutesforExpiry = 62 * 60000;
	 	var expiryTime = parseInt(currentTime) + parseInt(minutesforExpiry);
	 	$.cookie('expiryTime', expiryTime, {  path: '/' });
	 	expiryTime = $.cookie('expiryTime');
	   

	    setInterval(function(){ 
	    	expiryTime = $.cookie('expiryTime');
	    	popupTime = $.cookie('popupTime');
	    	var date = new Date();
	    	currentTime = date.getTime();
	    	
		    if(currentTime >= expiryTime) { //2 minutes
		    	formmodified=0;
		    	window.location.href = "/logout";
		    }
		    
		    if (currentTime >= popupTime) { // 10 minutes
		      	$('#inactive-timeout').modal('show');
		    } else {
		    	$('#inactive-timeout').modal('hide');
		    }			

	    }, 60000);

		setInterval(function(){ 
	    	expiryTime = $.cookie('expiryTime');
	    	popupTime = $.cookie('popupTime');
	    	var date = new Date();
	    	currentTime = date.getTime();
		    
		    if (currentTime >= popupTime) { // 10 minutes
		      
		    } else {
		    	$('#inactive-timeout').modal('hide');
		    }			

	    }, 2000);
	    //Zero the idle timer on mouse movement.
	    
	    $(this).keypress(function (e) { 

	    	var date = new Date();
		 	var currentTime = date.getTime(); 

	        // popup time set in cookie
		 	minutesforPopup = 60 * 60000;
		 	popupTime = parseInt(currentTime) + parseInt(minutesforPopup);
		 	$.cookie('popupTime', popupTime, {  path: '/' });
		 	popupTime = $.cookie('popupTime');

	        
	        minutesforExpiry = 62 * 60000;
		 	expiryTime = parseInt(currentTime) + parseInt(minutesforExpiry);
		 	$.cookie('expiryTime', expiryTime, {  path: '/' });
		 	expiryTime = $.cookie('expiryTime');

	    });
	});

	function closeInactivePopup() {
		$('#inactive-timeout').modal('hide');
		
		var date = new Date();
	 	var currentTime = date.getTime(); 

        // popup time set in cookie
	 	minutesforPopup = 60 * 60000;
	 	popupTime = parseInt(currentTime) + parseInt(minutesforPopup);
	 	$.cookie('popupTime', popupTime, {  path: '/' });
	 	popupTime = $.cookie('popupTime');

        
        minutesforExpiry = 62 * 60000;
	 	expiryTime = parseInt(currentTime) + parseInt(minutesforExpiry);
	 	$.cookie('expiryTime', expiryTime, {  path: '/' });
	 	expiryTime = $.cookie('expiryTime');

	}

</script>