var alertOpened = 0;
var alertIdleOpened = 0;

function ExpirationOfPublicSessionCountDown(logoutPage)
{
	// Checks if the Identification form exist or not
	if (typeof(document.forms['Identification'])!='undefined')
	{
		var IdentificationForm = document.forms["Identification"];
		if (typeof(IdentificationForm.SessionExpTime)!='undefined')
		{
			// Gets time defined to display alert
			var AlertBefore = IdentificationForm.AlertExpTime.value;
			
			// Total time is decreased second per second
			var TotalTime = parseInt(IdentificationForm.SessionExpTime.value);
			if (TotalTime > 0)
			// Countdown
			{
				// Calculates corresponding H,M and S
				var HTime = Math.floor(TotalTime / 3600);
				var MTime = Math.floor((TotalTime - (3600 * HTime)) / 60);
				var STime = TotalTime - (3600 * HTime) - (60 * MTime);
				
				// Assign new values ('RemainingTime' in sec, 'RemainingTimeDisplay' formatted as HH:MM:SS)
				IdentificationForm.SessionExpTime.value = TotalTime - 10;
				//var TimeDisplayed = HTime + ":" + MTime + ":" + STime;
				var TimeDisplayed = "";
				if (HTime>0)
				{
					TimeDisplayed = HTime + "h "
				}
				
				if (MTime>0)
				{ 
				    TimeDisplayed += MTime + " min "
				}
				
				if (HTime==0 && MTime==0) 
				{
				    TimeDisplayed += STime + " sec "
				}
				
				if(IdentificationForm.AlreadyAlerted)
				{
					// Do not display idle alert if the alert session already occured
					alertOpened=1;
				}
				if ((TotalTime <= AlertBefore) && (alertOpened==0))
				{
					alertOpened=1;
					AlertDisconnectPopup("session-control/AlertDisconnect/"+TotalTime+"?type=session",500,300,"AlertDisconnect");
				}
				document.getElementById("SessionExpDisplay").firstChild.nodeValue = "Temps restant : " + TimeDisplayed;
				
				// Restart function every ten seconds
				setTimeout('ExpirationOfPublicSessionCountDown(\'' + logoutPage + '\')',10000);
			}
			else
			// End of countdown
			{
				// ==> automatically disconnects
				window.location.href = logoutPage;
			}
		}
	}
}

function AlertDisconnectPopup(page,largeur,hauteur,nom)
{
/*
	var top = (screen.height-hauteur)/2;
	var left = (screen.width-largeur)/2;
	var options = 'scrollbars=0, resizable=0';
	
	if (AlertDisconnectPopup.arguments.length>4)
		options = AlertDisconnectPopup.arguments[4];
	
	var popupwindow = window.open(page,nom,"top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
	//check for a popup blocking mechanism
	try 
	{
		popupwindow.focus();
	}
	catch (error)
	{
		alert('Votre session va expirer dans peu de temps.');
	}
*/
}

var alerted=0;

function IdleOfPublicSessionCountDown(logoutPage)
{
	// Checks if the Identification form exist or not
	if (typeof(document.forms['Identification'])!='undefined')
	{
		var IdentificationForm = document.forms["Identification"];
		if (typeof(IdentificationForm.IdleExpTime)!='undefined')
		{
			// Gets time defined to display alert
			var AlertBefore = IdentificationForm.AlertExpTime.value;
			
			// Total time is decreased second per second
			var TotalTime = parseInt(IdentificationForm.IdleExpTime.value);
			
			if (TotalTime > 0)
			// Countdown
			{
				// Calculates corresponding H,M and S
				var HTime = Math.floor(TotalTime / 3600);
				var MTime = Math.floor((TotalTime - (3600 * HTime)) / 60);
				var STime = TotalTime - (3600 * HTime) - (60 * MTime);
				
				// Assign new values ('RemainingTime' in sec, 'RemainingTimeDisplay' formatted as HH:MM:SS)
				IdentificationForm.IdleExpTime.value = TotalTime - 10;
				var TimeDisplayed = HTime + ":" + MTime + ":" + STime;
				if ((TotalTime <= AlertBefore) && (alertIdleOpened==0))
				{
					alertIdleOpened=1;
					AlertDisconnectPopup("session-control/AlertDisconnect/"+TotalTime+"?type=idle",500,300,"AlertDisconnect");
				}
				//document.getElementById("IdleExpDisplay").firstChild.nodeValue = "timeout dans: " + TimeDisplayed + " sec";
				
				// Restart function every ten seconds
				setTimeout('IdleOfPublicSessionCountDown(\'' + logoutPage + '\')',10000);
			}
			else
			// End of countdown
			{
				// ==> automatically disconnects
				window.location.href = logoutPage;
			}
		}
	}
}
