/*********************************************************************************
  alaform-mozfix.js - created by Ryan Ballantyne on September 13, 2007
  Javascript that fixes alaforms under Firefox
  The contents of the callback function are copied straight from
    http://www.alistapart.com/articles/prettyaccessibleforms/
  which describes a nice technique for clean, maintainable, accessible form markup.
  I rewrote the node walking code, because the original version used JQuery.
  I don't know JQuery, nor am I particularly inclined to learn right now, and including
  the library on this page is like 22K extra that needs to be downloaded. In short,
  unnecessary bloat. Sure, the JQuery code is simpler and clearer, but not by
  enough to justify the bloat.
*********************************************************************************/
if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', alaform, false );

function alaform()  {
  var alaforms = document.getElementsByTagName ('form');
  
  // ==walkTheDOM==
  // Recursive function that walks through the DOM starting at Node nd, and applies a callback function to each Node encountered
  // Considers only element nodes
  var walkTheDOM = function (nd, callback)  {
    if (nd.nodeType == 1)  { callback (nd); }

    for (var i=0; i < nd.childNodes.length; i++) {
      walkTheDOM (nd.childNodes.item(i), callback);
    }
  };
  
  // Hide forms, process, show forms
  for (var i = 0; i < alaforms.length; i++)  {
    if (alaforms.item(i).className == 'alaform')  {
      alaforms.item(i).style.display = 'none';
      
      //Walk the elements and process
      walkTheDOM (
        alaforms.item(i),
        function (nd)  {
          if ((nd.tagName == 'LABEL' && nd.parentNode.tagName == 'LI') && nd.className != 'noala')  {
            //Process the form element
            var labelContent = nd.innerHTML;
            var labelWidth = document.defaultView.getComputedStyle( nd, '' ).getPropertyValue( 'width' );
            var labelSpan = document.createElement( 'span' );
                labelSpan.style.display = 'block';
                labelSpan.style.width = labelWidth;
                labelSpan.innerHTML = labelContent;
            nd.style.display = '-moz-inline-box';
            nd.innerHTML = '';
            nd.appendChild( labelSpan );
          }
      } );
      //Show forms
      if (alaforms.item(i).className == 'alaform')  alaforms.item(i).style.display = 'block';
    } 
  }
  
}