<!--				

/*	Remarques quand à l'utilisation du script
		La fonction initialisation() doit impérativement être appelée après la création
		du formulaire html. Elle prend en parametre le nom du formulaire.
		
		Par ailleurs les noms de chaque bouton de balise doivent être différents, ils doivent
		impérativement commencer par "btn" et comporter	comme code javascript
		=> onClick="add_balise(this,numéro de la balise d'ouverture dans RM_tags,nom du textarea associé au bouton)"

	Dernière chose : on peut implémenter de nouvelles balises en les ajoutant simplement par paires
	dans le tableau RM_tags, avec pour conditions qu'elles souvrent avec [balise] et se ferment comme [/balise]
*/

	function initialisation(formulaire)
	{
	formu = formulaire;
	RM_tags = new Array("[b]","[/b]","[i]","[/i]","[u]","[/u]","[url]","[/url]","[img]","[/img]","[com]","[/com]");
	indics = new Array();    
	var j =0;
	var nb_buttons = 0;
	var nbelements = document.forms[formu].elements.length;
	while(j < nbelements)
		{
		var elem = document.forms[formu].elements[j];
		var finalname = elem.form.name + elem.name;
		if (elem.type == "textarea")
			{
			indics[finalname+"Stack"] = new Array();
			}
		else if(elem.type == "button" && elem.name.substr(0,3) == "btn")
			{
			indics[finalname]= new Boolean(false);
			}
		j++;
		}
	}
	
Array.prototype.recherche = function(valeur)
	{
	var i = 0; var g = this.length;
	while (i < g)
		{
		if (this[i] == valeur) break;
		else i++;
		}
		return i;
	}

function recupcode(ch)
	{
		ch = ch.substring(ch.indexOf(',')+1,ch.length-1);
		ch = ch.substring(0,ch.indexOf(','));
		return parseInt(ch);
	}

function add_balise(bou,btn,te)
	{
	var fo = bou.form.name; var nbtn = bou.name;
	var txtarea = document.forms[fo].elements[te];
	var tmpch = document.forms[fo].elements[nbtn];
	if(indics[fo+nbtn])
		{
		txtarea.value += RM_tags[btn*2];
		txtarea.focus();
		//tmpch.value += ' *';
		indics[fo+nbtn]=false;
		indics[fo+te+"Stack"].push(nbtn);
		}
	else
		{
		while (indics[fo+te+"Stack"].length > 0)
			{
			var btn_crt = indics[fo+te+"Stack"].pop();
			var code = recupcode(new String(bou.form.elements[btn_crt].onclick));
			txtarea.value = txtarea.value + RM_tags[code*2+1];
			indics[fo+btn_crt] = true;
			tmpch2 = document.forms[fo].elements[btn_crt];
			//tmpch2.value = tmpch2.value.substring(0,(tmpch2.value.length)-2);
			if(btn_crt == nbtn)
				{
				break;
				}		
			}
		}
	}

function verif()
	{
/*	var j=0;
	while(j < document.forms[formu].elements.length)
		{
			var elem = document.forms[formu].elements[j];
			if (elem.type == "textarea")
				{
				var tmpile = indics[elem.form.name + elem.name + "Stack"];
				while(tmpile.length > 0)
					{
					p = tmpile.pop();
					elem.value += RM_tags[recupcode(new String(elem.form.elements[p].onclick))*2+1];
					indics[elem.form.name+p] = true;
					elem.form.elements[p].value = elem.form.elements[p].value.substring(0,(elem.form.elements[p].value.length)-2);
					}
				elem.value = nettoyage(elem.value);
				}
			j++;
			}
*/	}


// Classe Balise
function Balise(nom,typeb,pos,taille)
	{
	this.nom = nom;						 // son nom
	this.typeb = typeb;					 // son type, ouverture(0) ou fermeture(1)
	this.pos = pos;						 // sa position dans la chaine du textarea
	this.taille = taille;				 // sa longueur
	}
	
Balise.prototype.equals = function(b)
	{
	return this.nom == b.nom && this.typeb == b.typeb;
	}

Balise.prototype.versus = function(b)
	{
	return (this.typeb == 1 && b.typeb == 0 && RM_tags[RM_tags.recherche(this.nom)-1] == b.nom)
	|| (this.typeb == 0 && (b.typeb == 1 || b.typeb == 3 )&& this.nom == RM_tags[RM_tags.recherche(b.nom)-1]);
	}

Balise.prototype.fille = function(b)
	{
	return this.nom != b.nom && this.typeb == b.typeb;
	}

function supp_balise(bal,dech,ch)
	{
	dechet += bal.taille;
	return ch.substr(0,bal.pos-dech) + ch.substring(bal.pos-dech + bal.taille,ch.length);
	}

function nettoyage(chaine)
	{
	if(chaine.length > 0)
		{
	var masque = "/(";
	var i_expreg = 0;
	var tags_taille = (RM_tags.length)/2;
	while(i_expreg < tags_taille)
		{
		masque += '\\' + RM_tags[i_expreg*2].substr(0,RM_tags[i_expreg*2].length-1) + "\\]|"
		+ '\\[\\/'+ RM_tags[i_expreg*2+1].substring(2,RM_tags[i_expreg*2+1].length-1) + "\\]|";
		i_expreg++;
		}
	masque = masque.substr(0,masque.length-1) + ")/g";
	tab = chaine.match(eval(masque));
		baltab = new Array();
		var pos_crt = 0;
		var l = 0;
		var taillet = tab.length;
			while(l < taillet)
			 {
				pos_crt = chaine.indexOf(tab[l],pos_crt);
				var typ = 0;
				if(tab[l].substring(1,2) == "/") {typ = 1;}
				baltab.push(new Balise(tab[l],typ,pos_crt,tab[l].length));
				pos_crt++;
				l++;
			 }
		var tailletab = baltab.length;
		var d = 0;
		dechet = 0;
		while(d < tailletab)
			{
			var found = false;
			var found2 = false;
			if(baltab[d].typeb == 0)
				{
				var dist = baltab[d].pos + baltab[d].taille;
				var f = d+1;
				while(f < tailletab)
					{
					if(baltab[d].versus(baltab[f]))
						{
						if(dist < baltab[f].pos)
							{
							found = true;
							baltab[f].typeb = 3;
							}
						break;
						}
					if(baltab[d].equals(baltab[f]))
						{
						baltab[f].typeb = 2;
						}
					if(baltab[d].fille(baltab[f]))
						{
						var k = f+1;
						if(k < tailletab)
							{
							var dist2 = baltab[f].pos + baltab[f].taille;
							while(k < tailletab && !baltab[d].versus(baltab[k]))
								{
								if(baltab[f].versus(baltab[k]))
									{
									if(dist2 < baltab[k].pos)
										{
										found2 = true;
										baltab[k].typeb = 3;
										}
									break;
									}
								dist2 += baltab[k].taille;
								k++;
								}
							}
						if(!found2)
							{
							baltab[f].typeb = 2;
							}
						}
					dist += baltab[f].taille;
					f++;
					}
				if(!found)
					{
					chaine = supp_balise(baltab[d],dechet,chaine);
					}
				}
			else if(baltab[d].typeb == 1 || baltab[d].typeb == 2 )
				{
				chaine = supp_balise(baltab[d],dechet,chaine);
				}
			d++;
			}
		  }
	return chaine;
	}
		  -->

