// JavaScript window.opener.

function displayCouleur(id)
{	
	if (id == '')
	{ 
		document.getElementById('colFond').style.visibility = 'hidden';
		document.getElementById('colBack').style.visibility = 'hidden';
		document.getElementById('colFront').style.visibility = 'hidden';
		document.getElementById('smileys').style.visibility = 'hidden';		
	}
	else if (id == 'colFond')
	{
		if (document.getElementById('colFond').style.visibility == 'visible')
			document.getElementById('colFond').style.visibility = 'hidden';
		else
			document.getElementById('colFond').style.visibility = 'visible';
			
		document.getElementById('colBack').style.visibility = 'hidden';
		document.getElementById('colFront').style.visibility = 'hidden';
		document.getElementById('smileys').style.visibility = 'hidden';		
	}
	else if (id == 'colBack')
	{
		document.getElementById('colFond').style.visibility = 'hidden';
		
		if (document.getElementById('colBack').style.visibility == 'visible')
			document.getElementById('colBack').style.visibility = 'hidden';
		else
			document.getElementById('colBack').style.visibility = 'visible';
			
		document.getElementById('colFront').style.visibility = 'hidden';
		document.getElementById('smileys').style.visibility = 'hidden';	
	}
	else if (id == 'colFront')
	{
		document.getElementById('colFond').style.visibility = 'hidden';
		document.getElementById('colBack').style.visibility = 'hidden';
		if (document.getElementById('colFront').style.visibility == 'visible')
			document.getElementById('colFront').style.visibility = 'hidden';
		else
			document.getElementById('colFront').style.visibility = 'visible';
			
		document.getElementById('smileys').style.visibility = 'hidden';	
	}
	else if (id == 'smileys')
	{
		document.getElementById('colFond').style.visibility = 'hidden';
		document.getElementById('colBack').style.visibility = 'hidden';
		document.getElementById('colFront').style.visibility = 'hidden';
		if (document.getElementById('smileys').style.visibility == 'visible')
			document.getElementById('smileys').style.visibility = 'hidden';
		else
			document.getElementById('smileys').style.visibility = 'visible';
	}
}








// Ancien code WCODE


// Fonction pour afficher l'aide du WCode
function wcode_afficher_aide()
{
	window.open('wcode.aide.htm','_blank','toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=0, copyhistory=0, menuBar=0, width=730, height=500');
}


var est_mozilla = (navigator.userAgent.toLowerCase().indexOf('gecko')!=-1) ? true : false;
var wcode_liste_panneaux = ["smileys","taille","couleur","citation","code","liste","intro"];

function wcode_encadrer(champ, nom, valeur)
{
	displayCouleur('');
	// Si c'est Mozilla
	if (valeur=='')
		balise_ouverture = "["+nom+"]";
	else
		balise_ouverture = "["+nom+"="+valeur+"]";
	balise_fermeture = "[/"+nom+"]";

	if (est_mozilla)
	{
		champ = document.getElementById(champ);
		valeur = champ.value;
		debut = champ.selectionStart;
		fin = champ.selectionEnd;
		valeur_avant = valeur.substring(0,debut);
		valeur_dedans = valeur.substring(debut,fin);
		valeur_apres = valeur.substring(fin,champ.textLength);
		champ.value = valeur_avant+balise_ouverture+valeur_dedans+balise_fermeture+valeur_apres;
		champ.selectionStart = valeur_avant.length;
		champ.selectionEnd = (valeur_avant+balise_ouverture+valeur_dedans+balise_fermeture).length;
		champ.focus();
		x = valeur_avant.length + balise_ouverture.length;
		champ.setSelectionRange(x,x);
	}
	else
	{
		champ = document.getElementById(champ);
		selection = document.selection.createRange().text;
		if (selection.length>0)
		{
			var obj_selection = document.selection.createRange();
			obj_selection.text = balise_ouverture + selection + balise_fermeture;
			obj_selection.collapse();
			obj_selection.select();
		}
		else
		{
			champ.focus(champ.caretPos);
			champ.focus(champ.value.length);
			champ.caretPos = document.selection.createRange().duplicate();
			var bidon = "%~%";
			var valeur = champ.value;
			champ.caretPos.text = bidon;
			var i = champ.value.search(bidon);
			var valeur_avant = valeur.substr(0,i);
			var valeur_apres = valeur.substr(i,valeur.length);
			champ.value = valeur_avant + balise_ouverture + balise_fermeture + valeur_apres;
			var retours = 0;
			for (var j=0;j<valeur_avant.length;j++) if (valeur_avant.charAt(j)=="\n") retours++;
			position = i + balise_ouverture.length - retours;
			var obj = champ.createTextRange();
			obj.moveStart('character',position);
			obj.collapse();
			obj.select();
		}
	}
}

function wcode_inserer_2(champ, quoi)
{
	displayCouleur('');
        // Si c'est Mozilla
        if (est_mozilla)
        {
                champ = document.getElementById(champ);
                valeur = champ.value;
                debut = champ.selectionStart;
                fin = champ.selectionEnd;
                valeur_avant = valeur.substring(0,debut);
                valeur_apres = valeur.substring(fin,champ.textLength);
                champ.value = valeur_avant+quoi+valeur_apres;
                champ.selectionStart = (valeur_avant).length;
                champ.selectionEnd = (valeur_avant+quoi.length);
                champ.focus();
                x = valeur_avant.length + quoi.length;
                champ.setSelectionRange(x,x);
        } else {


                champ = document.getElementById(champ);
                selection = document.selection.createRange().text;



                if (selection.length>0){
                        var obj_selection = document.selection.createRange();
                        obj_selection.text = quoi;
                        obj_selection.collapse();
                        obj_selection.select();
                } else {
                        champ.focus(champ.caretPos);
                        champ.focus(champ.value.length);
                        champ.caretPos = document.selection.createRange().duplicate();
                        var bidon = "%~%";
                        var valeur = champ.value;
                        champ.caretPos.text = bidon;
                        var i = champ.value.search(bidon);
                        var valeur_avant = valeur.substr(0,i);
                        var valeur_apres = valeur.substr(i,valeur.length);
                        champ.value = valeur_avant + quoi + valeur_apres;
                        var retours = 0;
                        for (var j=0;j<valeur_avant.length;j++) if (valeur_avant.charAt(j)=="\n") retours++;
                        position = i + quoi.length - retours;
                        var r = champ.createTextRange();
                        r.moveStart('character',position);
                        r.collapse();
                        r.select();
                }


        }
}


function wcode_inserer(champ, quoi)
{
	// Si c'est Mozilla
	if (est_mozilla)
	{
		champ = window.opener.document.getElementById(champ);
		valeur = champ.value;
		debut = champ.selectionStart;
		fin = champ.selectionEnd;
		valeur_avant = valeur.substring(0,debut);
		valeur_apres = valeur.substring(fin,champ.textLength);
		champ.value = valeur_avant + quoi + valeur_apres;
		champ.selectionStart = valeur_avant.length;
		champ.selectionEnd = (valeur_avant+quoi).length;
		champ.focus();
		x = valeur_avant.length + quoi.length;
		champ.setSelectionRange(x,x);
	}
	else
	{
		champ = window.opener.document.getElementById(champ);
		selection = window.opener.document.selection.createRange().text;


		if (selection.length>0)
		{
			var obj_selection = window.opener.document.selection.createRange();
			obj_selection.text = quoi;
			obj_selection.collapse();
			obj_selection.select();
		}
		else
		{
			champ.focus(champ.caretPos);
			champ.focus(champ.value.length);
			champ.caretPos = window.opener.document.selection.createRange().duplicate();
			var bidon = "%~%";
			var valeur = champ.value;
			champ.caretPos.text = bidon;
			var i = champ.value.search(bidon);
			var valeur_avant = valeur.substr(0,i);
			var valeur_apres = valeur.substr(i,valeur.length);
			champ.value = valeur_avant + quoi + valeur_apres;
			var retours = 0;
			for (var j=0;j<valeur_avant.length;j++) if (valeur_avant.charAt(j)=="\n") retours++;
			position = i + quoi.length - retours;
			var r = champ.createTextRange();
			r.moveStart('character',position);
			r.collapse();
			r.select();
		}
	}
}
<!-- Script cr par KevBrok ;-) -->
<!-- Un pti peu adapt...        -->

<!-- Et MERCI A TOI KEVBROK !!!! -->
function wcode_div( nom, quoi ){

        if (document.getElementById && document.getElementById(nom)){
                Pdiv = document.getElementById( nom );
                PcH = true;
        } else if ( document.all && document.all[ "divID" ] ){
                Pdiv = document.all[ nom ];
                PcH = true;
        } else if ( document.layers && document.layers[ "divID" ] ){
                Pdiv = document.layers[ nom ];
                PcH = true;
        } else {
                PcH = false;
        }
        if ( PcH ){ Pdiv.className = ( quoi ) ? '' : 'cachediv'; }
}

function wcode_tout_masquer(){
        for(var i=0;i<wcode_liste_panneaux.length;i++){
                wcode_div("panneau_"+wcode_liste_panneaux[i],false);
        }
}

function wcode_panneau(nom){
        wcode_tout_masquer();
        wcode_div("panneau_"+nom,true);
}

function wcode_inserer_citation(c)
{
	wcode_cite = document.getElementById('ci_cite').value;

	if (wcode_cite == '')
		wcode_cite = 'Aucun';
	
	//wcode_cite = wcode_cite.replace(/\\/g,'\\\\');
	//wcode_cite = wcode_cite.replace(/\]/g,'\\]');
	//wcode_cite = wcode_cite.replace(/\[/g,'\\[');
	wcode_cita = document.getElementById('ci_citation').value;
	//wcode_cita = wcode_cita.replace(/\\/g,'\\\\');
	//wcode_cita = wcode_cita.replace(/\]/g,'\\]');
	//wcode_cita = wcode_cita.replace(/\[/g,'\\[');
	wcode_code = '[quote=' + wcode_cite + "]" + wcode_cita + '[/quote]';

	wcode_inserer(c, wcode_code);

	document.getElementById('ci_citation').value = '';
}

function wcode_inserer_code(c)
{
	wcode_cite = document.getElementById('co_lng').value;
	//wcode_cite = wcode_cite.replace(/\\/g,'\\\\');
	//wcode_cite = wcode_cite.replace(/\]/g,'\\]');
	//wcode_cite = wcode_cite.replace(/\[/g,'\\[');

	wcode_cita = document.getElementById('co_code').value;
	
	//wcode_cita = wcode_cita.replace(/\\/g,'\\\\');
	//wcode_cita = wcode_cita.replace(/\]/g,'\\]');
	//wcode_cita = wcode_cita.replace(/\[/g,'\\[');

	if (wcode_cite == 'General')
		wcode_code = '[code]' + wcode_cita + '[/code]';
	else
		wcode_code = '[code=' + wcode_cite + ']' + wcode_cita + '[/code]';

	wcode_inserer(c, wcode_code);

	document.getElementById('co_code').value = '';
}

function wcode_inserer_latex(c)
{
	wcode_cita = document.getElementById('co_latex').value;

	wcode_code = '[latex]' + wcode_cita + '[/latex]';
	
	wcode_inserer(c, wcode_code);

	document.getElementById('co_latex').value = '';
}


function wcode_lien(c)
{
	lien = prompt("URL :","http://");
	if (lien!=null)
		wcode_encadrer(c,'url',lien);
}

function wcode_email(c)
{
	email = prompt("Email :","");
	if (email!=null) 
		wcode_inserer_2(c,'[email='+email+']');
}
function wcode_image(c)
{
	image = prompt("Adresse de l'image :","http://");
	if (image!=null) 
		wcode_inserer_2(c, '[img]'+image+'[/img]' );
}
