// Modified
// Cross-Browser Rich Text Editor by Kevin Roth
// http://www.kevinroth.com/rte/demo.htm


var isRichText = false;

var isIE;
var isGecko;
var isSafari;
var isKonqueror;

var cssFile = 'css/rte2.css';

function initRTE() {
	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,rtename,imgOptions,rtevalue) {
	if (isRichText) {
    	document.writeln('<div class=\"rtePanel\">')
    	document.writeln(' <select id="' + rte + 'selectstyle" onChange="selectStyle(\'' + rte + '\')">');
    	document.writeln(' <option value="">-- V&aelig;lg stil --</option>');
    	document.writeln(' <option value="<p>">paragraf</option>');
    	document.writeln(' <option value="<h1>">Overskrift 1</option>');
    	document.writeln(' <option value="<h3>">Overskrift 2</option>');
    	document.writeln(' </select>');
//    	if(imgOptions != ''){
        	document.writeln(' <select id="' + rte + 'selectimage" onChange="addImage(\'' + rte + '\')">');
        	document.writeln(' <option value="">-- Inds&aelig;t billede --</option>');
        	document.write(imgOptions);
        	document.writeln(' </select>');
  //  	}
    	document.writeln('<button type="button" class=\"bold\" onClick="rteCommand(\'' + rte + '\', \'bold\', \'\')"><b>Fed</b></button>');
		document.writeln('<button type="button" class=\"italic\" onClick="rteCommand(\'' + rte + '\', \'italic\', \'\')"><i>Kursiv</i></button>');
		document.writeln('<button type="button" class=\"link\" onClick="insertLink(\'' + rte + '\')"><u  style="color:#77aa77">link</u></button>');
		
		
		document.writeln('</div><br clear="all"/>');
		document.writeln('<div class="rteiframe"><iframe id="' + rte + '" name="' + rte + '" frameborder="0" width="100%" height="100%" src="blank.htm" scrolling="yes"></iframe></div>');
		document.writeln('<input type="hidden" id="hdn' + rte + '" name="' + rtename + '" value="">');
		
		enableDesignMode(rte);
		
		var frameHtml;
		frameHtml = "<html id=\"" + rte + "\">\n";
	    frameHtml += "<head>\n";
	    frameHtml += "<link media=\"all\" type=\"text/css\" href=\"" + cssFile + "\" rel=\"stylesheet\">\n";
	    frameHtml += "</head>\n";
	    frameHtml += "<body>\n";
	    frameHtml += rtevalue+"\n";
	    frameHtml += "</body>\n";
	    frameHtml += "</html>";
	    var oRTE
        if (document.all) { 
            oRTE = frames[rte].document;
        }
	    else {
    	    oRTE = document.getElementById(rte).contentWindow.document;
    	    if (isGecko) {
				//attach a keyboard handler for gecko browsers to make keyboard shortcuts work
				oRTE.addEventListener("keypress", kb_handler, true);
            }
    	}
	    oRTE.open();
		oRTE.write(frameHtml);
		oRTE.close();
        
		
	} 
	else{
		document.writeln('<textarea name="' + rtename + '" id="' + rte + '" >' + rtevalue + '</textarea>');	
	}
}

function enableDesignMode(rte) {
	if (document.all) { 
    	frames[rte].document.designMode = "On";
    } 
	else {
		try {
        	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 + "');", 10);
			} else {
				return;
			}
		}
	}
}


function updateRTE(rte) {
	if (!isRichText) return;
	
	//set message value
	var oHdnMessage = document.getElementById('hdn' + rte);
	var oRTE = document.getElementById(rte);
	if (isRichText) {
		if (oHdnMessage.value == null) oHdnMessage.value = "";
		if (document.all) {
			oHdnMessage.value = frames[rte].document.body.innerHTML;
		} else {
			oHdnMessage.value = oRTE.contentWindow.document.body.innerHTML;
		}
		oHdnMessage.value = oHdnMessage.value.replace(/\'/g,"&#39;");
		oHdnMessage.value = oHdnMessage.value.replace(/\r\n/g," ");
		oHdnMessage.value = oHdnMessage.value.replace(/\n/g," ");
		oHdnMessage.value = oHdnMessage.value.replace(/\r/g," ");
		
		//if there is no content (other than formatting) set value to nothing
		if (stripHTML(oHdnMessage.value.replace("&nbsp;", " ")) == "" 
			&& oHdnMessage.value.toLowerCase().search("<hr") == -1
			&& oHdnMessage.value.toLowerCase().search("<img") == -1) oHdnMessage.value = "";
		//fix for gecko
		if (escape(oHdnMessage.value) == "%3Cbr%3E%0D%0A%0D%0A%0D%0A") oHdnMessage.value = "";
	}
}

function rteCommand(rte, command, option) {
	var oRTE;
	if (document.all) {
		oRTE = frames[rte];
	} else {
		oRTE = document.getElementById(rte).contentWindow;
	}
	oRTE.focus();
	oRTE.document.execCommand(command, false, option);
	oRTE.focus();
	
}

function selectStyle(rte) {
	var selected = document.getElementById(rte+'selectstyle').value;
	if (selected != '') {
		rteCommand(rte, 'formatblock', selected);
		document.getElementById(rte+'selectstyle').selectedIndex = 0;
	}
}

function insertLink(rte) {
	var szURL = prompt("Enter a URL:", "http://");
	try {
		//ignore error for blank urls
		rteCommand(rte, "Unlink", null);
		rteCommand(rte, "CreateLink", szURL);
	} catch (e) {
		//do nothing
	}
}
function addImage(rte) {
	//function to add image
	var imagePath = document.getElementById(rte+'selectimage').value;

	if ((imagePath != null) && (imagePath != "")) {
		rteCommand(rte, 'InsertImage', imagePath);
	}
	document.getElementById(rte+'selectimage').selectedIndex = 0;
}


function kb_handler(evt) {
	var rte = evt.target.id;
	
	//contributed by Anti Veeranna (thanks Anti!)
	if (evt.ctrlKey) {
		var key = String.fromCharCode(evt.charCode).toLowerCase();
		var cmd = '';
		switch (key) {
			case 'f': cmd = "bold"; break;
			case 'k': cmd = "italic"; break;
			case 'b': cmd = "bold"; break;
			case 'i': cmd = "italic"; break;
//			case 'u': cmd = "underline"; break;
		};

		if (cmd) {
			rteCommand(rte, cmd, null);
			
			// stop the event bubble
			evt.preventDefault();
			evt.stopPropagation();
		}
 	}
}

