var mozilla = (navigator.userAgent.toLowerCase().indexOf('gecko')!=-1) ? true : false;

var nbr_bbcode_open = 0;
var bbcode_open = new Array();
//-------------------------------------------
// BBOCDE POUR LES BOUTONS B, I, U
//-------------------------------------------
function boutons_bbcodes(tag, value_id, champ, champ_tag_ouvert, text, exemple, erreur)
{
	var textarea = document.getElementById(champ);
	var texta_valu = textarea.value;
	var tag_deb = '[' + tag + ']';
	var tag_fin = '[/' + tag + ']';
	
	if (typeof document.selection != 'undefined') //Pour IE
	{
		textarea.focus();
		
		var range = document.selection.createRange();
		var selection = range.text;
		
		if (selection.length > 0) //Si on a sélectionné du texte
			range.text = tag_deb + selection + tag_fin;
		else //Sinon on a pas sélectionné de texte
		{
			if (document.formulaire.bbmode[0].checked) //Si on est en mode normal
			{
				if (document.getElementById(value_id).value == value_id) //Si on a pas encore cliqué sur le bouton
				{
					range.text = tag_deb;
					
					document.getElementById(value_id).value += "*";
					
					nbr_bbcode_open = parseInt(nbr_bbcode_open) + 1;
					document.getElementById(champ_tag_ouvert).innerHTML = nbr_bbcode_open;
					
					bbcode_open.push(tag_fin);
				} //Fin du si on a pas cliqué sur le bouton
				
				else //Sinon on a déjà cliqué sur le bouton
				{
					range.text = tag_fin;
					
					document.getElementById(value_id).value = value_id;
					
					nbr_bbcode_open = parseInt(nbr_bbcode_open) - 1;
					document.getElementById(champ_tag_ouvert).innerHTML = nbr_bbcode_open;
					
					supr_bbcode_ouvert(tag_fin, bbcode_open);
				} //Fin du si on a déjà cliqué sur le bouton
			} //Fin du mode normal
			
			else //Sinon on est en mode aide
			{
				var texte = prompt(text, exemple);
				
				if (texte)
					range.text = tag_deb + texte + tag_fin;
				else
					alert('ERROR! ' + erreur);
			} //Fin du mode aide
		}
	} //Fin du code pour IE
	
	else //pour les navigateurs plus récent
	{
		var ff_pos_selection_deb = textarea.selectionStart; //Retourne la position de départ su texte sélectionné
		var ff_pos_selection_fin = textarea.selectionEnd; //Retourne la position de fin du texte sélectionné
		var ff_size_text = textarea.value.length;
		
		var ff_string_deb = texta_valu.substring(0 , ff_pos_selection_deb);
		var ff_selection = texta_valu.substring(ff_pos_selection_deb ,ff_pos_selection_fin);
		var ff_string_fin = texta_valu.substring(ff_pos_selection_fin , ff_size_text);
		
		if (ff_selection.length > 0) //Si quelque chose est sélectionné
		{
			var ff_string_replace = ff_string_deb + tag_deb + ff_selection + tag_fin + ff_string_fin;
			
			textarea.value = ff_string_replace;
			
			var length_focus = ff_string_deb.length + tag_deb.length + ff_selection.length + tag_fin.length;
		} //Fin du si quelque chose est sélectionné
		
		else //Si rien n'est sélectionné
		{
			if (document.formulaire.bbmode[0].checked) //Si on est en mode normal
			{
				if (document.getElementById(value_id).value == value_id) //Si on a pas encore cliqué sur le bouton
				{
					textarea.value = ff_string_deb + tag_deb + ff_string_fin;
					
					document.getElementById(value_id).value += "*";
					
					nbr_bbcode_open = parseInt(nbr_bbcode_open) + 1;
					document.getElementById(champ_tag_ouvert).innerHTML = nbr_bbcode_open;
					
					bbcode_open.push(tag_fin);
					
					var length_focus = ff_string_deb.length + tag_deb.length;
				} //Fin du si on a pas encore cliqué sur le bouton
				
				else //Si on à déjà cliquer sur le bouton
				{
					document.getElementById(value_id).value = value_id;
					
					textarea.value = ff_string_deb + tag_fin + ff_string_fin;
					
					nbr_bbcode_open = parseInt(nbr_bbcode_open) - 1;
					document.getElementById(champ_tag_ouvert).innerHTML = nbr_bbcode_open;
					
					supr_bbcode_ouvert(tag_fin, bbcode_open);
					
					var length_focus = ff_string_deb.length + tag_fin.length;
				}
			} //Fin du mode normal
			
			else //Si on est en mode aide
			{
				var texte = prompt(text, exemple);
				
				if (texte)
				{
					textarea.value = ff_string_deb + tag_deb + texte + tag_fin + ff_string_fin;
					
					var length_focus = ff_string_deb.length + tag_deb.length + texte.length + tag_fin.length;
				}
				else
					alert('ERROR! ' + erreur);
			}
		} //Fin du si rien n'est sélectionné
		
		//Ces trois lignes permettent de placer le curseur dans le textarea justa après le bbcode ajouté.
		textarea.selectionStart = length_focus;
		textarea.selectionEnd = length_focus;
		textarea.focus();
	} //Fin du code pour les navigateurs plus récents
}

//-------------------------------------------
// BBOCDE POUR LES BOUTONS A DOUBLE CHOIX
//-------------------------------------------
function boutons_bbcodes_2(tag, value_id, champ, champ_tag_ouvert, text1, text2, exemple1, exemple2, erreur1, erreur2)
{
	var textarea = document.getElementById(champ);
	var texta_valu = textarea.value;
	var tag_deb = '[' + tag + ']';
	var tag_fin = '[/' + tag + ']';
	
	if (typeof document.selection != 'undefined') //Pour IE
	{
		textarea.focus();
		
		var range = document.selection.createRange();
		var selection = range.text;
		
		if (selection.length > 0) //Si on a sélectionné quelque chose
			range.text = tag_deb + selection + tag_fin;
		else //Si on a rien sélectionné
		{
			if (document.formulaire.bbmode[0].checked) //Si on est en mode normal
			{
				if (document.getElementById(value_id).value == value_id) //Si on a pas encore cliqué sur le bouton
				{
					range.text = tag_deb;
					
					document.getElementById(value_id).value += "*";
					
					nbr_bbcode_open = nbr_bbcode_open + 1;
					document.getElementById(champ_tag_ouvert).innerHTML = nbr_bbcode_open;
					
					bbcode_open.push(tag_fin);
				} //Fin du si on a pas encore cliqué sur le bouton
				
				else //Si on a déjà cliqué sur le bouton
				{
					range.text = tag_fin;
					
					document.getElementById(value_id).value = value_id;
					
					nbr_bbcode_open = parseInt(nbr_bbcode_open) - 1;
					document.getElementById(champ_tag_ouvert).innerHTML = nbr_bbcode_open;
					
					supr_bbcode_ouvert(tag_fin, bbcode_open);
				} //Fin du si on a déjà cliqué sur le bouton
			} //Fin du mode normal
			
			else //Si on est en mode aide
			{
				var input1 = prompt(text1, exemple1);
				var input2 = prompt(text2, exemple2);
				
				if (input1 && input2)
					range.text = '[' + tag + '=' + input1 + ']' + input2 + tag_fin;
				
				if (!input1)
					alert('ERROR! ' + erreur1);
				if (!input2)
					alert('ERROR! ' + erreur2);
			} //Fin du mode aide
		}
	} //Fin du code pour IE
	
	else //Pour les navigateurs plus récents
	{
		var ff_pos_selection_deb = textarea.selectionStart; //Retourne la position de départ su texte sélectionné
		var ff_pos_selection_fin = textarea.selectionEnd; //Retourne la position de fin du texte sélectionné
		var ff_size_text = textarea.value.length;
		
		var ff_string_deb = texta_valu.substring(0 , ff_pos_selection_deb);
		var ff_selection = texta_valu.substring(ff_pos_selection_deb ,ff_pos_selection_fin);
		var ff_string_fin = texta_valu.substring(ff_pos_selection_fin , ff_size_text);
		
		if (ff_selection.length > 0) //Si quelque chose est sélectionné
		{
			var ff_string_replace = ff_string_deb + tag_deb + ff_selection + tag_fin + ff_string_fin;
			
			textarea.value = ff_string_replace;
			
			var length_focus = ff_string_deb.length + tag_deb.length + ff_selection.length + tag_fin.length;
		} //Fin du si quelque chose est sélectionné
		
		else //Si rien n'est sélectionné
		{
			if (document.formulaire.bbmode[0].checked) //Si on est en mode normal
			{
				if (document.getElementById(value_id).value == value_id) //Si on a pas encore cliqué sur le bouton
				{
					textarea.value = ff_string_deb + tag_deb + ff_string_fin;
					
					document.getElementById(value_id).value += "*";
					
					nbr_bbcode_open = nbr_bbcode_open + 1;
					document.getElementById(champ_tag_ouvert).innerHTML = nbr_bbcode_open;
					
					bbcode_open.push(tag_fin);
					
					var length_focus = ff_string_deb.length + tag_deb.length;
				} //Fin du si on a pas encore cliqué sur le bouton
				
				else //Si on a déjà cliqué sur le bouton
				{
					document.getElementById(value_id).value = value_id;
					
					textarea.value = ff_string_deb + tag_fin + ff_string_fin;
					
					nbr_bbcode_open = parseInt(nbr_bbcode_open) - 1;
					document.getElementById(champ_tag_ouvert).innerHTML = nbr_bbcode_open;
					
					supr_bbcode_ouvert(tag_fin, bbcode_open);
					
					var length_focus = ff_string_deb.length + tag_fin.length;
				} //Fin du si on a déjà cliqué sur le bouton
			} //Fin du mode normal
			
			else //Si on est en mode aide
			{
				var input1 = prompt(text1, exemple1);
				var input2 = prompt(text2, exemple2);
				
				if (input1 && input2)
				{
					textarea.value = ff_string_deb + '[' + tag + '=' + input1 + ']' + input2 + tag_fin + ff_string_fin;
					
					var length_focus = ff_string_deb.length + tag_deb.length + input1.length + input2.length + tag_fin.length; //Le 5 correspond aux: [, =, ], [, /, ]
				}
				
				if (!input1)
					alert('ERROR! ' + erreur1);
				if (!input2)
					alert('ERROR! ' + erreur2);
			} //Fin du mode aide
		} //Fin du si rien n'est sélectionné
		
		//Ces trois lignes permettent de placer le curseur dans le textarea justa après le bbcode ajouté.
		textarea.selectionStart = length_focus;
		textarea.selectionEnd = length_focus;
		textarea.focus();
	} //Fin du code pour les navigateurs plus récents
}

//-------------------------------------------
// BBOCDE POUR LES LISTES
//-------------------------------------------
function listes_bbcodes(tag, champ, champ_tag_ouvert, text, exemple, erreur)
{
	var textarea = document.getElementById(champ);
	var texta_valu = textarea.value;
	var valeur = document.getElementById(tag).value;
	var tag_deb = '[' + tag + '=' + valeur + ']';
	var tag_fin = '[/' + tag + ']';
	
	if (typeof document.selection != 'undefined') //Pour IE
	{
		textarea.focus();
		
		var range = document.selection.createRange();
		var selection = range.text;
		
		if (selection.length > 0) //Si on a sélectionné du texte
			range.text = tag_deb + selection + tag_fin;
		
		else //Si on a pas sélectionné de texte
		{
			if (document.formulaire.bbmode[0].checked) //Si on est en mode normal
			{
				range.text = tag_deb;
				
				nbr_bbcode_open = parseInt(nbr_bbcode_open) + 1;
				document.getElementById(champ_tag_ouvert).innerHTML = nbr_bbcode_open;
				
				bbcode_open.push(tag_fin);
			} //Fin du mode normal
			
			else //Si on est en mode aide
			{
				var val_aff = valeur;
				
				/**************** POUR L'ALIGNEMENT ****************/
					if (valeur == 'left') { val_aff = 'mettre à gauche' }
					if (valeur == 'center') { val_aff = 'centrer' }
					if (valeur == 'right') { val_aff = 'mettre à droite' }
					if (valeur == 'justify') { val_aff = 'justifier' }
				/***************** FIN DE L'ALIGNEMENT ****************/
				
				/**************** POUR LES COULEURS ****************/
					if (valeur == 'red') { val_aff = 'rouge' }
					if (valeur == 'green') { val_aff = 'vert' }
					if (valeur == 'yellow') { val_aff = 'jaune' }
					if (valeur == 'blue') { val_aff = 'bleu' }
					if (valeur == 'purple') { val_aff = 'violet' }
					if (valeur == 'olive') { val_aff = 'olive' }
				/***************** FIN DES COULEURS ****************/
				
				/**************** POUR LES TAILLES ****************/
					if (tag == 'size') { val_aff = valeur + 'px' }
				/***************** FIN DES TAILLES ****************/
		
				var texte = prompt(text + ' ' + val_aff, exemple);
				
				if (texte)
					range.text = tag_deb + texte + tag_fin;
				else
					alert('ERROR! '+ erreur);
			} //FIn du mode aide
		} //Fin du si on a pas sélectionné de texte
	} //Fin du code pour IE
	
	else //Pour les navigateurs plus récents
	{
		var ff_pos_selection_deb = textarea.selectionStart; //Retourne la position de départ su texte sélectionné
		var ff_pos_selection_fin = textarea.selectionEnd; //Retourne la position de fin du texte sélectionné
		var ff_size_text = textarea.value.length;
		
		var ff_string_deb = texta_valu.substring(0 , ff_pos_selection_deb);
		var ff_selection = texta_valu.substring(ff_pos_selection_deb ,ff_pos_selection_fin);
		var ff_string_fin = texta_valu.substring(ff_pos_selection_fin , ff_size_text);
		
		if (ff_selection.length > 0) //Si on a sélectionné du texte
		{
			
			textarea.value = ff_string_deb + tag_deb + ff_selection + tag_fin + ff_string_fin;
			
			var length_focus = ff_string_deb.length + tag_deb.length + ff_selection.length + tag_fin.length;
		} //FIn du si on à sélectionné du texte
		
		else //Si on a rien sélectionné
		{
			if (document.formulaire.bbmode[0].checked) //Si on est en mode normal
			{
					textarea.value = ff_string_deb + tag_deb + ff_string_fin;
					
					nbr_bbcode_open = parseInt(nbr_bbcode_open) + 1;
					document.getElementById(champ_tag_ouvert).innerHTML = nbr_bbcode_open;
					
					bbcode_open.push(tag_fin);
					
					var length_focus = ff_string_deb.length + tag_deb.length;
			} //Fin du mode normal
			
			else //Si on est en mode aide
			{
				var val_aff = valeur;
				
				/**************** POUR L'ALIGNEMENT ****************/
					if (valeur == 'left') { val_aff = 'mettre à gauche' }
					if (valeur == 'center') { val_aff = 'centrer' }
					if (valeur == 'right') { val_aff = 'mettre à droite' }
					if (valeur == 'justify') { val_aff = 'justifier' }
				/***************** FIN DE L'ALIGNEMENT ****************/
				
				/**************** POUR LES COULEURS ****************/
					if (valeur == 'red') { val_aff = 'rouge' }
					if (valeur == 'green') { val_aff = 'vert' }
					if (valeur == 'yellow') { val_aff = 'jaune' }
					if (valeur == 'blue') { val_aff = 'bleu' }
					if (valeur == 'purple') { val_aff = 'violet' }
					if (valeur == 'olive') { val_aff = 'olive' }
				/***************** FIN DES COULEURS ****************/
				
				/**************** POUR LES TAILLES ****************/
					if (tag == 'size') { val_aff = valeur + 'px' }
				/***************** FIN DES TAILLES ****************/
		
				var texte = prompt(text + ' ' + val_aff, exemple);
				
				if (texte)
				{
					textarea.value = ff_string_deb + tag_deb + texte + tag_fin + ff_string_fin;
					
					var length_focus = ff_string_deb.length + tag_deb.length + texte.length + tag_fin.length;
				}
				
				else
					alert('ERROR! '+ erreur);
			} //FIn du mode aide
		} //Fin du si on a rien sélectionné
		
		//Ces trois lignes sont pour repositionner le focus à la suite du bbcode entrez:
		textarea.selectionStart = length_focus;
		textarea.selectionEnd = length_focus;
		textarea.focus();
	} //Fin du code pour les navigateurs plus récents
}

//-------------------------------------------
// BBOCDE POUR LES SMILEYS
//-------------------------------------------
function smiley(smiley, champ)
{
	var textarea = document.getElementById(champ);
	var texta_valu = textarea.value;
	
	if (typeof document.selection != 'undefined') //Pour IE
	{
		textarea.focus();
		
		var range = document.selection.createRange();
		var selection = range.text;
		
		if (selection.length > 0) //Si on a sélectionné du texte
			range.text = smiley + selection + smiley;
		else //Si on a pas sélectionné de texte
			range.text = smiley;
	} //Fin du code pour IE
	
	else //Si on utilise un navigateur plus récent
	{
		var ff_pos_selection_deb = textarea.selectionStart; //Retourne la position de départ su texte sélectionné
		var ff_pos_selection_fin = textarea.selectionEnd; //Retourne la position de fin du texte sélectionné
		var ff_size_text = textarea.value.length;
		
		var ff_string_deb = texta_valu.substring(0 , ff_pos_selection_deb);
		var ff_selection = texta_valu.substring(ff_pos_selection_deb ,ff_pos_selection_fin);
		var ff_string_fin = texta_valu.substring(ff_pos_selection_fin , ff_size_text);


		
		if (ff_selection.length > 0) //Si on a sélectionné du texte
			textarea.value = ff_string_deb + smiley + ff_selection + smiley + ff_string_fin;
		else //Si on a rien sélectionné
			textarea.value = ff_string_deb + smiley + ff_selection + ff_string_fin;
		
		var length_focus = ff_string_deb.length + smiley.length
		
		//Ces trois lignes sont pour repositionner le focus à la suite du bbcode entrez:
		textarea.selectionStart = length_focus;
		textarea.selectionEnd = length_focus;
		textarea.focus();
	}//Fin de si on utilise un navigateur type mozilla
}

//-------------------------------------------
// BBOCDE POUR L'AFFICHAGE DES INFOS
//-------------------------------------------
function aff_expl(expl, div)
{
	document.getElementById(div).innerHTML = '<span style="font-style: italic;">' + expl + '</span>';
}

function mask_expl(div)
{
	document.getElementById(div).innerHTML = '&nbsp;';
}

//-------------------------------------------
// Fonction supr_bbcode_ouvert
//-------------------------------------------
function supr_bbcode_ouvert(bbcode_supr, array)
{
	var nbr_ligne = array.length;
	
	for (f=0; f <	 nbr_ligne; ++f)
	{
		if (array[f] == bbcode_supr)
		{
			array.splice(f, 1, '');
		}
	}
}

//-------------------------------------------
// Fonction vide_array
//-------------------------------------------
function vide_array(array)
{
	var nbr_ligne = array.length;
	
	for (i=0; i <= nbr_ligne; ++i)
	{
		array.splice(i, i, '');
	}
	
	//return array;
}

//-------------------------------------------
// Fonction fermer_bbcode_ouvert
//-------------------------------------------
function fermer_bbcode_ouvert(champ, champ_tag_ouvert, value_id1, value_id2, value_id3, value_id4, value_id5, value_id6, value_id7, value_id8, value_id9, value_id10)
{
	bbcode_open.reverse();
	var bbcode_fermant = bbcode_open.join('');
	
	document.getElementById(champ).value += bbcode_fermant;
	document.getElementById(champ).focus();
	
	nbr_bbcode_open = 0; //On remet le comtpteur de BBcode ouvert à 0
	document.getElementById(champ_tag_ouvert).innerHTML = nbr_bbcode_open;
	
	vide_array(bbcode_open); //On vide l'array contenant les BBcode à fermer
	
	//On va maintenant réinitialiser tout les bouton:
	for (i=1; i < 11; ++i)
	{
		if (document.getElementById(eval('value_id' + [i])).value != eval('value_id' + [i]))
		{
			document.getElementById(eval('value_id' + [i])).value = eval('value_id' + [i]);
		}
	}
}