
var timeElapsed = 0;
var timeLeft;
var minutes;
var seconds;
var strTemp;
var winModalWindow;
var NID;

//var oldx;
//var oldy;
//var oldwidth;
//var oldheight;

var oldWindowLeft = 0;
var oldWindowTop = 0;
var oldWindowWidth = 0;
var oldWindowHeight = 0;

var resizemode;
var timeoutDisabled;
var chromeWidth = 0;
var chromeHeight = 0;
var chromeLeft = 0;
var chromeTop = 0;
var chromeInitialized = false;

ID=window.setTimeout('update();', 1000);
if (window != window.top){
   window.top.disabletimeout();
}

function FormatNumber(expr, decplaces) {
	 return Math.floor(expr * Math.pow(10,decplaces))/Math.pow(10,decplaces);
}

function LeadingZero(expr) {
	 var str = '' +  Math.floor(expr * Math.pow(10,0))/Math.pow(10,0);
	 if (str.length == 1) {
		  str = '0' + str;
	 }
	 return str;
}

function disabletimeout(){
   timeoutDisabled = true;
}

function lockwindow() {
	 window.top.status = 'Session Timeout...';
	 timeElapsed = timeOut + 1;
	 // if (isIEWin){
	 //	  success = openwindow(false)
     //		  if (success){
	 //			resettimeout();
	 //	  }
	 // }
}

function getChromeSize(scrollBar){
  
   var windowWidth = 0;
   var windowHeight = 0;
   var windowTop = 0;
   var windowLeft = 0;
   var documentWidth = 0;
   var documentHeight = 0;
   var documentLeft = 0;
   var documentTop = 0;
   var openWindow;
   
   if(!chromeInitialized){
       
       openWindow = window;
       oldWindowLeft = openWindow.screenLeft;
       oldWindowTop = openWindow.screenTop;
       oldWindowWidth = openWindow.document.body.clientWidth;
       oldWindowHeight = openWindow.document.body.clientHeight;
      
       windowLeft = oldWindowLeft;
       windowTop = oldWindowTop;
       windowWidth = oldWindowWidth;
       windowHeight = oldWindowHeight;
      
       moveWindow(openWindow, windowLeft, windowTop, windowWidth, windowHeight);
   
       documentWidth = openWindow.document.body.clientWidth;
       documentHeight = openWindow.document.body.clientHeight;
       documentLeft = openWindow.screenLeft;
       documentTop = openWindow.screenTop;
   
       chromeWidth = windowWidth - documentWidth;
       chromeHeight = windowHeight - documentHeight;
       chromeTop = windowTop - documentTop;
       chromeLeft = windowLeft - documentLeft;

       actualLeft = oldWindowLeft + chromeLeft;
       actualTop = oldWindowTop + chromeTop;
       actualWidth = oldWindowWidth + chromeWidth;
       actualHeight = oldWindowHeight + chromeHeight;
       
       chromeInitialized = true;
   }
}

function moveWindow(openWindow, leftPosition, topPosition, resizeWidth, resizeHeight){
   openWindow.resizeTo(resizeWidth, resizeHeight);
   openWindow.moveTo(leftPosition, topPosition);
   openWindow.resizeTo(resizeWidth, resizeHeight);
   openWindow.moveTo(leftPosition, topPosition);
}

function update() {
	 var success;
	 if(!timeoutDisabled){
		  if (timeElapsed >= timeOut) {
				window.top.status = 'Session Timeout...';
				success = openwindow(true)
				if (success){
					 resettimeout();
				}
		  }
		  else {
				timeLeft = timeOut - timeElapsed;
				minutes = FormatNumber(timeLeft / 60, 0);
				seconds = LeadingZero(timeLeft - minutes * 60);
				//if (minutes < 5) {
				//	strTemp = minutes + ':' + seconds;
				//	window.top.status = strTemp;
				//}
				timeElapsed++;
				ID = window.setTimeout('update();',1000);
		  }
	 }
}

function openwindow(inactive){
   var width = 500;
   var height = 400;
   var left = 0;
   var top = 0;
   var value;
   var count = 0;
   if (isIEWin){
      resizemode = false;
      getChromeSize(document.body.scroll != 'no');
      oldWindowLeft = window.top.screenLeft + chromeLeft;
      oldWindowTop = window.top.screenTop + chromeTop;
      oldWindowWidth = window.top.document.body.clientWidth + chromeWidth;
      oldWindowHeight = window.top.document.body.clientHeight + chromeHeight;
      while (value != 'true' && count < 5){
         left = (screen.width - width) / 2;
         top = (screen.height - height) / 2;
         window.top.resizeTo(100,100);
         window.top.moveTo(left + 10, top + 10);
         value = window.top.showModalDialog(inactivityUrl + '?winie=true&inactive=' + inactive, window, 'scroll:no;status:no;dialogWidth:' + width + 'px;dialogHeight:' + height + 'px;');
         count++;
         if (value != 'true' && count < 5){
            if (value == 'false'){
               alert('Please enter the correct password.');
            }else{
               count = 6;
            }
         }
      }
      if (value == 'true'){
         window.top.resizeTo(oldWindowWidth, oldWindowHeight);
         window.top.moveTo(oldWindowLeft, oldWindowTop);
         window.top.resizeTo(oldWindowWidth, oldWindowHeight);
         window.top.moveTo(oldWindowLeft, oldWindowTop);
         return true;
      }else{
         timeElapsed = 0;
         closeMainWindow(window);
      }
   }else{
      resizemode = true;
      if (isIE){
         oldWindowLeft = 0;
         oldWindowTop = 0;
         oldWindowWidth = screen.availWidth;
         oldWindowHeight = screen.availHeight;
      }else{
         oldWindowLeft = window.top.screenX;
         oldWindowTop = window.top.screenY;
         oldWindowWidth = window.top.outerWidth;
         oldWindowHeight = window.top.outerHeight;
      }
      window.top.onresize = windowfocusevent;
      window.top.onfocus = windowfocusevent;
      window.top.onfocus = windowfocusevent;
      window.top.ondblclick = windowfocusevent;
      left = (screen.width - width) / 2;
      top = (screen.height - height) / 2;
      winModalWindow = window.top.open(inactivityUrl + '?winie=false', 'InactivityLogin', 'toolbar=no,location=no,scrollbars=no,status=no,left=' + left + ',top=' + top + ',width=' + width + ',height=' + height)
      winModalWindow.top.focus()
      window.top.resizeTo(100, 100);
      window.top.moveTo(left + 10, top + 10);
      NID = window.top.setTimeout('hidewindow();', 100);
      return false;
   }
}

function getTime () {
	 var now = new Date();
	 var hours = now.getHours();
	 var minutes = now.getMinutes();
	 var seconds = now.getSeconds()
	 var timeValue = "" + ((hours >12) ? hours -12 :hours)
	 if (timeValue == "0") timeValue = 12;
	 timeValue += ((minutes < 10) ? ":0" : ":") + minutes
	 timeValue += ((seconds < 10) ? ":0" : ":") + seconds
	 timeValue += (hours >= 12) ? " P.M." : " A.M."
	 return timeValue;
}

function hidewindow(){
	 var x;
	 var y;
	 if (timeElapsed >= timeOut) {
		  if (winModalWindow.closed){
				showwindow();
				closeWindow(window);
		  }else{
				winModalWindow.status = getTime();
				window.top.self.resizeTo(100, 100);
				if (isIE){
					 x = winModalWindow.screenLeft;
					 y = winModalWindow.screenTop;
				}else{
					 x = winModalWindow.screenX;
					 y = winModalWindow.screenY;
				}
				if (isIEMac){
					 x = (screen.width - 350) / 2;
					 y = (screen.height - 250) / 2;
					 winModalWindow.moveTo((screen.width - 400) / 2, (screen.height - 300) / 2);
				}
				window.top.self.moveTo(x + 10, y + 10);
				NID = window.top.setTimeout('hidewindow();', 100);
		  }
	 }
}

function closeMainWindow(windowref){
    windowref.top.opener = self;
    windowref.top.close();
}

function showwindow(){
	 window.top.self.moveTo(oldWindowLeft, oldWindowTop);
	 window.top.self.resizeTo(oldWindowWidth, oldWindowHeight);
}

function windowfocusevent(){
	 if (timeElapsed >= timeOut) {
		  winModalWindow.focus();
		  return false;
	 }else{
		  return true;
	 }
}

function windowresizeevent(){
	 if (timeElapsed >= timeOut) {
		  alert('Please enter your password.');
		  return false;
	 }else{
		  return true;
	 }
}

function resettimeout(){
	 window.top.status = 'Login Successful!';
	 timeElapsed = 0;
	 ID = window.setTimeout('update();', 1000);
	 if (resizemode){
		  window.top.onresize = null;
		  window.top.onfocus = null;
		  window.top.onfocus = null;
		  window.top.ondblclick = null;
		  showwindow();
	 }
}

function outputIFrame(){
	 RefreshDiv.innerHTML = '<iframe width=0 height=0 src="' + inactivityUrl + '?RequestMode=keepalive"></iframe>';
}

window.setTimeout('outputIFrame();', iframeDelay);

