//init variables
var isRichText = false;
var allRTEs = "";
var isIE;
var isGecko;
var isSafari;
var isKonqueror;

/**
 * pasDeEntree : Suppression de l'action "Entrée"
 * 
 * Cette fonction appelé sur un événement OnKeyDown
 * permet de ne rien faire si un utilisateur presse le bouton Entrée
 *
 * @param KeyboardEventObject keypress Evénement renvoyé au moment où on presse la touche
 *
 * @author Olivier Reyt <reyt@benchmark.fr>
 * @since 1.00 - 07/11/2007 Olivier Reyt <reyt@benchmark.fr>
 * @return Void 
 */
function pasDeEntree(keypress){
	if (keypress.keyCode == 13){
		keypress.cancelBubble = true;
		if (keypress.preventDefault) {
  			keypress.preventDefault();
		}
		keypress.returnValue = false;
	}
}

/**
 * chargeEvent : Evenement OnKeyDown
 * 
 * Cette fonction permet d'émuler l'événement OnKeyDown qui apparement
 * n'existe pas sur un Iframe
 *
 * @param String idElement Nom de l'élément sur lequel agi l'évenement
 *
 * @author Olivier Reyt <reyt@benchmark.fr>
 * @since 1.00 - 07/11/2007 Olivier Reyt <reyt@benchmark.fr>
 * @return Void 
 */
function chargeEvent(idElement){
	if (isIE) {
		var contenuIFrame = document.frames[idElement].document;
		if (contenuIFrame.attachEvent){
			contenuIFrame.attachEvent('onkeydown', pasDeEntree);
		}	
	}
	else {
		var contenuIFrame = document.getElementById(idElement).contentDocument;
		if (contenuIFrame.addEventListener){
			contenuIFrame.addEventListener('keydown', pasDeEntree, false);
		}	
	}			
}

/**
 * initRTE : Initiation du javascript
 * 
 * Cette fonction permet d'initialiser un certain nombre de données
 * et variable qui serviront dans le script, tel que le type de navigateur
 * et si le DesignMode est accessible ou pas
 *
 * @author Olivier Reyt <reyt@benchmark.fr>
 * @since 1.00 - 07/11/2007 Olivier Reyt <reyt@benchmark.fr>
 * @return Void 
 */
function initRTE() {
	
	//set browser vars
	var ua = navigator.userAgent.toLowerCase();
	isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1)); 
	isGecko = (ua.indexOf("gecko") != -1);
	isSafari = (ua.indexOf("safari") != -1);
	isKonqueror = (ua.indexOf("konqueror") != -1);
	
	//check to see if designMode mode is available
	if (document.getElementById && document.designMode && !isSafari && !isKonqueror) {
		isRichText = true;
	}
}

function writeRichText(rte, html, font, width, height, readOnly,className, noEntree) {
	if (isRichText) {
		if (allRTEs.length > 0) allRTEs += ";";
		allRTEs += rte;
		writeRTE(rte, html, font, width, height, readOnly,className, noEntree);
	} else {
		writeDefault(rte, html, font, width, height, readOnly);
	}
}

function writeDefault(rte, html, font, width, height, readOnly,className) {
	if(className == null){
		className = "";	
	}
	if (font=="Verdana") { size="10"; } else { size="13"; }
	if (!readOnly) {
		document.writeln('<textarea name="' + rte + '" class="' + className + '" id="' + rte + '" style="width: ' + width + 'px; height: ' + height + 'px;font-family:' + font + '; font-size: ' + size + 'px;">' + html + '</textarea>');
	} else {
		document.writeln('<textarea name="' + rte + '" class="' + className + '" id="' + rte + '" style="width: ' + width + 'px; height: ' + height + 'px;font-family:' + font + '; font-size: ' + size + 'px;" readonly>' + html + '</textarea>');
	}
}

function writeRTE(rte, html, font, width, height, readOnly,className, noEntree) {
	if(className == null){
		className = "";	
	}

	if(noEntree == null){
		document.writeln('<iframe id="' + rte + '" class="' + className + '" name="' + rte + '" width="' + width + 'px" height="' + height + 'px" marginwidth="3" marginheight="2" style="background-color:#FFFFFF"></iframe>');
	}
	else {
		document.writeln('<iframe id="' + rte + '" class="' + className + '" name="' + rte + '" width="' + width + 'px" height="' + height + 'px" marginwidth="3" marginheight="2" style="background-color:#FFFFFF" scrolling="no"></iframe>');
	}
	document.writeln('<input type="hidden" id="f_commentaire_' + rte + '" name="f_commentaire_' + rte + '" value="">');

	document.getElementById('f_commentaire_' + rte).value = html;

	enableDesignMode(rte, html, font, readOnly,noEntree);
}

function enableDesignMode(rte, html, font, readOnly, noEntree) {
	var frameHtml = "<html id=\"" + rte + "\">";
	frameHtml += "<head>";
	frameHtml += "<style type='text/css'>";
	if (font=="Verdana") { size="10"; } else { size="13"; }
	frameHtml += "body { font-family:"+font+";font-size:"+size+"px; }";
	frameHtml += "</style>";
	frameHtml += "</head>";
	frameHtml += "<body>";
	frameHtml += html;
	frameHtml += "</body>";
	frameHtml += "</html>";
		
	if (document.all) {
		var oRTE = document.getElementById(rte).contentWindow.document;
		//var oRTE = frames[rte].document;
		oRTE.open();
		oRTE.write(frameHtml);
		oRTE.close();
		
		if (!readOnly){
			oRTE.designMode = "On";
		}
		
		if (noEntree){
			chargeEvent(rte);
		}

	} else {
		try {
			if ((html != '') && !readOnly){
				// Gecko : html non vide, on initialise designMode avant write pour ne pas avoir de bug de curseur
				document.getElementById(rte).contentDocument.designMode = "on";
			}
			try {
				var oRTE = document.getElementById(rte).contentWindow.document;
				oRTE.open();
				oRTE.write(frameHtml);
				oRTE.close();
			} catch (e) {
				alert("Error preloading content.");
			}
			if (noEntree && !readOnly){
				chargeEvent(rte);
			}
			if ((html == '') && !readOnly){
				// Gecko : html vide, on initialise designMode après write afin d'avoir le curseur clignotant dans le champ lors d'un clic
				document.getElementById(rte).contentDocument.designMode = "on";
			}
		} catch (e) {
			//gecko may take some time to enable design mode.
			//Keep looping until able to set.
			if (isGecko) {
				setTimeout("enableDesignMode('" + rte + "', '" + html + "', " + font + ", " + readOnly + ");", 10);
			} else {
				return false;
			}
		}
	}
	if (isIE) {
		var hack = function () {rteKeyPress(document.getElementById(rte).contentWindow);};
		var oRTE = document.getElementById(rte).contentWindow;
		oRTE.document.onkeypress = hack;
	}
}

function updateRTEs() {
	var vRTEs = allRTEs.split(";");
	for (var i = 0; i < vRTEs.length; i++) {		
		updateRTE(vRTEs[i]);
	}
}

function updateRTE(rte) {
	if (!isRichText) return;	
	//set message value
	var oHdnMessage = document.getElementById('f_commentaire_' + rte);
	var oRTE = document.getElementById(rte);	
	var readOnly = false;
	if (isRichText && !readOnly) {
		if (oHdnMessage.value == null) oHdnMessage.value = "";
		oHdnMessage.value = oRTE.contentWindow.document.body.innerHTML;
		//fix for gecko
		if (escape(oHdnMessage.value) == "%3Cbr%3E%0D%0A") oHdnMessage.value = "";
	}
}

function rteKeyPress(window) {
	if (window.event.keyCode == 13) {
		var range=window.document.selection.createRange();
		window.event.returnValue = false; // cancel Standard-event
		range.pasteHTML('<br>');
		range.select(); // re-sets the cursor to the right position
	}
}
