function doc(id) {
  return document.getElementById(id);
}
function help(texte) {
doc('yourstyle_toolhelp').innerHTML = texte;
}
function showhide(id) {
	if (doc(id).style.display == "" || doc(id).style.display == "none") {
	doc(id).style.display = "block";
	}
	else {
	doc(id).style.display = "none";
	}
}

 function register_position(){
  
    mouse_x = 0;
  
     mouse_y = 0;
 
     document.onmousemove = position;
  
  
 }
  
  function colorperso() {
	var hexa = doc('hidden_color').value;
	doc('yourstyle_prevcolor').style.background = "#"+hexa;
	doc('yourstyle_color').style.background = "#"+hexa;
	doc('yourstyle_color').innerHTML = hexa;
}
  
 function position(evt){
  
     if(!evt) evt = window.event;    
  
     mouse_x = evt.clientX ;
  
     mouse_y = evt.clientY ;
    doc('mousex').value = mouse_x;
	doc('mousey').value = mouse_y + document.documentElement.scrollTop;
	//doc('mousey').value = document.body.scrollTop;
}

function showmenu(id) {

   
   
   var sourisx = doc('mousex').value;
   var sourisx2 = sourisx + 5;
   var sourisy = doc('mousey').value;
   var sourisy2 = sourisy + 5;
if (doc(id).style.display == "" || doc(id).style.display == "none") {
	doc(id).style.display = "block";
/**/	doc(id).style.position = "absolute";
	doc(id).style.top = sourisy+"px";
	doc(id).style.left = sourisx+"px";
} 
else {
	doc(id).style.display = "none";
}
}

function setparam(param,valeur,ouinon) {
if (ouinon == '1') {
if (doc('hidden_'+param).value == "non") {
doc('yourstyle_'+param).innerHTML = "oui";
doc('hidden_'+param).value = "oui";
}
else if (doc('hidden_'+param).value == "oui") {
doc('yourstyle_'+param).innerHTML = "non";
doc('hidden_'+param).value = "non";
}
}

else {
doc('yourstyle_'+param).innerHTML = valeur;
doc('hidden_'+param).value = valeur;
}

if (param == "color") {
doc('yourstyle_color').style.background = "#"+valeur;
doc('yourstyle_color').style.color = "#"+valeur;
}

}

function showtest() {
doc('texte').value += doc('hidden_align').value+doc('hidden_color').value+doc('hidden_bold').value+doc('hidden_ita').value+doc('hidden_underline').value+doc('hidden_font').value+doc('hidden_retrait').value;
}
function displaycolor(code) {

	doc('yourstyle_prevcolor').style.background = "#"+code;

}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// initialisation des variables
var imageTag = false;
var theSelection = false;

// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

function checkselect() {
	var txtarea = document.form1.texte;
if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text;
	if (theSelection > 0) {
	return (true);
	}
	else {
	return (false);
	}
	}
else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
{
return (true);
}
else {
return (false);
}

}

function insert(debut,fin) {

	var txtarea = document.form1.texte;
if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			// Add tags around selection
			document.selection.createRange().text = debut+ theSelection +fin;
			txtarea.focus();
			theSelection = '';
			return;
		}
		if (theSelection.length < 1) {

		doc('texte').value += debut+fin;
			insertValue(debut+fin);
			
		}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, debut, fin);
		return;
	}
	else {
					/*if (bbcode == '1') {
			doc('texte').value += debut;
			}
			else if (bbcode == '2') {
			doc('texte').value += fin;
			}
			else {*/
			//doc('texte').value += debut+fin;
				insertValue(debut+fin);
	//doc('texte').value += debut+fin;
			/*}*/
	}
}





//test balise bbcode
function insertbalise(bb,before) {
	var after = doc(bb+"_value").value;
//
if (bb == "bb4" && doc('bb7_open').value == "1" && doc('bb7').value <= doc(bb).value) {
alert('La balise d\'alignement doit se trouver avant la balise de retrait!');
return;
}

else {
if (checkselect()) {
insert(before,after);
}
	else {
	var opclo = doc(bb+"_open").value;
	var initcount = doc('countbalise').value - 1;
	var lastcount = initcount + 1;


if (opclo == "0") {
	doc(bb).value = lastcount;
	doc(bb+"_open").value = 1;
	doc('countbalise').value = lastcount + 1;
	insert(before,'');
	
}//fin opclo = 0

else if (opclo == "1") {
	var bbid = doc(bb).value;
var limit = doc('countbalise').value-1;
if (bbid < limit) {
var prevs = '';
for (j = limit;j>=bbid;j--) {
////////////////////////////////////////////////////////////////////////////////////////////////////////
for (i=1;i<=10;i++) {//i<=8 a modifier en fonction du nombre de balises
////////////////////////////////////////////////////////////////////////////////////////////////////////
if (doc('bb'+i).value == j && doc('bb'+i+'_open').value == 1 && doc('bb'+i).value != bbid) {
prevs += doc('bb'+i+"_value").value;
doc('bb'+i+'_open').value = 0;
}//fin if
}//fin for i

}//fin for j
var fin = prevs+doc(bb+"_value").value;
	insert('',fin);
doc(bb+"_open").value = 0;

}//fin <limit
	
	else if (bbid == limit) {
	
	doc(bb+"_open").value = 0;
	insert('',after);
	doc(bb).value = 0;
	doc('countbalise').value = limit;
	}

}//fin opclo = 1

}//fin else selection
}//fin else embriquement
}//fin function insertbalise

function closeall() {
var limit = doc('countbalise').value-1;
var prevs = '';
for (j = limit;j>=0;j--) {
////////////////////////////////////////////////////////////////////////////////////////////////////////
for (i=1;i<=10;i++) {//i<=8 a modifier en fonction du nombre de balises
////////////////////////////////////////////////////////////////////////////////////////////////////////
if (doc('bb'+i).value == j && doc('bb'+i+'_open').value == 1) {
prevs += doc('bb'+i+"_value").value;
doc('bb'+i+'_open').value = 0;
}//fin if
}//fin for i

}//fin for j
doc('texte').value += prevs;
}
//insert au curseur
/*function insertValue(chaineAj) {
  var myForm = document.WriteForm.text;
            //IE support
            if (document.selection) {
               myForm.focus();
               sel = document.selection.createRange();
               sel.text = chaineAj;
              document.WriteForm.focus();
           }
           //MOZILLA/NETSCAPE support
          else if (document.WriteForm.text.selectionStart || document.WriteForm.text.selectionStart == "0") {
              var startPos = document.WriteForm.text.selectionStart;
              var endPos = document.WriteForm.text.selectionEnd;
               var chaine = document.WriteForm.text.value;
    
               myForm.value = chaine.substring(0, startPos) + chaineAj + chaine.substring(endPos, chaine.length);
           } else {
               myForm.value += chaineAj;
           }
       }*/
function insertValue(chaineAj) {
	var myForm = document.form1.texte;
         //IE support
         if (document.selection) {
             myForm.focus();
             sel = document.selection.createRange();
             sel.text = chaineAj;
             document.post.focus();
         }

         //MOZILLA/NETSCAPE support
         else if (myForm.selectionStart || myForm.selectionStart == "0") {
             var startPos = myForm.selectionStart;
             var endPos = myForm.selectionEnd;
             var chaine = myForm.value;
 
             myForm.value = chaine.substring(0, startPos) + chaineAj + chaine.substring(endPos, chaine.length);
         } else {
             myForm.value += chaineAj;
         }
}


