var isRichText = false;
var rng;
var currentRTE;
var allRTEs = "";

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

var imagesPath;
var includesPath;
var cssFile;

var butpos=5;
//styleWithCSS

function initRTE(imgPath, incPath, css) {
	//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;
	}
	
	if (isIE) {
		document.onmouseover = raiseButton;
		document.onmouseout  = normalButton;
		document.onmousedown = lowerButton;
		document.onmouseup   = raiseButton;
	}
	
	//set paths vars
	imagesPath = imgPath;
	includesPath = incPath;
	cssFile = css;	
	
	if (isRichText) document.writeln('<style type="text/css">@import "' + includesPath + 'rte.css";</style>');
	
	//for testing standard textarea, uncomment the following line
	//isRichText = false;
}

function generate_buttons(rte,butstring,butsize,sep){
	var butinfo=butstring.split(":");
	var buthtml="";
	
	for (var i = 0; i < butinfo.length; i++){	
	
	
	//'insertHtml(\'<div stlye=\"position:absolute;height:29px;width:20px;\"></div>\')'

		
		switch(butinfo[i]){
			
			//case "b":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Fett" id="bold" onclick="rteCommand(\'' + rte + '\', \'insertbutton\', \'\')"></a>';break;	
			
			
			case "b":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Fett" id="bold" onclick="rteCommand(\'' + rte + '\', \'bold\', \'\')"></a>';break;	
			case "i":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Kursiv" onclick="rteCommand(\'' + rte + '\', \'italic\', \'\')"></a>';break;	
			case "u":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Unterstrichen" onclick="rteCommand(\'' + rte + '\', \'underline\', \'\')"></a>';break;			
			//case "ts":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Textg&ouml;sse" id="fontsize_' + rte + '" onclick="e_dialog(\'' + rte + '\', \'fontsize\', \'x_fs\');"></a>';break;		
			case "ts":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Textg&ouml;sse" id="fontsize_' + rte + '" onclick="w_dialog(\'' + rte + '\', \'fontsize\', \'x_fs\',null,null,null);"></a>';break;		
			
			case "tc":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Textfarbe" id="forecolor_' + rte + '" onclick="w_dialog(\'' + rte + '\', \'forecolor\', \'x_cp\',null,null,null)"></a>';break;	
			case "bc":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Hintergrundfarbe" id="hilitecolor_' + rte + '" onclick="w_dialog(\'' + rte + '\', \'hilitecolor\', \'x_cp\',null,null,null)"></a>';break;	
		
			case "al":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Linksb&uuml;ndig" onclick="rteCommand(\'' + rte + '\', \'justifyleft\', \'\')"></a>';break;	
			case "ar":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Rechtsb&uuml;ndig" onclick="rteCommand(\'' + rte + '\', \'justifyright\', \'\')"></a>';break;	
			case "ac":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Zentriert" onclick="rteCommand(\'' + rte + '\', \'justifycenter\', \'\')"></a>';break;	
			case "aj":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Blocksatz" onclick="rteCommand(\'' + rte + '\', \'justifyfull\', \'\')"></a>';break;	
			
			case "hr":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Horizontale Linie" onclick="rteCommand(\'' + rte + '\', \'inserthorizontalrule\', \'\')"></a>';break;
			case "ol":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Nummerierte Liste" onclick="rteCommand(\'' + rte + '\', \'insertorderedlist\', \'\')"></a>';break;	
			case "li":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Liste" onclick="rteCommand(\'' + rte + '\', \'insertunorderedlist\', \'\')"></a>';break;
			case "url":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Link einf&uuml;gen" id="table_' + rte + '" onclick="w_dialog(\'' + rte + '\', \'link\', \'x_li\',0,0,null);"></a>';break;	
			case "ti":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Einr&uuml;cken" onclick="rteCommand(\'' + rte + '\', \'outdent\', \'\')"></a>';break;	
			case "to":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Ausr&uuml;cken" onclick="rteCommand(\'' + rte + '\', \'indent\', \'\')"></a>';break;	
	
			case "ao":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Layout einf&uuml;gen" id="layout_' + rte + '" onclick="w_dialog(\'' + rte + '\', \'layout\', \'x_ao\',0,0,null);"></a>';break;		
			case "tb":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Tabelle einf&uuml;gen" id="table_' + rte + '" onclick="w_dialog(\'' + rte + '\', \'table\', \'x_tb\',0,0,null);"></a>';break;		
			case "im":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Bild einf&uuml;gen" id="addImage_' + rte + '" onclick="w_dialog(\'' + rte + '\', \'addImage\', \'x_pc\',0,0,null);"></a>';break;			
			case "dl":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Download einf&uuml;gen" id="addDown_' + rte + '" onclick="w_dialog(\'' + rte + '\', \'addDown\', \'x_dl\',0,0,null);"></a>';break;			
			
			case "ly":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Ebene einf&uuml;gen" id="addlayer_' + rte + '" onclick="w_dialog(\'' + rte + '\', \'addlayer\', \'x_cp\',null,null,\'e_s_c_a_t_o_n\')"></a>';break;	
		
			case "sc":  if (isIE) {buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Spell Check" onclick="checkspell()"></a>';}break;				
			case "x":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Cut" onclick="rteCommand(\'' + rte + '\', \'cut\')"></a>';break;		
			case "c":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Copy" onclick="rteCommand(\'' + rte + '\', \'copy\')"></a>';break;		
			case "v":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;"  title="Paste" onclick="rteCommand(\'' + rte + '\', \'paste\')"></a>';break;						
			case "z":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Redo" onclick="rteCommand(\'' + rte + '\', \'redo\')"></a>';break;
			
			case "xxx":	buthtml=buthtml+'<a href="#" style="left:'+butpos+'px;width:'+butsize+'px;" title="Formatierung entfernen" onclick="rteCommand(\'' + rte + '\', \'removeformat\');doAction(\'Unlink\',null);"></a>';break;			
			
			//case "10":	butpos=butpos+10;break;		
			//case "50":	butpos=butpos+50;break;	
			
			default:
				if (parseInt(butinfo[i])==butinfo[i]){
					butpos=butpos+parseInt(butinfo[i]);
					butpos=butpos-butsize-sep;					
				}
				break;
		}	
		butpos=butpos+butsize+sep;
	}
	butpos=butpos+5-sep;
	buthtml='<div class="rteBack" style ="width:' + butpos  + 'px;" id="Buttons2_' + rte + '">'+buthtml;	
	buthtml=buthtml+'</div>';
	//alert( butpos);
	return buthtml;	
}
function writeRichText(rte, html, height, buttons, readOnly,butstring,butsize,sep) {
	if (isRichText) {
		if (allRTEs.length > 0) allRTEs += ";";
		allRTEs += rte;		
		if (readOnly) buttons = false;	
		
		if (buttons == true) {
			var out_html=generate_buttons(rte,butstring,butsize,sep);
		}		
		document.writeln('<div class="rteDiv" style=width:'+(butpos)+'px;">');		
		//document.writeln('<iframe id="' + rte + '" name="' + rte + '" width="' + butpos + 'px" height="' + height + 'px" src="' + includesPath + 'blank.htm" class="rte_field" style="width:'+butpos+'px;height:'+height+'px;"></iframe>');
		document.writeln('<iframe id="' + rte + '" name="' + rte + '" width="' + butpos + 'px" height="' + height + 'px" src="' + includesPath + 'module/rte/dialogs/blank.htm" class="rte_field" style="width:'+butpos+'px;height:'+height+'px;"></iframe>');
		
		if (buttons == true) {			
			document.writeln(out_html);
		}
		if (!readOnly) document.writeln('<br /><input class="rtesource" type="checkbox" id="chkSrc' + rte + '" onclick="toggleHTMLSrc(\'' + rte + '\');" />');	
		
		document.writeln('<iframe width="132" height="100" id="x_cp' + rte + '" name="x_cp' + rte + '" src="module/rte/dialogs/rte_palette.php?ver=1" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;" onmouseout="showHideElement(\'x_cp'+rte+'\', \'hide\');" frameborder="0"></iframe>');
		document.writeln('<iframe width="110" height="200" id="x_fs' + rte + '" name="x_fs' + rte + '" src="module/rte/dialogs/rte_fontsize.php?ver=1" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;" onmouseout="showHideElement(\'x_fs'+rte+'\', \'hide\');" frameborder="0"></iframe>');
		document.writeln('<iframe width="' + butpos + '" height="' + (height+40) + '" id="x_pc' + rte + '" name="x_pc' + rte + '" src="module/rte/dialogs/rte_pic.php?ver=1" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;"  frameborder="0"></iframe>');
		document.writeln('<iframe width="' + butpos + '" height="' + (height+40) + '" id="x_tb' + rte + '" name="x_tb' + rte + '" src="module/rte/dialogs/rte_table.php?ver=1" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;"  frameborder="0"></iframe>');
		document.writeln('<iframe width="' + butpos + '" height="' + (height+40) + '" id="x_ao' + rte + '" name="x_ao' + rte + '" src="module/rte/dialogs/rte_layout.php?ver=1" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;"  frameborder="0"></iframe>');
		document.writeln('<iframe width="' + butpos + '" height="' + (height+40) + '" id="x_li' + rte + '" name="x_li' + rte + '" src="module/rte/dialogs/rte_link.php?ver=1" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;"  frameborder="0"></iframe>');
		document.writeln('<iframe width="' + butpos + '" height="' + (height+40) + '" id="x_dl' + rte + '" name="x_dl' + rte + '" src="module/rte/dialogs/rte_file.php?ver=1" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;"  frameborder="0"></iframe>');
		
		document.writeln('<input type="hidden" id="hdn' + rte + '" name="' + rte + '" value="">');
		document.writeln('</div>');
			
		document.getElementById('hdn' + rte).value = html;
		enableDesignMode(rte, html, readOnly);
	} else {
		if (!readOnly) {document.writeln('<textarea  class="rte_field" name="' + rte + '" id="' + rte + '" style="background-color:grey; width: ' + butpos + 'px; height: ' + height + 'px;">' + html + '</textarea>');} 
		else {document.writeln('<textarea  class="rte_field" name="' + rte + '" id="' + rte + '" style="background-color:grey; width: ' + butpos + 'px; height: ' + height + 'px;" readonly>' + html + '</textarea>');}
	}
	butpos=5;
}
function enableDesignMode(rte, html, readOnly) {	
	var frameHtml = "<html id=\"" + rte + "\">\n";
	frameHtml += "<head>\n";
	//to reference your stylesheet, set href property below to your stylesheet path and uncomment
	if (cssFile.length > 0) {
		frameHtml += "<link media=\"all\" type=\"text/css\" href=\"" + cssFile + "\" rel=\"stylesheet\">\n";
	} else {
		frameHtml += "<style type=\"text/css\">\n";
		frameHtml += "body {\n";
		frameHtml += "background: #FFFFFF;\n";
		frameHtml += "margin: 0px;\n";
		frameHtml += "padding: 0px;font-size:12px;font-family: Verdana, Arial, Helvetica, sans-serif;border:none;\n";
		frameHtml += "}\n";
		//frameHtml +="a {color: #fc9c34;font-weight: bold;text-decoration: none;} a:link {color: #fc9c34;font-weight: bold;text-decoration: none;}a:visited {text-decoration: none;color: #fc9c34;}a:hover {text-decoration: none;color: #A65802;}a:active {text-decoration: none;color: #fc9c34;}";
		frameHtml += "</style>\n";		
	}
	frameHtml += "</head>\n";
	frameHtml += "<body>\n";
	frameHtml += html + "\n";
	frameHtml += "</body>\n";
	frameHtml += "</html>";
	
	//frameHtml=frameHtml.replace("font","");
	//frameHtml = frameHtml.replace(/<font size=\"(.*?)\">(.*?)<\/font>/,  "<span style=\"font-size:$1px;\">$2</span>");
	
	if (document.all) {
		var oRTE = frames[rte].document;
		oRTE.open();
		oRTE.write(frameHtml);
		oRTE.close();
		if (!readOnly) {
			oRTE.designMode = "On";
			//oRTE.execCommand("styleWithCSS",false, false); 		
		}
	} else {
		try {
			if (!readOnly) {
				document.getElementById(rte).contentDocument.designMode = "on";
				//document.getElementById(rte).execCommand("styleWithCSS",false, false);
				
			}
			try {
				var oRTE = document.getElementById(rte).contentWindow.document;
				oRTE.open();
				oRTE.write(frameHtml);
				oRTE.close();
				if (isGecko && !readOnly) {
					//attach a keyboard handler for gecko browsers to make keyboard shortcuts work
					oRTE.addEventListener("keypress", kb_handler, true);
				}
			} catch (e) {
				alert("Error preloading content.");
			}
		} catch (e) {
			//gecko may take some time to enable design mode.
			//Keep looping until able to set.
			if (isGecko) {
				setTimeout("enableDesignMode('" + rte + "', '" + html + "', " + readOnly + ");", 10);
			} else {
				return false;
			}
		}
	}
}
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('hdn' + rte);
	var oRTE = document.getElementById(rte);
	var readOnly = false;
	
	//check for readOnly mode
	if (document.all) {
		if (frames[rte].document.designMode != "On") readOnly = true;
	} else {
		if (document.getElementById(rte).contentDocument.designMode != "on") readOnly = true;
	}
	
	if (isRichText && !readOnly) {
		//if viewing source, switch back to design view
		if (document.getElementById("chkSrc" + rte).checked) {
			document.getElementById("chkSrc" + rte).checked = false;
			toggleHTMLSrc(rte);
		}
		
		if (oHdnMessage.value == null) oHdnMessage.value = "";
		if (document.all) {
			oHdnMessage.value = frames[rte].document.body.innerHTML;
		} else {
			oHdnMessage.value = oRTE.contentWindow.document.body.innerHTML;
		}
		
		//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) {
	//function to perform command
	var oRTE;
	if (document.all) {
		oRTE = frames[rte];
	} else {
		oRTE = document.getElementById(rte).contentWindow;
	}
	
	try {
		oRTE.focus();
	  	oRTE.document.execCommand(command, false, option);		
		oRTE.focus();
	} catch (e) {
	//alert(e);
		//setTimeout("rteCommand('" + rte + "', '" + command + "', '" + option + "');", 10);
	}
	hidePopups(null);
	
}

function toggleHTMLSrc(rte) {
	//contributed by Bob Hutzel (thanks Bob!)
	var oRTE;
	if (document.all) {
		oRTE = frames[rte].document;
	} else {
		oRTE = document.getElementById(rte).contentWindow.document;
	}
	
	if (document.getElementById("chkSrc" + rte).checked) {
		//showHideElement("Buttons1_" + rte, "hide");
		showHideElement("Buttons2_" + rte, "hide");
		if (document.all) {
			oRTE.body.innerText = oRTE.body.innerHTML;
		} else {
			var htmlSrc = oRTE.createTextNode(oRTE.body.innerHTML);
			oRTE.body.innerHTML = "";
			oRTE.body.appendChild(htmlSrc);
		}
	} else {
		//showHideElement("Buttons1_" + rte, "show");
		showHideElement("Buttons2_" + rte, "show");
		if (document.all) {
			//fix for IE
			var output = escape(oRTE.body.innerText);
			output = output.replace("%3CP%3E%0D%0A%3CHR%3E", "%3CHR%3E");
			output = output.replace("%3CHR%3E%0D%0A%3C/P%3E", "%3CHR%3E");			
			oRTE.body.innerHTML = unescape(output);
		} else {
			var htmlSrc = oRTE.body.ownerDocument.createRange();
			htmlSrc.selectNodeContents(oRTE.body);
			oRTE.body.innerHTML = htmlSrc.toString();
		}
	}
}

function w_dialog(rte, command, dialog_id,xpos,ypos,altrange) {
	setRange(rte);	
	//get dialog position
	
	var oDialog = document.getElementById(dialog_id + rte);
	if(xpos!=null){
		oDialog.style.left = xpos + "px";
		oDialog.style.top = ypos + "px";
	}
	else{	
		var oDialog = document.getElementById(dialog_id + rte);
		var buttonElement = document.getElementById(command + '_' + rte);
		var iLeftPos = getOffsetLeft(buttonElement);
		var iTopPos = getOffsetTop(buttonElement) + (buttonElement.offsetHeight + 4);
		oDialog.style.left = (iLeftPos) + "px";
		oDialog.style.top = (iTopPos) + "px";
	}
	
	if ((command == parent.command) && (rte == currentRTE)) {
		//if current command dialog is currently open, close it
		if (oDialog.style.visibility == "hidden") {showHideElement(oDialog, 'show');} 
		else {showHideElement(oDialog, 'hide');}
	} else {
		//if opening a new dialog, close all others
		var vRTEs = allRTEs.split(";");
		for (var i = 0; i < vRTEs.length; i++) {
			showHideElement(dialog_id + vRTEs[i], 'hide');
		}
		showHideElement(oDialog, 'show');
	}	
	if(altrange==null){
		setRange(rte);
		if (isIE) {
			linkText = stripHTML(rng.htmlText);
		} else {
			linkText = stripHTML(rng.toString());
		}
	}
	else{
		linkText = altrange;
	}
	//alert(linkText);
	try {
		var iframeName = dialog_id + rte;		
		window.frames[iframeName].document.getElementById('d_info').value = linkText;		
	} catch (e) {
		setTimeout("setLinkText('" + linkText + "');", 10);
	}
	parent.command = command;
	currentRTE = rte;
	hidePopups(dialog_id);
}

function setColor(color) {
	var rte = currentRTE;
	var rangetext="";
	var parentCommand = parent.command;	
	if (document.all) {
		var sel = frames[rte].document.selection; 
		if (parentCommand == "hilitecolor") parentCommand = "backcolor";
		if (sel != null) {
			var newRng = sel.createRange();
			newRng = rng;
			newRng.select();
		}
	}
	
		
	
	
	
	
	
	
	//if (parentCommand == "hilitecolor") parentCommand = "backcolor";
	//alert(parentCommand);
	
	rteCommand(rte, parentCommand, color);	
	hidePopups(null);
}

function setColor_orginal(color) {
	var rte = currentRTE;
	var parentCommand = parent.command;	
	if (document.all) {
		var sel = frames[rte].document.selection; 
		if (parentCommand == "hilitecolor") parentCommand = "backcolor";
		if (sel != null) {
			var newRng = sel.createRange();
			newRng = rng;
			newRng.select();
		}
	}	
	rteCommand(rte, parentCommand, color);	
	hidePopups(null);
}
function doAction(command,option) {
	var rte = currentRTE;
	var parentCommand = parent.command;	
	if (document.all) {
		var sel = frames[rte].document.selection; 
		if (sel != null) {
			var newRng = sel.createRange();
			newRng = rng;
			newRng.select();
		}
	}	
	rteCommand(rte, command, option);	
}
function addImage(rte) {
	//function to add image
	imagePath = prompt('Enter Image URL:', 'http://');				
	if ((imagePath != null) && (imagePath != "")) {
		rteCommand(rte, 'InsertImage', imagePath);
	}
}
function getOffsetTop(elm) {
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	var parents_up = 2; //the positioning div is 2 elements up the tree
	
	while(parents_up > 0) {
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
		parents_up--;
	}
	
	return mOffsetTop;
}
function getOffsetLeft(elm) {
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	var parents_up = 2;
	
	while(parents_up > 0) {
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
		parents_up--;
	}	
	return mOffsetLeft;
}
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 '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();
		}
 	}
}
function insertHTML(html) {
	//function to add HTML -- thanks dannyuk1982
	var rte = currentRTE;
	
	var oRTE;
	if (document.all) {
		oRTE = frames[rte];
	} else {
		oRTE = document.getElementById(rte).contentWindow;
	}
	
	oRTE.focus();
	if (document.all) {
		oRTE.document.selection.createRange().pasteHTML(html);
	} else {
		oRTE.document.execCommand('insertHTML', false, html);
	}
	hidePopups(null);
}
function inserttableHTML(html) {
	//function to add HTML -- thanks dannyuk1982
	var rte = currentRTE;
	
	var oRTE;
	if (document.all) {
		oRTE = frames[rte];
	} else {
		oRTE = document.getElementById(rte).contentWindow;
	}
	
	oRTE.focus();
	if (document.all) {
		oRTE.document.selection.createRange().pasteHTML(html);
	} else {
		oRTE.document.execCommand('insertHTML', false, html);
	}
	showHideElement('it' + rte, "hide");
	
	hidePopups(null);
}

function showHideElement(element, showHide) {
	//function to show or hide elements
	//element variable can be string or object
	if (document.getElementById(element)) {
		element = document.getElementById(element);
	}
	
	if (showHide == "show") {
		element.style.visibility = "visible";
	} else if (showHide == "hide") {
		element.style.visibility = "hidden";
	}
}

function setRange(rte) {
	//function to store range of current selection
	var oRTE;
	if (document.all) {
		oRTE = frames[rte];
		var selection = oRTE.document.selection; 
		if (selection != null) rng = selection.createRange();
	} else {
		oRTE = document.getElementById(rte).contentWindow;
		var selection = oRTE.getSelection();
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
	}
}

function stripHTML(oldString) {
	//function to strip all html
	var newString = oldString.replace(/(<([^>]+)>)/ig,"");
	
	//replace carriage returns and line feeds
   newString = newString.replace(/\r\n/g," ");
   newString = newString.replace(/\n/g," ");
   newString = newString.replace(/\r/g," ");
	
	//trim string
	newString = trim(newString);
	
	return newString;
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") return inputString;
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
	
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length - 1, retValue.length);
	
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length - 1);
      ch = retValue.substring(retValue.length - 1, retValue.length);
   }
	
	// Note that there are two spaces in the string - look for multiple spaces within the string
   while (retValue.indexOf("  ") != -1) {
		// Again, there are two spaces in each of the strings
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length);
   }
   return retValue; // Return the trimmed string back to the user
}

function hidePopups(win){
	var rte = currentRTE;
	if(win!="x_cp"){showHideElement('x_cp' + rte, "hide");}
	if(win!="x_fs"){showHideElement('x_fs' + rte, "hide");}
	if(win!="x_tb"){showHideElement('x_tb' + rte, "hide");}
	if(win!="x_li"){showHideElement('x_li' + rte, "hide");}	
	if(win!="x_pc"){showHideElement('x_pc' + rte, "hide");}
	if(win!="x_ao"){showHideElement('x_ao' + rte, "hide");}
	if(win!="x_dl"){showHideElement('x_dl' + rte, "hide");}
}

function insertLink_2(rte,urldat) {
	//function to insert link
	var szURL = urldat;
	try {
		//ignore error for blank urls
		rteCommand(rte, "unlink", null);
		rteCommand(rte, "CreateLink", szURL);
	} catch (e) {
		alert(e);
	}
	hidePopups(null);
}


//*****************
//IE-Only Functions
//*****************
function checkspell() {
	//function to perform spell check
	try {
		var tmpis = new ActiveXObject("ieSpell.ieSpellExtension");
		tmpis.CheckAllLinkedDocuments(document);
	}
	catch(exception) {
		if(exception.number==-2146827859) {
			if (confirm("ieSpell not detected.  Click Ok to go to download page."))
				window.open("http://www.iespell.com/download.php","DownLoad");
		} else {
			alert("Error Loading ieSpell: Exception " + exception.number);
		}
	}
}

function raiseButton(e) {
	//IE-Only Function
	var el = window.event.srcElement;
	
	className = el.className;
	if (className == 'rteImage' || className == 'rteImageLowered') {
		el.className = 'rteImageRaised';
	}
}

function normalButton(e) {
	//IE-Only Function
	var el = window.event.srcElement;
	
	className = el.className;
	if (className == 'rteImageRaised' || className == 'rteImageLowered') {
		el.className = 'rteImage';
	}
}

function lowerButton(e) {
	//IE-Only Function
	var el = window.event.srcElement;
	
	className = el.className;
	if (className == 'rteImage' || className == 'rteImageRaised') {
		el.className = 'rteImageLowered';
	}
}


	

