/**
 ******************************************************************************
 *
 * 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        WDK Forms
 * File           componentnavigation.js
 * Description    WDK Components Navigation Mechanism - requires events.js
 *                                                   and formnavigation.js
 * Created on     21 May 2001
 * Tab width      3
 *
 ******************************************************************************
 *
 * PVCS Maintained Data
 *
 * Revision       $Revision: 8$ 
 * Modified on    $Date: 7/25/02 7:21:34 AM$
 *
 * Log at EOF
 * 
 ******************************************************************************
 */

/**
 * Post a server component page event to a given URL.
 * @param strFormId    The target form for the event.
 * @param strUrl       The url to jump to.
 * @param strTarget    The target frame (Optional - default is current frame)
 * If target frame does not exist then a new window will pop up.
 * @param  strEventArgName  event argument name (Optional)
 * @param  strEventArgValue  event argument value (Optional)
 *
 * Note: Multiple argument names and values may be specified.
 * Note: To specify a start page post an argument called '__dmfPage'.
 */
function postComponentPageEvent(strFormId, strComponent, strTarget, strEventArgName, strEventArgValue)
{
   var arrArgs = getArgs("postComponentPageEvent", 3);
   postComponentNavigationEvent(strFormId, strComponent, "onComponentPage", strTarget, arrArgs);
}

/**
 * Post a server component jump event to a given URL.
 * @param strFormId    The target form for the event.
 * @param strUrl       The url to jump to.
 * @param strTarget    The target frame (Optional - default is current frame)
 * If target frame does not exist then a new window will pop up.
 * @param  strEventArgName  event argument name (Optional)
 * @param  strEventArgValue  event argument value (Optional)
 *
 * Note: Multiple argument names and values may be specified.
 * Note: To specify a start page post an argument called '__dmfPage'.
 */
function postComponentJumpEvent(strFormId, strComponent, strTarget, strEventArgName, strEventArgValue)
{
   var arrArgs = getArgs("postComponentJumpEvent", 3);
   postComponentNavigationEvent(strFormId, strComponent, "onComponentJump", strTarget, arrArgs);
}

/**
 * Post a server component nest event to a given URL.
 * @param strFormId    The target form for the event.
 * @param strUrl       The url to jump to.
 * @param strTarget    The target frame (Optional - default is current frame)
 * If target frame does not exist then a new window will pop up.
 * @param  strEventArgName  event argument name (Optional)
 * @param  strEventArgValue  event argument value (Optional)
 *
 * Note: Multiple argument names and values may be specified.
 * Note: To specify a start page post an argument called '__dmfPage'.
 */
function postComponentNestEvent(strFormId, strComponent, strTarget, strEventArgName, strEventArgValue)
{
   if ((strEventArgName != null) && (strEventArgName != "") && (typeof strEventArgName != "undefined"))
   {
      var arrArgs = getArgs("postComponentNestEvent", 3);
      postComponentNavigationEvent(strFormId, strComponent, "onComponentNested", strTarget, arrArgs);
   }
   else
   {
      var emptyArray = new Array();
      postComponentNavigationEvent(strFormId, strComponent, "onComponentNested", strTarget, emptyArray);
   }
}

/**
 * Post a server event to a given component.
 * @param strFormId    The target form for the event.
 * @param strComponent The component to jump to.
 * @param strEvent     The event to fire.
 * @param strTarget    The target frame (Optional - default is current frame)
 * If target frame does not exist then a new window will pop up.
 * @param  arrArgs  An array of arguments in format as generated by
 *                  getArgs() (Zero length if no args).
 *
 * Note: Multiple argument names and values may be specified.
 * Note: To specify a start page post an argument called '__dmfPage'.
 */
function postComponentNavigationEvent(strFormId, strComponent, strEvent, strTarget, arrArgs)
{
   //Retrieve arguments if any
   var strCall = "";

   var strFramePath = getAbsoluteFramePath(strTarget);
   if (strFramePath != null)
   {
      //Frame exists. Walk down to leaf and see if it is a form.
      strFramePath = walkDownFrameSet(strFramePath);
      if ((eval(strFramePath).postServerEvent != null) && (typeof eval(strFramePath).postServerEvent != "undefined"))
      {
         //Target frame is a form
         strCall += strFramePath + ".safeCall('postServerEvent'," + strFormId + ", null, null, strEvent, \"__dmfTargetComponent\", strComponent";
         if (arrArgs.length > 0)
         {
            strCall += ", " + convertArgsToFunctionString(arrArgs);
         }
         strCall += ")";
      }
      else
      {
         //Target frame is not a form (specify start page if specified)
         strCall += strFramePath + ".document.location.href = \"/" + getVirtualDir() + "/component/" + strComponent;
         var strPage = retrievePage(arrArgs);
         if (strPage != null)
         {
            strCall += "/" + strPage;
            arrArgs = removePageFromArray(arrArgs);
         }
         if (arrArgs.length > 0)
         {
            strCall += "?" + convertArgsToUrlArgString(arrArgs);
         }
         strCall += "\"";
      }
   }
   else
   {
      //Frame doesn't exist or target is current page
      if ((strTarget == null) || (typeof strTarget == "undefined"))
      {
         //We are posting the event on the current frame.
         strCall += "safeCall('postServerEvent'," + strFormId + ", null, null, strEvent, \"__dmfTargetComponent\", strComponent";
         if (arrArgs.length > 0)
         {
            strCall += ", " + convertArgsToFunctionString(arrArgs);
         }
         strCall += ")";
      }
      else
      {
         //Open new window (with start page if specified)
         strCall += "window.open(\"/" + getVirtualDir() + "/component/" + strComponent;
         var strPage = retrievePage(arrArgs);
         if (strPage != null)
         {
            strCall += "/" + strPage;
            arrArgs = removePageFromArray(arrArgs);
         }
         if (arrArgs.length > 0)
         {
            strCall += "?" + convertArgsToUrlArgString(arrArgs);
         }
         strCall += "\", strTarget)";
      }
   }
   eval(strCall);
}

///////////////////////////////////////////////////////////////
// Helpers
///////////////////////////////////////////////////////////////

/**
 * Retrieve the specified page from the array of arguments (null if it
 * doesn't exist).
 * @param arrArgs Array of arguments generated by getArgs().
 */
function retrievePage(arrArgs)
{
   var strResult = null;
   if (arrArgs.length > 0)
   {
      for (var iArg = 0; iArg < arrArgs.length; iArg+=2)
      {
         if (arrArgs[iArg] == "__dmfPage")
         {
            strResult = arrArgs[iArg + 1];
            break;
         }
      }
   }
   return strResult;
}

/**
 * Remove the specified page from the array of arguments if it exists
 * @param arrArgs Array of arguments generated by getArgs().
 * @return Array  Processed array of arguments.
 */
function removePageFromArray(arrArgs)
{
   var arrResult = new Array();
   var nArrOffset = 0;
   if (arrArgs.length > 0)
   {
      for (var iArg = 0; iArg < arrArgs.length; iArg+=2)
      {
         if (arrArgs[iArg] != "__dmfPage")
         {
            //Add event name and event value to new array.
            arrResult[nArrOffset] = arrArgs[iArg];
            arrResult[nArrOffset + 1] = arrArgs[iArg + 1];
         }
      }
   }
   return arrResult;
}

