function modTextareaSelection(textarea,elt,strstart,strend,oneblockelt)
{
	textarea.focus(); // moyen de faire mieux pour avoir un curseur inchangé
	origtxt = textarea.value;
	startpos = textarea.selectionStart;
	endpos = textarea.selectionEnd;
	
	txtfromstart	= origtxt.substring(0,startpos);
	txtmiddle	= origtxt.substring(startpos,endpos);
	txttoend	= origtxt.substring(endpos,origtxt.length);
	
	if ( startpos != endpos || oneblockelt )
		return txtfromstart+strstart+txtmiddle+strend+txttoend;
	else if ( elt && elt.innerHTML.length > 1 ) 
	{
		// pas de sélection, fin d'élément
		elt.innerHTML = elt.innerHTML.substring(0,1);
		return txtfromstart+strend+txttoend;
	}
	else if ( elt && elt.innerHTML.length == 1 )
	{
		// pas de sélection, début d'élément
		elt.innerHTML = elt.innerHTML+'*';
		return txtfromstart+strstart+txttoend;
	}
}

function insertImage()
{
	var xmlhttp = getHTTPObject();
	
	if ( xmlhttp && !document.getElementById("insertimg"))
	{
		url = "inc/insertimg.php";
		xmlhttp.open("GET",url,true);
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState == 4) /* 4 : état "complete" */
			if (xmlhttp.status == 200) /* 200 : code HTTP pour OK */
			if ( elt = document.getElementById('options') )
				elt.innerHTML += xmlhttp.responseText;
		}
		xmlhttp.send(null);
	}
}

function imageInserted(elt,imgpath,width,align)
{
	img = "\n"+'<img src="'+imgpath+elt.value+'" alt="'+elt.value+'" class="maxwidth" style="width: '+width+'px; float: '+align+';" />'+"\n";
	if ( insertimg = document.getElementById('insertimg') )
	insertimg.parentNode.removeChild(insertimg);
	
	if ( textarea = document.getElementById("contentedit") )
	textarea.value = modTextareaSelection(textarea,null,img,'',true)
}

/**********************************************************************************
*
*	    This file section is part of beta-libs.
*
*    beta-libs is free software; you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation; either version 2 of the License.
*
*    beta-libs is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with beta-libs; if not, write to the Free Software
*    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*
*    Copyright (c) 2006 Baptiste SIMON <baptiste.simon AT e-glop.net>
*
***********************************************************************************/
function getHTTPObject()
{
  var xmlhttp = false;

  /* Compilation conditionnelle d'IE */
  /*@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 @*/

  /* on essaie de créer l'objet si ce n'est pas déjà fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }

  return xmlhttp;
}
