//Function for creating non-font tags

function editor_font(text1, text2, textareaname)
{
	function countInstances(ta, open, closed)
	{
		var opening = ta.value.split(open);
		var closing = ta.value.split(closed);
		return opening.length + closing.length - 2;
	}

	var ta = document.getElementById(textareaname);
	/*var ta = document.getElementById(TAtextbox);*/
	
	if (document.selection) 
	{	
	        ta.focus();
		var sel = document.selection.createRange();
		var str = sel.text;
	/*	var ran = sel.duplicate();
		var hack;*/

		if (text2 != "")
		{
			if (str == "")
			{
				var instances = countInstances(ta, text1, text2);
				if (instances % 2 != 0)
				{ 
					str = sel.text + text2;
				}
				else
				{ 
					str = sel.text + text1;
				}
			}
			else
			{
				str = text1 + sel.text + text2;
			}
		}
		else
		{
			str = sel.text + text1;
		}		
		sel.text = str;		
	/*	hack = ta.value.replace(/\r/g, '');
		ran.moveStart("character", 0);
		ran.moveEnd("character",   str.length - hack.length);
		ran.select();
		ta.focus();*/
	}
	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;

		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();

	}
	else
	{ // Opera
		var sel = ta; // document.post.message;

		var instances = countInstances(ta,text1,text2);
		sel.value = sel.value + text1 + text2;
		/* 
		if (instances%2 != 0 && text2 != ""){ sel.value = sel.value + text2; }
		else{ sel.value = sel.value + text1; }
		*/
	} 
	return false;
}

//Function for adding smilies

function editor_image (imgtag, textareaname) {
        var taa = document.getElementsByName(textareaname);
        var ta = taa[0];

		if (ta.selectionEnd > ta.value.length) { ta.selectionEnd = ta.value.length; }

		var firstPos = ta.selectionStart;
		var secondPos = ta.selectionEnd+imgtag.length;

		ta.value=ta.value.slice(0,firstPos)+imgtag+ta.value.slice(firstPos);
		ta.value=ta.value.slice(0,secondPos)+ta.value.slice(secondPos);

		ta.selectionStart = firstPos;
                ta.selectionEnd = secondPos;

		ta.focus();
}

function editor_smilie (smilie) {
        var post = document.editform.post;
        post.value += smilie;
        post.focus();
        return;
}

//HELP 
bold_help = "Texte en gras: [b]texte[/b]";
italic_help = "Texte en italic: [i]texte[/i]";
underline_help = "Texte souligne: [u]texte[/u]";
img_help = "Ajouter une image: [img]nom de l'image[/img]";
url_help = "Ajouter un lien: [url]http://lien[/url]";
fontcolor_help = "Couleur du texte: [color=red]texte rouge[/color]";
fontsize_help = "Taille du texte: [size=small]petit texte[/size]";
email_help = "Ajouter un email:  [email]mail@serveur.fr[/email]";
center_help = "Texte Centre: [center]texte[/center]"; 

//Function for displaying help information

// Shows the help messages in the helpline window
function editor_helpline(help, taname) {
        var tname = taname+"helpbox";
        var helpbox = document.getElementById(tname);
        helpbox.value = "Aide: " + eval(help + "_help");
}

function editor_checkForm(formobj)
{
}

