var regex_url	=	new RegExp("^(ht|f)tp://(.*)\.(php|html|htm|jpg|jpeg|/|gif|png|com|fr|net|org)", 'gi');

function bbcode_here(ta_id)
{
	document.writeln('<p class="bb_boutons">');
	button_bb(ta_id, 'b', true, 'gras');
	button_bb(ta_id, 'i', true , 'italique');
	button_bb(ta_id, 'u', true , 'souligné');
	if (ta_id != 'signature') button_bb(ta_id, 'img', true , 'image');
	
	button_bb(ta_id, 'quote', false , 'citation');
	button_bb(ta_id, 'url', false, 'lien');
	document.writeln('</p>');
}

function emoticon(ta_id, text) 
{
TAinsert(ta_id, text, '');
}

function button_bb(ta_id, tag, simple, texte)
{	
	var onclick_fct	=	"";
	if( tag	==	'url')
		onclick_ftc	=	"bb_url('" + ta_id + "')";
	else if ( tag == 'img')
		onclick_ftc	=	"bb_image('" + ta_id + "')";
	else if( simple)
		onclick_ftc	=	"simple_tag('" + ta_id + "', '" + tag + "')";
	else
		onclick_ftc	=	"param_tag('" + ta_id	+	"', '" + tag	+	"')";
	
	var id	=	ta_id	+	'__'	+	tag;
	
	if( !texte )
		texte	=	tag;
	
	document.writeln('<input type="button" value="' + texte + '" class="bb_' + tag + '" name="' +id + '" id="' + id + '" onclick="' + onclick_ftc + '"/>');
	
}

function simple_tag(ta_id, tag)
{
	TAinsert(ta_id, '[' + tag + ']', '[/' + tag + ']');
}

function param_tag(ta_id, tag)
{
	TAinsert(ta_id, '[' + tag + '=]', '[/' + tag + ']');
}

function bb_image(ta_id)
{
	var url	=	prompt("Indiquez l'adresse internet de votre image" , 'http://');
	if( !url || !regex_url.test(url))
		return alert("Cette adresse est invalide !");
		
	TAinsert(ta_id, '[img]' + url + '[/img]', ''); 
}

function bb_url(ta_id)
{
	var url	=	prompt("Indiquez l'adresse" , 'http://');
	if( !url || !regex_url.test(url))
		return alert("Cette adresse est invalide !");
	
	var texte	=	prompt("Indiquez l'intitulé du lien", "");
	
	if( texte.length	==	0)
		TAinsert(ta_id, "[url]" + url + "[/url]");
	else
		TAinsert(ta_id,"[url=" + url + "]" + texte + "[/url]", '');
	
}

function TAinsert(ta_id, text1,text2) {
	var ta = document.getElementById(ta_id);
	if (document.selection) {
		var str = document.selection.createRange().text;
		ta.focus();
		var sel = document.selection.createRange();
		if (text2!="") {
			if (str=="") {
				var instances = countInstances(text1,text2);
				if (instances%2 != 0) sel.text = sel.text + text2;
				else sel.text = sel.text + text1;
			} else sel.text = text1 + sel.text + text2;
		} else sel.text = sel.text + text1;
	} else if (ta.selectionStart || ta.selectionStart == 0) {
		if (ta.selectionEnd > ta.value.length) ta.selectionEnd = ta.value.length;
		var firstPos = ta.selectionStart;
		var secondPos = ta.selectionEnd+text1.length;
		var contenuScrollTop = ta.scrollTop;
		
		ta.value=ta.value.slice(0,firstPos)+text1+ta.value.slice(firstPos);
		ta.value=ta.value.slice(0,secondPos)+text2+ta.value.slice(secondPos);
		
		ta.selectionStart = firstPos+text1.length;
		ta.selectionEnd = secondPos;
		ta.focus();
		ta.scrollTop = contenuScrollTop;
	} else if ( document.hop.contenu ) { // Opera
		var sel = document.hop.contenu;
		var instances = countInstances(text1,text2);
		if (instances%2 != 0 && text2 != "") sel.value = sel.value + text2;
		else sel.value = text1 + sel.value + text2;
	}
	else
	{
	ta.value	+=	text1;
	ta.value	+=	text2;
	}
}