///////////////////////////////////////////////////////////////////////////////
//
// AccessKey Underlining Library v0.4
//
// Copyright (c) 2005 Data Connection Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// CHANGELOG:
//
// v0.1: initial version (2005-02-16)
// v0.2: made it validate, which needs nasty <div> hack
// v0.3: changed switching approach to something nicer, if a little less
//       cutting-edge. (2005-05-22)
// v0.4: Removed obsolete code and comments. (2005-06-26)
//
///////////////////////////////////////////////////////////////////////////////

addEvent(window, "load", underlineAccessKeys);

function underlineAccessKeys()
{
  addMarkup(document.getElementsByTagName('button'));
  addMarkup(document.getElementsByTagName('label'));
  addMarkup(document.getElementsByTagName('legend'));  
  addMarkup(document.getElementsByTagName('a'));  
}

function addMarkup(elems)
{
  var elem;
  var i = 0;

  while(elem = elems.item(i++))
  {
    // These go around the key itself
    var open = "<u class='accesskey'>";
    var close = "</u>";
    var attr = elem.getAttributeNode("accesskey");

    if (attr && attr.value) {
      var innerH = elem.innerHTML;
      if (innerH.match(attr.value))
      {
        // Use first exact case match
        elem.innerHTML = innerH.replace(attr.value, open + attr.value + close);
      }
      else
      {
        // No match; use brackets afterwards instead
        // Possible enhancement: deal with terminating colons better
        elem.innerHTML = innerH + "<span class='accesskeySurround'> (" + 
                                  open + attr.value + close + ")</span>";
      }
    }
  }
}

// This is a cross-browser function for event addition.
function addEvent(obj, evType, fn)
{
  if (obj.addEventListener)
  {
    obj.addEventListener(evType, fn, false);
    return true;
  }
  else if (obj.attachEvent)
  {
    var r = obj.attachEvent("on" + evType, fn);
    return r;
  }
  else
  {
    alert("Event handler could not be attached");
    return false;
  }
}