

/*
 * Copyright 2002-2008 inxire GmbH. All rights reserved.
 * ------------------------------------------------------
 * Version: $Id: popupWindows.jsp,v 1.9 2008/04/14 11:48:31 hkeller Exp $
 *
 * JavaScript functions to open and controll popup windows to select folder, groups,
 * users etc.
 *
 * Author:  Hans-Martin Keller
 */




/* Global variables to point to popup window objects */
var workGroupTree             = null;
var topicTree                 = null;
var folderTree                = null;
var memberSelectBox           = null;
var userSelectBox             = null;
var topicSelectBox            = null;
var processContainerSelectBox = null;
var newsChannelSelectBox      = null;
var forumPostSelectBox        = null;
// var datePickerWindow          = null;    // defined in date-picker window.jsp


/**
 * Close all known popup widows. This function may be used in 'body.onunload' trigger
 */
function closePopupWindows() 
{
  workGroupTree             = helpCloseWindow(window.workGroupTree);
  topicTree                 = helpCloseWindow(window.topicTree);
  folderTree                = helpCloseWindow(window.folderTree);
  memberSelectBox           = helpCloseWindow(window.memberSelectBox);
  userSelectBox             = helpCloseWindow(window.userSelectBox);
  topicSelectBox            = helpCloseWindow(window.topicSelectBox);
  processContainerSelectBox = helpCloseWindow(window.processContainerSelectBox);
  newsChannelSelectBox      = helpCloseWindow(window.newsChannelSelectBox);
  forumPostSelectBox        = helpCloseWindow(window.forumPostSelectBox);
  datePickerWindow          = helpCloseWindow(window.datePickerWindow);
}


/**
 * Utility function to close given window
 * @param  win  some object of type window;
 * @return  null (in any case)
 */
function helpCloseWindow(win) 
{
  // alert(win);
  // if (win) alert(typeof(win.close));
  // if (win && win.close) win.close();
  if (win && win.close && !(win.closed)) win.close();
  return null;
}


/*
 * Open a helper window with given URL, name, dimension and features.
 *
 * @param url    the page URL
 * @param name   target name, needed to reuse an existing windows
 * @param width  width and height of new window
 * @param height
 * @param left   x-offset in pixel
 * @param top    y-offset in pixel
 * @param align  one of 'center', 'left' or 'right' to position relative
 *               to **opener window**. The default is to center on **screen**.
 * @param features  additional features to be passed to window.open()
 * @return the window object (reused or opened)
 */
function openWindow(url, name, width, height, left, top, align, features) 
{
  // measure current window and screen
  var currLeft     = window.screenLeft ? window.screenLeft : (window.screenX ? window.screenX : 0);   // IE and Mozillla support
  var currTop      = window.screenTop  ? window.screenTop  : (window.screenY ? window.screenY : 0);
  var currWidth    = xWindowSize(window);
  var currHeight   = yWindowSize(window);
  var screenWidth  = window.screen ? window.screen.width : null;
  var screenHeight = window.screen ? window.screen.height : null;

  var posLeft = left;
  var posTop  = top;

  if (align == 'center') {           // center horizontally over window
    posLeft += currLeft + (currWidth - width) / 2;
    posTop  += currTop;
  }
  else if (align == 'left') {        // to the left
    posLeft += currLeft - width/2;
    posTop  += currTop;
  }
  else if (align == 'right') {       // to the right
    posLeft += currLeft + currWidth - width/2;
    posTop  += currTop;
  }
  else {                             // center on screen
    if (screenWidth && screenHeight) {
      posLeft += (screenWidth  - width) / 2;
      posTop  += (screenHeight - height) / 2;
    }
  }

  // check boundaries
  if (screenWidth && screenHeight) {
    if (width  > screenWidth ) width  = screenWidth;
    if (height > screenHeight) height = screenHeight;
    if (posLeft + width  > screenWidth ) posLeft = screenWidth - width;
    if (posTop  + height > screenHeight) posTop  = screenHeight - height;
  }
  if (posLeft < 0) posLeft = 0;
  if (posTop  < 0) posTop = 0;

  var settings = 'height=' + height + ',' + 'width=' + width + ',';
  if (posLeft || posLeft == 0) settings += 'left=' + posLeft + ',';
  if (posTop  || posTop  == 0) settings += 'top=' + posTop + ',';
  settings += features;

  var win = window.open(url, name, settings);
  //win.window.focus();  -> responsibility of caller
  return win;
}


/**
 * Utility method to construct servlet container URLs. This implementation
 * is a dummy placeholder without any complex logic to be overwritten 
 * within Oracle Portal environments.
 *
 * @param path   container path, e.g. "/workGroupTree.jsp"
 * @param query  optional query part
 */
function getServletContainerURL( path, query ) {
  return '/start' + path + ( query ? "?"+query : "" );
}


/*
 * Open window to select work group or member thereof. The ID and NAME of the
 * selected group will be written back to the input elements of the calling window.
 * @param  form  name of HTML form
 * @param  id    name of ID input element (to send with form data, normally hidden)
 * @param  name  name of NAME input element (to present to the user, normally visible)
 * @param  doSubmit either true, to submit the form on close, or a form method
 *                  to be called, or undefined 
 * @param  label description of desired object
 * @param  mode  indicates which object types can be selected, combine any of 'W', 'E', 'G' or 'U'
 *               for 'WorkGroup', 'ExtendedGroup', 'Group', or 'User', respectivly.
 *               Defaults to 'W'
 */
// function openWorkGroupTree(form, id, name, doSubmit, withExtGroups, withGroups, withLeaves) {
function openWorkGroupTree(form, id, name, doSubmit, label, mode) {
  closePopupWindows();
  // workGroupTree = helpCloseWindow(window.workGroupTree);
  // var currId = document.forms[form].elements[id].value;
  var currId = getFormElementByName(document.forms[form], id).value;
  var query = 'form=' + form + '&id=' + id + '&name=' + name + '&value=' + currId;
  if (doSubmit)      query = query + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)         query = query + '&label=' + encodeURIComponent(label);
//  if (withExtGroups) query = query + '&withExtGroups=true';
//  if (withGroups)    query = query + '&withGroups=true';
//  if (withLeaves)    query = query + '&withLeaves=true';
  if (mode)          query = query + '&mode=' + mode;
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  var url = getServletContainerURL('/workGroupTree.jsp', query);
  //workGroupTree = window.open(url,'WorkGroupTree','scrollbars,dependent,resizable,width=310,height=400');
  workGroupTree = openWindow(url,'WorkGroupTree', 330, 400, 0, 40, 'right', 'scrollbars,dependent,resizable');
}


/*
 * Open window to select topic. The ID and NAME of the
 * selected topic will be written back to the input elements of the calling window.
 * @param  form  name of HTML form
 * @param  id    name of ID input element (to send with form data, normally hidden)
 * @param  name  name of NAME input element (to present to the user, normally visible)
 * @param  doSubmit either true, to submit the form on close, or a form method
 *                  to be called, or undefined 
 * @param  label description of desired object
 * @param  rootPath  the root path of the tree, defaults to '/'
 * @param  mode  indicates which object types can be selected, combine 'T' or 'N'
 *               for 'Topic' or 'Null', respectivly
 * @param  onlyPublished  if true, only published documents are display with localized names
 * @param  lang  optional ISO 639 two-letter language code to enforce name localization,
 *               may be suffixed by ISO 3166 country code, e.g. "en-US" (DocBook/HTML notation)
 */
function openTopicTree(form, id, name, doSubmit, label, rootPath, mode, onlyPublished, lang) {
  closePopupWindows();
  // topicTree = helpCloseWindow(window.topicTree);
  var currId = getFormElementByName(document.forms[form], id).value;
  var query = 'form=' + form + '&id=' + id + '&name=' + name + '&value=' + currId;
  if (doSubmit)      query = query + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)         query = query + '&label=' + encodeURIComponent(label);
  if (rootPath)      query = query + '&rootPath=' + encodeURIComponent(rootPath);
  if (mode)          query = query + '&mode=' + mode;
  if (onlyPublished) query = query + '&onlyPublished=true';
  if (lang)          query = query + '&lang=' + lang;
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  var url = getServletContainerURL('/topicTree.jsp', query);
  //topicTree = window.open(url,'WorkGroupTree','scrollbars,dependent,resizable,width=310,height=400');
  topicTree = openWindow(url,'TopicTree',330, 400, 0, 40, 'right', 'scrollbars,dependent,resizable');
}


/*
 * Open window to select a folder or document. The PATH of the selected IFS
 * object will be written back to the input element of the calling window.
 * @param  form      name of HTML form
 * @param  path      name of PATH input element
 * @param  doSubmit  either true, to submit the form on select, or a form method
 *                   to be called, or undefined 
 * @param  label     description of desired object
 * @param  rootPath  the root path of the tree, defaults to '/'
 * @param  mode      indicates which object types can be selected, 
 *                   combine any of 'F', 'D' or 'N' for 'Folder', 'Document' or 'NULL', respectivly.
 *                   Defaults to 'F'
 * @param  onlyPublished  if true, only published documents are display with localized names
 * @param  lang      optional ISO 639 two-letter language code to enforce name localization,
 *                   may be suffixed by ISO 3166 country code, e.g. "en-US" (DocBook/HTML notation)
 * @param  sortAttr  one of PublicObject.NAME_ATTRIBUTE or PublicObject.SORTPRIORITY_ATTRIBUTE,
 *                   prefix by '-' to reverse order.
 */
//function openFolderTree(form, path, doSubmit, label, rootPath, withLeaves) {
function openFolderTree(form, path, doSubmit, label, rootPath, mode, onlyPublished, lang, sortAttr) {
  closePopupWindows();
  // folderTree = helpCloseWindow(window.folderTree);
  // var currPath = document.forms[form].elements[path].value;
  var currPath = getFormElementByName(document.forms[form], path).value;
  var query = 'form=' + form + '&path=' + path + '&currPath=' + encodeURIComponent(currPath);
  if (doSubmit)      query = query + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)         query = query + '&label=' + encodeURIComponent(label);
  if (rootPath)      query = query + '&rootPath=' + encodeURIComponent(rootPath);
//  if (withLeaves)    query = query + '&withLeaves=true';
  if (mode)          query = query + '&mode=' + mode;
  if (onlyPublished) query = query + '&onlyPublished=true';
  if (lang && !sortAttr && lang.toUpperCase() == lang) {        // backwards compatibility
    query = query + '&sortAttr=' + lang;
  }
  else {
    if (lang)          query = query + '&lang=' + lang;
    if (sortAttr)      query = query + '&sortAttr=' + sortAttr;
  }
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  var url = getServletContainerURL('/folderTree.jsp', query);
  //folderTree = window.open( url ,'FolderTree','scrollbars,dependent,resizable,width=310,height=400');
  folderTree = openWindow(url ,'FolderTree',330, 400, 0, 40, 'right', 'scrollbars,dependent,resizable');
}


/*
 * Open window to search for a DirectoryObject. The ID and NAME of the
 * selected IFS object will be written back to the input elements of the calling window.
 * @param  form  name of HTML form
 * @param  id    name of ID input element (to send with form data, normally hidden)
 * @param  name  name of NAME input element (to present to the user, normally visible)
 * @param  doSubmit either true, to submit the form on select, or a form method
 *                  to be called, or undefined 
 * @param  label description of desired object
 * @param  mode  indicates which object types can be selected, combine any of 'W', 'E', 'G' or 'U'
 *               for 'WorkGroup', 'ExtendedGroup', 'Group', or 'User', respectivly
 */
// function openMemberSelectBox(form, id, name, doSubmit, mode) {
function openMemberSelectBox(form, id, name, doSubmit, label, mode) {
  closePopupWindows();
  // memberSelectBox = helpCloseWindow(window.memberSelectBox);
  var query = 'form=' + form + '&id=' + id + '&name=' + name;
  if (doSubmit) query = query + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)    query = query + '&label=' + encodeURIComponent(label);
  if (mode) {
    query = query + '&mode=' + mode;
    if (mode.indexOf('W')>=0) query = query + '&withWorkGroups=true';  // check all available boxes
    if (mode.indexOf('E')>=0) query = query + '&withExtGroups=true';
    if (mode.indexOf('G')>=0) query = query + '&withGroups=true';
    if (mode.indexOf('U')>=0) query = query + '&withUsers=true';
  }
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  var url = getServletContainerURL('/searchMemberPopup.jsp', query);
  //memberSelectBox = window.open(url,'MemberSelectBox','scrollbars,dependent,resizable,width=310,height=400');
  memberSelectBox = openWindow(url,'MemberSelectBox',330, 400, 0, 40, 'right', 'scrollbars,dependent,resizable');
}


/*
 * Open window to search for a DirectoryUser. The ID and NAME of the
 * selected IFS object will be written back to the input elements of the calling window.
 * @param  form  name of HTML form
 * @param  id    name of ID input element (to send with form data, normally hidden)
 * @param  name  name of NAME input element (to present to the user, normally visible)
 * @param  doSubmit either true, to submit the form on select, or a form method
 *                  to be called, or undefined 
 * @param  label description of desired object
 */
function openUserSelectBox(form, id, name, doSubmit, label) {
  closePopupWindows();
  // userSelectBox = helpCloseWindow(window.userSelectBox);
  var query = 'form=' + form + '&id=' + id + '&name=' + name;
  if (doSubmit) query = query + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)    query = query + '&label=' + encodeURIComponent(label);
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  var url = getServletContainerURL('/searchUserPopup.jsp', query);
  //userSelectBox = window.open(url,'UserSelectBox','scrollbars,dependent,resizable,width=310,height=400');
  userSelectBox = openWindow(url,'UserSelectBox',330, 400, 0, 40, 'right', 'scrollbars,dependent,resizable');
}


/*
 * Open window to search for a Topics. The ID and NAME of the
 * selected IFS object will be written back to the input elements of the calling window.
 * @param  form  name of HTML form
 * @param  id    name of ID input element (to send with form data, normally hidden)
 * @param  name  name of NAME input element (to present to the user, normally visible)
 * @param  doSubmit either true, to submit the form on select, or a form method
 *                  to be called, or undefined 
 * @param  label description of desired object
 * @param  homonymId  homonymId to be searched, each topic tree should manage its own ID
 * @param  onlyPublished  if true, only published topics are displayed with localized names
 * @param  showDeleted    if true, deleted topics are show. May be rejoined to folder tree
 */
function openTopicSelectBox(form, id, name, doSubmit, label, homonymId, onlyPublished, showDeleted) {
  closePopupWindows();
  // topicSelectBox = helpCloseWindow(window.topicSelectBox);
  var query = 'form=' + form + '&id=' + id + '&name=' + name;
  if (doSubmit) query = query + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)    query = query + '&label=' + encodeURIComponent(label);
  if (homonymId) query = query + '&homonymId=' + encodeURIComponent(homonymId);
  if (onlyPublished) query = query + '&onlyPublished=true';
  if (showDeleted) query = query + '&showDeleted=true';
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  var url = getServletContainerURL('/searchTopicPopup.jsp', query);
  //topicSelectBox = window.open(url,'TopicSelectBox','scrollbars,dependent,resizable,width=310,height=400');
  topicSelectBox = openWindow(url,'TopicSelectBox',330, 400, 0, 40, 'right', 'scrollbars,dependent,resizable');
}


/*
 * Open window to search for a ProcessContainer. The PATH of the
 * selected IFS object will be written back to the input elements of the calling window.
 * @param  form  name of HTML form
 * @param  path  name of PATH input element
 * @param  doSubmit either true, to submit the form on select, or a form method
 *                  to be called, or undefined 
 * @param  label description of desired object
 * @param  processType type ID of process, defaults to PublishingProcess
 * @param  workGroupId ID of workgroup, defaults to null for show all
 * @param  mandatory  set this to "true", if empty values are not allowed
 */
function openProcessContainerSelectBox(form, path, doSubmit, label, processType, workGroupId, mandatory) {
  closePopupWindows();
  // processContainerSelectBox = helpCloseWindow(window.processContainerSelectBox);
  var query = 'form=' + form + '&path=' + path;
  if (doSubmit)    query = query + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)       query = query + '&label=' + encodeURIComponent(label);
  if (processType) query = query + '&processType=' + processType;
  if (workGroupId) query = query + '&workGroupId=' + workGroupId;
  if (mandatory)   query = query + '&mandatory=' + mandatory;
  var url = getServletContainerURL('/searchProcessContainerPopup.jsp', query);
  //processContainerSelectBox = window.open( url ,'ProcessContainerSelectBox','scrollbars,dependent,resizable,width=500,height=400');
  processContainerSelectBox = openWindow(url ,'ProcessContainerSelectBox',500, 400, 0, 40, 'right', 'scrollbars,dependent,resizable');
}


/*
 * Open window to search for a NewsChannels. The PATH of the selected IFS 
 * object will be written back to the input elements of the calling window.
 * @param  form       name of HTML form
 * @param  id         name of ID input element
 * @param  name       name of input element for channel description (path + language)
 * @param  doSubmit   either true, to submit the form on select, or a form method
 *                    to be called, or undefined 
 * @param  label description of desired object
 * @param  mode  indicates which object types can be selected, 
 *               combine any of 'F', 'D' or 'N' for 'Folder', 'Document' or 'NULL', respectivly
 *               Defaults to 'F'
 * @param  onlyPublished  if true, only published documents are display with localized titles
 */
function openNewsChannelSelectBox(form, id, name, doSubmit, label, mode, onlyPublished) {
  closePopupWindows();
  // processContainerSelectBox = helpCloseWindow(window.processContainerSelectBox);
  var query = 'form=' + form + '&id=' + id;
  if (name)          query = query + '&name=' + name;
  if (doSubmit)      query = query + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)         query = query + '&label=' + encodeURIComponent(label);
  if (mode)          query = query + '&mode=' + mode;
  if (onlyPublished) query = query + '&onlyPublished=true';
  var url = getServletContainerURL('/searchNewsChannelPopup.jsp', query);
  //newsChannelSelectBox = window.open( url ,'NewsChannelSelectBox','scrollbars,dependent,resizable,width=500,height=400');
  newsChannelSelectBox = openWindow(url ,'NewsChannelSelectBox',500, 400, 0, 40, 'right', 'scrollbars,dependent,resizable');
}


/*
 * Open window to search for a forum post or thread. The forum path and the post 
 * ID will be written back to the input elements of the calling window.
 * @param  form     name of HTML form
 * @param  path     name of PATH input element, used to read and write the forum path, optional
 * @param  postId   name of the ID input element, used to store the post ID
 * @param  doSubmit either true, to submit the form on select, a form method
 *                  to be called, or undefined 
 * @param  label    description of input element
 * @param  mode     indicates which object type can be selected, combine any of
 *                  'P' (post) or 'T' (thread -> initial post). Defaults to 'T'
 */
function openForumPostSelectBox(form, path, postId, doSubmit, label, mode) {
  closePopupWindows();
  var forumPath = path ? getFormElementByName(document.forms[form], path).value : null;
  var query = 'form=' + form + '&postId=' + postId;
  if (path)          query = query + '&path=' + path;
  if (forumPath)     query = query + '&forumPath=' + encodeURIComponent(forumPath);
  if (doSubmit)      query = query + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)         query = query + '&label=' + encodeURIComponent(label);
  if (mode)          query = query + '&mode=' + mode;
  var url = getServletContainerURL('/searchForumPostPopup.jsp', query);
  forumPostSelectBox = openWindow(url ,'ForumPostSelectBox', 500, 400, 0, 40, 'right', 'scrollbars,dependent,resizable');
}


/*
 * Utility function to clear given field without popup
 */
function clearInputElement(form, id, name, doSubmit) {
  var formNode = document.forms[form];
  // if (formNode && id) formNode.elements[id].value='';
  // if (formNode && name) formNode.elements[name].value='';
  if (formNode && id) getFormElementByName(formNode, id).value='';
  if (formNode && name) getFormElementByName(formNode, name).value='';
  if (doSubmit) {
    if (doSubmit == true) formNode.submit();
    else eval('formNode.' + doSubmit);
  };
}
