var navigateur = navigator.userAgent.toLowerCase();
var is_ie = ((navigateur.indexOf("msie") != -1) && (navigateur.indexOf("opera") == -1));
var is_moz = ((navigateur.indexOf('mozilla')!=-1) && (navigateur.indexOf("safari") == -1));
var tag_is_open=new Array();
var bbcode=new Array('false','[b]','[/b]','false','[i]','[/i]','false','[u]','[/u]','false','[strike]','[/strike]','false','[font=]','[/font]','false','[size=]','[/size]','false','[color=]','[/color]','false','[align=]','[/align]','false','[quote]','[/quote]','false','[img]','[/img]','false','[url]','[/url]','false','[email]','[/email]');

function loadpos(textaera)
{	
	if (textaera.createTextRange) textaera.caretPos = document.selection.createRange().duplicate();
}

function addsmile(text) {
	addcode(text, 1);
}

function addcode(status, smiley)
{
	if (smiley==1) {
		bbcode[97]=false;
		bbcode[98]="";
		bbcode[99]=status;
		tag_is_open[97]=1;		
		status=97;
	}
	var textselect=document.form1.Message;
        if (is_ie) {
		if(textselect.caretPos && textselect.caretPos.text.length>0) {
			textselect.caretPos.text=bbcode[status+1]+textselect.caretPos.text+bbcode[status+2];
		} else {
			if (textselect.caretPos) {
				if (tag_is_open[status]==1) {
					textselect.caretPos.text+=bbcode[status+2];	
					tag_is_open[status]=0;
				} else {
					textselect.caretPos.text+=bbcode[status+1];	
					tag_is_open[status]=1;
				}
			} else {
				textselect.value+="";
			}
		}
	} else if(is_moz) {
		var selLength = textselect.textLength;
		var selStart = textselect.selectionStart;
		var selEnd = textselect.selectionEnd;
		if (selEnd == 1 || selEnd == 2)	selEnd = selLength;
		var s1 = (textselect.value).substring(0,selStart);
		var s2 = (textselect.value).substring(selStart, selEnd)
		var s3 = (textselect.value).substring(selEnd, selLength);
		if (textselect.selectionEnd && (textselect.selectionEnd - textselect.selectionStart > 0)) {
			textselect.value = s1 + bbcode[status+1] + s2 + bbcode[status+2] + s3;
			textselect.selectionStart = s1.length;
			textselect.selectionEnd = textselect.textLength - s3.length;
		} else {
			if (tag_is_open[status]==1) {
				textselect.value = s1 + bbcode[status+2] + s3;
				tag_is_open[status]=0;
			} else {
				textselect.value = s1 + bbcode[status+1] + s3;
				tag_is_open[status]=1;
			}
			textselect.selectionStart = textselect.textLength - s3.length;
			textselect.selectionEnd = textselect.textLength - s3.length;
		}
	} else {
		if (tag_is_open[status]==1) {
			textselect.value+=bbcode[status+2];	
			tag_is_open[status]=0;
		} else {
			textselect.value+=bbcode[status+1];	
			tag_is_open[status]=1;
		}		
	}
	textselect.focus();	
}

function previsu()
{
	textselectvalue=document.form1.Message.value;
	this.open("aide.php?aide=1541&ok=1", "aide1541", "toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=0, copyhistory=0, menuBar=0, width=650, height=400");	
}