function bbcode(bbdebut,bbfin)
{
var input=window.document.formulaire.message;
input.focus();
if(typeof document.selection!='undefined')
{
	var range=document.selection.createRange();
	var insText=range.text;
	range.text=bbdebut+insText+bbfin;
	range=document.selection.createRange();
	if(insText.length==0)range.move('character',-bbfin.length);
	else range.moveStart('character',bbdebut.length+insText.length+bbfin.length);
	range.select()
}
else if(typeof input.selectionStart!='undefined')
{
	var start=input.selectionStart;
	var end=input.selectionEnd;
	var insText=input.value.substring(start,end);
	input.value=input.value.substr(0,start)+bbdebut+insText+bbfin+input.value.substr(end);
	var pos;
	if(insText.length==0)
	pos=start+bbdebut.length;
	else 
	pos=start+bbdebut.length+insText.length+bbfin.length;
	input.selectionStart=pos;
	input.selectionEnd=pos
}
else
{
	var pos;
	var re=new RegExp('^[0-9]{0,3}$');
	while(!re.test(pos))
	pos=prompt("insertion (0.."+input.value.length+"):","0");
	if(pos>input.value.length)
	pos=input.value.length;
	var insText=prompt("Veuillez taper le texte");
	input.value=input.value.substr(0,pos)+bbdebut+insText+bbfin+input.value.substr(pos)
}
}

function insertTag(startTag, endTag, textareaId, tagType){
	var field  = document.getElementById(textareaId); // On récupère la zone de texte
	var scroll = field.scrollTop;	// On met en mémoire la position du scroll
	field.focus(); // On remet le focus sur la zone de texte, suivant les navigateurs, on perd le focus en appelant la fonction. 
	
	/* === Partie 1 : on récupère la sélection === */
	if (window.ActiveXObject) {
		var textRange = document.selection.createRange();
		var currentSelection = textRange.text;
	}
	else {
		var startSelection   = field.value.substring(0, field.selectionStart);
		var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
		var endSelection     = field.value.substring(field.selectionEnd);
	}
	
	/* === Partie 2 : on analyse le tagType === */
	if (tagType) {
		switch (tagType) {
			case "lien":
				endTag = "[/url]";
				if (currentSelection) { // Il y a une sélection
					if (currentSelection.indexOf("http://") == 0 || currentSelection.indexOf("https://") == 0 || currentSelection.indexOf("ftp://") == 0 || currentSelection.indexOf("www.") == 0) {
						// La sélection semble être un lien. On demande alors le libellé
						var label = prompt("Quel est le libellé du lien ?") || "";
						startTag = "[url=" + currentSelection + "]";
						currentSelection = label;
					}
					else {
						// La sélection n'est pas un lien, donc c'est le libelle. On demande alors l'URL
						var URL = prompt("Quelle est l'url ?", "http://");
						startTag = "[url=" + URL + "]";
					}
				}
				else { // Pas de sélection, donc on demande l'URL et le libelle
					var URL = prompt("Quelle est l'url ?", "http://") || "";
					var label = prompt("Quel est le libellé du lien ?") || "";
					startTag = "[url=" + URL + "]";
					currentSelection = label;		     
				}
			break;
			case "citation":
				endTag = "[/citation]";
				if (currentSelection) { // Il y a une sélection
					if (currentSelection.length > 18) { // La longueur de la sélection est plus grande que 30. C'est certainement la citation, le pseudo fait rarement 20 caractères
						var auteur = prompt("Quel est l'auteur de la citation ?") || "";
						startTag = "[citation=" + auteur + "]";
					} else { // On a l'Auteur, on demande la citation
						var citation = prompt("Quelle est la citation ?") || "";
						startTag = "[citation=" + currentSelection + "]";
						currentSelection = citation;    
					}
				}
				else { // Pas de selection, donc on demande l'Auteur et la Citation
					var auteur = prompt("Quel est l'auteur de la citation ?") || "";
					var citation = prompt("Quelle est la citation ?") || "";
					startTag = "[citation=" + auteur + "]";
					currentSelection = citation;    
				}
			break;
			case "selecttitle":
				window.document.forms.formulaire.selecttitle.options[0].selected = true;
			break;
			case "selectalign":
				window.document.forms.formulaire.selectalign.options[0].selected = true;
			break;
			case "selectfloat":
				window.document.forms.formulaire.selectfloat.options[0].selected = true;
			break;
		}
	}
	
	/* === Partie 3 : on insère le tout === */
	if (window.ActiveXObject) {
		textRange.text = startTag + currentSelection + endTag;
		textRange.moveStart("character", -endTag.length - currentSelection.length);
		textRange.moveEnd("character", -endTag.length);
		textRange.select();
	}
	else {
		field.value = startSelection + startTag + currentSelection + endTag + endSelection;
		field.focus();
		field.setSelectionRange(startSelection.length + startTag.length, startSelection.length + startTag.length + currentSelection.length);
	} 

	field.scrollTop = scroll;
	
}



