function doCommand (idCommand)
{
// Vérifie qu'il s'agit d'une sélection de type texte
var oSelection;
oSelection = document.selection.createRange();

if (document.selection.type == "Control")
	{
	// - rien à faire si plus d'un contrôle sélectionné
	if (oSelection.length != 1)
		return;
	// - rien à faire si ce n'est pas une image
	if (oSelection(0).tagName != "IMG")
		return;
	// - met la sélection au niveau du contrôle
	var oImage = oSelection(0);
	oSelection = document.body.createTextRange();
	oSelection.moveToElementText(oImage);
	oSelection.select();
	}

// Vérifie que le texte sélectionné est modifiable
if (oSelection.parentElement().isContentEditable != true)
	if (idCommand != 'copy')
		return;

// Cas où insertion d'un lien
if (idCommand == 'createLink')
	{
	doCreateLink(oSelection);
	return;
	}

// Cas où insertion d'une image
if (idCommand == "insertImage")
	{
	doInsertImage(oSelection);
	return;
	}

// Exécute les autres commandes telles quelles
document.execCommand(idCommand);
}

function doInsertImage (oSelection)
{
// Configure la boite de dialogue
var sFeatures;
sFeatures = "dialogHeight:160px;dialogWidth:450px;help:no;scroll:no;status:no";

// Teste si création ou modification d'une image
var bIsImage = false;
var aArguments;
aArguments = new Array();
if (oSelection.parentElement().tagName == "IMG")
	{
	// - mémorise qu'il y a déjà une image
	bIsImage = true;
	// - récupère l'url, la légende et l'alignement de l'image
	aArguments[0] = oSelection.parentElement().src;
	aArguments[1] = oSelection.parentElement().alt;
	aArguments[2] = oSelection.parentElement().align;
	}
else
	{
	// - initialise une nouvelle image
	aArguments[0] = "";
	aArguments[1] = "";
	aArguments[2] = "";
	}

// Affiche la boite de dialogue d'insertion d'une image
var sNewImage;
sNewImage = window.showModalDialog('insertImage7.htm', aArguments, sFeatures);

// Rien à faire si clic sur le bouton [Annuler]
if (sNewImage == "~")
	return;

// Cas où pas d'image sélectionnée
if (sNewImage == "")
	{
	// => supprime éventuellement l'image en cours
	if (bIsImage == true)
		oSelection.parentElement().outerHTML = "";
	}

// Insère l'image saisie
if (sNewImage != "")
	{
	if (bIsImage == true)
		{
		// - remplace l'image en cours par la nouvelle image
		oSelection.parentElement().outerHTML = sNewImage;
		}
	else
		{
		// - insère la nouvelle image
		oSelection.pasteHTML(sNewImage);
		}
	}
}

function doCreateLink (oSelection)
{
// Configure la boite de dialogue
var sFeatures;
sFeatures = "dialogHeight:160px;dialogWidth:450px;help:no;scroll:no;status:no";

// Teste si création ou modification d'un lien
var bIsLink = false;
var sOldHtml = "";
var aArguments;
aArguments = new Array();
if (oSelection.parentElement().tagName == "A")
	{
	// La sélection correspond à un lien =>
	// - mémorise l'élément lié actuel
	bIsLink = true;
	sOldHtml = oSelection.parentElement().innerHTML;
	// - récupère l'url et la légende du lien
	aArguments[0] = oSelection.parentElement().href;
	aArguments[1] = oSelection.parentElement().title;
	}
else
	{
	// La sélection ne correspond pas à un lien
	// => teste si l'élément supérieur n'est pas un lien
	if (oSelection.parentElement().parentElement.tagName == "A")
		{
		// L'élément supérieur correspond à un lien =>
		// - mémorise l'élément lié actuel
		bIsLink = true;
		sOldHtml = oSelection.parentElement().parentElement.innerHTML;
		// - récupère l'url et la légende du lien
		aArguments[0] = oSelection.parentElement().parentElement.href;
		aArguments[1] = oSelection.parentElement().parentElement.title;
		}
	if (bIsLink == false)
		{
		// L'élément supérieur ne correspond pas à un lien =>
		// - initialise un nouveau lien
		aArguments[0] = "";
		aArguments[1] = "";
		// - sélectionne éventuellement le mot en cours
		if (oSelection.text == "")
			{
			oSelection.collapse();
			oSelection.expand("word");
			if (oSelection.text.charAt(oSelection.text.length - 1) == " ")
				oSelection.moveEnd("character", -1);
			oSelection.select();
			oSelection = document.selection.createRange();
			}
		}
	}

// Affiche la boite de dialogue de création d'un lien
var sNewLink;
sNewLink = window.showModalDialog('createLink6.htm', aArguments, sFeatures);

// Rien à faire si clic sur le bouton [Annuler] ou [X]
if (sNewLink == "~")
	return;

// Cas où pas de lien sélectionné
if (sNewLink == "")
	{
	// => supprime éventuellement le lien en cours
	if (bIsLink == true)
		document.execCommand('Unlink');
	}

// Insère le lien saisi
if (sNewLink != "")
	{
	if (bIsLink == true)
		{
		// - remplace le lien en cours par le nouveau lien
		sNewLink = sNewLink + sOldHtml + "</a>";
		if (oSelection.parentElement().tagName == "A")
			oSelection.parentElement().outerHTML = sNewLink;
		else
			oSelection.parentElement().parentElement.outerHTML = sNewLink;
		}
	else
		{
		// - insère le nouveau lien
		sNewLink = sNewLink + oSelection.htmlText + "</a>";
		oSelection.pasteHTML(sNewLink);
		}
	}
}


function CopyContent (sHtmlSource, sTextTarget)
{
// Récupère le code html de la zone de texte wysiwyg
var sHtml = document.getElementById(sHtmlSource).innerHTML;
// Transforme le code html en xhtml
var sXhtml = HtmlToXhtml(sHtml);
// Copie ce code xhtml dans un textarea pour récupération via un formulaire
document.getElementById(sTextTarget).innerText = sXhtml;
}

function HtmlToXhtml (sHtmlCode)
{
// Transformation de code html en xhtml (enfin c'est ce que ça fera plus tard)
return(sHtmlCode);
}