function example1(sNode){
	var node = document.getElementById(sNode);
	var msg  = 'node.nodeName -> '+ node.nodeName + '\nnode.nodeType -> '+ node.nodeType + '\nnode.nodeValue -> '+ node.nodeValue + '\nnode.value -> '+ node.value + '\n';
	alert(msg);
}

function example1bis(sNode){
	var node = document.getElementById(sNode);
	var msg  = 'node.nodeName -> '+ node.nodeName + '\nnode.nodeType -> '+ node.nodeType + '\n';
	if(node.firstChild.nodeType == 3){
		msg += 'node.firstChild.nodeType -> '+ node.firstChild.nodeType + '\nnode.firstChild.nodeValue -> '+ node.firstChild.nodeValue + '\n';
	}
	alert(msg);
}

function example2(){
	var content = document.getElementById('billets');
	var footer  = document.getElementById('footer');
	var msg     = 'document.getElementsByTagName("p").length -> ' + document.getElementsByTagName("P").length + ' \n';
	msg    += 'content.getElementsByTagName("p").length -> '  + content.getElementsByTagName("P").length  + ' \n';
	msg    += 'footer.getElementsByTagName("p").length -> '   + footer.getElementsByTagName("P").length   + ' \n';
	alert(msg);
}

function getChildElementsByTagNameOcc(node,tag){
	var tagOccurence = 0;
	for (var i=0,el;el=node.childNodes[i];i++){
		if(el.nodeName == tag){ 
			tagOccurence++;
		}
	}
	return tagOccurence;
}

function example3(){
	var content = document.getElementById("billets");
	var msg     = "\n === UTILISATION DE getElementsByTagName === \n\n";
	msg += "document.getElementsByTagName(\"P\").length -> "  + document.getElementsByTagName("P").length + " \n";
	msg += "content.getElementsByTagName(\"P\").length -> "   + content.getElementsByTagName("P").length + " \n";
	msg += "\n ======= UTILISATION DE childNodes ======= \n\n";
	msg += "getChildElementsByTagNameOcc(document,\"P\") -> " + getChildElementsByTagNameOcc(document,"P") + "\n";
	msg += "getChildElementsByTagNameOcc(content,\"P\") -> "  + getChildElementsByTagNameOcc(content,"P") + "\n";
	alert(msg);
}

function example4(sNode){
	var node = document.getElementById(sNode);
	var msg = "node = document.getElementById(\""+ node.id + "\")\n";
	msg += "node.id -> "+ node.id + "\n";
	msg += "node.nodeName -> "+ node.nodeName + "\n";
	msg += "node.nodeType -> "+ node.nodeType + "\n";	
	msg += "node.nodeValue -> "+ node.nodeValue + "\n";	
	var tag = getRdmTag(node);
	if(tag){ 
		msg += "node.getElementsByTagName(\""+ tag +"\").length -> " + node.getElementsByTagName(tag).length + " \n";
		msg += "document.getElementsByTagName(\""+ tag +"\").length -> " + document.getElementsByTagName(tag).length + " \n";
	}
	if(node.hasChildNodes()){
		var rdmChild = Math.floor(Math.random() * node.childNodes.length);
		msg += "node.childNodes.length -> "+ node.childNodes.length +" \n";
		msg += "node.firstChild.nodeName -> "+ node.firstChild.nodeName + "\n";
		if(node.childNodes.length > 1) msg += "node.lastChild.nodeName -> "+ node.lastChild.nodeName + "\n";
		if(node.childNodes.length > 2) {
			var rdmChild = Math.floor(Math.random() * node.childNodes.length);
			msg += "node.childNodes["+ rdmChild +"].nodeName ->"+ node.childNodes[rdmChild].nodeName + "\n";
		}
	}
	msg += "node.parentNode.nodeName -> "+ node.parentNode.nodeName + "\n";
	msg += "node.parentNode.childNodes.length -> "+ node.parentNode.childNodes.length +"\n";
	if(node.previousSibling && node.previousSibling.nodeName) msg += "node.previousSibling.nodeName -> "+ node.previousSibling.nodeName +"\n";
	if(node.nextSibling && node.nextSibling.nodeName) msg += "node.nextSibling.nodeName -> "+ node.nextSibling.nodeName +"\n";
	alert(msg);			
}

function example5(){
	if(document.getElementById("node").firstChild.nodeValue.indexOf("va") > -1 ){
		document.getElementById("node").firstChild.nodeValue = "regarder le texte entre parenth\xe8ses, \"IL A SUBI\" un petit ";
	}
	else{
		document.getElementById("node").firstChild.nodeValue = "regarder le texte entre parenth\xe8ses, \"il va subir\" un petit ";
	}
}

function exemple6(){
	var el = document.getElementById('billets');
	switch( el.className ) {
		case 'example':
			alert( el.id+ 'poss\xe8de un d\'attribut class \xe9gal \xe0 '+ el.className );
			el.className = 'toto';
			alert( 'Maintenant' +el.id+ 'poss\xe8de un d\'attribut class \xe9gal \xe0 '+ el.className );
			break;
		case 'toto':
			alert( el.id+ 'poss\xe8de un d\'attribut class \xe9gal \xe0 '+ el.className );
			el.className = '';
			alert('Maintenant content n\'a plus de d\'attribut class');
			break;
		case null:
		default:
			alert( el.id+ 'ne poss\xe8de pas d\'attribut class');
			el.className = 'example';
			alert( 'Maintenant' +el.id+ 'poss\xe8de un d\'attribut class \xe9gal \xe0 '+ el.className );
			break;
	}
}

function example7(nodeId){
	alert("node.attributes.length -> "+document.getElementById(nodeId).attributes.length); 
}

function example7bis(nodeId){
	var node = document.getElementById(nodeId);
	var nodeAttributesLength = 0;
	for(var i = 0, j=node.attributes.length; i<j; i++){
		if(node.attributes[i].specified) nodeAttributesLength++;
	} 
	alert("node.attributes.length -> "+node.attributes.length +"\n nodeAttributesLength ->" +nodeAttributesLength); 
}

function example8(which){
	var elts = document.getElementsByName(which);
	var msg = ['Utilisation de getElementsByName avec l\'attribut '+which+'\n'];
	for (var i=0,el;el=elts[i];i++) {
		msg[msg.length] = '\n=========== node #'+i+' ===========\n';
		msg[msg.length] = "el.nodeName -> "+ el.nodeName + "\nel.value -> "+ el.value + "\nel.id -> "+ el.id +"\nel.checked -> "+ el.checked +"\n";
	}
	alert( msg.join(' ') );
}

function getRdmTag(node){
	if(!node.hasChildNodes()) return false
		var tagList = new Array();
	var j = 0;
	for(var i = 0; i < node.childNodes.length; i++){
		if(node.childNodes[i].nodeType == 1){
			tagList[j] =  node.childNodes[i].nodeName;
			j++;
		}
	}
	return tagList[Math.floor(Math.random() * tagList.length)];
}

function isCSS(){
	var linkList = document.getElementsByTagName("LINK"),v;
	for(var i = 0,el; el=linkList[i]; i++){
		if(el.getAttribute('rel').indexOf('stylesheet') > -1){
			v = el.getAttribute('href');
			if(v != ''){
				el.setAttribute('href','');
				el.setAttribute('tempo',v);
				document.getElementById('nocss').firstChild.nodeValue = 'avec le CSS';
			} else {
				el.setAttribute('href',el.getAttribute('tempo'));
				document.getElementById('nocss').firstChild.nodeValue = 'sans CSS';
			}		
		}
	}
}

function createTable(nbrows,nbcols){
	//1) setting tbody node
	var tbody = document.createElement("TBODY");
	for(var i = 0; i < nbrows; i++){
		var tr = document.createElement("TR");
		for (j = 0; j < nbcols; j++){
			var td = document.createElement("TD");
			var td_text = "rows "+ eval(i+1) +", cols " + eval(j+1);
			td_text = document.createTextNode(td_text);
			td.appendChild(td_text);
			tr.appendChild(td);
		}
		tbody.appendChild(tr);
	}

	//2) setting thead and tfoot
	var thead   = document.createElement("THEAD");  
	var tfoot   = document.createElement("TFOOT"); 
	var trTitle = document.createElement("TR"); 
	for (j = 0; j < nbcols; j++){
		var th = document.createElement("TH");
		th.appendChild(document.createTextNode("colonne " + j));
		trTitle.appendChild(th);
	}
	thead.appendChild(trTitle.cloneNode(true));
	tfoot.appendChild(trTitle.cloneNode(true));

	//3) setting caption
	var caption = document.createElement("CAPTION");
	caption_text = document.createTextNode('ceci est un tableau de '+ nbrows +' lignes et '+ nbcols +' colonnes publi\xe9es \xe0 l\'aide du DOM');
	caption.appendChild(caption_text);

	//4) setting table
	var table = document.createElement("TABLE"); 
	table.appendChild(caption);
	table.appendChild(thead);
	table.appendChild(tfoot);
	table.appendChild(tbody);

	//5) appending table to the document tree
	document.getElementById("billets").appendChild(table);
}

// detect the last Node of #content and delete it if it is a table
function removeTable(){
	if(document.getElementById("billets").lastChild.nodeName == "TABLE")
		document.getElementById("billets").removeChild(document.getElementById("billets").lastChild);
}

// merge the 2 previous functions to allow the presence of 
// a maximum one table at a time
function createNewTable(rows,cols){
	if(document.getElementById("billets").lastChild.id == "newTable") removeTable();
	createTable(rows,cols);
	if(document.getElementById("billets").lastChild.nodeName == "TABLE") document.getElementById("billets").lastChild.id ="newTable";
}

function image(){
	var tag  = document.getElementsByTagName("img");
	for(var i = 0,img; img=tag[i]; i++){
		imgTitle = img.getAttribute('title');
		if(!imgTitle) continue;
		var div_text = document.createTextNode(imgTitle);
		var div = document.createElement("div");
		div.style.textAlign  = "center";
		div.style.fontWeight = "bold";
		div.appendChild(div_text);
		img.parentNode.appendChild(div);
		img.parentNode.style.textAlign = "center";
	}
}

function headerContent(){
	var content = document.getElementById("billets");
	var h2List  = content.getElementsByTagName("h2");
	var j = 1;
	if(h2List.length > 1){
		for(var i = 0; i < h2List.length; i++){
			h2List.item(i).firstChild.nodeValue = j+ ". "+	h2List.item(i).firstChild.nodeValue;
			j++;
		}
	}
}

function bodyOnload() {
	image();
	headerContent();
}
window.onload = bodyOnload;
