/*
 *------------------------------------------------------------------------------
 * Auswahl-Hilfe-Bibliothek
 * Autor: Bernd Clemenz
 * Datum: 07.2002 (begonnen)
 *        08.2002
 *        09.2002
 *------------------------------------------------------------------------------
 */

var l_asValidTypes = ["listbox","applet","droplist"];
//------------------------------------------------------------------------------
// Steuerdaten des Fensters diese werden nicht mehr verändert
var l_sWindowName  = "HELP";
var l_wnd          = null;
//------------------------------------------------------------------------------
// Daten des expliziten Fensters
// diese Daten hängen vom Template des Hilfefensters ab und/oder können als
// Ergebnis eines Platzhalterexpansion entstehen.

var l_sWindowDefList   = "dependend=yes,width=200,height=300,menubar=no,location=no,resizable=yes,status=no,toolbar=no,scrollbars=yes";
var l_sWindowDefDrop   = "dependend=yes,width=200,height=350,menubar=no,location=no,resizable=yes,status=no,toolbar=no,scrollbars=yes";
var l_sWindowDefApplet = "dependend=yes,width=250,height=280,menubar=no,location=no,resizable=yes,status=no,toolbar=no,scrollbars=yes";
var l_asWindowDef      = [l_sWindowDefList,l_sWindowDefApplet,l_sWindowDefDrop];
/*
 * Der Template eines Auswahlhilfefensters. Dieser String kann selbst wiederum das
 * Ergbnis einer Paltzhalterexpansion sein. Der Inhalt und die Komplexität des
 * Fensters sind partktisch beliebig. Das Fenster muss allerding über folgende
 * Schnittstelle verfügen:
 * - Daten werden über den Code: window.opener.document.forms.#FORM.#FIELD.value = AKTUELLER WERT;
 *   an das rufende Fenster zurückgeliefert.
 *   Die symbolischen Platzhalter beginnend mit "#" werden durch die Methoden
 *   des hier definierten HW-Objektes mit den aktuellen Daten des runfenden
 *   Fensters/Formulars ersetzt.
 *   Vorhanden sind die Platzhalter:
 *   #HEAD  Überschrift, die auch im Festertitel erscheint,
 *   #FORM  Name des aktuell rufenden Formulars im rufenden Fenster,
 *   #FIELD Name des Feldes im aktuell rufenden Formular,
 *   #DATA  Optionen als String, wenn es sich um ein Listbox-Fenster handelt.
 *          Hier im Beispiel ist der Leerstring standardmäßig der erste Eintrag,
 *          ohne explizit in den Daten aufgeführt zu werden.
 *   #SIZE  Die Anzhal der Zeilen die in der Liste dargestellt werden soll, wenn es sich
 *          um eine echte Listbox handelt.
 *   #TEXT  Erleuternder Hilfetext zu den zu selektierenden Daten.
 */

 l_sHelpWindowDropTemplate =
                               "<HTML><HEAD><title>#HEAD</title>"
                             + "<script language = \"JavaScript\" type=\"text/javascript\">\n"
                             + "<!--\n"
                             + "function getData()\n"
                             + "{\n"
                             + "  var oFrm = document.forms.helpFrm;\n"
                             + "  if(! oFrm) { alert(\"Form not found\"); return \"\"; }\n"
                             + "  var iSelectedIndex = oFrm.elements.hilfeSel.options.selectedIndex;\n"
                             + "  //alert(\"selected\" + iSelectedIndex);\n"
                             + "  if(iSelectedIndex != -1)\n"
                             + "  {\n"
                             + "     var sData = oFrm.elements.hilfeSel.options[iSelectedIndex].text;\n"
                             + "     return sData;\n"
                             + "  }\n"
                             + "  return \"\";"
                             + "}\n"
                             + "function putData(sValue)\n"
                             + "{ //alert(sValue);\n "
                             + "  if(!sValue || sValue == \"\") return;\n"
                             + "  if(! window.opener) { alert(\"opener is null\"); return; }\n"
                             + "  window.opener.document.forms.#FORM.#FIELD.value = sValue;\n"
                             + "  \n"
                             + "}\n"
                             + "//-->\n"
                             + "<\/script>\n"
                             + "<\/HEAD><BODY><h2>#HEAD<\/h2>#TEXT<form name=\"helpFrm\"><select name = \"hilfeSel\" onchange = \"putData(getData());\"><option value=\" \"> <\/option>\n"
                             + "#DATA<\/select><input type= \"button\" value = \"OK\" onclick=\"putData(getData()); window.close();\"><\/form><\/BODY><\/HTML>\n";

l_sHelpWindowListTemplate =
                               "<HTML><HEAD><title>#HEAD</title>"
                             + "<script language = \"JavaScript\" type=\"text/javascript\">\n"
                             + "<!--\n"
                             + "function getData()\n"
                             + "{\n"
                             + "  var oFrm = document.forms.helpFrm;\n"
                             + "  if(! oFrm) { alert(\"Form not found\"); return \"\"; }\n"
                             + "  var iSelectedIndex = oFrm.elements.hilfeSel.options.selectedIndex;\n"
                             + "  //alert(\"selected\" + iSelectedIndex);\n"
                             + "  if(iSelectedIndex != -1)\n"
                             + "  {\n"
                             + "     var sData = oFrm.elements.hilfeSel.options[iSelectedIndex].text;\n"
                             + "     return sData;\n"
                             + "  }\n"
                             + "  return \"\";"
                             + "}\n"
                             + "function putData(sValue)\n"
                             + "{ //alert(sValue);\n "
                             + "  if(!sValue || sValue == \"\") return;\n"
                             + "  if(! window.opener) { alert(\"opener is null\"); return; }\n"
                             + "  window.opener.document.forms.#FORM.#FIELD.value = sValue;\n"
                             + "  \n"
                             + "}\n"
                             + "//-->\n"
                             + "<\/script>\n"
                             + "<\/HEAD><BODY><h2>#HEAD<\/h2>#TEXT<form name=\"helpFrm\"><select name = \"hilfeSel\" onchange = \"putData(getData());\" size=\"#SIZE\"><option value=\" \"> <\/option>\n"
                             + "#DATA<\/select><input type= \"button\" value = \"OK\" onclick=\"putData(getData()); window.close();\"><\/form><\/BODY><\/HTML>\n";
                             
/*
 * Template eines Hilfefensters das ein Applet enthaelt. Es bestitzt dieselbe
 * Schnittstelle, wie das oben beschriebene Template.
 */
l_sHelpWindowAppletTemplate =
    "<HTML><HEAD><TITLE>#HEAD</TITLE>"
+ "<script language=\"JavaScript\" type=\"text/javascript\">\r\n"
+ "<!--\r\n"
+ "  function putDate()\r\n"
+ "  {\r\n"
+ "    var applet = document.applets.daySelectApplet;\r\n"
+ "    if(applet != null)\r\n"
+ "    {\r\n"
+ "       var theDate = applet.getDaySelected();\r\n"
+ "       window.opener.document.forms.#FORM.#FIELD.value  = theDate;\r\n"
+ "       return true;\r\n"
+ "    }\r\n"
+ "    else\r\n"
+ "    {\r\n"
+ "       alert(\"Applet not found!\");\r\n"
+ "    }\r\n"
+ "    return false;\r\n"
+ "  }\r\n"
+ "\/\/-->\r\n"
+ "<\/script>\r\n"
+ "</HEAD><BODY>#TEXT"
+ "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>"
+ "<APPLET style    = \"LEFT: 0px; TOP: 0px\""
+ "        codeBase = \".\""
+ "        height   = \"220\""
+ "        archive  = \"applets/dayselect.jar\""
+ "        width    = \"170\""
+ "        align    = \"middle\""
+ "        code     = \"isc.daySelect.class\""
+ "        name     = \"daySelectApplet\">"
+ "</APPLET>"
+ "</td><td valign=\"top\">"
+ "<input type    = \"button\""
+ "       name    = \"getBtn\""
+ "       value   = \"OK\""
+ "       onclick = \"putDate();\">"
+ "</td></tr></table>"
+ "</form></BODY></HTML>"
;
//----------------------------------------------------------------------------
function getTypeIndex()
{
  var i = 0;
  
  while(i < l_asValidTypes.length)
  {
    if(l_asValidTypes[i] == this.sType)
    {
      return i;
    }
    ++i;
  }
  
  return -1;  
}
  //----------------------------------------------------------------------------
  /*
   * Prüfen ob das Hilfefenster korrekt parametrisiert wurde
   */
  function isValid_HW()
  {
    if(this.sKey       == null || this.sKey       == "") { alert("Key");   return false; }
    if(this.sFormName  == null || this.sFormName  == "") { alert("Form");  return false; }
    if(this.sFieldName == null || this.sFieldName == "") { alert("Field"); return false; }
    if(this.sType      == null || this.sType      == "") { alert("Type");  return false; }
    if(this.sTemplate  == null || this.sTemplate  == "") { alert("Template"); return false; }

    var i   = 0;
    var bOK = false;

    while(i < l_asValidTypes.length)
    {
      if(l_asValidTypes[i] == this.sType)
      {
        bOK = true;
        break;
      }

      ++i;
    }

    if(bOK == false) {  alert("invalid type"); return false; }

    return true;
  }
  //----------------------------------------------------------------------------
  /*
   *  Das eigentliche Öffnen des Hilfe-Fenster. Impliziter Parameter ist die
   *  "this"-Referenz auf das Fensterobjekt.
   */
  function open_HW()
  {
    if(this.isValid() == false)
    {
      alert("Internal: Can not open window");
      return;
    }

    var iIndex = this.getTypeIndex();    
    var sWindowDef = iIndex >= 0 ? l_asWindowDef[iIndex] : l_asWindowDef[0]; 
    

    if(document.all)
    {
      sWindowDef += ",left=" + this.iLeft + ",top=" + this.iTop;
    }
    else
    {
      sWindowDef += ",screenX=" + this.iLeft + ",screenY=" + this.iTop;
    }

    // Breite und Hoehe bestimmen um das evtl. Vergoessern, verkleinern zu koennen
    var sMatcher = /width\s*=\s*(\d+)/i;
    if(this.iWidth == null || this.iWidth == 0)
    {
      sMatcher.exec(sWindowDef);
      if(RegExp.$1 != null && RegExp.$1 != "")
      {
        this.iWidth = parseInt(RegExp.$1);
      }
    }
     sMatcher = /height\s*=\s*(\d+)/i;
    if(this.iHeight == null || this.iHeight == 0)
    {
      sMatcher.exec(sWindowDef);
      if(RegExp.$1 != null && RegExp.$1 != "")
      {
        this.iHeight = parseInt(RegExp.$1);
      }
    }


    if(! l_wnd)               { l_wnd = window.open("about:blank",l_sWindowName,sWindowDef); }
    if(l_wnd && l_wnd.closed) { l_wnd = window.open("about:blank",l_sWindowName,sWindowDef); }


    l_wnd.resizeTo(this.iWidth,this.iHeight);
//    l_wnd.moveTo(this.iTop,this.iLeft);

    var sData = "";

    // eine Listbox ist anzuzeigen
    if(this.sType == l_asValidTypes[0] || this.sType == l_asValidTypes[2])
    {
      for(var i = 0; i < this.asData.length; ++i)
      {
        sData += "<option value = \"" + this.asData[i] + "\">" + this.asData[i] + "<\/option>";
      }
    }

    if(this.sWindowHTML == "")
    {
      var sWindowContent = this.sTemplate;

      sWindowContent = sWindowContent.replace(/#HEAD/g ,this.sHead);
      sWindowContent = sWindowContent.replace(/#FORM/g ,this.sFormName);
      sWindowContent = sWindowContent.replace(/#FIELD/g,this.sFieldName);
      sWindowContent = sWindowContent.replace(/#TEXT/g ,this.sHelpText);
      sWindowContent = sWindowContent.replace(/#DATA/g ,sData);
      sWindowContent = sWindowContent.replace(/#SIZE/g ,this.sListSize);

      this.sWindowHTML = sWindowContent;
    }

    var doc = l_wnd.document;
    doc.open("text/html");
    doc.write(this.sWindowHTML);
    doc.close();

    l_wnd.focus();
  }
  //----------------------------------------------------------------------------
  /*
   * Constructor des Helpwindows
   */
  function createHW(
                     sKey,        // Name des Helwindow-Objects
                     sHead,       // Ueberschrift über der Auswahlhilfe
                     sFormName,   // Name des Formulars
                     sFieldName,  // Name des zu besetzenden Feldes
                     sType,       // Typ der Wertanzeige: ListBox, ...
                     asData,      // Menge der Daten als Array, wenn es sich um eine Listbox handelt
                     sHelpText,   // Weiterer Hilfetext,
                     sTemplate,   // Template des Fensterinhaltes
                     sListSize    // Anzahl der Zeilen in der Liste
                    )
  {
      /*
       * Felder des Hilfeobjektes zuweisen
       */
      this.sKey       = sKey;
      this.sHead      = sHead;
      this.sFormName  = sFormName;
      this.sFieldName = sFieldName;
      this.sType      = sType;
      this.asData     = asData;
      this.sHelpText  = sHelpText;
      this.sTemplate  = sTemplate;
      
      if(sListSize != null && sListSize != "")
      {
        this.sListSize  = sListSize;
      }
      else
      {
        this.sListSize = "1";
      }
      
      if(this.sTemplate == null || this.sTemplate == "")
      {
        if(this.sType == "applet")
        {
          this.sTemplate = l_sHelpWindowAppletTemplate;
        }
        if(this.sType== "listbox")
        {
          if(this.sListSize == "1" || this.sListSize == "0")
          {
            this.sTemplate = l_sHelpWindowDropTemplate;
          }
          else
          {
            this.sTemplate = l_sHelpWindowListTemplate;
          }
        }
        if(this.sType == "droplist")
        {
          this.sTemplate = l_sHelpWindowDropTemplate;
        }
      }
      
      

      /*
       * Felder anlegen, die erst bei der Anzeige besetzt werden
       */
      this.iLeft       = 0;
      this.iTop        = 0;     
      this.iWidth      = 0;
      this.iHeight     = 0;
      this.sWindowHTML = "";

      /*
       * Methoden des Hilfeobjektes zuweisen
       */
      this.isValid      = isValid_HW;
      this.open         = open_HW;
      this.getTypeIndex = getTypeIndex;
  }
//-------------------------------------------------------------------------------------------------------
// Sicherstellen, daß das Hilfefenster beim Verlassen geschlossen wird,
// dazu in die unload-Chain einhängen
//-------------------------------------------------------------------------------------------------------

function closeHelpWindow()
{
  if(l_wnd && !l_wnd.closed)
  {
    l_wnd.close();
  }

  if(l_procOldOnUnload && (l_procOldOnUnload != closeHelpWindow))
  {
    var proc = l_procOldOnUnload;
    l_procOldOnUnload = null;
    proc();
  }
}

var l_procOldOnUnload = null;

if((!l_procOldOnUnload || l_procOldOnUnload == null) && window.onunload != closeHelpWindow)
{
  l_procOldOnUnload = window.onunload;
}

window.onunload = closeHelpWindow;

//------------------------------------------------------------------------------

  //----------------------------------------------------------------------------
  // Funktion in der die eigentliche Anzeige des Hilfefensters erledigt wird
  //----------------------------------------------------------------------------

  var l_aoHelpWindows = new Array(); // normales Array
  var l_hoHelpWindows = new Array(); // Hash-Table

  function showHelpWindow(
                          windowIndicator, // Index des Hilfefensters im Array, oder Name des Objektes
                          oElement         // Element zu dem das Fenster aufgerufen wird
                         )
  {
    if(!l_aoHelpWindows || l_aoHelpWindows.length == 0)
    {
      createAllHelpWindows();
    }
    
    var oHW;

    if(parseInt(windowIndicator) >= 0)
    {
      oHW = l_aoHelpWindows[parseInt(windowIndicator)];
    }
    else
    {
      oHW = l_hoHelpWindows[windowIndicator];
    }


    if(! oHW)
    {
      alert("Invalid helpwindow-object reference");
      return;
    }

    /*
     * Bestimmen der Position des Hilfefensters:
     * Ermitteln der relativen Position des auslösenden Elementes im
     * Browserfenster. Anschließend Verschiebung um die Position des
     * Browserfensters auf dem Screen selbst.
     * Im Internet-Explorer müssen noch die Ausmaße der sichtbaren und
     * relevanten Toolbars addiert werden...
     * Netscape macht sowieso was er will...
     */

    var iLeft = 0;
    var iTop  = 0;

    if(oElement)
    {
      var oParent = oElement.offsetParent;

      while(oParent)
      {
        iLeft += parseInt(oParent.offsetLeft);
        iTop  += parseInt(oParent.offsetTop);
        oParent = oParent.offsetParent;
      }

      if (document.all)
      {
        iLeft += window.screenLeft;
        iTop  += window.screenTop;
      }
      else if (document.layers)
      {
        iLeft += window.screenX;
        iTop  += window.screenY;
      }
    }

    oHW.iLeft = parseInt(iLeft);
    oHW.iTop  = parseInt(iTop);
        
    oHW.open();
  }
  
//----------------------------------------------------------------------------
// Konstruktion der Hilfefenster aus den Daten
// wird beim Versuch der ersten anzeige eines Hilfefensters aufgerufen.
//----------------------------------------------------------------------------  

function createAllHelpWindows()
{ 
  var iLaufIndex  = 0;
  var oHelpWindow;

  while(iLaufIndex < l_aaHelpWindows.length) 
  {
    oHelpWindow = new createHW(l_aaHelpWindows[iLaufIndex][0],
                               l_aaHelpWindows[iLaufIndex][1],
                               l_aaHelpWindows[iLaufIndex][2],
                               l_aaHelpWindows[iLaufIndex][3],
                               l_aaHelpWindows[iLaufIndex][4],
                               l_aaHelpWindows[iLaufIndex][5],
                               l_aaHelpWindows[iLaufIndex][6],
                               l_aaHelpWindows[iLaufIndex][7],
                               l_aaHelpWindows[iLaufIndex][8],
                               l_aaHelpWindows[iLaufIndex][9]);

    l_aoHelpWindows.push(oHelpWindow);
    l_hoHelpWindows[l_aaHelpWindows[iLaufIndex][0]] = oHelpWindow;

    ++iLaufIndex;
  } // while
    
}
  
//Auswahl-Hilfe-Bibliothek
