//
// FILE     : daisytools.js
// TASK     : Contains some basic functions.
// PROJECT  : doctronic standard JavaScript libraries.
// AUTHORS  : Ingo Kueper
// VERSION  : V1.00
// DATE     : 2000/06/08
// LAST     : See section "CVS INFO" for revision date.
// COPYRIGHT: (c) 2000 doctronic GmbH & Co. KG
//            Adenauerallee 45-49, D-53332 Bornheim, Germany
//
//--< CVS INFO >-----------------------------------------------------------
//
// $Revision: 1.1 $
// $RCSfile: daisytools.js,v $
// $Date: 2004/02/05 15:39:15 $
// $Author: hamaekers $
//
//--< MODULE INFO >--------------------------------------------------------
//
// Contents:
//   This library file contains some basic functions to simplify several
//   tasks.
//
//     isDefined (oTypeOf)
//       Checks the given typeof() information for belonging to a defined
//       element.
//
//     daisyCheckLibrary (oLib, oVersion)
//       Checks the given library for being available.
//
//     daisyCheckPackage (oPackage, oVersion)
//       Checks the given package for being available.
//
//     daisyRegisterLibrary (oLib, oVersion)
//       Registers the given library.
//
//     daisyRegisterPackage (oPackage, oVersion)
//       Registers the given package.
//
//     daisyCatchReturn (oEvent)
//       Handles 'onkeydown' events and should be used to prevent forms
//       from being send when pressing the <RETURN> key.
//
//     daisyReadonly (oFormName, oElementName)
//       Handles 'onfocus' events and should be used to create readonly
//       elements by removing the keyboard focus.
//
//     daisyExtractMainURL (oURL)
//       Extracts the main part of the given URL. That is the URL without
//       any search (?) or hash (#) part.
//
// To do:
//
// History:
//   2000/06/08: First Release.
//
// Known Bugs:
//

//-------------------------------------------------------------------------

//
// {group:DaisyTools}
//
// SUMMARY: Test an element for being defined.
// TASK   : Check the given {tt:typeof()} information for belonging to a
//          defined element. (Thanks to the Opera development team for
//          making this function necessary. :(((  )
// PARAMS : oTypOf - Information to check.
// RETURNS: Returns {tt:true} if the given {tt:typeof()} information
//          belongs to a defined variable, and {tt:false} else.
//
function isDefined (oTypeOf)
{
  var bIsDef;


  bIsDef = false;

  if (isDefined.arguments.length == 1)
  {
    bIsDef = ((oTypeOf != "undefined") && (oTypeOf != "null"))
  };

  return bIsDef;
}

//-------------------------------------------------------------------------

//
// {group:DaisyTools}
//
// SUMMARY: Test a library for being present.
// TASK   : Check the given library for being available by scanning the
//          global {tt:aoDaisyLibraries} variable.
// PARAMS : oLib     - Name of the library to check for.
//          oVersion - Version of the library to check for or {tt:null}.
//                     The given version must not contain the "V" prefix.
// RETURNS: Returns {tt:true} if the given library is present with a
//          version equal to or higher than the desired one, and
//          {tt:false} else.
//
function daisyCheckLibrary (oLib, oVersion)
{
  var bLibFound = false;


  if (daisyCheckLibrary.arguments.length == 2)
  {
    if (oLib != null)
    {
      oLib = oLib.toUpperCase ();

      if (isDefined (typeof (aoDaisyLibraries)))
      {
        bLibFound = isDefined (typeof (aoDaisyLibraries[oLib]));

        if (bLibFound && (oVersion != null))
        {
          bLibFound = aoDaisyLibraries[oLib] >= parseFloat (oVersion);
        };
      };
    };
  };

  return bLibFound;
}

//-------------------------------------------------------------------------

//
// {group:DaisyTools}
//
// SUMMARY: Test a package for being available.
// TASK   : Check the given library for being available by scanning the
//          global {tt:aoDaisyPackages} variable.
// PARAMS : oPackage - Name of the package to check for.
//          oVersion - Version of the package to check for or {tt:null}.
//                     The given version must not contain the "V" prefix.
// RETURNS: Returns {tt:true} if the given package is present with a
//          version equal to or higher than the desired one, and
//          {tt:false} else.
//
function daisyCheckPackage (oPackage, oVersion)
{
  var bPackageFound = false;


  if (daisyCheckPackage.arguments.length == 2)
  {
    if (oPackage != null)
    {
      oPackage = oPackage.toUpperCase ();

      if (isDefined (typeof (aoDaisyPackages)))
      {
        bPackageFound = isDefined (typeof (aoDaisyPackages[oPackage]));

        if (bPackageFound && (oVersion != null))
        {
          bPackageFound =
            aoDaisyPackages[oPackage] >= parseFloat (oVersion);
        };
      };
    };
  };

  return bPackageFound;
}

//-------------------------------------------------------------------------

//
// {group:DaisyTools}
//
// SUMMARY: Register a library.
// TASK   : Register the given library version.
// PARAMS : oLib     - Name of the library to register.
//          oVersion - Version of the library to register. If a library is
//                     already registered, the given version needs to be
//                     higher than the registered one.
// RETURNS: Nothing.
// EFFECT : May modify the global variable {tt:aoDaisyLibraries}.
//
function daisyRegisterLibrary (oLib, oVersion)
{
  if (daisyRegisterLibrary.arguments.length == 2)
  {
    if ((oLib != null) && (oVersion != null))
    {
      var fVersion;


      fVersion = parseFloat (oVersion);
      oLib     = oLib.toUpperCase ();

      if (!isDefined (typeof (window.aoDaisyLibraries)))
      {
        window.aoDaisyLibraries = new Object ();
      };

      if (!isDefined (typeof (aoDaisyLibraries[oLib])))
      {
        aoDaisyLibraries[oLib] = fVersion;
      }

      else
      {
        if (fVersion > aoDaisyLibraries[oLib])
        {
          aoDaisyLibraries[oLib] = fVersion;
        };
      };
    };
  };
}

//-------------------------------------------------------------------------

//
// {group:DaisyTools}
//
// SUMMARY: Register a package.
// TASK   : Register the given package version.
// PARAMS : oPackage - Name of the package to register.
//          oVersion - Version of the package to register. If a package is
//                     already registered, the given version needs to be
//                     higher than the registered one.
// RETURNS: Nothing.
// EFFECT : May modify the global variable {tt:aoDaisyPackages}.
//
function daisyRegisterPackage (oPackage, oVersion)
{
  if (daisyRegisterPackage.arguments.length == 2)
  {
    if ((oPackage != null) && (oVersion != null))
    {
      var fVersion;


      fVersion = parseFloat (oVersion);
      oPackage = oPackage.toUpperCase ();

      if (!isDefined (typeof (window.aoDaisyPackages)))
      {
        window.aoDaisyPackages = new Object ();
      };

      if (!isDefined (typeof (aoDaisyPackages[oPackage])))
      {
        aoDaisyPackages[oPackage] = fVersion;
      }

      else
      {
        if (fVersion > aoDaisyPackages[oPackage])
        {
          aoDaisyPackages[oPackage] = fVersion;
        };
      };
    };
  };
}

//-------------------------------------------------------------------------

//
// {group:DaisyTools}
//
// SUMMARY: Prevent forms from being send when pressing <RETURN>.
// TASK   : Handle the "key pressed" event. The <RETURN> key will be
//          blocked to prevent forms from being send.
// PARAMS : oEvent - The corresponding object of the event (Netscape).
// RETURNS: {tt:true}, if the <RETURN> key was *not* pressed. {tt:false}
//          else.
// EXAMPLE:
//   To prevent a form from being send when pressing the <RETURN> key,
//   use this function to handle the "key pressed" events of the input
//   fields:
//
//   <FORM>
//     <INPUT NAME="MyInput" TYPE="text" SIZE="20"
//            onKeyDown="daisyCatchReturn ()">
//   </FORM>
//
function daisyCatchReturn (oEvent)
{
  var bReturnPressed = false;


  if (daisyCatchReturn.arguments.length == 1)
  {
    if (oEvent != null)
    {
      bReturnPressed = (oEvent.which == 13);
    };
  }

  else
  {
    if (isDefined (typeof (window.event)))
    {
      if (isDefined (typeof (window.event.cancelBubble)))
      {
        bReturnPressed = (window.event.keyCode == 13);

        if (bReturnPressed)
        {
          window.event.returnValue  = false;
          window.event.cancelBubble = true;
        };
      };
    };
  };

  return !bReturnPressed;
}

//-------------------------------------------------------------------------

//
// {group:DaisyTools}
//
// SUMMARY: Realize readonly elements.
// TASK   : Handle the "got focus" event. The {tt:readonly} attribute of
//          the textarea and input tag is not recognized by all browsers.
//          This function removes the keyboard focus of the given element
//          to make it readonly.
// PARAMS : oFormName    - Name of the form the element is part of.
//          oElementName - Name of the element to remove the focus from.
// RETURNS: Nothing.
// EXAMPLE:
//   To create a read-only element, use this function to handle the
//   "got focus" event:
//
//   <FORM NAME="MyForm">
//     <TEXTAREA NAME="MyText" ROWS="10" COLS="58" WRAP="virtual"
//               onFocus="daisyReadonly ('MyForm', 'MyText')">
//     </TEXTAREA>
//   </FORM>
//
function daisyReadonly (oFormName, oElementName)
{
  if (daisyReadonly.arguments.length == 2)
  {
    if ((oFormName != null) && (oElementName != null))
    {
      if (isDefined (typeof (document.forms[oFormName])))
      {
        var oForm = document.forms[oFormName];


        if (isDefined (typeof (oForm.elements[oElementName])))
        {
          oForm.elements[oElementName].blur ();
        };
      };
    };
  };
}

//-------------------------------------------------------------------------

//
// {group:DaisyTools}
//
// SUMMARY: Extract the main part of the given URL.
// TASK   : Extract the main part of the given URL by removing the URL's
//          search and hash parts.
// PARAMS : oURL - URL to extract the main part of.
// RETURNS: The main part of the given URL.
//
function daisyExtractMainURL (oURL)
{
  var oMainURL;


  oMainURL = "";

  if (daisyExtractMainURL.arguments.length == 1)
  {
    if (oURL != null)
    {
      var nCut;


      oMainURL = oURL;
      nCut     = oMainURL.indexOf ("?");

      if (nCut >= 0)
      {
        oMainURL = oMainURL.substring (0, nCut);
      };

      if (oMainURL.length > 0)
      {
        nCut = oMainURL.indexOf ("#");

        if (nCut >= 0)
        {
          oMainURL = oMainURL.substring (0, nCut);
        };
      };
    };
  };

  return oMainURL;
}

//-------------------------------------------------------------------------

// Register this library.
daisyRegisterLibrary ("DaisyLib", "1.00");

//-------------------------------------------------------------------------
//-------------------------------------------------------------------------

