
var ompTimeoutWarnTime = 1080000;
var ompTimeoutTime = 1200000;


var timeoutID;
var timeoutWarningID;
var timeWarnPopup;



function startTimeoutCounter(t_elapsed)
{
	
	if(typeof(t_elapsed)=="undefined"){
		t_elapsed = 0;
	} else {
		 t_elapsed = t_elapsed * 1000;
	}
		
	if (timeoutID) clearTimeout(timeoutID);
	if (timeoutWarningID) clearTimeout(timeoutWarningID);
	
	/* Timeouts do not apply unless user is logged into the portal. */
	if(parent.isLoggedIn==false) return;

	timeoutID = window.setTimeout('ompCheckTimeout()', (ompTimeoutTime - t_elapsed));		
	timeoutWarningID = window.setTimeout('ompCheckTimeoutWarning()', (ompTimeoutWarnTime - t_elapsed));
	
	
}

function ompShowTimeoutWarning()
{
	timeWarnPopup = window.open(ompTimeoutWarnUrl, "timeWarn",
			"top=100,left=100,width=400px,height=300px,toolbars=0," +
			"status=0,menubar=0,resizable=0");
}

function ompTimeout()
{
	closeWarnPopUp();
	
	var pubBase=(URL_MAGMA_BASE.indexOf('/')==0)?"https://www.cmpa-acpm.ca"+URL_MAGMA_BASE:URL_MAGMA_BASE;
	
	if(parent.location.href.indexOf('/docs/oma-')>0){
		//OMA form requires moving back to login page.
		parent.document.location=URL_INFRA_BASE + "omp_logout.php?url=" + escape(pubBase) + "omp_logout%2Ecfm";
	} else {	
		//Logout user in hidden IFRAME, will not disturb content browsing.	
		createSessionTimerIframe("ompKeepAlive", URL_INFRA_BASE + "omp_logout.php?url=" + escape(pubBase) + "omp_logout%2Ecfm");
		var loginLinkText = "Log in";
		if(ompLang=="fr") {
			loginLinkText = "Connexion";
		}
		var logOutLink = findAnchor('Log_out');
		if(logOutLink!=null){
			logOutLink.innerHTML = loginLinkText;
		}
		parent.isLoggedIn=false;
	}
	
}

function findAnchor(a){

	for(i==0;i<parent.document.anchors.length;i++){
		if(parent.document.anchors[i].name==a){
			return parent.document.anchors[i];
		}
	}
	
	return null;
}

function ompCheckTimeoutWarning(){
   var frameURL=URL_PORTAL_BASE + "portal/page/portal/OMP_MBR_DOCLIB/warn_checkTime";
   createSessionTimerIframe("ompKeepAlive", frameURL);
}

function ompCheckTimeout(){
   var frameURL=URL_PORTAL_BASE + "portal/page/portal/OMP_MBR_DOCLIB/timeout_checkTime1";
   createSessionTimerIframe("ompKeepAlive", frameURL);
}

function createSessionTimerIframe(sessionFrame, frameURL){

   if(document.getElementById(sessionFrame)){
	document.getElementById(sessionFrame).src=frameURL;
   } else {
   	var IframeObj = createHiddenIframe (sessionFrame, frameURL);   	 
   }
	
}

function closeWarnPopUp(){

	try
	{
		if (timeWarnPopup) timeWarnPopup.close();
	}
	catch (err)
	{
		timeWarnPopup = null;
	}
	
}

function createHiddenIframe (iframeName, srcLoc) {
var iframe;
if (document.createElement && (iframe =
document.createElement('iframe'))) {
iframe.name = iframe.id = iframeName;
iframe.width = 1;
iframe.height = 1;
iframe.src = srcLoc;
document.body.appendChild(iframe);
iframe.style.position="absolute";
iframe.style.visibility="hidden";
}
return iframe;
}

