/*
*   Menu Builder, an HTML widget generator.
*
* Author:        T. Gochenour
* Date created:  6/2000
* Copyright (c) Peregrine Systems 2000 
* All rights reserved.  Use of this software in whole
* or in part without prior written permission of Peregrine
* Systems is strictly prohibited.
*/

/** 
 *   WebPage class          - Constructor which holds all variables needed
 *                            by this set of functions.
 *
 *   bAutosubmit            - Auto submit HTML form
 *   bFrame                 - Frame this page in Get.It! Weblication if not already
 *   vstrAlerts             - List of messages to be displayed in pop-up windows.
 *   strModule              - source module
 *   strActivity            - source activity
 *   strForm                - source form
 *   strBookmarkSessionKey  - Bookmark Session Key
 *   strOnLoad              - Javascript command to be invoked once page has loaded
 *   strAccess              - List of access rights that must match list when first logged in
 *   strNavigate            - Can be "TreeMenu" or anything elss
 *   strSidebar             - HTML value to be placed in Sidebar when navigation is TreeMenu
 *   vstrTokenKeys          - Keys for Replacement tokens for TreeMenu (Left side of token)
 *   vstrTokenValues        - Values for Replacement tokens for TreeMenu (right side of token)
 *   strCondition           - Condition key used to hide/show activities in TreeMenu
 */      
function WebPage() // Empty constructor, set variables individually
{
  this.bAutosubmit = false;         
  this.bFrame = false;
  this.vstrAlerts = [];
  this.strModule = "";
  this.strActivity = "";
  this.strForm = "";
  this.strBookmarkSessionKey = false;
  this.strNavigate = "";
  this.strOnLoad = "";
  this.strAccess = "";
  this.strSidebar = "";
  this.vstrTokenKeys = [];
  this.vstrTokenValues = [];
  this.strCondition = "";
}


/**
 *  Create new WebPage and pass it to this function before calling any others.
 */
var currentPage = null;
function setCurrentPage(webPage)
{
  currentPage = webPage;
}

/**
 *  Client side array of functions to invoke on page load  
 */
var invokeArray = new Array();
function invokeOnPageLoad(func)
{
  invokeArray[invokeArray.length] = func;
}

/**
 *  Client side function called when page loads  
 */
function doPageLoad()
{
  /*  Auto submit HTML form  */
  if( currentPage.bAutosubmit )
  {
    self.document.forms[0].submit();
    return;
  }

  /*  Frame this form if necessary  */
  if( currentPage.bFrame && parent.location.href == window.location.href )
  {
    top.document.cookie="_bookmark=" + top.location.pathname + top.location.search;
    top.document.cookie="_bookmarkSession="+currentPage.strBookmarkSession;
    if( currentPage.strModule == "admin" || currentPage.strModule == "adminlogin" )
      top.location.href = "admin.jsp";
    else
      top.location.href = "login.jsp";
    return;
  }


/* ==========================================================================
  Output the forms location as a bookmark so that when the browser attempts to
  refresh the top level page, we can reset the main form to the current form,
  rather than the home page.
  =========================================================================== */
  if ( top.getit_main != null )
  {
    top.document.cookie="_bookmark=" + top.getit_main.location.pathname + top.getit_main.location.search;
    top.document.cookie="_bookmarkSession="+currentPage.strBookmarkSession;
  }

  for( var i=0; i < currentPage.vstrAlerts.length; ++i )
  {
    alert( currentPage.vstrAlerts[i] );
  }

  setHeader();

  // Establish focus on first field in form
  if( self.document.forms.length > 0 && self.document.forms[0].elements.length > 0 
      && ! ( currentPage.strModule == "portal" && currentPage.strActivity == "home" && currentPage.strForm == "start" )
    )
  {
    for( var i = 0; i < self.document.forms[0].elements.length; ++i )
    {
      if( self.document.forms[0].elements[i].focus && self.document.forms[0].elements[i].type != "hidden" )
      {
        self.document.forms[0].elements[i].focus();
        break;
      }
    }
  }

  /*  Fire any form specific browser functions  */
  if( currentPage.strOnLoad != null )
    eval( currentPage.strOnLoad );

  for( var i = 0; i != invokeArray.length; ++i ) {
     eval( invokeArray[i] );
  }

  return true;
}

/**
 *  Select the appropriate module link  
 */
function setHeader()
{
  /*  If user's access rights have changed, update header  */
  if( currentPage != null && top.frames.getit_header != null )
  {
    if( top.frames.getit_header.refresh != null ) 
      top.frames.getit_header.refresh(currentPage.strAccess);

    if( currentPage.strNavigate == "TreeMenu" ) 
    {
      if( top.frames.getit_header.setSidebar != null )
        top.frames.getit_header.setSidebar(currentPage.strSidebar);
  
      if( top.frames.getit_header.setToken != null )
      {
        top.frames.getit_header.setToken("_moduleName",currentPage.strModule);
        top.frames.getit_header.setToken("_activityName",currentPage.strActivity);
        top.frames.getit_header.setToken("_formName",currentPage.strForm);
  
        for( var i=0; i < currentPage.vstrTokenKeys.length; ++i )
        {
          top.frames.getit_header.setToken( currentPage.vstrTokenKeys[i], currentPage.vstrTokenValues[i] );
        }
      }
  
      if( top.frames.getit_header.setContext != null )
        top.frames.getit_header.setContext( currentPage.strActivity, currentPage.strCondition ); 
      if( top.frames.getit_header.setCurrentItem != null ) 
        top.frames.getit_header.setCurrentItem( currentPage.strModule, currentPage.strActivity );
      if( top.frames.getit_header.build != null ) 
        top.frames.getit_header.build();
    }
    else if( top.frames.getit_header.setSelectedModule != null ) 
    {
      top.frames.getit_header.setSelectedModule( currentPage.strModule );
    }
  }
}

/**
 *  client side array of functions to invoke on page unload  
 */
var invokeArray2 = new Array();
function invokeOnPageUnload(func)
{
   invokeArray2[invokeArray2.length] = func;
}

/**
 *  Client side function called when page unloads before navigating to another page  
 */
function doPageUnload()
{
  for( var i = 0; i != invokeArray2.length; ++i ) 
  {
     eval( invokeArray2[i] );
  }
  return true;
}


/**
 * store user state string within the getit header
 */
function setUserState( strUserState )
{
  top.frames.getit_header.strUserState = strUserState;
}
function getUserState()
{
  return top.frames.getit_header.strUserState;
}
