// Cross-Browser Rich Text Editor
// Version 3.01a
// Written by Kevin Roth (http://www.kevinroth.com/rte/)
// Visit the support forums at http://www.kevinroth.com/forums/index.php?c=2
// License: http://creativecommons.org/licenses/by/2.5/

//init globals
var lang = "en";
var encoding = "UTF-8";
var debugMode = false;
var isRichText = false;
var defaultWidth = 540;
var defaultHeight = 200;
var allRTEs = "";
var currentRTE = "";
var selectionText = "";
var rng;

var imagesPath = "";
var includesPath = "";
var cssFile = "";
var generateXHTML = true;

//browser vars
var ua = navigator.userAgent.toLowerCase();
var isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1));
var ieVersion = parseFloat(ua.substring(ua.indexOf('msie ') + 5));
var isGecko = (ua.indexOf("gecko") != -1);
var isSafari = (ua.indexOf("safari") != -1);
var safariVersion = parseFloat(ua.substring(ua.lastIndexOf("safari/") + 7));
var isKonqueror = (ua.indexOf("konqueror") != -1);
var konquerorVersion = parseFloat(ua.substring(ua.indexOf('konqueror/') + 10));
var isOpera = (ua.indexOf("opera") != -1);
var isNetscape = (ua.indexOf("netscape") != -1);
var netscapeVersion = parseFloat(ua.substring(ua.lastIndexOf('/') + 1));
var isIphone = (ua.indexOf("iphone") != -1);

//command vars
var toolbar1Enabled = true;
var toolbar2Enabled = true;

var cmdFormatBlockEnabled = true;
var cmdFontNameEnabled = true;
var cmdFontSizeEnabled = true;
var cmdIncreaseFontSizeEnabled = false;
var cmdDecreaseFontSizeEnabled = false;

var cmdBoldEnabled = true;
var cmdItalicEnabled = true;
var cmdUnderlineEnabled = true;
var cmdStrikethroughEnabled = true;
var cmdSuperscriptEnabled = true;
var cmdSubscriptEnabled = true;

var cmdJustifyLeftEnabled = true;
var cmdJustifyCenterEnabled = true;
var cmdJustifyRightEnabled = true;
var cmdJustifyFullEnabled = true;

var cmdInsertHorizontalRuleEnabled = false;
var cmdInsertOrderedListEnabled = true;
var cmdInsertUnorderedListEnabled = true;

var cmdOutdentEnabled = true;
var cmdIndentEnabled = true;
var cmdForeColorEnabled = true;
var cmdHiliteColorEnabled = false;
var cmdInsertHTMLEnabled = true;
var cmdCreateLinkEnabled = true;
var cmdCreateLinkEnabled2 = true;
var cmdInsertImageEnabled = true;

var cmdCutEnabled = true;
var cmdCopyEnabled = true;
var cmdPasteEnabled = true;
var cmdUndoEnabled = true;
var cmdRedoEnabled = true;
var cmdRemoveFormatEnabled = true;
var cmdSpellEnabled = true;


function initRTE(imgPath, incPath, css, genXHTML) {
	//set browser vars
	try {
		//set global vars
		imagesPath = imgPath;
		includesPath = incPath;
		cssFile = css;
		generateXHTML = genXHTML;
		
		//check to see if designMode mode is available and disable unsupported commands
		if (document.designMode) {
			if (document.getElementById && !isIE && !isSafari && !isKonqueror) {
				isRichText = true;
				if (isGecko || isOpera) {
					if (isGecko) {
						cmdIncreaseFontSizeEnabled = true;
						cmdDecreaseFontSizeEnabled = true;
					}
					cmdCutEnabled = false;
					cmdCopyEnabled = false;
					cmdPasteEnabled = false;
					cmdCreateLinkEnabled2 = false;
				}
				if ((isNetscape && netscapeVersion < 8) || isOpera) {
				cmdCreateLinkEnabled2 = false;
				}
			} else if (isIE && ieVersion >= 5.5) {
				isRichText = true;
			} else if ((isSafari && safariVersion >= 312) || isKonqueror) {
				//Safari 1.3+ is capable of designMode, Safari 1.3 = webkit build 312
				isRichText = true;
				//toolbar1Enabled = false;
				cmdStrikethroughEnabled = false;
				//cmdJustifyFullEnabled = false;
				cmdInsertHorizontalRuleEnabled = false;
				cmdInsertOrderedListEnabled = false;
				cmdInsertUnorderedListEnabled = false;
				//cmdOutdentEnabled = false;
				//cmdIndentEnabled = false;
				cmdForeColorEnabled = false;
				cmdHiliteColorEnabled = false;
				cmdInsertImageEnabled = false;
				cmdPasteEnabled = false;
				//cmdInsertHTMLEnabled = false;
				//cmdCreateLinkEnabled = false;
				cmdCreateLinkEnabled2 = false;
				cmdRemoveFormatEnabled = false;
				if (isIphone) isRichText = false;
			}
		}
		
		if (isRichText) {
			document.writeln('<style type="text/css">@import "' + includesPath + 'rtecss.asp";</style>');
			document.writeln('<iframe width="154" height="104" id="cp" src="' + includesPath + 'palette.htm" marginwidth="0" marginheight="0" scrolling="no" style="display: none; position: absolute;"></iframe>');
			if (isIE) {
				document.onmouseover = raiseButton;
				document.onmouseout  = normalButton;
				document.onmousedown = lowerButton;
				document.onmouseup   = raiseButton;
			}
		}
		
		//for testing standard textarea, uncomment the following line
		//isRichText = false;
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function richTextEditor(id) {
	//creates a richTextEditor object
	this.rteID = id;
	
	//set defaults
	this.html = "";
	this.width = defaultWidth;
	this.height = defaultHeight;
	this.readOnly = false;
	this.toolbar1 = true;
	this.toolbar2 = true;
	
	this.cmdFormatBlock = true;
	this.cmdFontName = true;
	this.cmdFontSize = true;
	this.cmdIncreaseFontSize = false;
	this.cmdDecreaseFontSize = false;
	
	this.cmdBold = true;
	this.cmdItalic = true;
	this.cmdUnderline = true;
	this.cmdStrikethrough = false;
	this.cmdSuperscript = false;
	this.cmdSubscript = false;
	
	this.cmdJustifyLeft = true;
	this.cmdJustifyCenter = true;
	this.cmdJustifyRight = true;
	this.cmdJustifyFull = true;
	
	this.cmdInsertHorizontalRule = true;
	this.cmdInsertOrderedList = true;
	this.cmdInsertUnorderedList = true;
	
	this.cmdOutdent = true;
	this.cmdIndent = true;
	this.cmdForeColor = true;
	this.cmdHiliteColor = true;
	this.cmdInsertLink = true;
	this.cmdInsertImage = false;
	this.cmdInsertTable = false;
	
	this.cmdCut = true;
	this.cmdCopy = true;
	this.cmdPaste = true;
	this.cmdUndo = true;
	this.cmdRedo = true;
	this.cmdRemoveFormat = false;
	this.cmdSpell = true;
	
	this.toggleSrc = false;
	
	//add methods
	this.build = build;
}

function build() {
	with (this) {
		if (isRichText) {
			if (this.readOnly) {
				this.toolbar1 = false;
				this.toolbar2 = false;
			}
			writeRichText(this);
			enableDesignMode(this.rteID, this.html, this.readOnly);
		} else {
			writePlainText(this);
		}
	}
}

function writePlainText(rte) {
	if (!rte.readOnly) {
		document.writeln('<textarea name="' + rte.rteID + '" id="' + rte.rteID + '" style="width: 100%; height: ' + rte.height + 'px;">' + rte.html + '</textarea>');
	} else {
		document.writeln('<textarea name="' + rte.rteID + '" id="' + rte.rteID + '" style="width: ' + rte.width + 'px; height: ' + rte.height + 'px;" readonly>' + rte.html + '</textarea>');
	}
}

function writeRichText(rte) {
	try {
		if (allRTEs.length > 0) allRTEs += ";";
		allRTEs += rte.rteID;
		
		document.writeln('<table width="' + rte.width + '" cellpadding="0" cellspacing="0" style="border:solid 1px #666666">');
		document.writeln('	<tr>');
		document.writeln('		<td>');
		if (toolbar1Enabled && rte.toolbar1) {
			document.writeln('		<table class="rteBack" cellpadding="2" cellspacing="0" id="toolbar1_' + rte.rteID + '" width="100%">');
			document.writeln('			<tr>');
			if (cmdFormatBlockEnabled && rte.cmdFormatBlock) {
				document.writeln('				<td>');
				document.writeln('					<select id="formatblock_' + rte.rteID + '" onchange="selectFont(\'' + rte.rteID + '\', event, this.id);">');
				document.writeln('						<option value="">[Style]</option>');
				document.writeln('						<option value="<p>">Normal</option>');
				document.writeln('						<option value="<h1>">Heading 1</option>');
				document.writeln('						<option value="<h2>">Heading 2</option>');
				document.writeln('						<option value="<h3>">Heading 3</option>');
				document.writeln('						<option value="<h4>">Heading 4</option>');
				document.writeln('						<option value="<h5>">Heading 5</option>');
				document.writeln('						<option value="<h6>">Quote</option>');
				document.writeln('						<option value="<address>">Address</option>');
				document.writeln('						<option value="<pre>">Formatted</option>');
				document.writeln('					</select>');
				document.writeln('				</td>');
			}
			if (cmdFontNameEnabled && rte.cmdFontName) {
				document.writeln('				<td>');
				document.writeln('					<select id="fontname_' + rte.rteID + '" onchange="selectFont(\'' + rte.rteID + '\', event, this.id)">');
				document.writeln('						<option value="Font" selected>[Font]</option>');
				document.writeln('						<option value="Arial, Helvetica, sans-serif">Arial</option>');
				document.writeln('						<option value="Arial Black, Helvetica, sans-serif">Arial Black</option>');
				document.writeln('						<option value="Arial Narrow, Helvetica, sans-serif">Arial Narrow</option>');
				document.writeln('						<option value="Comic Sans MS, sans-serif">Comic Sans MS</option>');
				document.writeln('						<option value="Courier New, Courier, mono">Courier New</option>');
				document.writeln('						<option value="System, sans-serif">System</option>');
				document.writeln('						<option value="Times New Roman, Times, serif">Times New Roman</option>');
				document.writeln('						<option value="Verdana, Arial, Helvetica, sans-serif">Verdana</option>');
				//document.writeln('						<option value="Wingdings, sans-serif">Wingdings</option>');
				document.writeln('					</select>');
				document.writeln('				</td>');
			}
			if (cmdFontSizeEnabled && rte.cmdFontSize) {
				document.writeln('				<td>');
				document.writeln('					<select id="fontsize_' + rte.rteID + '" onchange="selectFont(\'' + rte.rteID + '\', event, this.id);">');
				document.writeln('						<option value="Size">[Size]</option>');
				document.writeln('						<option value="1">1</option>');
				document.writeln('						<option value="2">2</option>');
				document.writeln('						<option value="3">3</option>');
				document.writeln('						<option value="4">4</option>');
				document.writeln('						<option value="5">5</option>');
				document.writeln('						<option value="6">6</option>');
				document.writeln('						<option value="7">7</option>');
				document.writeln('					</select>');
				document.writeln('				</td>');
			}
			if (cmdIncreaseFontSizeEnabled && rte.cmdIncreaseFontSize)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'increase_font.gif" width="25" height="24" alt="Increase Font Size" title="Increase Font Size" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'increasefontsize\')"></td>');
			if (cmdDecreaseFontSizeEnabled && rte.cmdDecreaseFontSize)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'decrease_font.gif" width="25" height="24" alt="Decrease Font Size" title="Decrease Font Size" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'decreasefontsize\')"></td>');
			document.writeln('				<td width="100%"></td>');
			document.writeln('			</tr>');
			document.writeln('		</table>');
		}
		if (rte.toolbar2) {
			document.writeln('		<table class="rteBack" cellpadding="0" cellspacing="0" id="toolbar2_' + rte.rteID + '" width="100%">');
			document.writeln('			<tr>');
			if (cmdCutEnabled && rte.cmdCut)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'cut.gif" width="25" height="24" alt="Cut" title="Cut" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'cut\')"></td>');
			if (cmdCopyEnabled && rte.cmdCopy)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'copy.gif" width="25" height="24" alt="Copy" title="Copy" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'copy\')"></td>');
			if (cmdPasteEnabled && rte.cmdPaste){
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'paste.gif" width="25" height="24" alt="Paste" title="Paste" onClick="rteCommand(\'' + rte.rteID + '\', event, \'paste\')"></td>');
				document.writeln('				<td><div style="width:2px; height: 25px; border-left:solid 1px threeDshadow; border-right: solid 1px threeDhighlight"></div></td>');
				}
			if (cmdUndoEnabled && rte.cmdUndo)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'undo.gif" width="25" height="24" alt="Undo" title="Undo" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'undo\')"></td>');
			if (cmdRedoEnabled && rte.cmdRedo){
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'redo.gif" width="25" height="24" alt="Redo" title="Redo" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'redo\')"></td>');
				document.writeln('				<td><div style="width:2px; height: 25px; border-left:solid 1px threeDshadow; border-right: solid 1px threeDhighlight"></div></td>');
				}
			if (cmdBoldEnabled && rte.cmdBold)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'bold.gif" width="25" height="24" alt="Bold" title="Bold" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'bold\')"></td>');
			if (cmdItalicEnabled && rte.cmdItalic)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'italic.gif" width="25" height="24" alt="Italic" title="Italic" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'italic\')"></td>');
			if (cmdUnderlineEnabled && rte.cmdUnderline){
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'underline.gif" width="25" height="24" alt="Underline" title="Underline" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'underline\')"></td>');
				document.writeln('				<td><div style="width:2px; height: 25px; border-left:solid 1px threeDshadow; border-right: solid 1px threeDhighlight"></div></td>');
				}
			if (cmdStrikethroughEnabled && rte.cmdStrikethrough)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'strikethrough.gif" width="25" height="24" alt="Strikethrough" title="Strikethrough" onClick="rteCommand(\'' + rte.rteID + '\', event, \'strikethrough\', \'\')"></td>');
			if (cmdSuperscriptEnabled && rte.cmdSuperscript)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'superscript.gif" width="25" height="24" alt="Superscript" title="Superscript" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'superscript\')"></td>');
			if (cmdSubscriptEnabled && rte.cmdSubscript){
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'subscript.gif" width="25" height="24" alt="Subscript" title="Subscript" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'subscript\')"></td>');
				document.writeln('				<td><div style="width:2px; height: 25px; border-left:solid 1px threeDshadow; border-right: solid 1px threeDhighlight"></div></td>');
				}
//			document.writeln('				<td><img class="rteVertSep" src="' + imagesPath + 'blackdot.gif" width="1" height="20" border="0" alt=""></td>');
			if (cmdJustifyLeftEnabled && rte.cmdJustifyLeft)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'left_just.gif" width="25" height="24" alt="Align Left" title="Align Left" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'justifyleft\')"></td>');
			if (cmdJustifyCenterEnabled && rte.cmdJustifyCenter)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'centre.gif" width="25" height="24" alt="Center" title="Center" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'justifycenter\')"></td>');
			if (cmdJustifyRightEnabled && rte.cmdJustifyRight)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'right_just.gif" width="25" height="24" alt="Align Right" title="Align Right" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'justifyright\')"></td>');
			if (cmdJustifyFullEnabled && rte.cmdJustifyFull){
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'justifyfull.gif" width="25" height="24" alt="Justify Full" title="Justify Full" onclick="rteCommand(\'' + rte.rteID + '\', event, \'justifyfull\')"></td>');
				document.writeln('				<td><div style="width:2px; height: 25px; border-left:solid 1px threeDshadow; border-right: solid 1px threeDhighlight"></div></td>');
				}
			if (cmdInsertHorizontalRuleEnabled && rte.cmdInsertHorizontalRule){
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'hr.gif" width="25" height="24" alt="Horizontal Rule" title="Horizontal Rule" onClick="rteCommand(\'' + rte.rteID + '\', event, \'inserthorizontalrule\')"></td>');
				document.writeln('				<td><div style="width:2px; height: 25px; border-left:solid 1px threeDshadow; border-right: solid 1px threeDhighlight"></div></td>');
				}
			if (cmdInsertOrderedListEnabled && rte.cmdInsertOrderedList)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'numbered_list.gif" width="25" height="24" alt="Ordered List" title="Ordered List" onClick="rteCommand(\'' + rte.rteID + '\', event, \'insertorderedlist\')"></td>');
			if (cmdInsertUnorderedListEnabled && rte.cmdInsertUnorderedList)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'list.gif" width="25" height="24" alt="Unordered List" title="Unordered List" onClick="rteCommand(\'' + rte.rteID + '\', event, \'insertunorderedlist\')"></td>');
			if (cmdOutdentEnabled && rte.cmdOutdent)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'outdent.gif" width="25" height="24" alt="Reduce Indent" title="Outdent" onClick="rteCommand(\'' + rte.rteID + '\', event, \'outdent\')"></td>');
			if (cmdIndentEnabled && rte.cmdIndent){
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'indent.gif" width="25" height="24" alt="Increase Indent" title="Indent" onClick="rteCommand(\'' + rte.rteID + '\', event, \'indent\')"></td>');
				document.writeln('				<td><div style="width:2px; height: 25px; border-left:solid 1px threeDshadow; border-right: solid 1px threeDhighlight"></div></td>');
				}
			if (cmdForeColorEnabled && rte.cmdForeColor)
				document.writeln('				<td><div id="forecolor_' + rte.rteID + '"><img class="rteImage" src="' + imagesPath + 'textcolor.gif" width="23" height="22" alt="Text Color" title="Text Color" onClick="dlgColorPalette(\'' + rte.rteID + '\', \'forecolor\', \'\')"></div></td>');
			if (cmdHiliteColorEnabled && rte.cmdHiliteColor){
				document.writeln('				<td><div id="hilitecolor_' + rte.rteID + '"><img class="rteImage" src="' + imagesPath + 'bgcolor.gif" width="23" height="22" alt="Background Color" title="Background Color" onClick="dlgColorPalette(\'' + rte.rteID + '\', \'hilitecolor\', \'\')"></div></td>');
				document.writeln('				<td><div style="width:2px; height: 25px; border-left:solid 1px threeDshadow; border-right: solid 1px threeDhighlight"></div></td>');
				}
			if ((cmdInsertHTMLEnabled || cmdCreateLinkEnabled) && rte.cmdInsertLink){
				if (cmdCreateLinkEnabled2){
					document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'hyperlink.gif" width="25" height="24" alt="Insert Link" title="Insert Link" onclick="rteCommand(\'' + rte.rteID + '\', event, \'createlink\')"></td>');
					document.writeln('				<td><div style="width:2px; height: 25px; border-left:solid 1px threeDshadow; border-right: solid 1px threeDhighlight"></div></td>');
					}
				else{
					document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'hyperlink.gif" width="25" height="24" alt="Insert Link" title="Insert Link" onmousedown="dlgInsertLink(\'' + rte.rteID + '\', \'link\')"></td>');
					document.writeln('				<td><div style="width:2px; height: 25px; border-left:solid 1px threeDshadow; border-right: solid 1px threeDhighlight"></div></td>');
					}
				}
			if (cmdInsertImageEnabled && rte.cmdInsertImage)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'image.gif" width="25" height="24" alt="Add Image" title="Add Image" onClick="addImage(\'' + rte.rteID + '\')"></td>');
			if (cmdInsertHTMLEnabled && rte.cmdInsertTable)
				document.writeln('				<td><div id="table_' + rte.rteID + '"><img class="rteImage" src="' + imagesPath + 'insert_table.gif" width="25" height="24" alt="Insert Table" title="Insert Table" onClick="dlgInsertTable(\'' + rte.rteID + '\', event, \'table\', \'\')"></div></td>');
			if (cmdSpellEnabled && rte.cmdSpell){
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'spellcheck.gif" width="25" height="24" alt="Spell Check" title="Spell Check" onClick="checkspell(\'' + rte.rteID + '\')"></td>');
				document.writeln('				<td><div style="width:2px; height: 25px; border-left:solid 1px threeDshadow; border-right: solid 1px threeDhighlight"></div></td>');
				}
			if (cmdRemoveFormatEnabled && rte.cmdRemoveFormat)
				document.writeln('				<td><img class="rteImage" src="' + imagesPath + 'removeformat.gif" width="25" height="24" alt="Remove Formatting" title="Remove Formatting" onmousedown="rteCommand(\'' + rte.rteID + '\', event, \'removeformat\')"></td>');
			document.writeln('				<td width="100%"></td>');
			document.writeln('			</tr>');
			document.writeln('		</table>');
		}
		document.writeln('			<iframe id="' + rte.rteID + '" name="' + rte.rteID + '" width="100%" height="' + rte.height + 'px" src="' + includesPath + 'blankpost.asp" frameborder="0"></iframe>');
		document.writeln('		</td>');
		document.writeln('	</tr>');
		document.writeln('</table>');
		document.writeln('<div style="margin: 0; padding: 0;">');
		if (!rte.readOnly && rte.toggleSrc)
			document.writeln('<input type="checkbox" id="chkSrc' + rte.rteID + '" onclick="toggleHTMLSrc(\'' + rte.rteID + '\',' + rte.toolbar1 + ',' + rte.toolbar2 + ');" />&nbsp;<label for="chkSrc' + rte.rteID + '">View Source</label>');
		document.writeln('<input type="hidden" id="hdn' + rte.rteID + '" name="' + rte.rteID + '" value="">');
		document.writeln('</div>');
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function enableDesignMode(rte, html, readOnly) {
	try {
		var frameHtml = "<html id=\"" + rte + "\">\n";
		frameHtml += "<head>\n";
		//if (cssFile.length > 0 && !isGecko) {
		//	frameHtml += "<link media=\"all\" type=\"text/css\" href=\"" + cssFile + "\" rel=\"stylesheet\">\n";
		//} else {
		//	frameHtml += "<style>\n";
		//	frameHtml += "body {\n";
		//	frameHtml += "	background: #FFF;\n";
		//	frameHtml += "	margin: 0px;\n";
		//	frameHtml += "	padding: 0px;\n";
		//	frameHtml += "}\n";
		//	frameHtml += "</style>\n";
		//}
		frameHtml += "<link rel=\"stylesheet\" type=\"text/css\" href=\"\/_include/stylesheet.asp\">\n";
		frameHtml += "</head>\n";
		frameHtml += "<body style=\"margin-top:20px\">\n";
		frameHtml += html + "\n";
		frameHtml += "</body>\n";
		frameHtml += "</html>";
		
		var oRTE = document.getElementById(rte);
		try {
			if (isGecko) {
				if (!readOnly) oRTE.contentDocument.designMode = "on";
				try {
					var oRTEDoc = oRTE.contentWindow.document;
					oRTEDoc.open("text/html","replace");
					oRTEDoc.write(frameHtml);
					oRTEDoc.close();
					if (!readOnly) oRTEDoc.addEventListener("keypress", geckoKeyPress, true);
				} catch (e) {
					//alert("Error preloading content.");
				}
			} else if (oRTE.contentWindow || isOpera) {
				//IE 5.5+
				try {
					var oRTEDoc = oRTE.contentWindow.document;
					oRTEDoc.open("text/html","replace");
					oRTEDoc.write(frameHtml);
					oRTEDoc.close();
					if (!readOnly && isIE) oRTEDoc.attachEvent("onkeypress", evt_ie_keypress);
				} catch (e) {
					//alert("Error preloading content.");
				}
				if (!readOnly) oRTEDoc.designMode = "on";
			} else {
				//IE5 and Opera
				var oRTEDoc = oRTE.document;
				oRTEDoc.open("text/html","replace");
				oRTEDoc.write(frameHtml);
				oRTEDoc.close();
				if (!readOnly && isIE) oRTEDoc.attachEvent("onkeypress", evt_ie_keypress);
				if (!readOnly) oRTEDoc.designMode = "on";
			}
		} catch (e) {
			//some browsers may take some time to enable design mode.
			//Keep looping until able to set.
			setTimeout("enableDesignMode('" + rte + "', '" + html + "', " + readOnly + ");", 10);
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function updateRTE(rte) {
	try {
		if (isRichText) {
			//if viewing source, switch back to design view
			if (document.getElementById("chkSrc" + rte) && document.getElementById("chkSrc" + rte).checked) document.getElementById("chkSrc" + rte).click();
			setHiddenVal(rte);
		} else {
			return;
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function setHiddenVal(rte) {
	//set hidden form field value for current rte
	try {
		var oHdnField = document.getElementById('hdn' + rte);
		if (oHdnField.value == null) oHdnField.value = "";
		
		var oRTE = document.getElementById(rte);
		if (oRTE.contentWindow) {
			var oRTEDoc = oRTE.contentWindow.document;
		} else {
			var oRTEDoc = oRTE.document;
		}
		
		if (generateXHTML) {
			//convert html output to xhtml (thanks Timothy Bell and Vyacheslav Smolin!)
			oHdnField.value = get_xhtml(oRTEDoc.body, lang, encoding);
		} else {
			oHdnField.value = oRTEDoc.body.innerHTML;
		}
		
		//if there is no content (other than formatting) set value to nothing
		if (stripHTML(oHdnField.value.replace("&nbsp;", " ")) == "" &&
			oHdnField.value.toLowerCase().search("<hr") == -1 &&
			oHdnField.value.toLowerCase().search("<img") == -1) oHdnField.value = "";
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function updateRTEs() {
	try {
		var vRTEs = allRTEs.split(";");
		for (var i = 0; i < vRTEs.length; i++) {
			updateRTE(vRTEs[i]);
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function rteCommand(rte, event, command, option) {
	//function to perform command
	try {
		var oRTE = document.getElementById(rte);
		if (oRTE.contentWindow) {
			oRTE.contentWindow.focus();
			oRTE.contentWindow.document.execCommand(command, false, option);
			oRTE.contentWindow.focus();
		} else {
			oRTE.focus();
		  	oRTE.execCommand(command, false, option);
			oRTE.focus();
		}
		
		try {
			//safari needs the following lines to keep focus
			event.preventDefault();
			event.returnValue = false;
		} catch (e2) {
		}
		
		return false;
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function toggleHTMLSrc(rte, toolbar1, toolbar2) {
	//contributed by Bob Hutzel (thanks Bob!)
	try {
		var oHdnField = document.getElementById('hdn' + rte);
		var oRTE = document.getElementById(rte);
		if (oRTE.contentWindow) {
			var oRTEDoc = oRTE.contentWindow.document;
		} else {
			var oRTEDoc = oRTE.document;
		}
		
		if (document.getElementById("chkSrc" + rte).checked) {
			//we are checking the box
			if (toolbar1) showHideElement("toolbar1_" + rte, "hide");
			if (toolbar2) showHideElement("toolbar2_" + rte, "hide");
			setHiddenVal(rte);
			var htmlSrc = oRTEDoc.createTextNode(oHdnField.value);
			oRTEDoc.body.innerHTML = "";
			oRTEDoc.body.appendChild(htmlSrc);
		} else {
			//we are unchecking the box
			if (toolbar1) showHideElement("toolbar1_" + rte, "show");
			if (toolbar2) showHideElement("toolbar2_" + rte, "show");
			if (isIE) { //fix for IE
				var output = escape(oRTEDoc.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");
				oRTEDoc.body.innerHTML = unescape(output);
			} else {
				var htmlSrc = oRTEDoc.body.ownerDocument.createRange();
				htmlSrc.selectNodeContents(oRTEDoc.body);
				oRTEDoc.body.innerHTML = htmlSrc.toString();
			}
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function dlgColorPalette(rte, command) {
	//function to display or hide color palettes
	try {
		setRange(rte);
		
		//get dialog position
		var buttonElement = document.getElementById(command + '_' + rte);
		var iLeftPos = getOffsetLeft(buttonElement);
		var iTopPos = getOffsetTop(buttonElement) + (buttonElement.offsetHeight + 4);
		var oDialog = document.getElementById('cp');
		oDialog.style.left = (iLeftPos + 100) + "px";
		oDialog.style.top = buttonElement.style.top;//(iTopPos) + "px";
		
		if ((command == parent.command) && (rte == currentRTE)) {
			//if current command dialog is currently open, close it
			if (oDialog.style.display == "none") {
				showHideElement('cp', 'show');
			} else {
				showHideElement('cp', 'hide');
			}
		} else {
			showHideElement('cp', 'show');
		}
		
		//save current values
		parent.command = command;
		currentRTE = rte;
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function dlgInsertTable(rte, event, command) {
	//function to open/close insert table dialog
	try {
		//save current values
		setRange(rte);
		parent.command = command;
		currentRTE = rte;
		InsertTable = popUpWin(includesPath + 'insert_table.htm', 'InsertTable', 360, 180, '');
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function dlgInsertLink(rte, command) {
	//function to open/close insert table dialog
	try {
		//if (cmdInsertHTMLEnabled) {
		//	//save current values
		//	setRange(rte);
		//	parent.command = command;
		//	currentRTE = rte;
		//	InsertLink = popUpWin(includesPath + 'insert_link.asp?selectionText=' + selectionText, 'InsertLink', 360, 180, '');
		//} else {
			var url = prompt("Enter URL", "http://");
			rteCommand(rte, null, "createlink", url);
		//}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function popUpWin (url, win, width, height, options) {
	try {
		var leftPos = (screen.availWidth - width) / 2;
		var topPos = (screen.availHeight - height) / 2;
		options += 'width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
		return window.open(url, win, options);
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function setColor(color) {
	//function to set color
	try {
		var rte = currentRTE;
		var parentCommand = parent.command;
		
		if (isSafari || isIE) {
			if (parentCommand == "hilitecolor") parentCommand = "backcolor";
			
			//retrieve selected range
			rng.select();
		}
		
		rteCommand(rte, null, parentCommand, color);
		showHideElement('cp', "hide");
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function addImage(rte) {
	//function to add image
	try {
		imagePath = prompt('Enter Image URL:', 'http://');
		if ((imagePath != null) && (imagePath != "")) {
			rteCommand(rte, event, 'InsertImage', imagePath);
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

// Ernst de Moor: Fix the amount of digging parents up, in case the RTE editor itself is displayed in a div.
// KJR 11/12/2004 Changed to position palette based on parent div, so palette will always appear in proper location regardless of nested divs
function getOffsetTop(elm) {
	try {
		var mOffsetTop = elm.offsetTop;
		var mOffsetParent = elm.offsetParent;
		var parents_up = 4; //the positioning div is 2 elements up the tree
		
		while(parents_up > 0) {
			mOffsetTop += mOffsetParent.offsetTop;
			mOffsetParent = mOffsetParent.offsetParent;
			parents_up--;
		}
		
		return mOffsetTop;
	} catch (e) {
		if (debugMode) alert(e);
	}
}

// Ernst de Moor: Fix the amount of digging parents up, in case the RTE editor itself is displayed in a div.
// KJR 11/12/2004 Changed to position palette based on parent div, so palette will always appear in proper location regardless of nested divs
function getOffsetLeft(elm) {
	try {
		var mOffsetLeft = elm.offsetLeft;
		var mOffsetParent = elm.offsetParent;
		var parents_up = 4;
		
		while(parents_up > 0) {
			mOffsetLeft += mOffsetParent.offsetLeft;
			mOffsetParent = mOffsetParent.offsetParent;
			parents_up--;
		}
		
		return mOffsetLeft;
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function selectFont(rte, event, selectname) {
	//function to handle font changes
	try {
		var idx = document.getElementById(selectname).selectedIndex;
		// First one is always a label
		if (idx != 0) {
			var selected = document.getElementById(selectname).options[idx].value;
			var cmd = selectname.replace('_' + rte, '');
			rteCommand(rte, event, cmd, selected);
			document.getElementById(selectname).selectedIndex = 0;
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function insertHTML(html) {
	//function to add HTML -- thanks dannyuk1982
	try {
		if (document.all) {
			rng.pasteHTML(html);
			rng.collapse(false);
			rng.select();
		} else {
			rteCommand(currentRTE, null, 'inserthtml', html);
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function showHideElement(element, showHide) {
	//function to show or hide elements
	try {
		//element variable can be string or object
		if (document.getElementById(element)) {
			element = document.getElementById(element);
			
			if (showHide == "show") {
				element.style.display = "block";
				if (element.id == 'cp') {
//					if (isGecko) {
//						element.contentDocument.location = includesPath + 'palette.htm';
//					} else if (element.contentWindow) {
//						element.contentWindow.document.location = includesPath + 'palette.htm';
//					} else {
//						element.document.location = includesPath + 'palette.htm';
//					}
				}
			} else if (showHide == "hide") {
				element.style.display = "none";
			}
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function setRange(rte) {
	//function to store range of current selection
	try {
		var oRTE = document.getElementById(rte);
		if (oRTE.contentWindow) {
			var oRTEDoc = oRTE.contentWindow.document;
		} else {
			var oRTEDoc = oRTE.document;
		}
		
		if (document.all) {
			sel = oRTEDoc.selection;
			rng = sel.createRange();
			selectionText = rng.text.toString();
		} else if (document.getSelection) {
			rng = oRTEDoc.createRange();
			selectionText = oRTEDoc.getSelection();
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}


function stripHTML(oldString) {
	//function to strip all html
	try {
		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;
	} catch (e) {
		if (debugMode) alert(e);
	}
}

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.
	try {
	   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
	} catch (e) {
		if (debugMode) alert(e);
	}
}

//********************
//Gecko-Only Functions
//********************
function geckoKeyPress(evt) {
	//function to add bold, italic, and underline shortcut commands to gecko RTEs
	//contributed by Anti Veeranna (thanks Anti!)
	try {
		var rte = evt.target.id;
		
		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, evt, cmd);
				
				// stop the event bubble
				evt.preventDefault();
				evt.stopPropagation();
			}
	 	}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

//*****************
//IE-Only Functions
//*****************
function evt_ie_keypress(event) {
	try {
		ieKeyPress(event, rte);
	} catch (e) {
		if (debugMode) alert(e);
	}
} 

function ieKeyPress(evt, rte) {
	try {
		var key = (evt.which || evt.charCode || evt.keyCode);
		var stringKey = String.fromCharCode(key).toLowerCase();
		
		//the following breaks list and indentation functionality in IE (don't use)
		//	switch (key) {
		//		case 13:
		//			//insert <br> tag instead of <p>
		//			//change the key pressed to null
		//			evt.keyCode = 0;
		//			
		//			//insert <br> tag
		//			currentRTE = rte;
		//			insertHTML('<br>');
		//			break;
		//	};
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function checkspell(spellID) {
	//function to perform spell check
	//try {
	//	var tmpis = new ActiveXObject("ieSpell.ieSpellExtension");
	//	tmpis.CheckAllLinkedDocuments(document);
	//} catch(e) {
	//	if(e.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 " + e.number);
	//	}
	//}
checkSpellingById(spellID)
}

function raiseButton(e) {
	try {
		var el = window.event.srcElement;
		
		className = el.className;
		if (className == 'rteImage' || className == 'rteImageLowered') {
			el.className = 'rteImageRaised';
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function normalButton(e) {
	try {
		var el = window.event.srcElement;
		
		className = el.className;
		if (className == 'rteImageRaised' || className == 'rteImageLowered') {
			el.className = 'rteImage';
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

function lowerButton(e) {
	try {
		var el = window.event.srcElement;
		
		className = el.className;
		if (className == 'rteImage' || className == 'rteImageRaised') {
			el.className = 'rteImageLowered';
		}
	} catch (e) {
		if (debugMode) alert(e);
	}
}

/****************************************************
* Spell Checker Client JavaScript Code
****************************************************/
// spell checker constants
var spellURL = "/NewsStories/Spell/SpellCheck.aspx";
var showCompleteAlert = true;


var tagGroup = new Array("INPUT", "TEXTAREA", "DIV", "SPAN");
// global elements to check
var checkElements = new Array();

function getText(index)
{
    var oElement = document.getElementById(checkElements[index]);
    var sText = "";

    switch (oElement.tagName)
	{
		case "INPUT" :
        case "TEXTAREA" :
			sText = oElement.value;
			break;
		case "DIV" :
		case "SPAN" :
        case "BODY" :
			sText = oElement.innerHTML;
			break;
        case "IFRAME" :
            var oFrame = eval(oElement.id);
            sText = oFrame.document.body.innerHTML;
    }

    return sText;
}

function setText(index, text)
{
    var oElement = document.getElementById(checkElements[index]);

    switch (oElement.tagName)
	{
		case "INPUT" :
        case "TEXTAREA" :
			oElement.value = text;
			break;
		case "DIV" :
		case "SPAN" :
			oElement.innerHTML = text;
			break;
        case "IFRAME" :
            var oFrame = eval(oElement.id);
            oFrame.document.body.innerHTML = text;
            break;
    }
}

function checkSpelling()
{
    checkElements = new Array();
    //loop through all tag groups
    for (var i = 0; i < tagGroup.length; i++)
    {
        var sTagName = tagGroup[i];
        var oElements = document.getElementsByTagName(sTagName);
        //loop through all elements
        for(var x = 0; x < oElements.length; x++)
        {
            if ((sTagName == "INPUT" && oElements[x].type == "text") || sTagName == "TEXTAREA")
                checkElements[checkElements.length] = oElements[x].id;
            else if ((sTagName == "DIV" || sTagName == "SPAN") && oElements[x].isContentEditable)
                checkElements[checkElements.length] = oElements[x].id;
        }
    }
    openSpellChecker();
}

function checkSpellingById(id)
{
    checkElements = new Array();
    checkElements[checkElements.length] = id;
    openSpellChecker();
}

function checkElementSpelling(oElement)
{
    checkElements = new Array();
    checkElements[checkElements.length] = oElement.id;
    openSpellChecker();
}

function openSpellChecker()
{
    if (window.showModalDialog)
        var result = window.showModalDialog(spellURL + "?Modal=true", window, "dialogHeight:320px; dialogWidth:400px; edge:Raised; center:Yes; help:No; resizable:No; status:No; scroll:No");
    else
        var newWindow = window.open(spellURL, "newWindow", "height=300,width=400,scrollbars=no,resizable=no,toolbars=no,status=no,menubar=no,location=no");
}


/****************************************************
* Spell Checker Suggestion Window JavaScript Code
****************************************************/
var iElementIndex = -1;
var parentWindow;

function initialize()
{
    iElementIndex = parseInt(document.getElementById("ElementIndex").value);

    if (parent.window.dialogArguments)
        parentWindow = parent.window.dialogArguments;
    else if (top.opener)
        parentWindow = top.opener;

    var spellMode = document.getElementById("SpellMode").value;

    switch (spellMode)
    {
        case "start" :
            //do nothing client side
            break;
        case "suggest" :
            //update text from parent document
            updateText();
            //wait for input
            break;
        case "end" :
            //update text from parent document
            updateText();
            //fall through to default
        default :
            //get text block from parent document
            if(loadText())
                document.SpellingForm.submit();
            else
                endCheck()

            break;
    }
}

function loadText()
{
    if (!parentWindow.document)
        return false;

    // check if there is any text to spell check
    for (++iElementIndex; iElementIndex < parentWindow.checkElements.length; iElementIndex++)
    {
        var newText = parentWindow.getText(iElementIndex);
        if (newText.length > 0)
        {
			updateSettings(newText, 0, iElementIndex, "start");
			document.getElementById("StatusText").innerText = "Spell Checking Text ...";
			return true;
        }
    }

    return false;
}

function updateSettings(currentText, wordIndex, elementIndex, mode)
{
    document.getElementById("CurrentText").value = currentText;
    document.getElementById("WordIndex").value = wordIndex;
    document.getElementById("ElementIndex").value = elementIndex;
    document.getElementById("SpellMode").value = mode;
}

function updateText()
{
    if (!parentWindow.document)
        return false;

	var newText = document.getElementById("CurrentText").value;
    parentWindow.setText(iElementIndex, newText);
}

function endCheck()
{
    if (showCompleteAlert)
    	alert("Spell Check Complete");
    closeWindow();
}

function closeWindow()
{
    if (top.opener || parent.window.dialogArguments)
	   self.close();
}

function changeWord(oElement)
{
    var k = oElement.selectedIndex;
    oElement.form.ReplacementWord.value = oElement.options[k].value;
}
