
// Fonction pour carger une image dinamyquement et la modifier
// img = le ID de l'image : <img src="" id="">
function changerImage(img, src, maxWidth, maxHeight)
{
   var image = new Image();
 
   image.onerror = function()
   {
      alert("Erreur lors du chargement de l'image");
   }
 
   image.onabort = function()
   {
      alert("Chargement interrompu");
   }
 
   // une fois l'image chargée :
   image.onload = function()
   {
      // si l'image est désignée par son id
      if(typeof img == "string")
         img = document.getElementById(img);
 
      // si l'image doit être redimensionnée
      var reduction = 1;
      if(maxWidth && maxHeight)
         if(image.width > maxWidth || image.height > maxHeight)
            reduction = Math.max(image.width/maxWidth, image.height/maxHeight);
 
      // on affiche l'image
      img.src = image.src;
      img.width = Math.round(image.width / reduction);
      img.height = Math.round(image.height / reduction);
   }
   image.src = src;
}



// fonction BBcode
function insertTag(startTag, endTag, textareaId, tagType) 
{
    var field = document.getElementById(textareaId); 
    field.focus();
        
    if (window.ActiveXObject) 
	{ // C'est IE
        var textRange = document.selection.createRange();            
        var currentSelection = textRange.text;
                
        textRange.text = startTag + currentSelection + endTag;
                textRange.moveStart("character", -endTag.length - currentSelection.length);
                textRange.moveEnd("character", -endTag.length);
                textRange.select();     
        } else { // Ce n'est pas IE
                var startSelection   = field.value.substring(0, field.selectionStart);
                var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
                var endSelection     = field.value.substring(field.selectionEnd);
                
                field.value = startSelection + startTag + currentSelection + endTag + endSelection;
                field.focus();
                field.setSelectionRange(startSelection.length + startTag.length, startSelection.length + startTag.length + currentSelection.length);
        }       
}