/**
 ******************************************************************************
 *
 * Confidential Property of Documentum, Inc.
 * (c) Copyright Documentum, Inc. 2001.
 * All Rights reserved.
 * May not be used without prior written agreement
 * signed by a Documentum corporate officer.
 *
 ******************************************************************************
 *
 * Project        Quartet
 * File           framenavigation.js
 * Description    WDK Forms Navigation Mechanism - requires events.js
 * Created on     18 April 2003
 * Tab width      3
 *
 ******************************************************************************
 *
 * PVCS Maintained Data
 *
 * Revision       $Revision: 1$
 * Modified on    $Date: 4/29/03 6:53:39 PM$
 *
 * Log at EOF
 *
 ******************************************************************************
 */

// set new browser id if none is set
if (typeof(getTopLevelWnd().__browserId) == "undefined")
{
   getTopLevelWnd().__browserId = (new Date()).getTime();
   if (Trace_FRAMENAVIGATION) framenavigation_trace("assigned new browser id: " + getBrowserId());
}

/**
 * Gets the id of the current browser.
 */
function getBrowserId()
{
   return getTopLevelWnd().__browserId;
}

/**
 * Changes the location of a frame forcing its reload. The method makes sure that any frame id
 * gets propagated as url parameter is if present
 *
 * @param parentFrame parent frame containing the frameset
 * @param frameName name of the frame to change the location of
 * @param newLocation new location string for the frame
 */
function changeFrameLocationInFrameset(parentFrame, frameName, newLocation)
{
   var allFrames = parentFrame.document.getElementsByTagName("frame");
   var id = null;
   for (i=0; i<allFrames.length; i++)
   {
      var f = allFrames.item(i);
      if (f.getAttribute("name") == frameName)
      {
         id = f.getAttribute("id");
         break;
      }
   }
   // set the frame location and tack on reload and frameid
   changeFrameLocationWithId(parentFrame.frames[frameName], newLocation, id);
}

/**
 * Changes the location of a frame forcing its reload and appends the frame id.
 *
 * @param targetFrame target frame to change the location in
 * @param newLocation new location string for the frame
 * @param frameId frame id
 */
function changeFrameLocationWithId(targetFrame, newLocation, frameId)
{
   var newUrl = newLocation + ((newLocation.indexOf("?") == -1) ? "?" : "&") +
      'Reload=' + new Date().getTime();
   // append frame id if available
   if (frameId != null && frameId.length > 0)
   {
      newUrl += '&__dmfFrameId=' + frameId + '&__dmfBrowserId=' + getBrowserId();
   }

   if (Trace_FRAMENAVIGATION) framenavigation_trace("replacing frame location with: " + newUrl);

   targetFrame.location.replace(newUrl);
}

/**
 * Sets a cookie.
 * @param name cookie name
 * @param value cookie value
 * @param expire expiration date as Date (optional). If this value is null the cookie will only last
 * during the current browser session
 * @param path cookie path (optional)
 */
function setCookie(name, value, expire, path)
{
   var newCookie = name + "=" + escape(value)
      + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
      + ((path == null) ? "" : ("; path=" + path));

   document.cookie = newCookie;

   if (Trace_FRAMENAVIGATION) framenavigation_trace("set cookie: " + newCookie);
}

/**
 * Get cookie by name.
 * @return cookie value or null
 */
function getCookie(Name)
{
   var cookieVal = null;
   var search = Name + "=";
   if (document.cookie.length > 0)
   { // if there are any cookies
      offset = document.cookie.indexOf(search);
      if (offset != -1)
      { // if cookie exists
         offset += search.length;
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset);
         // set index of end of cookie value
         if (end == -1)
         {
            end = document.cookie.length;
         }

         cookieVal = unescape(document.cookie.substring(offset, end))
      }
   }

   if (Trace_FRAMENAVIGATION) framenavigation_trace("getting cookie: " + cookieVal);

   return cookieVal;
}

/**
 * Delete a cookie with the given name and path. To delete a cookie the name and path must be
 * just as when the cookie was originally set.
 * @param name cookie name
 * @param path cookie path
 */
function deleteCookie(name, path)
{
   if (getCookie(name) != null)
   {
      // expire the cookie
      var expire = new Date();
      // expire with yesterday's date
      expire = new Date(expire.getTime() - (24 * 60 * 60 * 1000));
      setCookie(name, "", expire, path);
   }
}

/**
 * Trace frame navigation.
 * @param msg trace message
 */
function framenavigation_trace(msg)
{
   Trace_println("framenavigation.js: " + msg);
}

