//init variables
var isRichText = false;
var allRTEs = "";
var isIE;
var isGecko;
var isSafari;
var isKonqueror;


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) {
	if (isRichText) {
		if (allRTEs.length > 0) allRTEs += ";";
		allRTEs += rte;
		writeRTE(rte, html, font, width, height, readOnly);
	} else {
		writeDefault(rte, html, font, width, height, readOnly);
	}
}

function writeDefault(rte, html, font, width, height, readOnly) {
	if (font=="Verdana") { size="10"; } else { size="13"; }
	if (!readOnly) {
		document.writeln('<textarea name="' + rte + '" id="' + rte + '" style="width: ' + width + 'px; height: ' + height + 'px;font-family:' + font + '; font-size: ' + size + 'px;">' + html + '</textarea>');
	} else {
		document.writeln('<textarea name="' + rte + '" 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) {
	document.writeln('<iframe id="' + rte + '" name="' + rte + '" width="' + width + 'px" height="' + height + 'px" marginwidth="3" marginheight="2" style="background-color:#FFFFFF"></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);
}

function enableDesignMode(rte, html, font, readOnly) {
	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>";
	
	//alert("contenu de frame : "+frameHtml);
		
	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";
	} 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 ((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
	}
}
