

function addListener(element, baseName, handler) {
    if (element.addEventListener)
        element.addEventListener(baseName, handler, false);
    else if (element.attachEvent)
        element.attachEvent('on' + baseName, handler);
} // addListener


   function nbText(node,value) {
      var i=0
	var nb=0;
      while (i!=-1) {
         i=node.nodeValue.indexOf(value,i);
         if (i>=0) {
            nb++;
            i+=value.length;
         }
      }
      return nb;
   }


function rechercheText(node,value) {
	result=0;
	if (Node.TEXT_NODE == node.nodeType){
		return nbText(node,value);
        }
	if (Node.ELEMENT_NODE != node.nodeType)
		return 0;
	for (var index = 0; index < node.childNodes.length; ++index)
		result += rechercheText(node.childNodes.item(index),value);
	return result;
} // rechercheText




/*
 * Exemple d'utilisation de NodeList.
 */

// Compensation pour MSIE...
if ('undefined' == typeof Node)
    Node = { ELEMENT_NODE: 1, TEXT_NODE: 3 };



//** Tout le reste sert juste à faire une démo de getInnerText... **

function demo() {
      var value=document.forms[0].elements[0].value;
	if (value!=""){
		var text = rechercheText(document.getElementById('fragmentRoot'),value);
		var results = document.getElementById('results');
		if (results.hasChildNodes())
			results.firstChild.nodeValue = text;
		else {
			var txt = document.createTextNode(text);
			results.appendChild(txt);
		}
		results.style.display = '';
	}
} // demo

function initButton() {
	addListener(document.forms[0].elements[1], 'click', demo);
} // initButton

function observe(element, event, fx) {
	if (element.addEventListener)
		element.addEventListener(event, fx, false);
	else if (element.attachEvent)
		element.attachEvent('on' + event, fx);
} // observe

observe(window, 'load', initButton);
