// Copyright (c) 2003 World Wide Web Consortium,
// (Massachusetts Institute of Technology, European Research Consortium for
// Informatics and Mathematics, Keio University). All Rights Reserved. This
// work is distributed under the W3C(r) Software License [1] in the hope that
// it will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// 
// [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
//
// $Id: 06-dom-support-js.js,v 1.2 2004/05/14 23:43:47 plehegar Exp $

// XHTML 1.0 namespace
var XHTML_NS = "http://www.w3.org/1999/xhtml";

var isXHTML  = false;

// The tbody element in the page
//   http://www.w3.org/2003/02/06-dom-support.html
var tbody = null;

// return the first element in the following siblings _starting from the
// node itself_.
function getElement(node) {
  while (node != null) {
    if (node.nodeType == 1) {
      return node;
    }
    node = node.nextSibling;
  }
  // we reached the end, no element found.
  return null;
} // getElement(node)

// change the status from not supported to supported if needed for a
// given cell.
function setSupported(node, feature, version) {
  if (!document.implementation.hasFeature(feature, version)) {
    // not supported
    return;
  }
  // it is supported, so now change the cell state

  // changing colors is not very useful for blind people, so let's try
  // to change the content of the cell, using DOM Level 1 core methods,
  // as well to make a significant difference.
  if (node.firstChild
      && node.firstChild.firstChild
      && node.firstChild.firstChild.data) {
    var data = node.firstChild.firstChild.data;
    if (data == null || data != "N/A") {
      node.firstChild.firstChild.data = "supported";
    }
  }

  if (isXHTML) {
    if (node.setAttributeNS) {
      // if the page is written using XHTML 1.0, attributes must be put
      // in the per-element-type partition. This method is a DOM Level 2
      // Core method.
      node.setAttributeNS(null, "class", "supported");
    }
  } else if (node.setAttribute) {
    // the page is written using HTML 4.01 (or is using the HTML
    // compatiblity guidelines defined in XHTML 1.0), use the DOM Level
    // 1 core method then.
    node.setAttribute("class", "supported");
  }

  // in case setting the attribute didn't work, or the display engine is
  // buggy, or you're using a user agent which does not recognize the
  // class attribute, let's try with a DOM Level 2 CSS2 method.
  if (node.style) {
    node.style.background = "white";
  }

} // setSupported(node, feature, version)

function init() {
    if (!(document
	  && document.implementation
	  && document.implementation.hasFeature)) {
      //no dom implementation? then exit
      return;
    }

    // If the page is written using XHTML 1.0, try with the appropriate
    // method using a DOM Level 2 Core method.
    if (document.getElementsByTagNameNS) {
      var l = document.getElementsByTagNameNS(XHTML_NS, "tbody");
      if (l.item) {
	tbody = l.item(0);
	if (tbody != null) isXHTML = true;
      }
    }

    // if the first try did not work, it means the page is written in
    // HTML 4.01 and we should try with a DOM Level 1 core method.
    if (tbody == null && document.getElementsByTagName) {
      var l = document.getElementsByTagName("tbody");
      if (l.item) {
	tbody = l.item(0);
      }
    }
    if (tbody == null) {
      // ok, nothing worked, not worth going further since the document
      // does not even work with DOM Level 1 core method.
      return;
    }

    // starting from now, all attributes are protected with an excape
    // mechanism compatible with DOM Level 0 user agents. if the
    // attribute does not exist (ie returns null), then it's not worth
    // continuing since we're using DOM Level 1 core methods to access
    // them.

    var tr = getElement(tbody.firstChild); if (tr == null) return;

    // Note that I am also testing the N/A cells in the following code.
    // I don't take into account modules dependencies.

    while (tr != null) {

      // get the th element of the first column, it contains the name of
      // the module, ie the DOM feature
      var n = getElement(tr.firstChild);
       // if n is null or its text node is null, not worth continuing.
       if (n == null || n.firstChild == null || n.firstChild.firstChild == null) return;
          
      // from the th element, get its text node content to obtain the
      // DOM feature to test
      var feature = n.firstChild.firstChild.data; if (feature == null) return;

      // second column is for DOM Level 1
      n = getElement(n.nextSibling); if (n == null) return;
      setSupported(n, feature, "1.0");

      // third column is for DOM Level 2
      n = getElement(n.nextSibling); if (n == null) return;
      setSupported(n, feature, "2.0");

      // fourth column is for DOM Level 3
      n = getElement(n.nextSibling); if (n == null) return;
      setSupported(n, feature, "3.0");

      // and do this again for each tr elements in the tbody
      tr = getElement(tr.nextSibling);
    }
} // init()
