// AddLoadHandler
// Ajoute une fonction au chargement de la page.
function addLoadHandler(handler) {

	if(window.addEventListener) {
		window.addEventListener("load",handler,false);
	} else if(window.attachEvent) {
		window.attachEvent("onload",handler);
	} else if(window.onload) {
		var oldHandler = window.onload;
		window.onload = function piggyback() {
			oldHandler();
			handler();
		};
	} else {
		window.onload = handler;
	}
}

// GetChildsByTagName
// Retourne un tableau des enfants direct de  DOMobj qui sont des <tag>
function getChildsByTagName(DOMobj, tag) {

	var liste=DOMobj.getElementsByTagName(tag);
	var newListe=new Array();
	
	if(liste!=null) {
		for(var i=0; i<liste.length; i++) {
			var NoeudParent=liste[i].parentNode;			
			if(NoeudParent==DOMobj) {
				newListe.push(liste[i]);
			}
		}
	liste=newListe;
	}
	return liste;
}

// Affiche
// Affiche/Masque un élément
function affiche(ObjId) {
	var div=document.getElementById(ObjId);
	div.style.display=="block" ? div.style.display="none" : div.style.display="block";
}

// SetDisaply
// Défini le display d'un élément
function setDisplay(ObjId, p_display) {
	document.getElementById(ObjId).style.display=p_display;
}

// Rw_link
// Fonction de réécriture des liens.
function rw_link(selection, lien) {
	// Récupération de l'id à insérer, de l'adresse à modifier
	var id=document.getElementById(selection).options[document.getElementById(selection).selectedIndex].value;
	var adresse=document.getElementById(lien).href;
	// et des indices des dernier et avant-dernier ";" de l'adresse.
	var indice_fin=adresse.lastIndexOf(";");
	var temp=adresse.substring(0, indice_fin);
	var indice_deb=temp.lastIndexOf(";");
	
	// Réécriture de l'adresse du lien.
	adresse = adresse.substring(0, indice_deb+1)+id+adresse.substring(indice_fin);	
	document.getElementById(lien).href=adresse;
}

// AddPicto
// Ajoute un ensemble pour la définition d'un pictogramme au formulaire des gestion produit.
function addPicto() {
	// Isolation du conteneur.
	var pictogrammes=document.getElementById('liste_pictogramme');
	
	// isolation de la liste de pictogrammes
	var une_entree=getChildsByTagName(pictogrammes, 'dt')[0];
	var liste_picto=getChildsByTagName(une_entree, 'select')[0];
	
	// Duplication de la liste des pictogrammes
	var newListe=document.createElement('select');
		newListe.setAttribute('name', liste_picto.name);
	for(var i=0; i<liste_picto.options.length; i++) {
		var opt=new Option(liste_picto.options[i].text, liste_picto.options[i].value);
		newListe.options[i]=opt;
	}
	
	// DT
	var dt=document.createElement('dt');
		dt.appendChild(newListe);
		
	// DD
	var ul=document.createElement('ul');
	
	var label=document.createElement('label');
		label.innerHTML='Position';
	
	var newListe=document.createElement('select');
		newListe.setAttribute('name', 'position[]');
	for(var i=0; i<10; i++) {
		var opt=new Option(i, i);
		newListe.options[i]=opt;
	}
	
	var li=document.createElement('li');
		li.appendChild(label);
		li.appendChild(newListe);
		
		ul.appendChild(li);
	
	for(var i=0; i<listeLangue["id"].length; i++) {
		var input=document.createElement('input');
			input.setAttribute('type', 'text');
			input.setAttribute('name', 'commentaire_'+listeLangue['id'][i]+'[]');
			
		var label=document.createElement('label');
			label.innerHTML=listeLangue['nom'][i];
			
		var li=document.createElement('li');
			li.appendChild(label);
			li.appendChild(input);
			
			ul.appendChild(li);
	}
	var dd=document.createElement('dd');
		dd.appendChild(ul);
	
	// Constitution d'une nouvelle dl
	var dl=document.createElement('dl');
		dl.appendChild(dt);
		dl.appendChild(dd);
		
	var hr=document.createElement('hr');
	
		pictogrammes.appendChild(dl);
		pictogrammes.appendChild(hr);
}


// AddProposition
// Ajoute un ensemble pour la définition d'une proposition au formulaire de gestion des questions.
function addProposition() {
	// Isolation du conteneur.
	var propositions=document.getElementById('liste_proposition');
	
	var li=document.createElement('li');
	
	var texte=document.createTextNode('Réponse correcte');
	
	// radio
	var rad=document.createElement('input');
		rad.setAttribute('type', 'radio');
		rad.setAttribute('name', 'indice_reponse');
		rad.className='check-radio';
		rad.setAttribute('value', ++NbProposition);
			
		li.appendChild(rad);
		li.appendChild(texte);
		li.appendChild(document.createElement('br'));
		
	// input
	for(var i=0; i<listeLangue["id"].length; i++) {
		var input=document.createElement('input');
			input.setAttribute('type', 'text');
			input.setAttribute('name', 'proposition_'+listeLangue['id'][i]+'[]');
			
		var label=document.createElement('label');
			label.innerHTML=listeLangue['nom'][i];
			
		var sep=document.createElement('br');
			
		li.appendChild(label);
		li.appendChild(input);
		li.appendChild(sep);
	}
	
	var hr=document.createElement('hr');
	
		li.appendChild(hr);
		propositions.appendChild(li);
		
		NbProposition++;
}

// HaveaBreak
// Effectue une pause durant p_time milisecondes.
function haveaBreak(p_time) {
	var date = new Date();
	var curDate = null;
	
	do {
		curDate = new Date();
	} while(curDate-date < p_time);
}