// -----------------------------------------------
// Ouvre la carte d'identité d'un personnage/joueur passé :
// -----------------------------------------------
function infos(email) {
window.open('/membre_fiche.php?popup='+email, '_blank', 'toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=0,height=400,width=400');
}

// -----------------------------------------------
// Ouvre un popup de la carte passée :
// -----------------------------------------------
function carte(numero) {
   window.open('/popup_carte.php?carte='+numero, 'carte', 'toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=0,height=400,width=700');
}

// -----------------------------------------------
// Ouvre un popup du panel superviseur :
// -----------------------------------------------
function superviseur(sur) {
   window.open('/popup_superviseur.php?sur='+sur, 'superviseur', 'toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=0,height=450,width=700');
}

// -----------------------------------------------
// Propose de confirmer une action :
// -----------------------------------------------
function confirmer(message, url) {
   if (confirm(message)) {
      if (url) {
         window.top.location=url;
      } else {
         return true;
      }
   } else if (!url) {
      return false;
   }
}

// -----------------------------------------------
//   applicFormText()
// Applique un code BBCode à un texte.
// -----------------------------------------------
function applicFormText(text1,text2) {
   var ta=document.getElementsByName(leTextarea)[0];
   ta.focus();
   Nom = navigator.appName;
   if (Nom == 'Microsoft Internet Explorer') {
        if(typeof document.selection != 'undefined') {
            /* Insertion du code de formatage */
            var range = document.selection.createRange();
            var insText = range.text;
            range.text = text1 + insText + text2;
            /* Ajustement de la position du curseur */
            range = document.selection.createRange();
            if (insText.length == 0) {
              range.move('character', -text2.length);
            } else {
              range.moveStart('character', text1.length + insText.length + text2.length);
            }
            range.select();
         }
   } else if (Nom == 'Netscape') {
      if (ta.selectionEnd > ta.value.length) ta.selectionEnd=ta.value.length;
      var firstPos=ta.selectionStart;
      var secondPos=ta.selectionEnd+text1.length;
      var contenuScrollTop=ta.scrollTop;

      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();
      ta.scrollTop=contenuScrollTop;
   }
 }

// -----------------------------------------------
//   applicCouleur()
// Applique un code couleur BBCode à un texte.
// -----------------------------------------------
function applicCouleur(couleur) {
   //applique couleur à la liste déroulante
   document.getElementById("ListCouleur").style.background=couleur;
   //appel a la fonction
   applicFormText("[color="+couleur+"]","[/color]");
}

// -----------------------------------------------
//   insertSmiley()
// Applique un code smiley BBCode à un texte.
// -----------------------------------------------
function insertSmiley(titre) {
   //initialisation des variables
   text1=titre;
   text2=" ";
   //appel a la fonction
   applicFormText(text1,text2);
}

// -----------------------------------------------
//   BBPreview()
// Montre l'aperçu du BBCode
// -----------------------------------------------
function BBPreview() {
   previewPage="/common/bbformat/preview.php";
   lesForms = document.getElementsByTagName("form");
   if (lesForms.length==1) {
      leForm = lesForms[0];
      // On récupère les valeurs originales :
      leFormTarget = leForm.getAttribute("target");
      leFormAction = leForm.getAttribute("action");
      // On change les valeurs du formulaire :
      leForm.setAttribute("target", "apercu", false);
      leForm.setAttribute("action", previewPage, false);
      if (leForm.getAttribute("action")==leFormAction) {
         alert("Une erreur s'est produite.");
         alert(leFormAction+" -> "+leForm.getAttribute("action")+'('+leForm.target+')');
      } else {
         window.open("about:blank", "apercu", "toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=0,height=500,width=400");
         leForm.submit();
      }
      // On restaure le formulaire à son état original :
      leForm.setAttribute("target", leFormTarget, false);
      leForm.setAttribute("action", leFormAction, false);
   // Il y a plusieurs formulaires :
   } else {
      alert('Désolé, il est impossible de pouvoir\nafficher un aperçu à partir de cette page.');
   }
}

// -----------------------------------------------
//   getBBPanel()
// Affiche le panneau d'édition BBCode.
// -----------------------------------------------
function getBBPanel (textarea) {
   leTextarea=textarea;
   document.write('<div style="height: 28px; float: left;">');
      document.write('<img src="/common/bbformat/center.gif" width="26" height="28" alt="Centré" onclick="applicFormText(\'[center]\',\'[/center]\');" class="nocadre"/>');
      document.write('<img src="/common/bbformat/list.gif" width="26" height="28" alt="Liste" onclick="applicFormText(\'[list]\',\'[/list]\');" class="nocadre"/>');
      document.write('<img src="/common/bbformat/hr.gif" width="26" height="28" alt="Barre horizontale" onclick="applicFormText(\'\',\'[hr]\');" class="nocadre"/>');
      document.write('<img src="/common/bbformat/image.gif" width="26" height="28" alt="insertion url image" onclick="applicFormText(\'[img]\',\'[/img]\');" class="nocadre"/>');
      document.write('<img src="/common/bbformat/url.gif" width="26" height="28" alt="url" onclick="applicFormText(\'[url]\',\'[/url]\');" class="nocadre"/>');
      //document.write('<img src="/common/bbformat/email.gif" width="26" height="28" alt="lien email" onclick="applicFormText(\'[email]\',\'[/email]\');" class="nocadre"/>');
      document.write('<img src="/common/bbformat/preview.gif" width="26" height="28" alt="Aperçu" onclick="BBPreview();" class="nocadre"/>');
      //document.write('<img src="/common/bbformat/help.gif" width="27" height="28" alt="Aide" onclick="window.open(\'http://fr.wikipedia.org/wiki/Bbcode\');" class="nocadre" /></a>');
   document.write('</div>');
   document.write('<div style="height: 28px; float: left; margin: 0px 2px;">');
      document.write('<select id="ListCouleur" name="ListCouleur" onchange="applicCouleur(this.value);" style="width: 50px; height: 25px; border-style: outset;"> ');
      document.write('<option style="background-color: black;" value="black" selected="selected"></option>');
      document.write('<option style="background-color: blue;" value="blue"></option>');
      document.write('<option style="background-color: darkcyan;" value="darkcyan"></option>');
      document.write('<option style="background-color: dimgray;" value="dimgray"></option>');
      document.write('<option style="background-color: hotpink;" value="hotpink"></option>');
      document.write('<option style="background-color: chartreuse;" value="chartreuse"></option>');
      document.write('<option style="background-color: forestgreen;" value="forestgreen"></option>');
      document.write('<option style="background-color: sienna;" value="sienna"></option>');
      document.write('<option style="background-color: firebrick;" value="firebrick"></option>');
      document.write('<option style="background-color: yellow;" value="yellow"></option>');
      document.write('</select>');
   document.write('</div>');
   document.write('<div style="height: 28px; float: left; margin: 0px 2px;">');
      document.write('<select onchange="applicFormText(\'[size=\'+this.value+\']\',\'[/size]\');" style="height: 25px; border-style: outset;"> ');
      document.write('<option value="10">10</option>');
      document.write('<option value="13" selected="selected">13</option>');
      document.write('<option value="16">16</option>');
      document.write('<option value="18">18</option>');
      document.write('<option value="20">20</option>');
      document.write('</select>');
   document.write('</div>');
   document.write('<div style="height: 28px; float: left;">');
      document.write('<img src="/common/bbformat/bold.gif" width="27" height="28" alt="gras" onclick="applicFormText(\'[b]\',\'[/b]\');" class="nocadre"/>');
      document.write('<img src="/common/bbformat/italic.gif" width="25" height="28" alt="italic" onclick="applicFormText(\'[i]\',\'[/i]\');" class="nocadre"/>');
      document.write('<img src="/common/bbformat/underline.gif" width="26" height="28" alt="souligné" onclick="applicFormText(\'[u]\',\'[/u]\');" class="nocadre"/>');
      document.write('<img src="/common/bbformat/strike.gif" width="26" height="28" alt="texte barré" onclick="applicFormText(\'[strike]\',\'[/strike]\');" class="nocadre"/>');
   document.write('</div>');
   document.write('<div style="clear: both; height: 0px;"></div>');
   document.getElementById("ListCouleur").style.background=document.getElementById("ListCouleur").value;
   document.getElementById("ListCouleur").selectedIndex=0;
}

// -----------------------------------------------
//   getElementsByClass()
//  Retourne des éléments par leur classe
// -----------------------------------------------
function getElementsByClass(maClass) {
   var tabRetour = new Array();
   var tabTmp = new Array();
   tabTmp = document.getElementsByTagName("*");
   j=0;
   for (i=0; i<tabTmp.length; i++) {
       if (tabTmp[i].className==maClass) {
           tabRetour[j]=tabTmp[i];
          j++;
       }
   }
   return tabRetour;
}

// -----------------------------------------------
//   chatShow()
//  Retourne des éléments par leur classe
// -----------------------------------------------
function chatVoir(chatType) {
   laCheckBox = document.getElementById(chatType);
   chatElements = getElementsByClass(chatType);

   if (laCheckBox.checked==true)    leDisplay='block';
   else                             leDisplay='none';

   for (i=0; i<chatElements.length; i++) {
      chatElements[i].style.display=leDisplay;
   }
   //chatNb();
}

// -----------------------------------------------
//  getHTTPObject()
// Créé un objet HTTP pour AJAX
// -----------------------------------------------
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
        xmlhttp = false;
        }
      }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
      } catch (e) {
      xmlhttp = false;
      }
    }
  return xmlhttp;
}

function callInProgress (xmlhttp) {
   switch (xmlhttp.readyState) {
      case 1: case 2: case 3:
         return true;
         break;
      default:
         return false;
         break;
   }
}
