

/*
 * Copyright 2002,2003,2004 inxire GmbH. All rights reserved.
 * ----------------------------------------------------------
 * Version: $Id: popupWindows.jsp,v 1.2.8.6 2007/07/04 10:21:44 fhenze 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 datePickerWindow          = null;    // defined in date-picker window.jsp


/**
 * Close all known popup widows. This function may be used in 'body.onunload' trigger
 */
function closePopupWindows() 
{
  if (window.workGroupTree)             { workGroupTree.close();             workGroupTree = null; }
  if (window.topicTree)                 { topicTree.close();                 topicTree = null; }
  if (window.folderTree)                { folderTree.close();                folderTree = null; }
  if (window.memberSelectBox)           { memberSelectBox.close();           memberSelectBox = null; }
  if (window.userSelectBox)             { userSelectBox.close();             userSelectBox = null; }
  if (window.topicSelectBox)            { topicSelectBox.close();            topicSelectBox = null; }
  if (window.processContainerSelectBox) { processContainerSelectBox.close(); processContainerSelectBox = null; }
  if (window.datePickerWindow)          { datePickerWindow.close();          datePickerWindow = null; }
}


/*
 * 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) {
  if (window.workGroupTree) { workGroupTree.close(); workGroupTree = null; }
  // var currId = document.forms[form].elements[id].value;
  var currId = getFormElementByName(document.forms[form], id).value;
  var url = '/adp/workGroupTree.jsp?form=' + form + '&id=' + id + '&name=' + name + '&value=' + currId;
  if (doSubmit)      url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)         url = url + '&label=' + encodeURIComponent(label);
//  if (withExtGroups) url = url + '&withExtGroups=true';
//  if (withGroups)    url = url + '&withGroups=true';
//  if (withLeaves)    url = url + '&withLeaves=true';
  if (mode)          url = url + '&mode=' + mode;
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  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) {
  if (window.topicTree) { topicTree.close(); topicTree = null; }
  var currId = getFormElementByName(document.forms[form], id).value;
  var url = '/adp/topicTree.jsp?form=' + form + '&id=' + id + '&name=' + name + '&value=' + currId;
  if (doSubmit)      url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)         url = url + '&label=' + encodeURIComponent(label);
  if (rootPath)      url = url + '&rootPath=' + encodeURIComponent(rootPath);
//  if (mode)          url = url + '&mode=' + mode;
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  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) {
  if (window.folderTree) { folderTree.close(); folderTree = null; }
  // var currPath = document.forms[form].elements[path].value;
  var currPath = getFormElementByName(document.forms[form], path).value;
  var url = '/adp/folderTree.jsp?form=' + form + '&path=' + path + '&currPath=' + encodeURIComponent(currPath);
  if (doSubmit)      url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)         url = url + '&label=' + encodeURIComponent(label);
  if (rootPath)      url = url + '&rootPath=' + encodeURIComponent(rootPath);
//  if (withLeaves)    url = url + '&withLeaves=true';
  if (mode)          url = url + '&mode=' + mode;
  if (onlyPublished) url = url + '&onlyPublished=true';
  if (sortAttr)      url = url + '&sortAttr=' + sortAttr;
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  folderTree = window.open( url ,'FolderTree','scrollbars,dependent,resizable,width=310,height=400');
}


/*
 * Special implementaion for edit user relations
 * 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
 */
function openUserRelSelectBox(form, path, doSubmit, label) {
  if (window.userRelSelectBox) { userRelSelectBox.close(); userRelSelectBox = null; }

  var currPath = getFormElementByName(document.forms[form], path).value;
  var url = '/adp/searchUserRelPopup.jsp?form=' + form + '&path=' + path + '&currPath=' + encodeURIComponent(currPath);
  if (label)         url = url + '&label=' + encodeURIComponent(label);
//    alert("123 2"+ encodeURIComponent(doSubmit) );
  if (doSubmit)     url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
//    alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> ");
  userRelSelectBox = window.open(url,'UserRelSelectBox','scrollbars,dependent,resizable,width=660,height=400');
}



/*
 * Open window to select a folder or document. The PATH of the selected IFS
 * object will be written back to the value of the input element of the calling window
 * and the localized NAME will be written to the display
 * value of the input element.
 * @param  form  name of HTML form
 * @param  path  name of PATH input element
 * @param  name  name of NAME 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 openFolderTitleTree(form, path, doSubmit, label, rootPath, withLeaves) {
function openFolderTitleTree(form, path, name, doSubmit, label, rootPath, mode, onlyPublished, sortAttr) {
  if (window.folderTree) { folderTree.close(); folderTree = null; }
  // var currPath = document.forms[form].elements[path].value;
  var currPath = getFormElementByName(document.forms[form], path).value;
  var url = '/adp/folderTitleTree.jsp?form=' + form + '&path=' + path + '&currPath=' + encodeURIComponent(currPath);
  if (name)          url = url + '&name=' + name;
  if (doSubmit)      url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)         url = url + '&label=' + encodeURIComponent(label);
  if (rootPath)      url = url + '&rootPath=' + encodeURIComponent(rootPath);
//  if (withLeaves)    url = url + '&withLeaves=true';
  if (mode)          url = url + '&mode=' + mode;
  if (onlyPublished) url = url + '&onlyPublished=true';
  if (sortAttr)      url = url + '&sortAttr=' + sortAttr;
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  folderTree = window.open( url ,'FolderTree','scrollbars,dependent,resizable,width=500,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) {
  if (window.memberSelectBox) { memberSelectBox.close(); memberSelectBox = null; }
  var url = '/adp/searchMemberPopup.jsp?form=' + form + '&id=' + id + '&name=' + name;
  if (doSubmit) url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)    url = url + '&label=' + encodeURIComponent(label);
  if (mode) {
    url = url + '&mode=' + mode;
    if (mode.indexOf('W')>=0) url = url + '&withWorkGroups=true';  // check all available boxes
    if (mode.indexOf('E')>=0) url = url + '&withExtGroups=true';
    if (mode.indexOf('G')>=0) url = url + '&withGroups=true';
    if (mode.indexOf('U')>=0) url = url + '&withUsers=true';
  }
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  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) {
  if (window.userSelectBox) { userSelectBox.close(); userSelectBox = null; }
  var url = '/adp/searchUserPopup.jsp?form=' + form + '&id=' + id + '&name=' + name;
  if (doSubmit) url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)    url = url + '&label=' + encodeURIComponent(label);
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  userSelectBox = window.open(url,'UserSelectBox','scrollbars,dependent,resizable,width=310,height=400');
}


/*
 * Open window to search for a Material. 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  number  name of NUMBER 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 openMaterialSelectBox(form, id, name, number, doSubmit, label) {
  if (window.materialSelectBox) { materialSelectBox.close(); materialSelectBox = null; }
  var url = '/adp/searchMaterialPopup.jsp?form=' + form + '&id=' + id + '&name=' + name + '&number=' + number ;
  if (doSubmit) url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)    url = url + '&label=' + encodeURIComponent(label);
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
//  alert("currPath = " + url + "/n/n     <" + encodeURIComponent(url) + "> ");
  materialSelectBox = window.open(url,'MaterialSelectBox','scrollbars,dependent,resizable,width=310,height=400');
}

/*
 * Open window to search for a Module or Article. 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 openModuleSelectBox(form, id, name, number, doSubmit, label) {
  if (window.moduleSelectBox) { moduleSelectBox.close(); moduleSelectBox = null; }
  var url = '/adp/searchModulePopup.jsp?form=' + form + '&id=' + id + '&name=' + name + '&number=' + number ;
  if (doSubmit) url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)    url = url + '&label=' + encodeURIComponent(label);
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
//  alert("currPath = " + url + "/n/n     <" + encodeURIComponent(url) + "> ");
  moduleSelectBox = window.open(url,'ModuleSelectBox','scrollbars,dependent,resizable,width=310,height=400');
}

/*
 * Open window to search for a Product. 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 openProductRelationSelectBox(form, id, name, path, doSubmit, label) {
  if (window.productSelectBox) { productSelectBox.close(); productSelectBox = null; }
  var url = '/adp/searchProductRelationPopup.jsp?form=' + form + '&id=' + id + '&name=' + name + '&path=' + path  ;
  if (doSubmit) url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)    url = url + '&label=' + encodeURIComponent(label);
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
//  alert("currPath = " + url + "/n/n     <" + encodeURIComponent(url) + "> ");
  productSelectBox = window.open(url,'ProductSelectBox','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  mode  not used (reserved for future extensions)
 */
function openTopicSelectBox(form, id, name, doSubmit, label, mode) {
  if (window.topicSelectBox) { topicSelectBox.close(); topicSelectBox = null; }
  var url = '/adp/searchTopicPopup.jsp?form=' + form + '&id=' + id + '&name=' + name;
  if (doSubmit) url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)    url = url + '&label=' + encodeURIComponent(label);
  // alert("currPath = " + currPath + "<" + encodeURIComponent(currPath) + "> , submitPara : " + submitPara);
  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) {
  if (window.processContainerSelectBox) { processContainerSelectBox.close(); processContainerSelectBox = null; }
  var url = '/adp/searchProcessContainerPopup.jsp?form=' + form + '&path=' + path;
  if (doSubmit)    url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)       url = url + '&label=' + encodeURIComponent(label);
  if (processType) url = url + '&processType=' + processType;
  if (workGroupId) url = url + '&workGroupId=' + workGroupId;
  if (mandatory)   url = url + '&mandatory=' + mandatory;
  processContainerSelectBox = window.open( url ,'ProcessContainerSelectBox','scrollbars,dependent,resizable,width=500,height=400');
}

/*
 * Open window to search for a Shop Root Folder. 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
 */
function openShopRootFolderSelectBox(form, path, doSubmit, label) {
  if (window.shopRootFolderSelectBox) { shopRootFolderSelectBox.close(); shopRootFolderSelectBox = null; }
  var url = '/adp/searchShopRootFoldersPopup.jsp?form=' + form + '&path=' + path;
  if (doSubmit)    url = url + '&doSubmit=' + encodeURIComponent(doSubmit);
  if (label)       url = url + '&label=' + encodeURIComponent(label);
  shopRootFolderSelectBox = window.open( url ,'ShopRootFolderSelectBox','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);
  };
}

