/**
 ******************************************************************************
 *
 * 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           formnavigation.js
 * Description    WDK Forms Navigation Mechanism - requires events.js
 * Created on     21 May 2001
 * Tab width      3
 *
 ******************************************************************************
 *
 * PVCS Maintained Data
 *
 * Revision       $Revision: 11$ 
 * Modified on    $Date: 7/25/02 7:21:48 AM$
 *
 * Log at EOF
 * 
 ******************************************************************************
 */

/** global browser setting */
var g_isIE = (document.all) ? true : false;

/** global dialog setting */
var g_isModalDialog = (typeof window.parent._modalDialog != "undefined");

/**
 * launch a modal dialog containing the given URL.
 * 
 * @param strUrl        The url to jump to.
 * @param strTitle      Title of the dialog
 * @param width         dialog width
 * @param height        dialog height
 * @param resizable     true if resizable, false otherwise
 */
function launchModalDialog(strUrl, strTitle, width, height, resizable)
{
   var strLaunchUrl = "/" + getVirtualDir() + "/wdk/modalDialog.jsp?";
   strLaunchUrl += "__dmfUrl=" + escape(strUrl);
   strLaunchUrl += "&__dmfDialogTitle=" + strTitle;
   
   var isResizable = "yes";
   if (resizable == false)
   {
      isResizable = "no";
   }
   
   if (g_isIE == true)
   {
      window.showModalDialog(strLaunchUrl, "", "dialogHeight:" + height + "px; dialogWidth:" + width + "px; center:yes; help:no; resizable:" + isResizable + ";");
   }
   else
   {
      window.open(strLaunchUrl, strTitle, "height=" + height + ",width=" + width + ",location=no,status=no,menubar=no,toolbar=no,scrollbars=yes,modal=yes,resizable=" + isResizable);
   }
}

/**
 * Post a server form 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.
 */
function postFormPageEvent(strFormId, strUrl, strTarget, strEventArgName, strEventArgValue)
{
   var arrArgs = getArgs("postFormPageEvent", 3);
   postNavigationEvent(strFormId, strUrl, "onFormPage", strTarget, arrArgs);
}

/**
 * Post a server form 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.
 */
function postFormJumpEvent(strFormId, strUrl, strTarget, strEventArgName, strEventArgValue)
{
   var arrArgs = getArgs("postFormJumpEvent", 3);
   postNavigationEvent(strFormId, strUrl, "onFormJump", strTarget, arrArgs);
}

/**
 * Post a server form 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.
 */
function postFormNestEvent(strFormId, strUrl, strTarget, strEventArgName, strEventArgValue)
{
   var arrArgs = getArgs("postFormNestEvent", 3);
   postNavigationEvent(strFormId, strUrl, "onFormNested", strTarget, arrArgs);
}

/**
 * Post a server event to a given URL.
 * @param strFormId    The target form for the event.
 * @param strUrl       The url to jump to.
 *                     (Format= /dirbelowvirtualdir/.../file.jsp)
 * @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() below. (Zero length if no args).
 *
 * Note: Multiple argument names and values may be specified.
 */
function postNavigationEvent(strFormId, strUrl, 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 + ", " + strFramePath + ".document.forms[0], null, null, strEvent, \"__dmfUrl\", strUrl";
         if (arrArgs.length > 0)
         {
            strCall += ", " + convertArgsToFunctionString(arrArgs);
         }
         strCall += ")";
      }
      else
      {
         //Target frame is not a form
         strCall += strFramePath + ".document.location.href = \"/" + getVirtualDir() + strUrl;
         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, \"__dmfUrl\", strUrl";
         if (arrArgs.length > 0)
         {
            strCall += ", " + convertArgsToFunctionString(arrArgs);
         }
         strCall += ")";
      }
      else
      {
         //Open new window
         strCall += "window.open(\"/" + getVirtualDir() + strUrl;
         if (arrArgs.length > 0)
         {
            strCall += "?" + convertArgsToUrlArgString(arrArgs);
         }
         strCall += "\", strTarget)";
      }
   }
   eval(strCall);
}

///////////////////////////////////////////////////////////////
// Helpers
///////////////////////////////////////////////////////////////

/**
 * Get the virtual directory name (e.g. http://host/"<virtualdir>"/...)
 *
 * @return String The virtual directory name.
 */
function getVirtualDir()
{
    var strUrl = document.location.href;
    //Find the third slash (e.g. http://...'/'...)
    var nStart = strUrl.indexOf("/");
    nStart = strUrl.indexOf("/", nStart + 2) + 1;
    //Find the fourth slash (after the virtual dir name)
    var nEnd = strUrl.indexOf("/", nStart + 1);
    
    return strUrl.substring(nStart, nEnd);
}

/**
 * Get the arguments from the given function, starting at the given offset,
 * @param strFn - The function - must be called from within this function.
 * @param nOffset - The offset to start from
 *
 * @return Array - The arguments as a string array (e.g. ["x", "y", "a", "b"])
 *                  Returns empty array if no arguments are found.
 */
function getArgs(strFn, nOffset)
{
   var arrResult = new Array();
   var nArrOffset = 0;
   var functionArgs = eval(strFn).arguments;
   for (var iArg = nOffset; iArg < functionArgs.length; iArg+=1)
   {
      var strArgument = functionArgs[iArg];
      if (strArgument != null)
      {
         //Transfer argument to array
         arrResult[nArrOffset] = strArgument;
      }
      nArrOffset += 1;
   }
   return arrResult;
}

/**
 * Convert array of arguments to string format:
 * i.e. ["x", "y", "a", "b"] => '"x", "y", "a", "b"'
 * @param arrArgs An array of arguments
 * @return String The srguments as a string, separated by ", "
 */
function convertArgsToFunctionString(arrArgs)
{
   var strResult = "";
   if (arrArgs.length > 0)
   {
      for (var iArg = 0; iArg < arrArgs.length; iArg+=1)
      {
         if (strResult != "")
         {
            strResult += ", ";
         }
         
         strResult += '"';
         
         var strArg = arrArgs[iArg];
         for (var i = 0; i < strArg.length; i++)
         {
            var ch = strArg.charAt(i);
            if (ch == '"' || ch == '\\')
            {
               strResult += '\\';
            }
            strResult += ch;
         }
         
         strResult += '"';
      }
   }
   if (strResult == "")
   {
      strResult = null;
   }
   return strResult;
}

/**
 * Convert array of arguments to jsp argument string format:
 * i.e. ["x", "y", "a", "b"] => "x=y&a=b"
 * @param arrArgs An array of arguments
 * @return String The arguments as a string to be appended to a url.
 */
function convertArgsToUrlArgString(arrArgs)
{
   var strResult = "";
   if (arrArgs.length > 0)
   {
      for (var iArg = 0; iArg < arrArgs.length; iArg+=2)
      {
         if (strResult != "")
         {
            strResult += "&";
         }
         
         var strArg = arrArgs[iArg];
         var strEncodedArg = "";
         for (var i = 0; i < strArg.length; i++)
         {
            var ch = strArg.charAt(i);
            if (ch == '"' || ch == '\\')
            {
               strEncodedArg += '\\';
            }
            strEncodedArg += ch;
         }
         
         var strVal = arrArgs[iArg + 1];
         var strEncodedVal = "";
         for (var i = 0; i < strVal.length; i++)
         {
            var ch = strVal.charAt(i);
            if (ch == '"' || ch == '\\')
            {
               strEncodedVal += '\\';
            }
            strEncodedVal += ch;
         }
         
         strResult += strEncodedArg + '=' + strEncodedVal;
      }
   }
   if (strResult == "")
   {
      strResult = null;
   }
   return strResult;
}

