// bbcode
	// IBC Code stuff
	var text_enter_url      = "Saisissez l'URL complète pour le lien";
	var text_enter_url_pop  = "Saisissez l'URL complète pour l'image";
	var text_enter_url_name = "Saisissez le titre de la page web";
	var text_enter_url_name_pop  = "Saisissez le texte du lien";
	var text_enter_ancre    = "Saisissez le nom de votre ancre";
	var text_enter_image    = "Saisissez l'URL complète de l'image";
	var text_enter_image_class = "Choisiser le style de l'image";
	var text_enter_email    = "Saisissez l'adresse email";
	var text_enter_email_name  = "Saisissez le texte associer à l'adresse email";
	var text_code           = "Usage : [code] Votre Code Ici.. [/code]";
	var text_quote          = "Usage : [quote] Votre Citation Ici.. [/quote] ou [quote=Nom] Votre Citation Ici...[/quote]";
	var error_no_url        = "Vous devez saisir une URL";
	var error_no_name       = "Vous devez saisir un nom pour votre ancre";
	var error_no_title      = "Vous devez saisir un titre";
	var error_no_email      = "Vous devez saisir une adresse email";
	var error_no_width      = "Vous devez saisir une largeur";
	var error_no_height     = "Vous devez saisir une hauteur";
	var prompt_start        = "Saisissez le texte devant être formaté";
	var prompt_quote        = "Saisisez le nom de la personne à citer (cliquer sur annuler si vous ne voulez pas enter de nom)";
	
	var help_smilies        = "Ajouter un smilie (alt + s)";
	var help_hr             = "Ajouter une barre de séparation (alt + c)";
	var help_tpl            = "Ajouter un model de mise en forme";
	var help_hrinvi         = "Ajouter une barre de séparation invisible (eviter que les images flotantes dépassent de leur paragraphes )(alt + v)";
	var help_para           = "Nouveau paragraphe (alt + d)";
	var help_aj_lien        = "Ajouter un lien interne au site";
	var help_bold           = "Insérer du texte gras (alt + b)";
	var help_italic         = "Insérer du texte italique (alt + i)";
	var help_under          = "Insérer du texte souligné (alt + u)";
	var help_barre          = "Insérer du texte barré (alt + r)";
	var help_left           = "Aligner le Texte à gauche";
	var help_center         = "Centrer le Texte";
	var help_justify        = "Justifier le Texte";
	var help_right          = "Aligner le Texte à droite";
	var help_titre          = "Insérer un Titre ";
	var help_font           = "Insérer tags police";
	var help_size           = "Insérer tags taille police";
	var help_color          = "Insérer tags couleur police";
	var help_close          = "Fermer tous les tags ouverts";
	var help_url            = "Insérer hyperlien (alt+ h)";
	var help_ancre          = "Insérer une ancre (alt+ a)";
	var help_popup          = "Insérer un lien vers une image à ouvrir dans une popup (alt+ j)";
	var help_img            = "Image (alt + g) [img]http://www.dom.com/img.gif[/img]";
	var help_email          = "Insérer une adresse Email (alt + e)";
	var help_quote          = "Insérer une citation (alt + q)";
	var help_list           = "Créer une liste (alt + l)";
	var help_listnum        = "Créer une liste numerique(alt + n)";
	var help_code           = "Insérer du texte monotype (alt + p)";
	var help_click_close    = "Cliquer à nouveau sur le bouton pour fermer";
	var list_prompt         = "Saisissez un objet de liste. Cliquez sur 'annuler' ou laissez un blanc pour terminer la liste";
	var help_guide          = "Mode guidé...cochez cette case si vous voulez de aide dans l'utilistion des balises...";
	var help_help           = "A l'aide .... (alt + ,)";
	
var prompt_select = new Array();
prompt_select['defaut'] = ' (laisser tel quel si vous ne voulez pas changer la valeur ^^)';
prompt_select['font'] = 'chosir le nom de la police [il est recommandé de choisir l\'une des polices presente dans la liste...]';
prompt_select['color'] = 'chosir la couleur [peut etre sous forme hexadecimale : #FF0000 = red]';
prompt_select['size'] = 'chosir la taille [entre 8 et 20 pour une lisibilitée correcte]';
	
	
function get_easy_mode_state(champ)
{
	if (document.getElementById(champ+'_bbmode').checked) {
		return true;
	}else {
		return false;
	}
}

//--------------------------------------------
// Set the help bar status
//--------------------------------------------

function hstat(msg,champ)
{
	document.getElementById('helpbox_'+champ).value = eval( "help_" + msg );
}

// Set the number of tags open box

function cstat(champ)
{
	var c = stacksize(bbtags[champ]);
	
	if ( (c < 1) || (c == null) ) {
		c = 0;
	}
	
	if ( ! bbtags[champ][0] ) {
		c = 0;
	}
	document.getElementById('tagcount_'+champ).value = c;
}

//--------------------------------------------
// Get stack size
//--------------------------------------------

function stacksize(thearray)
{
	for (i = 0 ; i < thearray.length; i++ ) {
		if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') ) {
			return i;
		}
	}
	
	return thearray.length;
}

//--------------------------------------------
// Push stack
//--------------------------------------------

function pushstack(thearray, newval)
{
	arraysize = stacksize(thearray);
	thearray[arraysize] = newval;
}

//--------------------------------------------
// Pop stack
//--------------------------------------------

function popstack(thearray)
{
	arraysize = stacksize(thearray);
	theval = thearray[arraysize - 1];
	delete thearray[arraysize - 1];
	return theval;
}


//--------------------------------------------
// Close all tags
//--------------------------------------------

function closeall(champ)
{
	if (bbtags[champ][0]) {
		while (bbtags[champ][0]) {
			tagRemove = popstack(bbtags[champ])
			
			
			// Change the button status
			// Ensure we're not looking for font, size or color as these
			// buttons don't exist, they are select lists instead.
			if ( (tagRemove == 'left') || (tagRemove == 'center') || (tagRemove == 'justify') || (tagRemove == 'right'))
			{
				document.getElementById(champ+"_left").className= 'toolbar_btn';
				document.getElementById(champ+"_center").className= 'toolbar_btn';
				document.getElementById(champ+"_justify").className= 'toolbar_btn';
				document.getElementById(champ+"_right").className= 'toolbar_btn';
				eval("align_open_"+champ+"= 0");
				document.getElementById(champ).value += "[/align]";
			}else{
				document.getElementById(champ).value += "[/" + tagRemove + "]";
				if ( (tagRemove == 'h1') || (tagRemove == 'h2') || (tagRemove == 'h3') || (tagRemove == 'h4') || (tagRemove == 'h5') || (tagRemove == 'h6'))
				{
					eval("titre_open_"+champ+"= 0");
				}else{
						
					if ( (tagRemove != 'font') && (tagRemove != 'size') && (tagRemove != 'color') )
					{
						eval(tagRemove + "_open_"+champ+"= 0");
						document.getElementById(champ+"_" + tagRemove).className= 'toolbar_btn';
					}
				}
					
			}
			
		}
	}
	
	// Ensure we got them all
	document.getElementById('tagcount_'+champ).value = 0;
	bbtags[champ] = new Array();
	document.getElementById(champ).focus();
}

//--------------------------------------------
// IMAGES ET EMOTICONS
//--------------------------------------------
function tag_image(champ)
{
    var FoundErrors = '';
    var enterURL   = prompt(text_enter_image, "http://");

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert("[img]"+enterURL+"[/img]", "", false,champ);
}

function emoticon(theSmilie,champ)
{
	doInsert(" " + theSmilie + " ", "", false,champ);
}

//--------------------------------------------
// ADD CODE
//--------------------------------------------

function add_code(NewCode,champ)
{
    document.getElementById(champ).value += NewCode;
    document.getElementById(champ).focus();
}

//--------------------------------------------
// ALTER font
//--------------------------------------------

function alterfont(theval, thetag, champ)
{
    if (theval == 0)
    	return;
		if ( (thetag != 'font')){
			new_theval= prompt(prompt_select[thetag],theval);
		}else{
			new_theval=theval;
		}
	
	if(doInsert("[" + thetag + "=" + new_theval + "]", "[/" + thetag + "]", true,champ))
		pushstack(bbtags[champ], thetag);
	
   //document.getElementById(champ+'_titre').value  = "0";
    document.getElementById(champ+'_ffont').value  = "0";
    document.getElementById(champ+'_fsize').value  = "0";
    document.getElementById(champ+'_fcolor').value = "0";
    
    cstat(champ);
	
}

function altertitre(thetag,champ)
{
	var tagOpen = eval("titre_open_"+champ);
	
	if ( get_easy_mode_state(champ) )
	{
		inserttext = prompt(prompt_start + "\n[" + thetag + "]xxx[/" + thetag + "]");
		if ( (inserttext != null) && (inserttext != "") )
		{
			doInsert("[" + thetag + "]" + inserttext + "[/" + thetag + "] ", "", false,champ);
		}
	}else {
		if (tagOpen == 0)
		{
			if(doInsert("[" + thetag + "]", "[/" + thetag + "]", true,champ))
			{
				eval("titre_open_"+champ+"= 1");
				document.getElementById(champ+"_titre").value= '0';
				// Change the button status
		
				pushstack(bbtags[champ], thetag);
				cstat(champ);
				hstat('click_close',champ);
			}
		}
		else {
			// Find the last occurance of the opened tag
			lastindex = 0;
			
			for (i = 0 ; i < bbtags[champ].length; i++ )
			{
				if ( bbtags[champ][i] == 'h1' || bbtags[champ][i] == 'h2' || bbtags[champ][i] == 'h3' || bbtags[champ][i] == 'h4' || bbtags[champ][i] == 'h5' ||bbtags[champ][i] == 'h6')
				{
					lastindex = i;
				}
			}
			
			// Close all tags opened up to that tag was opened
			while (bbtags[champ][lastindex])
			{
				tagRemove = popstack(bbtags[champ]);
				doInsert("[/"+ tagRemove + "]", "", false,champ)
				
				// Change the button status
				document.getElementById(champ+"_titre").value= '0';
					eval("titre_open_"+champ+"= 0");
			}
			
			cstat(champ);
		}
	}
}

//--------------------------------------------
// SIMPLE TAGS (such as B, I U, etc)
//--------------------------------------------
function uniquetag(thetag,champ){
	doInsert(thetag, "", false,champ);
}

function simpletag(thetag,champ)
{
	//ici les alertes ^^
	var insertname ='';
	var tagOpen = eval(thetag + "_open_"+champ);
	
	if ( get_easy_mode_state(champ) )
	{
		if ( (thetag == 'quote')){
			insertname = prompt(prompt_quote);
		}
		
		if ( (insertname != null) && (insertname != "") ){
			inserttext = prompt(prompt_start + "\n[" + thetag + "="+insertname+"]xxx[/" + thetag + "]");
		}else{
			inserttext = prompt(prompt_start + "\n[" + thetag + "]xxx[/" + thetag + "]");
		}
		
		if ( (inserttext != null) && (inserttext != "") )
		{
			if ( (insertname != null) && (insertname != "") ){
				doInsert("[" + thetag + "=" + insertname +" ]" + inserttext + "[/" + thetag + "] ", "", false,champ);
			}else{
				doInsert("[" + thetag + "]" + inserttext + "[/" + thetag + "] ", "", false,champ);
			}
			
		}
	}else {
		if (tagOpen == 0)
		{
		if ( (thetag == 'quote')){
			insertname = prompt(prompt_quote);
		}
			if ( (insertname != null) && (insertname != "") ){
				var debut_tag="[" + thetag + "="+insertname+"]";
			}else{
				var debut_tag="[" + thetag + "]";
			}

		if(doInsert(debut_tag, "[/" + thetag + "]", true,champ))
			{
				eval(thetag + "_open_"+champ+"= 1");
				// Change the button status
						document.getElementById(champ+"_" + thetag).className= 'toolbar_btn_sel';
		
				pushstack(bbtags[champ], thetag);
				cstat(champ);
				hstat('click_close',champ);
			}
		}
		else {
			// Find the last occurance of the opened tag
			lastindex = 0;
			
			for (i = 0 ; i < bbtags[champ].length; i++ )
			{
				if ( bbtags[champ][i] == thetag )
				{
					lastindex = i;
				}
			}
			
			// Close all tags opened up to that tag was opened
			while (bbtags[champ][lastindex])
			{
				tagRemove = popstack(bbtags[champ]);
				doInsert("[/" + tagRemove + "]", "", false,champ)
				
				// Change the button status
				if ( (tagRemove != 'font') && (tagRemove != 'size') && (tagRemove != 'color') )
				{
					document.getElementById(champ+"_" + tagRemove).className= 'toolbar_btn';
					eval(tagRemove + "_open_"+champ+"= 0");
				}
			}
			
			cstat(champ);
		}
	}
}

//--------------------------------------------
// Alignement
//--------------------------------------------
function aligntag(thetag,champ)
{
	var tagOpen = eval("align_open_"+champ);
	
	if ( get_easy_mode_state(champ) )
	{
		inserttext = prompt(prompt_start + "\n[align=" + thetag + "]xxx[/align]");
		if ( (inserttext != null) && (inserttext != "") )
		{
			doInsert("[align=" + thetag + "]" + inserttext + "[/align] ", "", false,champ);
		}
	}else {
		if (tagOpen == 0)
		{
			if(doInsert("[align=" + thetag + "]", "[/align]", true,champ))
			{
				eval("align_open_"+champ+"= 1");
				// Change the button status
				document.getElementById(champ+"_" + thetag).className= 'toolbar_btn_sel';
		
				pushstack(bbtags[champ], thetag);
				cstat(champ);
				hstat('click_close',champ);
			}
		}
		else {
			// Find the last occurance of the opened tag
			lastindex = 0;
			
			for (i = 0 ; i < bbtags[champ].length; i++ )
			{
				if ( bbtags[champ][i] == 'align' )
				{
					lastindex = i;
				}
			}
			
			// Close all tags opened up to that tag was opened
			while (bbtags[champ][lastindex])
			{
				tagRemove = popstack(bbtags[champ]);
				doInsert("[/align]", "", false,champ)
				
				// Change the button status
				document.getElementById(champ+"_left").className= 'toolbar_btn';
				document.getElementById(champ+"_center").className= 'toolbar_btn';
				document.getElementById(champ+"_justify").className= 'toolbar_btn';
				document.getElementById(champ+"_right").className= 'toolbar_btn';
					eval("align_open_"+champ+"= 0");
			}
			
			cstat(champ);
		}
	}
}

//--------------------------------------------
// Listes
//--------------------------------------------
function tag_list(champ)
{
	var listvalue = "init";
	var thelist = "";
	
	while ( (listvalue != "") && (listvalue != null) )
	{
		listvalue = prompt(list_prompt, "");
		if ( (listvalue != "") && (listvalue != null) )
		{
			thelist = thelist+"[*]"+listvalue+"[/*]\n";
		}
	}
	
	if ( thelist != "" )
	{
		doInsert( "[list]\n" + thelist + "[/list]\n", "", false,champ);
	}
}

function tag_list_num(champ)
{
	var listvalue = "init";
	var thelist = "";
	
	while ( (listvalue != "") && (listvalue != null) )
	{
		listvalue = prompt(list_prompt, "");
		if ( (listvalue != "") && (listvalue != null) )
		{
			thelist = thelist+"[*]"+listvalue+"[/*]\n";
		}
	}
	
	if ( thelist != "" )
	{
		doInsert( "[listn]\n" + thelist + "[/listn]\n", "", false,champ);
	}
}

//--------------------------------------------
// Url et liens
//--------------------------------------------
function tag_ancre(champ)
{
    var FoundErrors = '';
    var enterName   = prompt(text_enter_ancre, "");

    if (!enterName) {
        FoundErrors += " " + error_no_name;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert("[a="+enterName+"]", "", false,champ);
}

function tag_url(champ)
{
    var FoundErrors = '';
    var enterURL   = prompt(text_enter_url, "http://");
    var enterTITLE = prompt(text_enter_url_name, "titre de la page");

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }
    if (!enterTITLE) {
        FoundErrors += " " + error_no_title;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert("[url="+enterURL+"]"+enterTITLE+"[/url]", "", false,champ);
}
function tag_popup(champ)
{
    var FoundErrors = '';
    var enterURL   = prompt(text_enter_url_pop, "http://");
    var enterTITLE = prompt(text_enter_url_name_pop, "texte du lien");

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }
    if (!enterTITLE) {
        FoundErrors += " " + error_no_title;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert("[popup="+enterURL+"]"+enterTITLE+"[/popup]", "", false,champ);
}

function tag_url_int(thetag,champ,selchamp){
	
    var FoundErrors = '';
    var enterTexte   = prompt(text_enter_lienint_name, "ici");

    if (!enterTexte) {
        FoundErrors += " " + error_no_text;
    }

    if (FoundErrors) {
		document.getElementById(selchamp).value=0;
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert(" [jinsei="+thetag+"]"+enterTexte+"[/jinsei] ", "", false,champ);

	
}


function tag_email(champ)
{
    var FoundErrors = '';
	var emailAddress = prompt(text_enter_email, "");
    var enterTitle = prompt(text_enter_email_name, "texte");

    if (!emailAddress) { 
        FoundErrors += " " + error_no_email;
	}
    if (!enterTitle) {
        FoundErrors += " " + error_no_title;
    }
   
   if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert("[email="+emailAddress+"]"+enterTitle+"[/email]", "", false , champ);
}


//--------------------------------------------
// insert ^^
//--------------------------------------------
function doInsert(ibTag, ibClsTag, isSingle ,champ)
{
	var isClose = false;
	var obj_ta = document.getElementById(champ);
	
	//----------------------------------------
	// It's IE!
	//----------------------------------------
	if ( (myVersion >= 4) && is_ie && is_win)
	{
		if (obj_ta.isTextEdit)
		{
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null)
			{
				if(ibClsTag != "" && rng.text.length > 0)
					ibTag += rng.text + ibClsTag;
				else if(isSingle)
					isClose = true;
	
				rng.text = ibTag;
			}
		}
		else
		{
			if(isSingle)
			{
				isClose = true;
			}
			
			obj_ta.value += ibTag;
		}
	}
	//----------------------------------------
	// It's MOZZY!
	//----------------------------------------
	
	else if ( obj_ta.selectionEnd )
	{ 
		var ss = obj_ta.selectionStart;
		var st = obj_ta.scrollTop;
		var es = obj_ta.selectionEnd;
		
		if (es <= 2)
		{
			es = obj_ta.textLength;
		}
		
		var start  = (obj_ta.value).substring(0, ss);
		var middle = (obj_ta.value).substring(ss, es);
		var end    = (obj_ta.value).substring(es, obj_ta.textLength);
		
		//-----------------------------------
		// text range?
		//-----------------------------------
		
		if (obj_ta.selectionEnd - obj_ta.selectionStart > 0)
		{
			middle = ibTag + middle + ibClsTag;
		}
		else
		{
			middle = ibTag + middle;
			
			if (isSingle)
			{
				isClose = true;
			}
		}
		
		obj_ta.value = start + middle + end;
		
		var cpos = ss + (middle.length);
		
		obj_ta.selectionStart = cpos;
		obj_ta.selectionEnd   = cpos;
		obj_ta.scrollTop      = st;


	}
	//----------------------------------------
	// It's CRAPPY!
	//----------------------------------------
	else
	{
		if (isSingle)
		{
			isClose = true;
		}
		
		obj_ta.value += ibTag;
	}
	
	obj_ta.focus();

	return isClose;
}	