/*
   These functions are part of the session timeout warning system
*/

    var sessionTimeoutInSecs;
    var sesionTimeoutWarningInMsecs;
    var timerID = null;
    var timerRunning = false;
    var xmlhttp = null;
    var reloginPage = '';
    var sessionExpired = false;
    var overrideTimeout = false;

    function InitializeTimer(secs, reloginPageTemp, overrideTimeoutTemp) {
       // Set the length of the timer, in seconds
       sessionTimeoutInSecs = secs; // 2580;  //   43 minutes - better safe than sorry.
       sesionTimeoutWarningInMsecs = (sessionTimeoutInSecs - 60) * 1000;
       reloginPage = reloginPageTemp;
       overrideTimeout = overrideTimeoutTemp;
       StopTheClock();
       StartTheTimer();
    }

    function StopTheClock() {
       if (timerRunning)
          clearTimeout(timerID);
        
       timerRunning = false;
    }

    function StartTheTimer() {
       timerRunning = true;
       timerID = self.setTimeout("makeDummyRequest()", sesionTimeoutWarningInMsecs);
    }

    function makeDummyRequest() {
       window.focus();
       StopTheClock();
       var result = true;
       if (!overrideTimeout) {
          result = confirm('Your session will time out in 60 seconds.  \nClick okay to refresh your session.');
       }
       if (result) {
             xmlhttp=false;
             try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
             } 
             catch (e) {
                try {
                   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) {
                   xmlhttp = false;
                }
             }
  
             if (!xmlhttp && typeof XMLHttpRequest!='undefined')
                xmlhttp = new XMLHttpRequest();
           
             xmlhttp.open("POST", "/sns/webstore/application/wsapp?origin=primary_nav.jsp&event=link(myaccount)", true);
             xmlhttp.onreadystatechange = function() {
                // The string being searched for below will only exist on the myaccount_landing.jsp
                if ((xmlhttp.readyState==4) && (xmlhttp.responseText.indexOf('987123ABC') >= 0)) {
                   InitializeTimer(sessionTimeoutInSecs, reloginPage, overrideTimeout);
                   if (!overrideTimeout) {
                       alert('Your session has been extended.');
                   }
                } else if (xmlhttp.readyState==4) {
                   if (!overrideTimeout) {
                       alert('Your session has now timed out. \nIf you click the Ok button you will be able to login again.'); 
                   } else {
                       alert('Your session could not be automatically refreshed. \nYou have been timed out. \nIf you click the Ok button you will be able to login again.'); 
                   }
                   //forward to welcome page
                   window.location = reloginPage;
                }
             }
             xmlhttp.send(null);
       }
    }

