// JScript File

var cxlEnv = new Object();
var isIE5or6 = false;
var isIE6 = false;
var isIE7 = false;


function addMenuEffects(obj, doShim, doHover)
{
  var iframeCode = '<iframe class="menushim" src="about:blank" scrolling="no" frameborder="0"></iframe>';

  if (!obj)
    return;
  if (doShim)
  {
    // add an IFRAME to all UL's in the menu,
    // this will make IE display the menu on top of all other elements
    var menus = obj.getElementsByTagName("ul");
    for (var i=0; i<menus.length; i++)
    {
      submenu = menus[i];
      submenu.innerHTML=(iframeCode + menus[i].innerHTML);
    }
  }

  if (doHover)
  {
    // one function to show and hide the submenu
    // it also resizes the iframe in the course of it
    // this can not be done in the doShim part
    // (see above) since natural height is calculated
    // during rendering, not during loading of the page
    var items = obj.getElementsByTagName("li");
    for (i=0; i<items.length; i++)
    {
      // the mouse over is quite complex
      items[i].onmouseover = function(){
        var menus = this.getElementsByTagName("ul");
        // apply the hover class
        this.className+=" over";
        // loop through all visible submenus that need to display
        // the iframe, and show it
        for (var i=0; i<menus.length; i++)
        {
          element = menus[i];
          // check if there actually is something there
          if (!element.firstChild)
            continue;
          // and of course it should be an iframe
          if (!element.firstChild.tagName.toUpperCase()=='IFRAME')
            continue;
          iframe = element.firstChild;
          // if the thing actually has a height use this for the iframe
          if (parseInt(element.clientHeight)>0)
            // the 1px is a quick fix, it should actually be the border size
            // since IE 'forgets' to take the border height into account
            iframe.style.height = (parseInt(element.clientHeight)-1) + "px";
        }
      };
      // the mouse out removes the .over class from the list items
      items[i].onmouseout=function(){
        this.className=this.className.replace(" over","");
      };
    }
  }
}

/*
 * @name  startList
 * @description adds the menu effects for several menus
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
startList = function()
{
  // check current environment
/*
  isIE = document.all?true:false;

var agent = navigator.userAgent;
if ((agent.indexOf("MSIE"))!=-1) {
 browserName  = "Microsoft Internet Explorer";
 fullVersion  = parseFloat(agent.substring(verOffset+5));
 majorVersion = parseInt(''+fullVersion);
}
*/
  // document.all checks for IE. Al other browsers are OK
  var supportsID = (document.getElementById)?true:false;
  if (cxlEnv.isIE5or6 && supportsID)
  {
    var menus = ['dynamicMenu','subMenu'];
    for (n in menus)
    {
      var menuName = menus[n];
      var menu = document.getElementById(menuName);
      if (menu)
        addMenuEffects(menu, true, true);
    }
  }
};

/*
 * @name  addEvent
 * @description adds an event handler to an element
 * this way multiple functions can be triggered by an event
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function addEvent(obj, evType, fn)
{
  var ret = false;
  // MB:check if fn exists
  if (!fn)
    return false;
  // adds an eventListener for browsers which support it
  // Written by Scott Andrew: nice one, Scott
  if (obj.addEventListener)
  {
    obj.addEventListener(evType,fn,true);
    ret = true;
  }
  else
    if (obj.attachEvent)
    {
      var r = obj.attachEvent("on"+evType,fn);
      ret = r;
    }
  return ret;
}

addEvent(window, "load", startList);

    /* (c) 2005 CaribMedia */

/*
 * @name  isTag
 * @description simply checks if an element is a tag or a text
 *        IE sees tags+text, FF sees only tags
 * @author  Michiel van der Blonk
 * @date  Oct 18, 2005
*/
function isTag(node)
{
  return (node.nodeType == 1);
}

/*
 * @name fold
 * @description folds/unfolds part of a page (tile)
 * @param skip  - the number of elements to skip after
 *        - in fact, skip is the Nth child from the parent
 * @author Michiel van der Blonk
 * @date July 11, 2005
 * @updated Oct 18, 2005
*/
function fold(el, skip)
{
  // the block to hide should be the first child after
  // the show-hide button
  var tile, node, parent;

  // find the element
  parent = el.parentNode;
  if (parent.hasChildNodes())
  {
    // init node
    node = parent.childNodes[0];
    size = parent.childNodes.length;
    nCountElements = 0;
    nCountTags = 0;
    // if there are any elements, continue
    if (size > 0)
    {
      // loop until bounds
      while (nCountTags < skip && nCountElements<size)
      {
        node = parent.childNodes[nCountElements];
        nCountElements++;
        // count the tags
        if (isTag(node))
          nCountTags++;
      }
      tile = node;
    }
    else
      alert('error: list cannot display');
  }
  else
    alert('error: list cannot display');
  // now show/hide the element
  var isHidden = false;
  if (tile.className)
    isHidden = tile.className.match(/hide/)!==null;

  if (!isHidden)
  {
    tile.className += " hide";
    el.className += " collapsed";
  }
  else
  {
    tile.className = tile.className.replace(" hide","");
    el.className = el.className.replace(" collapsed","");
    // also for elements with a single class name
    tile.className = tile.className.replace("hide","");
    el.className = el.className.replace("collapsed","");
  }

  return false;
}


/*
 * @name  collapse
 * @description collapses all elements in a list by calling fold()
 * @author  Michiel van der Blonk
 * @date  Oct 18, 2005
*/
function collapse(listId)
{
  var list = document.getElementById(listId);
  var foldIcons = list.getElementsByTagName("span");
  for (var i=0; i<foldIcons.length; i++)
    {
    if ( foldIcons[i].className.match(/fold/)!==null )
        fold(foldIcons[i], 3);
    }
}


/*
 * @name  expand
 * @description expands an item by walking up the tree
 * @author  Michiel van der Blonk
 * @date  Oct 18, 2005
*/
function expand(objElement)
{
// THIS IS NOT WORKING YET.
// WE NEED TO ACTUALLY _FOLD_ HERE, SINCE THE + NEEDS TO BE SHOWN

  activeElement = objElement;
  do
  {
    if (activeElement.tagName == "UL")
    {
      tile.className = tile.className.replace(" hide","");
      el.className = el.className.replace(" collapsed","");
      // also for elements with a single class name
      tile.className = tile.className.replace("hide","");
      el.className = el.className.replace("collapsed","");
    }
    activeElement = activeElement.parentNode;
  } while (activeElement.tagName!="DIV" && activeElement.tagName!="BODY")
}

/*
 * @name  showHide
 * @description hides an element effectively by adding the 'hide' class
 * @precond the class named 'hide' should exist in the css
 * @author  Michiel van der Blonk
 * @date  Oct 18, 2005
*/
function showHide(id)
{
  var isHidden;
  tile = document.getElementById(id);
  showHideObject(tile);
}

/*
 * @name  showHide
 * @description hides an element effectively by adding the 'hide' class
 * @precond the class named 'hide' should exist in the css
 * @author  Michiel van der Blonk
 * @date  Oct 18, 2005
*/
function showHideObject(objElement)
{
  var isHidden;
  tile = objElement;
  // check for existence of tile element
  if (!tile)
    return;
  isHidden = tile.className.match(/hide/)!==null;

  if (isHidden)
  {
    tile.className = tile.className.replace(" hide","");
    // also for elements with a single class name
    tile.className = tile.className.replace("hide","");
  }
  else
    tile.className += " hide";
}

/*
 * @name  debug
 * @description shows debug info in a 'debug' box in html
 * @precond the element named 'debug' should exist in the css
 * @author  Michiel van der Blonk
 * @date  Oct 18, 2005
*/
function debug(s)
{
  debugBox = document.getElementById('debug');
  if (debugBox)
    debugBox.innerHTML += s + "<br>";
}

/*
 * @name  findOwner
 * @description finds the parent DIV
 * @param evt - the event
 * @author  PPK (www.quirksmode.org)
 * @date  Oct 18, 2005
*/
function findOwner(element)
{
  node = element;
    while (node)
    {
        if (isTag(node) && node.nodeName == "DIV")
                return node;
        node = node.parentNode;
    }
    return null;
}

/*
 * @name  autoFold
 * @description sets a timer to automatically hide the list
 * @param selectId - the id of the faux-select
 * @param listId - the id of the UL
 * @param target - the element that triggered the event
 * @param delay - nr of seconds to wait before closing
 * @author  Michiel van der Blonk
 * @date  Oct 18, 2005
*/
function autoFold(selectId, listId, target, delay)
{
  // prevent bubbles
  if (window.event)
    window.event.cancelBubble = true;
  // if this is not the list return
  if (target.id!=listId)
    return;
  var isHidden;
  list = document.getElementById(listId);
  selectBox = document.getElementById(selectId);
  isListVisible = list.className.match(/hide/)===null;
  if (isListVisible)
    window.setTimeout("fold(selectBox, 2)", delay*1000);
}

/*
 * @name  getTarget
 * @description cross browser code for finding out an event target
 * @param e - the event
 * @author  Michiel van der Blonk
 * @date  Oct 18, 2005
*/
function getTarget(e)
{
  var objEvent;

  // in IE the event param is null
  if (!e)
    objEvent = window.event;
  else
    objEvent = e;
  // IE has srcElement attr
  if (objEvent.srcElement)
    target = objEvent.srcElement;
  // FF/NN has target attr
  if (objEvent.target)
    target = objEvent.target;
  return target;
}

/* this function has been integrated into
   initSelectList for performance reasons

   it was set to #, which has bad usability
   this is a temporary fix, Kia should fix it
*/
function restoreHrefs(selectBox)
{
  var links;
  var link;
  var curLink;

  var list = document.getElementById(selectBox);

  if (!list)
    return false;

  links = list.getElementsByTagName("a");
  for (var i=0; i<links.length; i++)
  {
    curLink = links[i];
    rel = curLink.getAttribute('rel');
    if (!isNaN(rel))
    {
      var page = "/" + cxlEnv.context + "do/categoryId/@rel/categoryBranchId/@rel/getDocumentList.html";
      //curLink.href = page.replace(new RegExp('@rel','g'), rel);
    }
  }
  return true;
}

/************************************************************************************************************/
/************************************************************************************************************/
function docOnclick(e,selectId,listId)
{
    selectBox = document.getElementById(selectId);
    list = document.getElementById(listId);
  
    if(e == null)
    	e = window.event;
    t = getTarget(e);
    owner = findOwner(t);
    if (owner && owner.id==listId)
      return;
    if (t.id!=listId && t.id!=selectId )
    {
      if (!list.className.match(/hide/))
          fold(selectBox, 2);
    }
}

function initSelectListNew(selectId, listId, formId, fieldId, autoHide, currentNode, onAction)
{
  initSelectListNewHandler(selectId, listId, formId, fieldId, autoHide, currentNode, onAction, docOnclick);
}

function initSelectListNewHandler(selectId, listId, formId, fieldId, autoHide, currentNode, onAction, func)
{
  selectBox = document.getElementById(selectId);
  list = document.getElementById(listId);
  
  var prev_handler = document.onclick;
  if(typeof document.onclick != "function")
  {
    document.onclick = function(e){func(e,selectId,listId);};
  }
  else
    document.onclick=function(e)
    {
      prev_handler(e,selectId,listId);
      func(e,selectId,listId);
    };
  //collapse(listId);
  //fold(selectBox, 2);

  try
  {
    var items = list.getElementsByTagName("a");
    for (var i=0; i<items.length; i++)
    {
        fSelect = function()
          {
            forSelectBox = document.getElementById(selectId);
            forSelectBox.innerHTML = this.innerHTML;
            fold(forSelectBox, 2);
            form = document.getElementById(formId);
            field = document.getElementById(fieldId);
            field.value = this.getAttribute('rel');
            if (onAction !== null) eval(onAction);
            return false;
          };

      items[i].onclick= fSelect;
      if (items[i].getAttribute('rel') == currentNode)
      {
        selectBox.innerHTML = items[i].innerHTML;
        activeElement = items[i].parentNode.parentNode;
      }
    }//end for
  }
  catch(e)
  {
    alert(e);
  }
}

/************************************************************************************************************/
/************************************************************************************************************/

function resetFirstItem(formId) {
  var frm = document.getElementById(formId);
  frm.firstItem.value = '0';
}