//
// Base JavaScript code for the OrdinaSoft library.
//
// Author:  OrdinaSoft
//          Patrick Lanz
//          Lausanne
//          info@ordinasoft.ch
//
// First version: May 1st, 2007
//
// (c) 2007, OrdinaSoft, all rights reserved

//-----------------------------------------------------------------------------------------------
// Namespace initialization.

if (typeof OrdinaSoft == 'undefined')
  OrdinaSoft = new Object ();
if (typeof OrdinaSoft.Base == 'undefined')
  OrdinaSoft.Base = new Object ();
if (typeof OrdinaSoft.Base.Select == 'undefined')
  OrdinaSoft.Base.Select = new Object ();

//-----------------------------------------------------------------------------------------------
// Global variables.

OrdinaSoft.Base.OnLoadFired = false;  // indicates if we already received the "onload" event
OrdinaSoft.Base.OnLoadFuncs = [];     // functions to execute on load

//-----------------------------------------------------------------------------------------------
// "onload" management.

// Adds a function to be called when the page is completely loaded.
//  - Func: the function to call.

function AddLoadEvent (Func) {

  if (OrdinaSoft.Base.OnLoadFired) {
    Func ();
    return true;
  }  // OrdinaSoft.Base.OnLoadFired
  OrdinaSoft.Base.OnLoadFuncs [OrdinaSoft.Base.OnLoadFuncs.length] = Func;
  return true;
} // AddLoadEvent



// Initialization: to call the registered functions.

OrdinaSoft.Base.Init = function () {

  OrdinaSoft.Base.OnLoadFired = true;
  var Funcs = OrdinaSoft.Base.OnLoadFuncs;
  for (var i = 0; i < Funcs.length; i++)
    Funcs [i] ();
  return true;
} // OrdinaSoft.Base.Init



// Initializes the firing of the "onload" event.

if (typeof window.addEventListener != 'undefined')
  window.addEventListener ('load', OrdinaSoft.Base.Init, false);
else if (typeof document.addEventListener != 'undefined')
  document.addEventListener ('load', OrdinaSoft.Base.Init, false);
else if (typeof window.attachEvent != 'undefined')
  window.attachEvent ('onload', OrdinaSoft.Base.Init);
else {
  if (typeof window.onload != 'function')
    window.onload = OrdinaSoft.Base.Init;
  else {
    OldFn = window.onload;
    window.onload =
      function () {
        OldFn ();
        OrdinaSoft.Base.Init ();
      }
  }
}  // Can't use DOM function

//-----------------------------------------------------------------------------------------------
// Management of elements.

// Gets the absolute horizontal position of an element.
//  - Element is the element to process.
//  - The result will be the absolute horizontal position of the element.

OrdinaSoft.Base.GetElementLeftAbs = function (Element) {

  var Pos = Element.offsetLeft;
  var Parent = Element.offsetParent;
  while (Parent != null) {
    Pos += Parent.offsetLeft;
    Parent = Parent.offsetParent;
  }  // while Parent != null

  return Pos;
} // OrdinaSoft.Base.GetElementLeftAbs



// Gets the absolute vertical position of an element.
//  - Element is the element to process.
//  - The result will be the absolute vertical position of the element.

OrdinaSoft.Base.GetElementTopAbs = function (Element) {

  var Pos = Element.offsetTop;
  var Parent = Element.offsetParent;
  while (Parent != null) {
    Pos += Parent.offsetTop;
    Parent = Parent.offsetParent;
  }  // while Parent != null

  return Pos;
} // OrdinaSoft.Base.GetElementTopAbs

//-----------------------------------------------------------------------------------------------
// Tools for select elements.

// Gets the value of the currently selected item in a select. If no itemm is selected, the result
// will be the default value.
//  - Select: the select element, or its identifier.
//  - Default: the default value.

OrdinaSoft.Base.Select.GetCurrent = function (Select, Default) {

  switch (typeof Select) {
    case 'undefined'  :
      return Default;
    case 'string'  :
      Select = document.getElementById (Select);
      if (Select == null)
        return Default;
      break;
  }  // switch (typeof Select)

  var ix = Select.selectedIndex;
  if (ix == -1)
    return Default;
  return Select.options [ix].value;
}  // OrdinaSoft.Base.Select.GetCurrent

//-----------------------------------------------------------------------------------------------

OrdinaSoft_Base_Initialized = true;  // indicates that the library is initialized