var SessionTime        = 20; // asp.net default 
var WarningTime        = 3;
// functions to call when something needs to happen, user interaction
// or when it actually timeouts, perform some custom actions
var OnTimeoutWarning   = null;
var OnTimeout          = null;

var TimeOutID          = 0;
var TimeB4Warning      = 1000*60*(SessionTime - WarningTime);
var t_latest           = new Date();

function timeoutPing(){
   var t_now   = new Date();
   t_delta = t_now.getTime()-t_latest.getTime();      
   // show msg box 
   if(t_delta >= TimeB4Warning) {
      show_alert();
   }               
   setTimeout('timeoutPing()', (1000*60) );
}
// session timeout has occurred
function OnSessionTimeout(){
    if( TimeOutID == 0 ) return;    
    if( OnTimeout ) // custom function to call when timeout occurs
        if( OnTimeout() )  // call it and if returns true replace        
            location.replace( TimeoutUrl );            
}
// reset the timer
function reset_session(){               
   // reset variables
   reset_clock();          
}  
// reset the timer for the user
function reset_clock(){
      clearTimeout( TimeOutID );
      TimeOutID = 0;
      t_latest = new Date();
      timeoutPing();
}
//refresh the session
function RefreshSession(){
    // resets the timeout ping
    reset_session();
    // create an Iframe
    var iframe = null;
    var id     = 'SessionRefresher';
    var ifDOM  = document.getElementById( id );
    var t_qs   = new Date();
    var src    = ReloadUrl + '?t=' + t_qs.getTime();    
    if ( ifDOM ){
        ifDOM.setAttribute( 'src', src ); 
        return;
    }    
    iframe = document.createElement('iframe');    
    iframe.setAttribute('id', id);
    iframe.setAttribute('name', 'SessionRefresh_name' );
    iframe.setAttribute('frameborder','0');
    iframe.setAttribute('marginwidth','0');
    iframe.setAttribute('marginheight','0');
    iframe.setAttribute('style', 'overflow:visible;width:1px;');
    iframe.setAttribute('scrolling', 'no' );            
    // set width and height of the iframe
    iframe.setAttribute( 'width', '1' );
    iframe.setAttribute( 'height', '1' );  
    // load content
    iframe.setAttribute('src', src); 
    // append the iframe to the body
    document.body.appendChild( iframe );
}
// show an alert to the User
function show_alert(){
    if( TimeOutID != 0 ) return;
    // custom dialog box
    if( OnTimeoutWarning )
        OnTimeoutWarning( WarningTime );
    // give the user time to respond, otherwise redirect to timeout page
    TimeOutID = setTimeout( OnSessionTimeout, (WarningTime*1000*59) );
}
// end of script timeout
