var secondsleft = 30 * 60;
var showntimeout = false; // has the alert been shown?
var timewin; // handle for the timeout warning window
var remind = 160; // remind user when this much time is left 160 and 20
var title_holder=document.title;

function getBrowserWidth(type_of_div)
{
	
	if (type_of_div=="trans"){
		if(self.outerWidth){
			return self.outerWidth+self.scrollMaxX;
		}else if (self.innerWidth){
			return self.innerWidth+self.scrollMaxX;
		}
		return document.body.offsetWidth+20;
	}else{
		if (self.innerWidth){
			return self.innerWidth;
		}
		return document.body.offsetWidth;	
	}
}

function getBrowserHeight(type_of_div)
{
	if (type_of_div=="trans"){
		if(self.outerHeight){
			return self.outerHeight+self.scrollMaxY;
		}else if (self.innerHeight){
			return self.innerHeight+self.scrollMaxY;
		}
		return document.body.offsetHeight+100;
	}else{
		if (self.innerHeight){
			return self.innerHeight;
		}
		return document.body.offsetHeight;
		//document.documentElement.offsetHeight;
	}
}

function pop_close(){
     if (timewin && timewin.open && !timewin.closed){
          timewin.close();
     }
}
function show_alert(){
	svn=document.getElementsByTagName("SELECT");
	for (a=0;a<svn.length;a++){
		svn[a].style.visibility="hidden";
	}
	
	var trans=document.getElementById("transparent_div");
	var shadowOffset=5;
	var bodyWidth=getBrowserWidth("body");
	var bodyHeight=getBrowserHeight("body");
	var topOffset=Math.max(document.body.scrollTop,document.documentElement.scrollTop);

	var divHeight=getBrowserHeight("trans");
	var divWidth=getBrowserWidth("trans");
	trans.style.height=divHeight +'px';
	trans.style.width=divWidth +'px';
	trans.style.left='0px';
	trans.style.top='0px';
	trans.style.display='block';

	//clientinfo=browser_info();
	var alert_div=document.getElementById("alert_div");
	alert_div.className='alert';
	alert_div.style.left='-1000';
	alert_div.style.display='block';
	alert_div.style.width='400px';
	//normal.style.height='125px';
	alert_div.style.height='200px';

	var tmpWidth=alert_div.offsetWidth;	
	var tmpHeight=alert_div.offsetHeight;
	var normalleft=Math.ceil((bodyWidth-tmpWidth)/ 2);
	var normaltop=(Math.ceil((bodyHeight-tmpHeight)/ 2)+ topOffset);

	alert_div.style.left=normalleft+'px';
	alert_div.style.top=normaltop+'px';
	
	var divs_shadow=document.getElementById("shadow_div");
	divs_shadow.className='alert_shadow';
	divs_shadow.style.left=(alert_div.style.left.replace('px','')/1+shadowOffset)+'px';
	divs_shadow.style.top=((alert_div.style.top.replace('px','')/1+shadowOffset))+'px';
	divs_shadow.style.height=(tmpHeight)+'px';
	divs_shadow.style.width=tmpWidth+'px';
	divs_shadow.style.display='block';
	
	
}
function show_alert_expired(){
	svn=document.getElementsByTagName("SELECT");
	for (a=0;a<svn.length;a++){
		svn[a].style.visibility="hidden";
	}
	var bodyWidth=getBrowserWidth("body");
	var bodyHeight=getBrowserHeight("body");
	var topOffset=Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	
	var alert_div=document.getElementById("alert_div");
	alert_div.style.display='none';
	var divs_shadow=document.getElementById("shadow_div");
	divs_shadow.style.display='none';
	var shadowOffset=5;
	
	var alert_expired_div=document.getElementById("alert_expired_div");
	alert_expired_div.style.display='block';
	alert_expired_div.className='alert';
	alert_expired_div.style.left='-1000';
	alert_expired_div.style.display='block';
	alert_expired_div.style.width='400px';
	//normal.style.height='125px';
	alert_expired_div.style.height='200px';

	var tmpWidth=alert_expired_div.offsetWidth;	
	var tmpHeight=alert_expired_div.offsetHeight;
	var normalleft=Math.ceil((bodyWidth-tmpWidth)/ 2);
	var normaltop=(Math.ceil((bodyHeight-tmpHeight)/ 2)+ topOffset);

	alert_expired_div.style.left=normalleft+'px';
	alert_expired_div.style.top=normaltop+'px';
	
	var divs_shadow=document.getElementById("shadow_div");
	divs_shadow.className='alert_shadow';
	divs_shadow.style.left=(alert_expired_div.style.left.replace('px','')/1+shadowOffset)+'px';
	divs_shadow.style.top=((alert_expired_div.style.top.replace('px','')/1+shadowOffset))+'px';
	divs_shadow.style.height=(tmpHeight)+'px';
	divs_shadow.style.width=tmpWidth+'px';
	divs_shadow.style.display='block';
	
}
function close_alert(){
	svn=document.getElementsByTagName("SELECT");
	for (a=0;a<svn.length;a++){
		svn[a].style.visibility="visible";
	}
	var trans=document.getElementById("transparent_div");
	trans.style.display='none';
	var divs_shadow=document.getElementById("shadow_div");
	divs_shadow.style.display='none';
	var alert_div=document.getElementById("alert_div");
	alert_div.style.display='none';
	var alert_expired_div=document.getElementById("alert_expired_div");
	alert_expired_div.style.display='none';
}
function alertUser(){
		if (location.pathname!="/login.php" && location.pathname!="/download.php"){
        	if (secondsleft < 0) {
        		show_alert_expired();
				return;
			}     
			if (showntimeout == false && secondsleft > 0 && secondsleft <= remind) {
				show_alert();
				showntimeout=true;		
			}
        	var minutes = Math.floor (secondsleft / 60);
			var seconds = secondsleft % 60;
			if (seconds < 10)
				seconds = "0" + seconds;		
			document.title = title_holder + ": Your session will timeout in: " + minutes + ":" + seconds;
			var period = 1;
			setTimeout("alertUser()", period*1000);
			secondsleft -= period;
		}
}

function extend_time(){
	LoadXMLDoc('/extend_session.php');
}
function LoadXMLDoc(url){ 
  if (window.XMLHttpRequest){ //Mozilla, Firefox, Opera 8.01, Safari
    reqXML = new XMLHttpRequest(); 
    reqXML.onreadystatechange = BuildXMLResults; 
    reqXML.open("GET", url, true); 
    reqXML.send(null); 
  }
  else if(window.ActiveXObject){ //IE
    reqXML = new ActiveXObject("Microsoft.XMLHTTP"); 
    if (reqXML) { 
      reqXML.onreadystatechange = BuildXMLResults; 
      reqXML.open("GET", url, true); 
      reqXML.send(); 
    } 
  }
  else{ //Older Browsers
    alert("Your Browser does not support Ajax!");
  }
} 

function BuildXMLResults(){
  if(reqXML.readyState == 4){ //completed state
    if(reqXML.status == 200){ //We got a sucess page back   
      //Check to verify the message from the server 
      if(reqXML.responseText.indexOf("Session Updated - Server Time:") == 0){
        	close_alert();
      		showntimeout=false;
        	secondsleft=20*60;
      }
      else{
        //display that that session expired
        alert("Your session appears to have expired. You may loose your current data.");
      }
    } 
    else{
      //display server code not be accessed
      alert("There was a problem retrieving the XML data:\n" + reqXML.statusText);
    }           
  }
}