

/*
 * Copyright 2002-2007 inxire GmbH. All rights reserved.
 * ------------------------------------------------------
 * Version: $Id: popupWindows.jsp,v 1.8 2007/06/11 22:26:08 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 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);
  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;
}


/**
 * 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 '/io30' + 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');
}


/*
 * 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  -- not used -- (reserved for future use)
 */
function openTopicTree(form, id, name, doSubmit, label, rootPath, mode) {
  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;
  // 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');
}


/*
 * 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 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, 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 (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');
}


/*
 * 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');
}


/*
 * 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');
}


/*
 * 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
 */
function openTopicSelectBox(form, id, name, doSubmit, label, homonymId) {
  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);
  // 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');
}


/*
 * 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');
}


/*
 * 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');
}


/*
 * 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);
  };
}

