﻿////////////////////////////////////////////////////////////////////////////////////////////////////
// Norman Geiersbach
// Eitido (c) 2008
// Version 1.0.0.0
// Created 03/07/2007
//
// Description
//   
//
// History
//   03/07/2007 File created
////////////////////////////////////////////////////////////////////////////////////////////////////


// Browser utility functions
////////////////////////////////////////////////////////////////////////////////////////////////////

/// getBrowserCode
/// Returns the browser code
  function getBrowserCode()
  {
    if( !document.all && document.getElementById )
      return "ns6";
    if( document.layers )
      return "ns4";
    if( document.all )
      return "ie";
  }


// redirectToUrl
// Redirects the current frame / window to another page or site
  function redirectToUrl(url)
  {
    self.location.href = url;
  }


// Element utility functions
////////////////////////////////////////////////////////////////////////////////////////////////////

// getElement
// Returns the reference to an element
  function getElement(elementId)
  {
    if( document.getElementById )
      return document.getElementById(elementId);
    if( document.all )
      return document.all[elementId];

    return null;
  }


// getRootElement
// Returns the reference to the root element
  function getRootElement()
  {
    if( document.all )
      return document.all;
    else
      return document.getElementsByTag("*");
  }
  
  
// getElementsByName
// Returns an array of elements by name
  function getElementsByName(elementName)
  {
    if( document.getElementsByName )
      return document.getElementsByName(element_name);

    return null;
  }
  
  
// getElementByName
// Returns the first one of elements by name
  function getElementByName(elementName)
  {
    if( document.getElementsByName )
    {
      var array = document.getElementsByName(elementName);
      if( array.length > 0 )
        return array[0];
      else
        return null;
    }

    return null;
  }
  
  
// getElementsByTag
// Returns an array of child elements by tag name 
  function getElementsByTag(rootElement, tagName)
  {
    var elementArray = new Array();
    var index = 0;    
    
    if( rootElement.length == null )
      rootElement = rootElement.childNodes;
      
    tagName = tagName.toUpperCase();
    
    for(i = 0; i < rootElement.length; i++)
    {
      if( rootElement[i].tagName == tagName )      
        element_array[index++] = root[i];     
    }

    return elementArray;
  }
  
  
// getElementByTag
// Returns the first one of child elements by tag name
  function getElementByTag(rootElement, tagName)
  {
    if( rootElement.length == null )
      rootElement = rootElement.childNodes;
      
    tagName = tagName.toUpperCase();
    
    for(i = 0; i < rootElement.length; i++)
    {
      if( rootElement[i].tagName == tagName )      
        return rootElement[i];     
    }
    
    return null;
  }  


// getElementsByClass
// Returns an array of elements by class name 
  function getElementsByClass(rootElement, className, recursive)
  {
    var elementArray = new Array();
    var index = 0;   
    
    if( rootElement.length == null )
      rootElement = rootElement.childNodes;

    for(i = 0; i < rootElement.length; i++)
    {
      if( rootElement[i].className == className )      
        elementArray[index++] = rootElement[i];
      
      if( recursive && (rootElement[i].childNodes.length > 0) )
      {
        var subElementArray = getElementsByClass(rootElement[i].childNodes, className, recursive);
        if( subElementArray.length > 0 )
        {
          elementArray = elementArray.concat(subElementArray);
          index += subElementArray.length;
        }
      }
      
    }

    return element_array;
  }
