/**
 ******************************************************************************
 *
 * Confidential Property of Documentum, Inc.
 * (c) Copyright Documentum, Inc. 2004.
 * All Rights reserved.
 * May not be used without prior written agreement
 * signed by a Documentum corporate officer.
 *
 ******************************************************************************
 *
 * Project        WDK
 * File           keepFresh.js
 * Description    keep fresh javascript
 * Created on     Dec 8, 2003
 * Tab width      3
 *
 ******************************************************************************
 *
 * PVCS Maintained Data
 *
 * Revision       $$
 * Modified on    $$
 *
 * Log at EOF
 *
 ******************************************************************************
 */

// Constants used in this js
var clientDelimiter = '%%';
var escapedEqual = '##';
var allClientCookie = 'allFreshClient';
var stale = 'stale';
var fresh = 'fresh';
var lockCookie = 'lockFresh';

/**
*  Thread-safe function to keep a form fresh
*/
function syncKeepFresh(timeStampCookie, freshnessCookie, freshnessFormCookie,
                   scriptTimeStamp, currentForm, refreshURL)
{
   var lock = getCookie(lockCookie);

   if (lock == null || lock == 'free' || lock == timeStampCookie)
   {
      lock = getCookie(lockCookie);
      if (lock == null || lock == 'free' || lock == timeStampCookie)
      {
         document.cookie = lockCookie + '=' + timeStampCookie + '; path=/';
         internalKeepFresh(timeStampCookie, freshnessCookie, freshnessFormCookie,
                   scriptTimeStamp, currentForm, refreshURL);
         document.cookie = lockCookie + '=free; path=/';
         return true;
      }
      else
      {
         return false;
      }
   }
   else
   {
      return false;
   }
}

/**
 * Internal function to keep a form fresh
 */
function internalKeepFresh(timeStampCookie, freshnessCookie, freshnessFormCookie,
                   scriptTimeStamp, currentForm, refreshURL)
{
   var cookietimeStamp = 0;
   var strTimeStamp = getClientCookie(timeStampCookie);
   if (strTimeStamp != null)
   {
      cookietimeStamp = Number(strTimeStamp);
   }

   if (scriptTimeStamp >= cookietimeStamp)
   {
      cookietimeStamp = scriptTimeStamp + 1;
      setClientCookie(timeStampCookie, cookietimeStamp);
      setClientCookie(freshnessCookie, fresh);
      setClientCookie(freshnessFormCookie, currentForm);
   }

   // Get the form setting before making it stale
   var freshnessFlag = getClientCookie(freshnessCookie);
   var freshnessForm = getClientCookie(freshnessFormCookie);

   // Make the page stale before redirect the page
   setClientCookie(freshnessCookie, stale);

   if (freshnessFlag != null && freshnessFlag == stale)
   {
      if (freshnessForm != null && currentForm == freshnessForm)
      {
         window.location = refreshURL;
      }

      // else
      // form is stale but the URL is not meant for the current form
      // so just do nothing
   }
}


/**
 * Set a client cookie
 */
function setClientCookie(cookieName, value)
{
   var newValue = clientDelimiter + cookieName + escapedEqual + value;
   var allClient = getCookie(allClientCookie);
   if (allClient != null)
   {
      var startPos = allClient.indexOf(clientDelimiter + cookieName + escapedEqual);
      if (startPos == -1)
      {
         // The old cookie has never been saved before
         newValue = allClient + newValue;
      }
      else
      {
         var firstSubString, secondSubString;
         var endPos = allClient.indexOf(clientDelimiter, startPos + clientDelimiter.length);  // end of old cookie value

         if (startPos == 0)
         {
            // The old cookie is the first token
            firstSubString = '';
         }
         else
         {
            firstSubString = allClient.substring(0, startPos);
         }

         if (endPos == -1)
         {
            // The old cookie is the last token
            secondSubString = '';
         }
         else
         {
            // get the second sub string
            secondSubString = allClient.substring(endPos, allClient.length);
         }

         newValue = firstSubString + newValue + secondSubString;
      }
   }

   document.cookie = allClientCookie + '=' + newValue + '; path=/';
}

/**
 * Get a client cookie
 */
function getClientCookie(cookieName)
{
   var allClient = getCookie(allClientCookie);
   if (allClient == null)
   {
      return null;
   }
   else
   {
      var pos = allClient.indexOf(clientDelimiter + cookieName + escapedEqual);
      if (pos == -1)
      {
         return null;
      }
      else
      {
         var start = pos + clientDelimiter.length + cookieName.length + escapedEqual.length; // start of cookie value
         var end = allClient.indexOf(clientDelimiter, start);  // end of cookie value
         if (end == -1)
         {
            end = allClient.length;
         }

         return allClient.substring(start, end);
      }
   }
}

