// Insertion de texte au niveau du curseur.
// object = champ textarea.
// openTag = tag ouvrant d'insertion pré-sélection.
// closeTag = tag fermant d'insertion post-sélection. 
function InsertAtCaret(object, openTag, closeTag)
{
  // On récupère l'objet textarea sur lequel travailler.
  object = document.getElementById(object);
  object.focus();     // Confirmation du focus.
                
  // Variable de scrollbar conservée.
  var selScroll = object.scrollTop;
        
  // Récupération des positions de début et de fin de sélection, ainsi que la longueur du texte du textarea.
  var selStart = GetSelectionStart(object);
  var selEnd = GetSelectionEnd(object);
  var objLength = object.textLength;
        
  // Récupère le texte avant et après la sélection, et celui de la sélection.
  var textStart = object.value.substring(0,selStart);
  var textCenter = object.value.substring(selStart, selEnd);
  var textEnd = object.value.substring(selEnd,objLength);
        
  // Insertion des tags !
  object.value = textStart + openTag + textCenter + closeTag + textEnd;   
        
  // On augmente les positions de sélection.
  selStart += openTag.length;
  selEnd += openTag.length;
        
  // Positionne la sélection/curseur après restauration du focus.
  object.focus();
  SetCaretPosition(object,selStart,selEnd)     
                
  // Restaure le scroll.
  object.scrollTop = selScroll;
}
      
// Positionne le curseur/la sélection.
function SetCaretPosition(object, start, end)
{
  object.focus();
        
  // Si la fonction membre positionnant la sélection existe... (FF)
	if (object.setSelectionRange)
		object.setSelectionRange(start, end);
	else // Sinon dans le cas de IE...
  {
    var selection = object.createTextRange();
		selection.moveStart('character', start);
		selection.moveEnd('character', - object.value.length + end);
		selection.select();
	}
}
      
// Récupère la position de début de la sélection
function GetSelectionStart(object)
{
  // Si on est sous Firefox, si selectionStart existe, on retourne le début de la sélection
  if (typeof object.selectionStart != 'undefined') return object.selectionStart;
      
  // On récupère la position de début de la sélection pour IE.
  var caretStart = object.createTextRange() ;	
  caretStart.moveToBookmark(document.selection.createRange().getBookmark());
	caretStart.moveEnd('character', object.value.length);
  caretStart = object.value.length - caretStart.text.length;
          
  return caretStart;
}
        
// Récupère la position de fin de la sélection.
function GetSelectionEnd(object)
{
  // Si on est sous Firefox, si selectionEnd existe, on retourne le début de la sélection
  if (typeof object.selectionEnd != 'undefined') return object.selectionEnd;
        
  // On récupère la position de fin de la sélection (IE).
  var caretEnd = object.createTextRange() ;	
  caretEnd.moveToBookmark(document.selection.createRange().getBookmark());
	caretEnd.moveStart('character', -object.value.length);
  caretEnd = caretEnd.text.length;
        
  return caretEnd;
}
