/* magiccorp_editpost.js                              */
/* ================================================== */
/* Insertion de BBCodes et de Smilleys                */
/* ================================================== */
/* Auteurs : Sliver (sliver@magiccorporation.com)     */
/*           Amon-Ra (amonra@magiccorporation.com)    */
/* Copyright Avril 2004 Magic Corporation             */
/* ================================================== */
/* Dernière Mise à Jour le 28 Avril 2004              */
/* ================================================== */

function x () // déplacement de la fonction X de javascript.php vers magiccorp_editpost.js
{
 return;
}

function countInstances(open,closed)
{
   var opening = document.coolsus.contenu.value.split(open);
   var closing = document.coolsus.contenu.value.split(closed);
   return opening.length + closing.length - 2;
}

function MagicCorpInsertCode(text1,text2)
{
   var ta = document.getElementById('contenu');

   if (document.selection) {
      var str = document.selection.createRange().text;
      ta.focus();
      var sel = document.selection.createRange();
      if (text2!="")
      {
         if (str=="")
         {
            var instances = countInstances(text1,text2);
            if (instances%2 != 0){ sel.text = sel.text + text2;}
            else{ sel.text = sel.text + text1;}
         }
         else
         {
            sel.text = text1 + sel.text + text2;
         }
      }
      else
      {
         sel.text = sel.text + text1;
      }

   }
   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 = document.coolsus.contenu;

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