top.dojo.require("dojo.io.*");

function SessionExpirationHandler()
{
   function isSessionExpiredInternal(synchronous, todoFunc)
   {
      if ((typeof synchronous=="undefined")||(synchronous==null))
      {
         synchronous==false;
      }

      // we send the current-url in the browser as a paramenter since if this is an "inner-page-request" as mediaRender.do
      // and not a 'submit-request' the server will might send a wrong destination URL. The server decides on the
      // destination-URL according to the URL that appears in the request.
      var url = "sessionExpiration.do?" + top.sessionTimeoutParams.SESSION_EXPIRED_PAGE_URL_PARAM_NAME + "=" + top.location.href;
      var res=false;
      top.dojo.io.bind({
         url: url,
         sync:synchronous,
         load: function(type, data, evt)
         {
            if (data==null)
            {
               top.logToServer("no session-expiration answer received, do nothing.", top.addContextPath);
               return false;
            }
            res = todoFunc(data);
            return res;
         },
         error: function(type, errObj)
         {
            top.logToServer("Ajax returned with error. type = "+type +" ; errorObj = "+errorObj, top.addContextPath);
         },
         mimetype: "text/javascript"
      });
      return res;
   }
   this.isSessionExpiredInternal=isSessionExpiredInternal;

      function isSessionExpiredAndRedirectAsync()
      {
         function todoFunc(data)
         {
            if( data.isSessionExpired == top.booleanEnum.TRUE )
            {
               top.location.href = top.getContextPath() + "/" + data.redirectURL;
               top.logToServer("session-expired. should redirect to: " + data.redirectURL, top.addContextPath);
               return true;
            }
            return false;
         }
         return this.isSessionExpiredInternal(false, todoFunc);
      }
   this.isSessionExpiredAndRedirectAsync=isSessionExpiredAndRedirectAsync;

      function isSessionExpiredAndRedirectSync()
      {
         function todoFunc(data)
         {
            if( data.isSessionExpired == top.booleanEnum.TRUE )
            {
               top.location.href = top.getContextPath() + "/" + data.redirectURL;
               top.logToServer("session-expired. should redirect to: " + data.redirectURL, top.addContextPath);
               return true;
            }
            return false;
         }
         var expired=this.isSessionExpiredInternal(true, todoFunc);
         return expired;
      }
   this.isSessionExpiredAndRedirectSync=isSessionExpiredAndRedirectSync;

   function XisSessionExpiredAndRedirectAsync()
   {
      // we send the current-url in the browser as a paramenter since if this is an "inner-page-request" as mediaRender.do
      // and not a 'submit-request' the server will might send a wrong destination URL. The server decides on the
      // destination-URL according to the URL that appears in the request.
      var url = "sessionExpiration.do?" + top.sessionTimeoutParams.SESSION_EXPIRED_PAGE_URL_PARAM_NAME + "=" + top.location.href;

      top.dojo.io.bind({
         url: url,
         sync:synchronous,
         load: function(type, data, evt)
         {
            if (data==null)
            {
               top.logToServer("no session-expiration answer received, do nothing.", top.addContextPath);
               return false;
            }
            if( data.isSessionExpired == top.booleanEnum.TRUE )
            {
               top.location.href = top.getContextPath() + "/" + data.redirectURL;
               top.logToServer("session-expired. should redirect to: " + data.redirectURL, top.addContextPath);
               return true;
            }
            return false;
         },
         error: function(type, errObj)
         {
            top.logToServer("Ajax returned with error. type = "+type +" ; errorObj = "+errorObj, top.addContextPath);
         },
         mimetype: "text/javascript"
      });

      // Check if the session has expired.
   //         jsquery.callToServerThenDo('<html:rewrite page="/jsquerySessionExpired.jsp"/>', '', 'handleCheckSession')
   }
   this.XisSessionExpiredAndRedirectAsync = XisSessionExpiredAndRedirectAsync;
}
var sessionExpirationHandler = new SessionExpirationHandler();