User:Cacycle/editor dev.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* <pre><nowiki> */
///////////// d4d0cc

/*

Comfortable JavaScript editor extension for Wikipedia edit pages by [[User:Cacycle]]
See [[User:Cacycle/Editor]] for a description and [[User:Cacycle/Editor.js]] for this code.
Features include:
* Regular expression search and replace
* Server-independent ''Show preview'' and ''Show changes''
* One-click fixing of common mistakes
* Convert html tables and other markup to wikicode
* Undo/redo
* Input boxes with history
* Fullscreen view
* Find ahead as you type
* Horizontal cursor memory
See [[User:Cacycle/Editor]] for an installation guide.
The program works only for the mozilla browsers Mozilla, Mozilla Firefox, and Mozilla SeaMonkey.
The code is currently under active development and might change rapidly.
This code has been released into the public domain.

*/

var projectHomepage = 'http://en.wikipedia.org/wiki/User:Cacycle/wikEd';

//
// configuration variables
//


// CSS rules edit frame
var frameCSS = frameCSS || [];

frameCSS['body']            = frameCSS['body']           || 'background: #FFFFFF; margin: 0px; padding: 0.2em; overflow: -moz-scrollbars-vertical; overflow-x: auto; font-family: monospace;';

frameCSS['.rteBlock']       = frameCSS['.rteBlock']      || 'background-color: #f0f0f0; display: inline;';
frameCSS['.rteInline']      = frameCSS['.rteInline']     || 'color: #0000e0;';
frameCSS['.rteUnknown']     = frameCSS['.rteUnknown']    || 'background-color: red; color: white; font-weight: bold;';
frameCSS['.rteSub']         = frameCSS['.rteSub']        || 'background-color: #f8f8f8; position: relative; top: 0.3em;';
frameCSS['.rteSup']         = frameCSS['.rteSup']        || 'background-color: #f8f8f8; position: relative; top: -0.3em;';
frameCSS['.rteBold']        = frameCSS['.rteBold']       || 'background-color: #f8f8f8; font-weight: bold;';
frameCSS['.rteComment']     = frameCSS['.rteComment']    || 'background-color: #ffff80;';
frameCSS['.rteDel']         = frameCSS['.rteDel']        || 'background-color: #f8f8f8; text-decoration: line-through;';
frameCSS['.rteIns']         = frameCSS['.rteIns']        || 'background-color: #f8f8f8; text-decoration: underline;';
frameCSS['.rteItalic']      = frameCSS['.rteItalic']     || 'background-color: #f8f8f8; font-style: italic;';
frameCSS['.rteRGB']         = frameCSS['.rteRGB']        || '';

// horizontal rule
frameCSS['.rteHR']          = frameCSS['.rteHR']         || 'background-color: #d0d0d0; display: inline;';
frameCSS['.rteHRInline']    = frameCSS['.rteHRInline']   || 'background-color: #d0d0d0;';

// wiki code
frameCSS['.rteWiki']        = frameCSS['.rteWiki']       || 'color: #0000e0;';
frameCSS['.rteWikiRedir']   = frameCSS['.rteWikiRedir']  || 'color: #0000e0; background-color: #ffffff; font-weight: bold;';

// headings
frameCSS['.rteHeading']     = frameCSS['.rteHeading']    || 'background-color: #f0f0f0; display: inline;';
frameCSS['.rteHeadingWp']   = frameCSS['.rteHeadingWp']  || 'background-color: #ffc0ff; display: inline;';

// tables
frameCSS['.rteTableBlock']  = frameCSS['.rteTableBlock'] || 'background-color: #f0f0f0; display: inline;';
frameCSS['.rteTableLine']   = frameCSS['.rteTableLine']  || 'background-color: #f0f0f0;';
frameCSS['.rteTableTag']    = frameCSS['.rteTableTag']   || 'background-color: #f0f0f0; color: #0000e0;';

// list
frameCSS['.rteListBlock']   = frameCSS['.rteListBlock']  || 'background-color: #f0f0f0; display: inline;';
frameCSS['.rteListLine']    = frameCSS['.rteListLine']   || 'background-color: #f0f0f0';
frameCSS['.rteListTag']     = frameCSS['.rteListTag']    || 'background-color: #f0f0f0; color: #0000e0; font-weight: bold;';

// space-pre
frameCSS['.rteSpaceBlock']  = frameCSS['.rteSpaceBlock'] || 'background-color: #f0f0f0; display: inline;';
frameCSS['.rteSpaceLine']   = frameCSS['.rteSpaceLine']  || 'background-color: #f0f0f0;';
frameCSS['.rteSpaceTag']    = frameCSS['.rteSpaceTag']   || 'color: #0000e0; font-weight: bold;';

// wiki links, images, categories, templates
frameCSS['.rteLinkTag']     = frameCSS['.rteLinkTag']    || 'color: #0000e0;';

frameCSS['.rteLink']        = frameCSS['.rteLink']       || '';
frameCSS['.rteImage']       = frameCSS['.rteImage']      || 'background-color: #d5ffaa;';
frameCSS['.rteCat']         = frameCSS['.rteCat']        || 'background-color: #ffe0ff;';
frameCSS['.rteTempl']       = frameCSS['.rteTempl']      || 'background-color: #ffe0ff;';

// interlanguage
frameCSS['.rteInter']       = frameCSS['.rteInter']      || '';
frameCSS['.rteLinkInter']   = frameCSS['.rteLinkInter']  || 'background-color: #ffc0ff;';
frameCSS['.rteImageInter']  = frameCSS['.rteImageInter'] || 'background-color: #bfff80;';
frameCSS['.rteCatInter']    = frameCSS['.rteCatInter']   || 'background-color: #ffc0ff;';
frameCSS['.rteTemplInter']  = frameCSS['.rteTemplInter'] || 'background-color: #ffc0ff;';

// name
frameCSS['.rteLinkName']    = frameCSS['.rteLinkName']   || 'color: #f00000; font-weight: bold;';
frameCSS['.rteImageName']   = frameCSS['.rteImageName']  || 'font-weight: bold;';
frameCSS['.rteCatName']     = frameCSS['.rteCatName']    || 'color: #f00000; font-weight: bold;';
frameCSS['.rteTemplName']   = frameCSS['.rteTemplName']  || 'color: #f00000; font-weight: bold;';
frameCSS['.rteURLLink']     = frameCSS['.rteURLLink']    || 'color: #f00000; font-weight: bold;';

// text and parameters
frameCSS['.rteLinkText']    = frameCSS['.rteLinkText']   || 'font-weight: bold;';
frameCSS['.rteImageText']   = frameCSS['.rteImageText']  || '';
frameCSS['.rteCatText']     = frameCSS['.rteCatText']    || '';
frameCSS['.rteTemplText']   = frameCSS['.rteTemplText']  || '';
frameCSS['.rteURLText']     = frameCSS['.rteURLText']    || 'font-weight: bold;';

// insert wikicode here
frameCSS['.rteInsertHere']  = frameCSS['.rteInsertHere'] || 'background-color: orange; font-style: italic;';


// CSS rules main window
var mainCSS = mainCSS || [];
mainCSS['.customEdit']         = mainCSS['.customEdit']        || 'font-size: smaller; padding-left: 0.1em; padding-right: 0.1em; margin-left: 0.1em; margin-right: 0.1em; height: 1.6em; vertical-align: bottom;';
mainCSS['.previewBox']         = mainCSS['.previewBox']        || 'background-color: #f9f9f9;';

mainCSS['.rteButtonsFormat']   = mainCSS['.rteButtonsFormat']  || 'background: #d4d0cc; border: 1px black solid; padding: 0.1em; margin: 0.2em 0.75em 0 0; float: left;';
mainCSS['.rteButtonsFind']     = mainCSS['.rteButtonsFind']    || 'background: #d4d0cc; border: 1px black solid; padding: 0.1em; margin: 0.2em 0.75em 0 0; float: left;';
mainCSS['.rteButtonsWindow']   = mainCSS['.rteButtonsWindow']  || 'background: #d4d0cc; border: 1px black solid; padding: 0.1em; margin: 0.2em 0 0 0.75em; float: right;';
mainCSS['.rteButtonsFix']      = mainCSS['.rteButtonsFix']     || 'background: #d4d0cc; border: 1px black solid; padding: 0.1em; margin: 0.2em 0.75em 0.5em 0; float: left';

mainCSS['.rteButton']          = mainCSS['.rteButton']         || 'vertical-align: top; margin: 1px 2px; padding: 0; background: #d4d0cc; border: 1px #d4d0cc solid; cursor: pointer;';
mainCSS['.rteButton:hover']    = mainCSS['.rteButton:hover']   || 'background: #e4e0dd; border: 1px outset; cursor: pointer;';
mainCSS['.rteButton:active']   = mainCSS['.rteButton:active']  || 'background: #e4e0dc; border: 1px inset;  cursor: pointer;';
mainCSS['.rteButtonFloat']     = mainCSS['.rteButtonFloat']    || 'background: #d4d0cc; border: 1px outset; cursor: pointer; display: none; position: absolute; z-index: 5;';
mainCSS['.rteButtonChecked']   = mainCSS['.rteButtonChecked']  || 'vertical-align: top; margin: 1px 2px; padding: 0; background: #f8f8f8; border: 1px inset;  cursor: pointer;';
mainCSS['.rteButtonUnchecked'] = mainCSS['.rteButtonUnchecked']|| 'vertical-align: top; margin: 1px 2px; padding: 0; background: #e4e0dd; border: 1px outset; cursor: pointer;';

// levels of undo (each level holds the whole text)
var undoBufferMax = undoBufferMax || 20;

// history length for summary, find and replace fields
var findHistoryLength = findHistoryLength || 10;

// presets for input field dropdown options, {using} appends a link to this script
var presetOptions = presetOptions || [];
presetOptions['summary'] = presetOptions['summary'] || [
	'Copyedit {using}',
	'Linkfix',
	'Reverting vandalism',
	'Formatting source text {using}'
];

// text for summary link to this script
var summaryUsing = summaryUsing || '(Using [[User:Cacycle/editor.js|editor]])';

// expiration time span for history cookies in seconds
var cookieExpireSec = cookieExpireSec || (365 * 24 * 60 * 60);

// enable cursor horizontal position memory
var cursorMemory = cursorMemory || true;

// show at least this number of lines ahead of cursor movement
var scrollMargin = scrollMargin || 1;

// show at least this number of lines ahead of cursor movement for
var findMargin = findMargin || 2;

// find ahead checkbox selected by default
var findAheadSelected = findAheadSelected || true;

// highlight syntax
var highlightSyntax = highlightSyntax || true;

// use classic
var useClassic = useClassic || true;

// image source (button images)
var imagesPath = imagesPath || 'file:///C:/rte/images/';
var image = image || [];
image['logo']             = image['logo']             || imagesPath + 'JSEditor_logo.png';

image['undo']             = image['undo']             || imagesPath + 'JSEditor_undo.png';
image['redo']             = image['redo']             || imagesPath + 'JSEditor_redo.png';
image['jump_prev']        = image['jump_prev']        || imagesPath + 'JSEditor_jump_prev.png';
image['jump_next']        = image['jump_next']        || imagesPath + 'JSEditor_jump_next.png';
image['bold']             = image['bold']             || imagesPath + 'JSEditor_bold.png';
image['italic']           = image['italic']           || imagesPath + 'JSEditor_italic.png';
image['underline']        = image['underline']        || imagesPath + 'JSEditor_underline.png';
image['superscript']      = image['superscript']      || imagesPath + 'JSEditor_superscript.png';
image['subscript']        = image['subscript']        || imagesPath + 'JSEditor_subscript.png';
image['case']             = image['case']             || imagesPath + 'JSEditor_case.png';
image['decrease_heading'] = image['decrease_heading'] || imagesPath + 'JSEditor_decrease_heading.png';
image['increase_heading'] = image['increase_heading'] || imagesPath + 'JSEditor_increase_heading.png';
image['bullet_list']      = image['bullet_list']      || imagesPath + 'JSEditor_bullet_list.png';
image['number_list']      = image['number_list']      || imagesPath + 'JSEditor_number_list.png';
image['indent_list']      = image['indent_list']      || imagesPath + 'JSEditor_indent_list.png';
image['definition_list']  = image['definition_list']  || imagesPath + 'JSEditor_definition_list.png';
image['wikilink']         = image['wikilink']         || imagesPath + 'JSEditor_wikilink.png';
image['weblink']          = image['weblink']          || imagesPath + 'JSEditor_weblink.png';
image['image']            = image['image']            || imagesPath + 'JSEditor_image.png';
image['table']            = image['table']            || imagesPath + 'JSEditor_table.png';
image['undo_all']         = image['undo_all']         || imagesPath + 'JSEditor_undo_all.png';
image['redo_all']         = image['redo_all']         || imagesPath + 'JSEditor_redo_all.png';
image['align_top']        = image['align_top']        || imagesPath + 'JSEditor_align_top.png';
image['align_buttons']    = image['align_buttons']    || imagesPath + 'JSEditor_align_buttons.png';
image['fullscreen']       = image['fullscreen']       || imagesPath + 'JSEditor_fullscreen.png';
image['source']           = image['source']           || imagesPath + 'JSEditor_source.png';
image['get_selection']    = image['get_selection']    || imagesPath + 'JSEditor_get_selection.png';
image['find_prev']        = image['find_prev']        || imagesPath + 'JSEditor_find_prev.png';
image['find_next']        = image['find_next']        || imagesPath + 'JSEditor_find_next.png';
image['jump_top_bottom']  = image['jump_top_bottom']  || imagesPath + 'JSEditor_jump_top_bottom.png';
image['replace_all']      = image['replace_all']      || imagesPath + 'JSEditor_replace_all.png';
image['replace_prev']     = image['replace_prev']     || imagesPath + 'JSEditor_replace_prev.png';
image['replace_next']     = image['replace_next']     || imagesPath + 'JSEditor_replace_next.png';
image['preview']          = image['preview']          || imagesPath + 'JSEditor_preview.png';
image['diff']             = image['diff']             || imagesPath + 'JSEditor_diff.png';
image['close']            = image['close']            || imagesPath + 'JSEditor_close.png';
image['syntax']           = image['syntax']           || imagesPath + 'JSEditor_syntax.png';
image['wikify']           = image['wikify']           || imagesPath + 'JSEditor_wikify.png';

image['fix_units']        = image['fix_units']        || imagesPath + 'JSEditor_fix_units.png';
image['fix_math']         = image['fix_math']         || imagesPath + 'JSEditor_fix_math.png';
image['fix_dash']         = image['fix_dash']         || imagesPath + 'JSEditor_fix_dash.png';
image['fix_pipes']        = image['fix_pipes']        || imagesPath + 'JSEditor_fix_pipes.png';
image['fix_punct']        = image['fix_punct']        || imagesPath + 'JSEditor_fix_punct.png';
image['fix_html']         = image['fix_html']         || imagesPath + 'JSEditor_fix_html.png';
image['fix_basic']        = image['fix_basic']        || imagesPath + 'JSEditor_fix_basic.png';
image['fix_caps']         = image['fix_caps']         || imagesPath + 'JSEditor_fix_caps.png';
image['fix_all']          = image['fix_all']          || imagesPath + 'JSEditor_fix_all.png';

image['regexp']           = image['regexp']           || imagesPath + 'JSEditor_regexp.png';
image['case_sensitive']   = image['case_sensitive']   || imagesPath + 'JSEditor_case_sensitive.png';
image['find_ahead']       = image['find_ahead']       || imagesPath + 'JSEditor_find_ahead.png';

image['classic']          = image['classic']          || imagesPath + 'JSEditor_classic.png';


// global variables


// history
var fieldHist = [];
var cookieName = [];
var inputElement = [];
var selectElement = [];

var checkMarker = [];
checkMarker[true] = '\u2022';
checkMarker[false] = '\u22c5';

// undo
var undoBuffer = new Array(undoBufferMax);
var undoBufferSelStart = new Array(undoBufferMax);
var undoBufferSelEnd = new Array(undoBufferMax);
var undoBufferFirst = 0;
var undoBufferLast = 0;
var undoBufferCurr = 0;

var editformOrig = '';
var editformLast = null;

// fullscreen
var normalTextareaWidth;
var normalTextareaHeight;
var normalTextareaMargin;
var normalTextareaRows;
var normalPageXOffset;
var normalPageYOffset;
var normalTreePos = {};
var fullScreenMode = false;
var fullButtonValue = 'Full screen';
var fullButtonTitle = 'Full screen editing mode';
var normalButtonValue = 'Normal view';
var normalButtonTitle = 'Back no normal page view';
var normalFloatButtonValue = 'Back';

// textarea text info object
var textRows = new Object();
textRows.lineStart = [];
textRows.lineLength = [];
textRows.rowStart = [];
textRows.rowLength = [];

var textareaObj = {};
var frameBody = {};
var frameDocument = {};
var frameWindow = {};

var lastChangePos;

// counters
var i;
var j;


// load the editor after page loading
if (window.addOnloadHook != null) {
	addOnloadHook(SetupEditor);
}


//
// setup routine for javascript editor
//

function SetupEditor() {

	var html = '';

// check if the editor is already installed
	if (document.getElementById('findText') != null) { return; }

// at the moment this works only for mozilla browsers (Mozilla, Mozilla Firefox, Mozilla SeaMonkey)
	if (navigator.appName == null) { return; }
	var nameMatch = navigator.appName.match(/Netscape/i);
	if (nameMatch == null) { return; }
	var name = nameMatch[0];
	if ( (name == null) || (name == '') ) { return; }
	var version = navigator.appVersion.match(/\d+(\.\d+)/)[0];
	if (version == null) { return; }
	if (version < 5.0) { return; }

// insert logo at top
  var logoImg = document.createElement('img');
  logoImg.src = image['logo'];
  logoImg.alt = 'WikEd';

  var logoLink = document.createElement('a');
  logoLink.href = projectHomepage;
  logoLink.title = 'WikEd Homepage';
  logoLink.appendChild(logoImg);

//	var logoText = document.createTextNode('WikEd');
//	logoLink.appendChild(logoText);

  var listObj = document.createElement('li');
  listObj.id = 'wikEdLogo';
  listObj.appendChild(logoLink);

  var personalTools = document.getElementById('p-personal').getElementsByTagName('ul')[0];
  personalTools.appendChild(listObj);

// get the textarea object
	textareaObj = document.getElementById('wpTextbox1');
	if (textareaObj == null) { return; }

// setup the undo buffers and get the original text for instant change view
	editformOrig = textareaObj.value;

// set textarea size to maximal row number, always show vertical scrollbar
  textareaObj.style.overflow = '-moz-scrollbars-vertical';
  textareaObj.style.overflowX = 'auto';

// convert strange spaces, remove non-\n linebreak characters
	ConvertStrangeSpaces();

// add stylesheet definitions
	var mainStyle = new StyleSheet();
	for (var rule in mainCSS) {
		mainStyle.addRule(rule, mainCSS[rule]);
	}

// create inputWrapper for textarea and buttons (fullscreen elements)
	var inputWrapper = document.createElement('div');
	inputWrapper.id = 'inputWrapper';
	textareaObj.parentNode.insertBefore(inputWrapper, textareaObj);

// move textareaObj to textareaWrapper
	var textareaWrapper = document.createElement('div');
	textareaWrapper.id = 'textareaWrapper';
	inputWrapper.appendChild(textareaWrapper);
	textareaWrapper.appendChild(textareaObj);

// add all other buttons and inputs to buttonsWrapper
	var buttonsWrapper = document.createElement('div');
	buttonsWrapper.id = 'buttonsWrapper';
	inputWrapper.appendChild(buttonsWrapper);

// add custom formatting buttons
	var customEditButtons = document.createElement('div');
	customEditButtons.id = 'customEditButtons';
	html = '';

// format buttons
	html += '<div class="rteButtonsFormat">';

	html += '<img class="rteButton" src="' + image['undo']             + '" width="16" height="16" alt="Undo"             title="Undo"                            onClick="javascript:Edit(\'undo\');">';
	html += '<img class="rteButton" src="' + image['redo']             + '" width="16" height="16" alt="Redo"             title="Redo"                            onClick="javascript:Edit(\'redo\');">';

	html += '<img class="rteButton" src="' + image['bold']             + '" width="16" height="16" alt="Bold"             title="Bold text"                       onClick="javascript:Edit(\'bold\');">';
	html += '<img class="rteButton" src="' + image['italic']           + '" width="16" height="16" alt="Italic"           title="Italic text"                     onClick="javascript:Edit(\'italic\');">';
	html += '<img class="rteButton" src="' + image['underline']        + '" width="16" height="16" alt="Underline"        title="Underline text"                  onClick="javascript:Edit(\'underline\');">';
	html += '<img class="rteButton" src="' + image['superscript']      + '" width="16" height="16" alt="Superscript"      title="Superscript text"                onClick="javascript:Edit(\'sup\');">';
	html += '<img class="rteButton" src="' + image['subscript']        + '" width="16" height="16" alt="Subscript"        title="Subscript text"                  onClick="javascript:Edit(\'sub\');">';
	html += '<img class="rteButton" src="' + image['case']             + '" width="16" height="16" alt="Casing"           title="Toggle between lowercase, uppercase first letters, and uppercase" onClick="javascript:Edit(\'case\');">';

	html += '<img class="rteButton" src="' + image['undo_all']         + '" width="16" height="16" alt="Undo all"         title="Undo all changes"                onClick="javascript:Edit(\'undoall\');">';
	html += '<img class="rteButton" src="' + image['redo_all']         + '" width="16" height="16" alt="Redo all"         title="Redo all changes"                onClick="javascript:Edit(\'redoall\');">';
	html += '<img class="rteButton" src="' + image['syntax']           + '" width="16" height="16" alt="Syntax"           title="Update syntax highlighting"      onClick="javascript:Edit(\'update_syntax\');">';

	html += '<br>';

	html += '<img class="rteButton" src="' + image['wikilink']         + '" width="16" height="16" alt="Link"             title="Wiki link"                       onClick="javascript:Edit(\'wikilink\');">';
	html += '<img class="rteButton" src="' + image['weblink']          + '" width="16" height="16" alt="Weblink"          title="External Weblink"                onClick="javascript:Edit(\'weblink\');">';

	html += '<img class="rteButton" src="' + image['decrease_heading'] + '" width="16" height="16" alt="Heading-"         title="Decrease heading levels"         onClick="javascript:Edit(\'headingless\');">';
	html += '<img class="rteButton" src="' + image['increase_heading'] + '" width="16" height="16" alt="Heading+"         title="Increase heading levels"         onClick="javascript:Edit(\'headingmore\');">';

	html += '<img class="rteButton" src="' + image['bullet_list']      + '" width="16" height="16" alt="Bullet list"      title="Bulleted list"                   onClick="javascript:Edit(\'bulletlist\');">';
	html += '<img class="rteButton" src="' + image['number_list']      + '" width="16" height="16" alt="Number list"      title="Numbered list"                   onClick="javascript:Edit(\'numberlist\');">';
	html += '<img class="rteButton" src="' + image['indent_list']      + '" width="16" height="16" alt="Indent list"      title="Indented list"                   onClick="javascript:Edit(\'indentlist\');">';
	html += '<img class="rteButton" src="' + image['definition_list']  + '" width="16" height="16" alt="Def list"         title="Definition list"                 onClick="javascript:Edit(\'deflist\');">';

	html += '<img class="rteButton" src="' + image['image']            + '" width="16" height="16" alt="Image"            title="Image"                           onClick="javascript:Edit(\'image\');">';
	html += '<img class="rteButton" src="' + image['table']            + '" width="16" height="16" alt="Table"            title="Table"                           onClick="javascript:Edit(\'table\');">';

	html += '<img class="rteButton" src="' + image['wikify']           + '" width="16" height="16" alt="Wikify"           title="Wikify pasted content"           onClick="javascript:Edit(\'wikify\');">';

	html += '</div>';

// window functions: syntax highlighting, classic textarea, align, fullscreen
	html += '<div class="rteButtonsWindow">';

	html += '<img src="'                          + image['syntax']        + '" width="16" height="16" alt="Syntax"             title="Highlight syntax"                                id="syntax"          onClick="javascript:Button(\'syntax\');"  onLoad="javascript:Button(\'syntax\', highlightSyntax);" >';
	html += '<img src="'                          + image['classic']       + '" width="16" height="16" alt="Classic"            title="Switch between classic and rich-text edit area"  id="classic"         onClick="javascript:Button(\'classic\');" onLoad="javascript:Button(\'classic\', useClassic);" >';

	html += '<img class="rteButton" src="'        + image['source']        + '" width="16" height="16" alt="Source"             title="Show the source code for testing purposes"       id="source"          onClick="javascript:Button(\'source\');"  onLoad="javascript:Button(\'source\', false);" >';

	html += '<img class="rteButtonFloat" src="'   + image['fullscreen']    + '" width="16" height="16" alt="Fullscreen"         title="Switch to fullscreen mode"                       id="fullScreenButtonFloat" onClick="javascript:Button(\'fullScreenButtonFloat\');" onLoad="javascript:Button(\'fullScreenButtonFloat\', false);" >';
	html += '<img class="rteButton" src="'        + image['fullscreen']    + '" width="16" height="16" alt="Fullscreen"         title="Switch to fullscreen mode"                       id="fullScreenButton"      onClick="javascript:Button(\'fullScreenButton\');" onLoad="javascript:Button(\'fullScreenButton\', false);" >';

	html += '<img src="'                          + image['align_buttons'] + '" width="16" height="16" alt="Scroll to buttons"  title="Scroll to edit buttons"                          id="scrolltobuttons" onClick="javascript:Button(\'scrolltobuttons\');" onLoad="javascript:Button(\'scrolltobuttons\', \'rteButton\');" >';
	html += '<img src="'                          + image['align_top']     + '" width="16" height="16" alt="Scroll to textarea" title="Scroll to textarea"                              id="scrolltotop"     onClick="javascript:Button(\'scrolltotop\');" onLoad="javascript:Button(\'scrolltotop\', \'rteButton\');" >';
	html += '</div>';

// find / replace buttons

	html += '<div class="rteButtonsFind">';

	html += '<img class="rteButton" src="' + image['get_selection']    + '" width="16" height="16" alt="Get find"         title="Copy selection to find field (double click: copy selection to find and to replace field)"    onClick="javascript:Edit(\'getfind\');" ondblclick="javascript:Edit(\'getfindreplace\');">';
	html += '<img class="rteButton" src="' + image['find_prev']        + '" width="16" height="16" alt="Find prev"        title="Find previous match"             onClick="javascript:Edit(\'findprev\');">';

	html += '<span style="position: relative; padding: 0; margin: 0 0.2em;" id="findComboInput">';
	html += '<input class="customEdit" type="text" value="" style="height: 1.4em; font-family: monospace; height: 1.2em; padding: 0; margin: 0; position: absolute; left: 0; top: 0; z-index: 2;" onfocus="javascript:this.setSelectionRange(0, this.textLength);" id="findText" title="">';
	html += '<select class="customEdit" id="findSelect" style="height: 1.5em; font-family: monospace; border: none; padding: 0; margin: 0; position: relative; vertical-align: baseline; z-index: 1;" onfocus="javascript:SetComboOptions(\'find\')" onChange="javascript:ChangeComboInput(\'find\');">';
	html += '</select>';
	html += '</span>';

	html += '<img class="rteButton" src="' + image['find_next']        + '" width="16" height="16" alt="Find next"        title="Find next match"                 onClick="javascript:Edit(\'findnext\');">';
	html += '<img class="rteButton" src="' + image['jump_top_bottom']  + '" width="16" height="16" alt="Jump up/down"     title="Jump to the top / bottom"        onClick="javascript:Edit(\'updown\');">';
	html += '<img class="rteButton" src="' + image['jump_prev']        + '" width="16" height="16" alt="Jump prev"        title="Jump to previous position"       onClick="javascript:Edit(\'lastchangepos\');">';
	html += '<img class="rteButton" src="' + image['jump_next']        + '" width="16" height="16" alt="Jump next"        title="Jump to next position"           onClick="javascript:Edit(\'nextchangepos\');">';

	html += '<br>';

	html += '<img class="rteButton" src="' + image['replace_all']      + '" width="16" height="16" alt="Replace all"      title="Replace all matches in whole text or selection"  onClick="javascript:Edit(\'replaceall\');">';
	html += '<img class="rteButton" src="' + image['replace_prev']     + '" width="16" height="16" alt="Replace prev"     title="Replace previous match"          onClick="javascript:Edit(\'replaceprev\');">';

	html += '<span style="position: relative; padding: 0; margin: 0 0.2em;" id="replaceComboInput">';
	html += '<input class="customEdit" type="text" value="" style="height: 1.4em; font-family: monospace; height: 1.2em; padding: 0; margin: 0; position: absolute; left: 0; top: 0; z-index: 2;" onfocus="this.setSelectionRange(0, this.textLength);" id="replaceText" title="">';
	html += '<select class="customEdit" id="replaceSelect" style="height: 1.5em; font-family: monospace; border: none; padding: 0; margin: 0; position: relative; vertical-align: baseline; z-index: 1;" onfocus="SetComboOptions(\'replace\')" onChange="javascript:ChangeComboInput(\'replace\');">';
	html += '</select>';
	html += '</span>';

	html += '<img class="rteButton" src="' + image['replace_next']     + '" width="16" height="16" alt="Replace next"    title="Replace next match"              onClick="javascript:Edit(\'replacenext\');">';

	html += '<img src="'                   + image['case_sensitive']   + '" width="16" height="16" alt="Case sensitive"  title="Search is case sensitive"                               id="caseSensitive" onClick="javascript:Button(\'caseSensitive\');" onLoad="javascript:Button(\'caseSensitive\', false);" >';
	html += '<img src="'                   + image['regexp']           + '" width="16" height="16" alt="RegExp"          title="Search field is a regular expression"                   id="regExp"        onClick="javascript:Button(\'regExp\');" onLoad="javascript:Button(\'regExp\', false);" >';
	html += '<img src="'                   + image['find_ahead']       + '" width="16" height="16" alt="Find ahead"      title="Find ahead as you type (only for non-regexp searches)"  id="findAhead"     onClick="javascript:Button(\'findAhead\');" onLoad="javascript:Button(\'findAhead\', findAheadSelected);" >';

	html += '</div>';

// fixing buttons
	html += '<div class="rteButtonsFix">';

	html += '<img class="rteButton" src="' + image['fix_basic']        + '" width="16" height="16" alt="Fix basic"        title="Fix blanks and empty lines, always done by other fixing functions"  onClick="javascript:Edit(\'fixbasic\');">';

	html += '<img class="rteButton" src="' + image['fix_pipes']        + '" width="16" height="16" alt="Fix pipes"        title="Fix blanks around vertical bars"    onClick="javascript:Edit(\'fixpipes\');">';
	html += '<img class="rteButton" src="' + image['fix_punct']        + '" width="16" height="16" alt="Fix puntuation"   title="Fix spaces before puntuation"       onClick="javascript:Edit(\'fixpunct\');">';
	html += '<img class="rteButton" src="' + image['fix_math']         + '" width="16" height="16" alt="Fix math"         title="Fix _math, DO NOT USE ON WHOLE TEXT OR <math></math> WIKICODE"  onClick="javascript:Edit(\'fixmath\');">';

	html += '<img class="rteButton" src="' + image['fix_units']        + '" width="16" height="16" alt="Fix units"        title="Fix units"                          onClick="javascript:Edit(\'fixunits\');">';
	html += '<img class="rteButton" src="' + image['fix_dash']         + '" width="16" height="16" alt="Fix dashes"       title="Fix dashes"                         onClick="javascript:Edit(\'fixdashes\');">';

	html += '<img class="rteButton" src="' + image['fix_html']         + '" width="16" height="16" alt="Fix html"         title="Fix html to wikicode"               onClick="javascript:Edit(\'fixhtml\');">';
	html += '<img class="rteButton" src="' + image['fix_caps']         + '" width="16" height="16" alt="Fix caps"         title="Fix caps in headers and lists"      onClick="javascript:Edit(\'fixcaps\');">';
	html += '<img class="rteButton" src="' + image['fix_all']          + '" width="16" height="16" alt="Fix all"          title="Fix units, dashes, html, and caps"  onClick="javascript:Edit(\'fixall\');">';

	html += '</div>';

	html += '<br style="clear: both">';

	customEditButtons.innerHTML = html;
	buttonsWrapper.appendChild(customEditButtons);

// add elements to buttonsWrapper
	var element = document.getElementById('editpage-copywarn');
	while (element != null) {
		if (element.id == 'editpage-specialchars') {
			break;
		}
		next_element = element.nextSibling;
		buttonsWrapper.appendChild(element);
		element = next_element;
	}

// add preview box upper buttons
	var previewBoxButtons = document.createElement('div');
	previewBoxButtons.id = 'previewBoxButtons';
	previewBoxButtons.className = 'rteButtonsWindow';

	html = '';
	html += '<img src="' + image['preview']       + '" width="16" height="16" alt="Preview"        title="Instant preview"        id="preview"          onClick="javascript:Button(\'preview\');"          onLoad="javascript:Button(\'preview\', \'rteButton\');" >';
	html += '<img src="' + image['diff']          + '" width="16" height="16" alt="Changes"        title="Instant changes"        id="diff"             onClick="javascript:Button(\'diff\');"             onLoad="javascript:Button(\'diff\', \'rteButton\');" >';
	html += '<img src="' + image['close']         + '" width="16" height="16" alt="Close"          title="Close instant window"   id="close"            onClick="javascript:Button(\'close\');"            onLoad="javascript:Button(\'close\', \'rteButton\');" >';
	html += '<img src="' + image['align_buttons'] + '" width="16" height="16" alt="Scroll buttons" title="Scroll to edit buttons" id="scrolltobuttons2" onClick="javascript:Button(\'scrolltobuttons2\');" onLoad="javascript:Button(\'scrolltobuttons2\', \'rteButton\');" >';
	html += '<img src="' + image['align_top']     + '" width="16" height="16" alt="Scroll top"     title="Scroll to textarea"     id="scrolltotop2"     onClick="javascript:Button(\'scrolltotop2\');"     onLoad="javascript:Button(\'scrolltotop2\', \'rteButton\');" >';

	previewBoxButtons.innerHTML = html;
	document.getElementById('wpSave').parentNode.insertBefore(previewBoxButtons, document.getElementById('wpSave').parentNode.firstChild);

// add preview box
	var previewBox = document.createElement('div');
	previewBox.id = 'customPreviewBox';
	previewBox.style.display = 'none';

	html = '';
	html += '<div style="clear: both; margin-top: 0.5em; margin-bottom: 0; border-width: 1px; border-style: solid; border-color: #808080 #d0d0d0 #d0d0d0 #808080;" id="PreviewBoxOutline">';
	html += '<div class="previewBox" style="padding: 5px; border-width: 1px; border-style: solid; border-color: #404040 #ffffff #ffffff #404040;" id="PreviewBox">';
	html += '</div>';
	html += '</div>';

	html += '<div class="rteButtonsWindow">';
	html += '<img src="' + image['preview']       + '" width="16" height="16" alt="Preview"        title="Instant preview"        id="preview2"         onClick="javascript:Button(\'preview2\');"         onLoad="javascript:Button(\'preview2\', \'rteButton\');" >';
	html += '<img src="' + image['diff']          + '" width="16" height="16" alt="Changes"        title="Instant changes"        id="diff2"            onClick="javascript:Button(\'diff2\');"            onLoad="javascript:Button(\'diff2\', \'rteButton\');" >';
	html += '<img src="' + image['close']         + '" width="16" height="16" alt="Close"          title="Close instant window"   id="close2"           onClick="javascript:Button(\'close2\');"           onLoad="javascript:Button(\'close2\', \'rteButton\');" >';
	html += '<img src="' + image['align_buttons'] + '" width="16" height="16" alt="Scroll buttons" title="Scroll to edit buttons" id="scrolltobuttons3" onClick="javascript:Button(\'scrolltobuttons3\');" onLoad="javascript:Button(\'scrolltobuttons3\', \'rteButton\');" >';
	html += '<img src="' + image['align_top']     + '" width="16" height="16" alt="Scroll top"     title="Scroll to textarea"     id="scrolltotop3"     onClick="javascript:Button(\'scrolltotop3\');"     onLoad="javascript:Button(\'scrolltotop3\', \'rteButton\');" >';
	html += '</div>';

	previewBox.innerHTML = html;
	inputWrapper.parentNode.insertBefore(previewBox, inputWrapper.nextSibling);

// move linebreak before checkboxes down
	var summary = document.getElementById('wpSummary');
	var checkboxSep = document.createTextNode('');
	summary.parentNode.replaceChild(checkboxSep, summary.nextSibling);

// move 'Summary:' into submit button div
	var summary = document.getElementById('wpSummary');
	var summaryLabel = document.getElementById('wpSummaryLabel');
	summary.parentNode.insertBefore(summaryLabel, summary.parentNode.firstChild);
	summary.parentNode.style.marginTop = '0.25em';

// make the summary a combo box
	var summary = document.getElementById('wpSummary');
	var htmlPre = '';
	var htmlPost = '';
	html = '';
	htmlPre  += ' <span style="position: relative;" id="summaryComboInput">';
	html     += ' style="padding: 0; margin: 0; position: absolute; left: 0; top: 0; z-index: 2;" onfocus="this.setSelectionRange(0, this.textLength);"';
	htmlPost += '<select style="border: none; padding: 0; margin: 0; position: relative; vertical-align: middle; z-index: 1;" id="wpSummarySelect" onfocus="javascript:SetComboOptions(\'summary\')" onchange="javascript:ChangeComboInput(\'summary\');">';
	htmlPost += '</select>';
	htmlPost += '</span>';
	summary.parentNode.innerHTML = summary.parentNode.innerHTML.replace(/\s*(<input.*?id\=\"wpSummary\")(.*?>)/, htmlPre + '$1' + html + '$2' + htmlPost);

// add margin around submit buttons
	var saveButton = document.getElementById('wpSave');
	saveButton.parentNode.style.marginTop = '0.5em';
	saveButton.parentNode.style.marginBottom = '0.5em';

// move copywarn down
	var copywarn = document.getElementById('editpage-copywarn');
	inputWrapper.parentNode.insertBefore(copywarn, previewBox.nextSibling);

// shorten submit button texts and add onclick handler
	document.getElementById('wpPreview').value = 'Preview';
	document.getElementById('wpDiff').value = 'Changes';

// onsubmit handler
	window.onsubmit = function() {

 	  var obj = {};
		obj.html = frameBody.innerHTML;

// wikify the text
		WikifyHTML(obj);
		frameBody.innerHTML = obj.html;

// add summary to summary history
		AddToHistory('summary');

	};

// set up combo input boxes with history
	fieldHist ['find'] = [];
	cookieName['find'] = 'findHistory';
	inputElement['find'] = new Object(document.getElementById('findText'));
	selectElement['find'] = new Object(document.getElementById('findSelect'));
	selectElement['find'].style.height = (inputElement['find'].clientHeight + 1) +'px';

	fieldHist ['replace'] = [];
	cookieName['replace'] = 'replaceHistory';
	inputElement['replace'] = new Object(document.getElementById('replaceText'));
	selectElement['replace'] = new Object(document.getElementById('replaceSelect'));
	selectElement['replace'].style.height = (inputElement['replace'].clientHeight + 1) +'px';

	fieldHist ['summary'] = [];
	cookieName['summary'] = 'summaryHistory';
	inputElement['summary'] = new Object(document.getElementById('wpSummary'));
	selectElement['summary'] = new Object(document.getElementById('wpSummarySelect'));
	selectElement['summary'].style.height = (inputElement['summary'].clientHeight + 1) +'px';

	ResizeComboInput('find');
	ResizeComboInput('replace');
	ResizeComboInput('summary');

// setup fullscreen mode

// save textbox properties
	normalTextareaWidth = GetStyle(textareaObj, 'width');
	normalTextareaHeight = GetStyle(textareaObj, 'height');
	normalTextareaMargin = GetStyle(textareaObj, 'margin');
	normalTextareaRows = textareaObj.rows;

// set fullscreen style fixes
	var inputWrapper = document.getElementById('inputWrapper');
	var content = document.getElementById('content');
	var content = document.getElementById('content');
	inputWrapper.style.lineHeight = GetStyle(content, 'line-height');

// move globalWrapper elements to new subGlobalWrapper
	var globalWrapper = document.getElementById('globalWrapper');
	var subGlobalWrapper = document.createElement('div');
	subGlobalWrapper.id = 'subGlobalWrapper';
	globalWrapper.appendChild(subGlobalWrapper);
	var element = globalWrapper.firstChild;
	while (element != null) {
		if (element.id == 'subGlobalWrapper') {
			break;
		}
		next_element = element.nextSibling;
		subGlobalWrapper.appendChild(element);
		element = next_element;
	}

// create iframe

// moving an iframe in design mode may crash seamonkey
	var frameWrapper = document.createElement('div');
	frameWrapper.id = 'frameWrapper';

	var frameId = 'frameBody';
	var width = 800;
	var height = 400;

  html = '';
	html += '<div id="frameBorderOuter" style="width: 100%; margin-top: 0.5em; margin-bottom: 0.1em; border-width: 1px; border-style: solid; border-color: #808080 #d0d0d0 #d0d0d0 #808080;">';
	html += '<div id="frameBorderInner" style="padding: 0; border-width: 1px; border-style: solid; border-color: #404040 #ffffff #ffffff #404040;">';
	html += '</div>';
	html += '<iframe id="' + frameId + '" name="' + frameId + '" style="width: 100%; height: 20em; padding: 0; margin: 0; border: none;"></iframe>';
	html += '</div>';
	html += '</div>';

	html += '<iframe width="154" height="104" id="cp' + frameId + '" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;"></iframe>';
	html += '<input type="hidden" id="hdn' + frameId + '" name="' + frameId + '" value="">';

  frameWrapper.innerHTML = html;
  inputWrapper.insertBefore(frameWrapper, textareaWrapper);

	frameWindow = document.getElementById(frameId).contentWindow;
	frameDocument = frameWindow.document;

// fill the frame with content
	html = '';
	html += '<html><head></head>';
	html += '<body id="frameBody" onload="window.document.designMode = \'on\'; window.document.execCommand(\'styleWithCSS\', false, false);">';
	html += '</body></html>';
	frameDocument.open();
	frameDocument.write(html);
	frameDocument.close();

// add stylesheet definitions
	var frameStyle = new StyleSheet(frameDocument);
	for (var rule in frameCSS) {
		frameStyle.addRule(rule, frameCSS[rule]);
	}

// copy textarea to frame
  textboxHtml = editformOrig;
  textboxHtml = textboxHtml.replace(/</g, '&lt;');
  textboxHtml = textboxHtml.replace(/>/g, '&gt;');
  textboxHtml = textboxHtml.replace(/(\n?\r|\n)/g, '<br>');

// highlight the syntax
  var obj = {};
  obj.html = textboxHtml;
	if (highlightSyntax == true) {
	  HighlightSyntax(obj);
	}
  textboxHtml = obj.html;

  if (useClassic == true) {
// ?????
  }

// set frame content
	frameBody = frameDocument.body;
	frameBody.innerHTML = textboxHtml;

// set original tree position of input area
	normalTreePos = inputWrapper.nextSibling;

// set fullscreen button texts
	var fullScreenButton = document.getElementById('fullScreenButton');
	var floatButton = document.getElementById('fullScreenButtonFloat');
	fullScreenButton.value = fullButtonValue;
	fullScreenButton.title = fullButtonTitle;
	floatButton.value = normalFloatButtonValue;
	floatButton.title = normalButtonTitle;

// set frame events
	frameDocument.addEventListener('keypress', KeyFrame, true);
	frameDocument.addEventListener('mousedown', KeyFrame, true);

// set fullscreen events
	fullScreenButton.addEventListener('click', FullScreen, true);
	floatButton.addEventListener('click', NormalScreen, true);

	floatButton.onblur = function() {
		floatButton.style.right = '0.5em';
		floatButton.style.bottom = '0.5em';
		floatButton.style.top = '';
		floatButton.style.left = '';
	};

// find ahead events
	var findText = document.getElementById('findText');
	findText.addEventListener('keyup', FindAhead, true);

// cursor memory, jump to last changed position event
	textareaObj.addEventListener('keypress', KeyTextArea, true);
	textareaObj.addEventListener('keyup', KeyTextArea, true);
	textareaObj.addEventListener('click', ClickTextArea, true);

// submit button events
	var saveButton = document.getElementById('wpSave');
	var previewButton = document.getElementById('wpPreview');
	var diffButton = document.getElementById('wpDiff');
	saveButton.onclick = function() { NormalScreen(); saveButton.onclick = null; saveButton.click(); };
	previewButton.onclick = function() { NormalScreen(); previewButton.onclick = null; previewButton.click(); };
	diffButton.onclick = function() { NormalScreen(); diffButton.onclick = null; diffButton.click(); };

// insert an invisible clone of the textarea for resizing
  var textareaClone = textareaObj.cloneNode(false);
  textareaClone.id = 'textareaClone';
  textareaClone.name = null;
  textareaClone.accesskey = null
  textareaClone.tabindex = null;
	textareaClone.style.position = 'relative';
  textareaClone.style.display = 'none';
	textareaClone.style.zIndex = '-5';
	textareaClone.style.height = '';
	textareaClone.rows = 1;
  textareaClone.style.overflow = 'scroll';
  textareaObj.parentNode.insertBefore(textareaClone, textareaObj.nextSibling);

// set textarea cursor to start
	textareaObj.setSelectionRange(0, 0);

// default checkboxes
  if (findAheadSelected) {
    document.getElementById('findAhead').checked = true;
  }

// scroll to edit window
	var wrapperObj = document.getElementById('frameWrapper');
	var wrapperTop = GetOffsetTop(wrapperObj);
	window.scroll(0, wrapperTop);

	return;
}


//
// Button: toggle or set button checked state
//

function Button(what, buttonClass) {

	var buttonObj = document.getElementById(what);

// init the button
	if (buttonClass != null) {
		if (buttonClass == false) {
			buttonObj.className = 'rteButtonUnchecked';
			buttonObj.checked = false;
		}
		else if (buttonClass == true) {
			buttonObj.className = 'rteButtonChecked';
			buttonObj.checked = true;
		}
		else {
			buttonObj.className = buttonClass;
		}
	}
	else {

// perform specific actions
		var toggle = false;
		switch (what) {
			case 'syntax':
			  var obj = {};
				obj.html = frameBody.innerHTML;
				if (buttonObj.checked == false) {
					highlightSyntax = true;
					HighlightSyntax(obj);
				}
				else {
					highlightSyntax = true;
					RemoveHighlighting(obj);
				}
				toggle = true;
				frameBody.innerHTML = obj.html;
				break;
			case 'source':
			  var obj = {};
				if (buttonObj.checked == false) {
					Edit('sourceon');
				}
				else {
					Edit('sourceoff');
				}
				toggle = true;
				break;
			case 'scrolltotop':
			case 'scrolltotop2':
			case 'scrolltotop3':
				var wrapperObj = document.getElementById('frameWrapper');
				var wrapperTop = GetOffsetTop(wrapperObj);
				window.scroll(0, wrapperTop);
				break;
			case 'scrolltobuttons':
			case 'scrolltobuttons2':
			case 'scrolltobuttons3':
				var wrapperObj = document.getElementById('buttonsWrapper');
				var wrapperTop = GetOffsetTop(wrapperObj);
				window.scroll(0, wrapperTop);
				break;
			case 'preview':
			case 'preview2':
				NormalScreen();
				document.getElementById('PreviewBox').innerHTML = 'wiki2html';/////// wiki2html(textareaObj.value);
				document.getElementById('customPreviewBox').style.display = 'block';
				break;
			case 'diff':
			case 'diff2':
				NormalScreen();
				document.getElementById('PreviewBox').innerHTML = 'StringDiff' /////// StringDiff(editformOrig, textareaObj);
				document.getElementById('customPreviewBox').style.display = 'block';
				break;
			case 'close':
			case 'close2':
				document.getElementById('customPreviewBox').style.display = 'none';
				break;
			case 'classic':
				if (buttonObj.checked == true) {
					document.getElementById('textareaWrapper').style.display = 'none';
				}
				else {
					document.getElementById('textareaWrapper').style.display = 'block';
				}
				toggle = true;
				break;
			case 'caseSensitive':
				toggle = true;
				break;
			case 'regExp':
				toggle = true;
				break;
			case 'findAhead':
				toggle = true;
				break;
			case 'fullScreenButton':
			case 'fullScreenFloat':
				fullScreenButton
				fullScreenButtonFloat
		}

// toggle the button
		if (toggle) {
			if (buttonObj.checked == true) {
				buttonObj.className = 'rteButtonUnchecked';
				buttonObj.checked = false;
			}
			else {
				buttonObj.className = 'rteButtonChecked';
				buttonObj.checked = true;
			}
		}
	}
	return;
}


//
// editing functions
//

function Edit(what) {

// add focus to textbox
	textareaObj.focus();

// get the scroll position
	var scrollTopPx = textareaObj.scrollTop;
	var scrollHeightPx = textareaObj.scrollHeight;

// convert strange spaces, remove non-\n linebreak characters
//	ConvertStrangeSpaces();

// generate several different text ranges to apply the changes to later
  var obj = {};

// setup whole object for the whole text
	obj.whole = {};
	obj.whole.plainArray = [];
	obj.whole.plainNode = [];
	obj.whole.plainStart = [];
  obj.whole.from = 'whole';

// get whole range
	obj.whole.range = document.createRange();
	obj.whole.range.setStartBefore(frameBody.firstChild);
	obj.whole.range.setEndAfter(frameBody.lastChild);

// get whole plain text
	GetInnerHTML(obj.whole, frameBody);

  RemoveHighlightingWikify(obj.whole);
  obj.whole.plain = obj.whole.html;
  obj.whole.plain = obj.whole.plain.replace(/<br>/g, '\n');

	obj.whole.range.setStartBefore(frameBody.firstChild);
	obj.whole.range.setEndAfter(frameBody.lastChild);

// setup selection object for the selected text
	obj.selection = {};
  obj.selection.from = 'selection';

// set selection range
 	obj.sel = frameWindow.getSelection();
	obj.selection.range = obj.sel.getRangeAt(obj.sel.rangeCount - 1);

// copy range to document fragment
	var documentFragment = obj.selection.range.cloneContents();

// get selected text
	GetInnerHTML(obj.selection, documentFragment);

//  RemoveHighlightingWikify(obj.selection);
	obj.selection.plain = obj.selection.html;
  obj.selection.plain = obj.selection.plain.replace(/<br>/g, '\n');

// setup cursor object for the cursor position
	obj.cursor = {};
	obj.cursor.from = 'cursor';
	obj.cursor.range = document.createRange();
	obj.cursor.range.setStart(obj.sel.focusNode, obj.sel.focusOffset);
	obj.cursor.range.setEnd(obj.sel.focusNode, obj.sel.focusOffset);
	obj.cursor.plain = '';

// setup focusWord object for the word under the cursor
	obj.focusWord = {};
	obj.focusWord.from = 'focusWord';
	obj.focusWord.range = document.createRange();

// setup focusLine object for the line under the cursor
	obj.focusLine = {};
	obj.focusLine.from = 'focusLine';
	obj.focusLine.range = document.createRange();

// setup selectionWord object for the words under the selection
	obj.selectionWord = {};
	obj.selectionWord.from = 'selectionWord';
	obj.selectionWord.range = document.createRange();

// setup selectionLine object for the lines under the selection
	obj.selectionLine = {};
	obj.selectionLine.from = 'selectionLine';
	obj.selectionLine.range = document.createRange();

// find the respective word and line boundaries
	FindBoundaries(obj.focusWord, obj.focusLine, obj.whole, obj.cursor);
	FindBoundaries(obj.selectionWord, obj.selectionLine, obj.whole, obj.selection);

// get the wikified plain text for the word under the cursor
	var documentFragment = obj.focusWord.range.cloneContents();
	GetInnerHTML(obj.focusWord, documentFragment);
  RemoveHighlightingWikify(obj.focusWord);
	obj.focusWord.plain = obj.focusWord.html;
  obj.focusWord.plain = obj.focusWord.plain.replace(/<br>/g, '\n');

// get the wikified plain text for the line under the cursor
	var documentFragment = obj.focusLine.range.cloneContents();
	GetInnerHTML(obj.focusLine, documentFragment);
  RemoveHighlightingWikify(obj.focusLine);
	obj.focusLine.plain = obj.focusLine.html;
  obj.focusLine.plain = obj.focusLine.plain.replace(/<br>/g, '\n');

// get the wikified plain text for the words under the selection
	var documentFragment = obj.selectionWord.range.cloneContents();
	GetInnerHTML(obj.selectionWord, documentFragment);
  RemoveHighlightingWikify(obj.selectionWord);
	obj.selectionWord.plain = obj.selectionWord.html;
  obj.selectionWord.plain = obj.selectionWord.plain.replace(/<br>/g, '\n');

// get the wikified plain text for the lines under the selection
	var documentFragment = obj.selectionLine.range.cloneContents();
	GetInnerHTML(obj.selectionLine, documentFragment);
  RemoveHighlightingWikify(obj.selectionLine);
	obj.selectionLine.plain = obj.selectionLine.html;
//  obj.selectionLine.plain = obj.selectionLine.plain.replace(/\n/g, '');
  obj.selectionLine.plain = obj.selectionLine.plain.replace(/<br>/g, '\n');

// select the appropriate object to change (whole, selection, cursor, focusWord, focusLine, selectionWord, or selectionLine)
	obj.changed = {};
	switch (what) {

// no text: null
		case 'undo':
		case 'redo':
			obj.changed = null;
			break;

// basic wiki character formatting: selection / focusWord / cursor
		case 'bold':
		case 'italic':
		case 'underline':
		case 'sup':
		case 'sub':
		case 'wikilink':
		case 'weblink':
			if (obj.selection.plain != '') {
				obj.changed = obj.selection;
			}
			else if (obj.focusWord.plain != '') {
				obj.changed = obj.focusWord;
			}
			else  {
				obj.changed = obj.cursor;
			}
			break;

// character formatting: selection / focusWord
		case 'case':
			if (obj.selection.plain != '') {
				obj.changed = obj.selection;
			}
			else {
				obj.changed = obj.focusWord;
			}
			break;

// whole text changes: whole
		case 'undoall':
		case 'redoall':
			obj.changed = obj.whole;
			break;

// update syntax highlighting: selection / whole
		case 'update_syntax':
			if (obj.selection.plain != '') {
				obj.changed = obj.selection;
			}
			else {
				obj.changed = obj.whole;
			}
			break;

// multiple line changes: selectionLine
		case 'headingless':
		case 'headingmore':
		case 'bulletlist':
		case 'numberlist':
		case 'indentlist':
		case 'deflist':
			if (obj.selection.plain != '') {
				obj.changed = obj.selectionLine;
			}
			else {
				obj.changed = obj.focusLine;
			}
			break;

// image: selectionWord (if text is selected) / cursor
//whole, selection, cursor, focusWord, focusLine, selectionWord, or selectionLine
		case 'image':
			if (obj.selection.plain != '') {
				obj.changed = obj.selectionWord;
			}
			else  {
				obj.changed = obj.cursor;
			}
			break;

// table: selectionLine / cursor
		case 'table':
			if (obj.selection.plain != '') {
				obj.changed = obj.selectionLine;
			}
			else  {
				obj.changed = obj.cursor;
			}
			break;

// wikify: selection / whole
		case 'wikify':
			if (obj.selection.plain != '') {
				obj.changed = obj.selection;
			}
			else {
				obj.changed = obj.whole;
			}
			break;

// character formatting: selection / focusWord
		case 'getfind':
		case 'getfindreplace':
			if (obj.selection.plain != '') {
				obj.changed = obj.selection;
			}
			else {
				obj.changed = obj.focusWord;
			}
			break;

// find and replace: null
		case 'findprev':
		case 'findnext':
			obj.changed = null;
			break;

		case 'updown':
		case 'lastchangepos':
		case 'nextchangepos':
		case 'replaceall':
		case 'replaceprev':
		case 'replacenext':
			break;

// fixing buttons: selection / whole
		case 'fixbasic':
		case 'fixpipes':
		case 'fixpunct':
		case 'fixmath':
		case 'fixunits':
		case 'fixdashes':
		case 'fixhtml':
		case 'fixcaps':
		case 'fixall':
			if (obj.selection.plain != '') {
				obj.changed = obj.selection;
			}
			else {
				obj.changed = obj.whole;
			}
			break;

// source on / off: selection / whole
		case 'sourceon':
		case 'sourceoff':
//			if (obj.selection.plain != '') {
//				obj.changed = obj.selection;
//			}
//			else {
				obj.changed = obj.whole;
//			}
			break;

// unknown edit function
		default:
		  alert('Unknown edit function \'' + what + '\'');
	}

	if (obj.changed != null) {

// convert strange spaces
		obj.changed.plain = obj.changed.plain.replace(/[\t\v\u2028\u2029]+/g, ' ');
		obj.changed.plain = obj.changed.plain.replace(/[\r\f]/g, '');
	}

// set local syntax highlighting flag
	var highlightSyntaxEdit = highlightSyntax;

// manipulate the text
	switch (what) {

// undo
		case 'undo':
		  FrameExecCommand('undo');
			break;

// redo
		case 'redo':
		  FrameExecCommand('redo');
			break;

// bold
		case 'bold':
			if ( /\'\'\'([^\'].*?)\'\'\'/.test(obj.changed.plain) ) {
				obj.changed.plain = obj.changed.plain.replace(/\'\'\'([^\'].*?)\'\'\'/g, '$1');
			}
			else {
				obj.changed.plain = '\'\'\'' + obj.changed.plain + '\'\'\'';
				obj.changed.plain = obj.changed.plain.replace(/(\'\'\')( *)(.*?)( *)(\'\'\')/, '$2$1$3$5$4');
			}
			break;

// italic
		case 'italic':
			if ( /\'\'([^\'].*?)\'\'/.test(obj.changed.plain) ) {
				obj.changed.plain = obj.changed.plain.replace(/\'\'([^\'].*?)\'\'/g, '$1');
			}
			else {
				obj.changed.plain = '\'\'' + obj.changed.plain + '\'\'';
				obj.changed.plain = obj.changed.plain.replace(/(\'\')( *)(.*?)( *)(\'\')/, '$2$1$3$5$4');
			}
			break;

// underline
		case 'underline':
			if ( /&lt;u&gt;(.*?)&lt;\/u&gt;/i.test(obj.changed.plain) ) {
				obj.changed.plain = obj.changed.plain.replace(/&lt;u&gt;(.*?)&lt;\/u&gt;/g, '$1');
			}
			else {
				obj.changed.plain = '&lt;u&gt;' + obj.changed.plain + '&lt;\/u&gt;';
				obj.changed.plain = obj.changed.plain.replace(/(&lt;u&gt;)( *)(.*?)( *)(&lt;\/u&gt;)/, '$2$1$3$5$4');
			}
			break;

// superscript
		case 'sup':
			if ( /&lt;sup&gt;(.*?)&lt;\/sup&gt;/i.test(obj.changed.plain) ) {
				obj.changed.plain = obj.changed.plain.replace(/&lt;sup&gt;(.*?)&lt;\/sup&gt;/g, '$1');
			}
			else {
				obj.changed.plain = '&lt;sup&gt;' + obj.changed.plain + '&lt;/sup&gt;';
				obj.changed.plain = obj.changed.plain.replace(/(&lt;sup&gt;)( *)(.*?)( *)(&lt;\/sup&gt;)/, '$2$1$3$5$4');
			}
			break;

// subscript
		case 'sub':
			if ( /&lt;sub&gt;(.*?)&lt;\/sub&gt;/i.test(obj.changed.plain) ) {
				obj.changed.plain = obj.changed.plain.replace(/&lt;sub&gt;(.*?)&lt;\/sub&gt;/g, '$1');
			}
			else {
				obj.changed.plain = '&lt;sub&gt;' + obj.changed.plain + '&lt;/sub&gt;';
				obj.changed.plain = obj.changed.plain.replace(/(&lt;sub&gt;)( *)(.*?)( *)(&lt;\/sub&gt;)/, '$2$1$3$5$4');
			}
			break;

// toggle lowercase / uppercase
		case 'case':

// lowercase all uppercased text
			if (obj.changed.plain.toUpperCase() == obj.changed.plain) {
				obj.changed.plain = obj.changed.plain.toLowerCase();
			}

// first-letter-uppercase all lowercased text
			else if (obj.changed.plain.toLowerCase() == obj.changed.plain) {
				obj.changed.plain = obj.changed.plain.replace(/\b(\w)(\w*)/g,
					function (p, p1, p2) {
						return(p1.toUpperCase() + p2.toLowerCase());
					}
				);
			}

// uppercase mixed upper and lowercased text
			else {
				obj.changed.plain = obj.changed.plain.toUpperCase();
			}
			break;

		case 'undoall':
			obj.changed.plain = editformOrig;
			break;
		case 'redoall':
			if (editformLast != null) {
				obj.changed.plain = editformLast;
			}
			break;

// update syntax highlighting
		case 'update_syntax':
		  highlightSyntaxEdit = true;
			break;

// various editing buttons
		case 'wikilink':
			if ( /\[\[(.*?)\]\]/.test(obj.changed.plain) ) {
				obj.changed.plain = obj.changed.plain.replace(/\[\[(.*?)\]\]/g, '$1');
			}
			else {
				obj.changed.plain = '\[\[' + obj.changed.plain + '\]\]';
				obj.changed.plain = obj.changed.plain.replace(/(\[\[)( *)(.*?)( *)(\]\])/, '$2$1$3$5$4');
			}
			break;
		case 'weblink':
			if ( /\[(.*?)\]/.test(obj.changed.plain) ) {
				obj.changed.plain = obj.changed.plain.replace(/\[(.*?)\]/g, '$1');
			}
			else {
				obj.changed.plain = '\[' + obj.changed.plain + '\]';
				obj.changed.plain = obj.changed.plain.replace(/(\[)( *)(.*?)( *)(\])/, '$2$1$3$5$4');
			}
			break;
		case 'headingless':
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)=(=+) *([^\n]*?) *=+(?=\n|$)/g, '$1$2 $3 $2'); // decrease heading
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)=(?!=) *([^\n]*?) *=+(?=\n|$)/g, '$1$2'); // remove heading
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)(=+) *([^\n]*?) *=+(?=\n|$)/g, '$1$2 $3 $2'); // adjust closing tags
			break;
		case 'headingmore':
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)(=+) *([^\n]*?) *=+(?=\n|$)/g, '$1=$2 $3 $2='); // increase heading
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)([^=\n\s][^\n]*?)(?=\n|$)/g, '$1== $2 =='); // create new heading
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)(=+) *([^\n]*?) *=+(?=\n|$)/g, '$1$2 $3 $2'); // adjust closing tags
			break;
		case 'bulletlist':
			obj.changed.plain = obj.changed.plain.replace(/(^|\n) +(?=\n)/g, '$1');
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)([\*\#\:\;]*) */g, '$1*$2 ');
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)(\*{4,})([\*\#\:\;]*) */g, '$1$3 ');
			break;
		case 'numberlist':
			obj.changed.plain = obj.changed.plain.replace(/(^|\n) +(?=\n)/g, '$1');
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)([\*\#\:\;]*) */g, '$1#$2 ');
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)(\#{4,})([\*\#\:\;]*) */g, '$1$3 ');
			break;
		case 'indentlist':
			obj.changed.plain = obj.changed.plain.replace(/(^|\n) +(?=\n)/g, '$1');
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)([\*\#\:\;]*) */g, '$1:$2 ');
			obj.changed.plain = obj.changed.plain.replace(/(^|\n)(\:{4,})([\*\#\:\;]*) */g, '$1$3 ');
			break;
		case 'deflist':
			obj.changed.plain = obj.changed.plain.replace(/(^|\n) +(?=\n)/g, '$1');
			obj.changed.plain = obj.changed.plain.replace(/(^|\n) *([^\n\s\;]+) *([^\n]+?)/g, '$1; $2 : $3');
			break;
		case 'image':
			if (obj.changed.plain != '') {
				obj.changed.plain = '[[Image:<span class="rteInsertHere">filename</span>|thumb|<span class="rteInsertHere">width</span>px|' + obj.changed.plain + ']]';
			}
			else {
				obj.changed.plain = '[[Image:<span class="rteInsertHere">filename</span>|thumb|<span class="rteInsertHere">width</span>px|<span class="rteInsertHere"> </span>]]';
				if (obj.focusWord.plain != '') {
					obj.changed.plain = ' ' + obj.changed.plain + ' ';
				}
			}
			break;
		case 'table':
			if (obj.changed.plain != '') {
				obj.changed.plain = obj.changed.plain.replace(/(^|\n) */g, '\n|-\n| ');
				obj.changed.plain = obj.changed.plain.replace(/^\n\|\-\n/, '\n{| {{Prettytable}}\n');
				obj.changed.plain = obj.changed.plain.replace(/$/g, '\n|}\n');
			}
			else {
				obj.changed.plain = '\n{| {{Prettytable}}\n|+ <span class="rteInsertHere">caption</span>\n! <span class="rteinserthere">heading</span> !! <span class="rteInsertHere">heading</span>\n|-\n| <span class="rteInsertHere">cell</span> || <span class="rteInsertHere">cell</span>\n|-\n| <span class="rteInsertHere">cell</span> || <span class="rteInsertHere">cell</span>\n|}\n';
				if (obj.focusLine.plain != '') {
					obj.changed.plain = '\n' + obj.changed.plain + '\n';
				}
			}
			break;

// wikify: always done above
		case 'wikify':
			break;

// copy selection to find field
		case 'getfind':
			inputElement['find'].value = obj.changed.plain;
			obj.changed = null;
			break;

// copy selection to find and to replace field
		case 'getfindreplace':
			inputElement['find'].value = obj.changed.plain;
			inputElement['replace'].value = obj.changed.plain;
			obj.changed = null;
			break;

// find and replace
		case 'findprev':
		case 'findnext':
		case 'replaceall':
		case 'replaceprev':
		case 'replacenext':

// get the find text
			var findText = inputElement['find'].value;

// get the replace text
			var replaceText = inputElement['replace'].value;

// get the find and replace checkbutton states
			var caseSensitiveButton = document.getElementById('caseSensitive');
			var regExpButton = document.getElementById('regExp');
			var findAheadButton = document.getElementById('findAheadButton');

// get current pos
			var startNode = obj.cursor.range.startContainer;
			var startNodeOffset = obj.cursor.range.startOffset;
			if (startNode.nodeName == 'BR') {
				startNode = startNode.childNodes[startNodeOffset];
				startNodeOffset = 0;
			}
			var startNodeIndex;
			for (i = 0; i < obj.whole.plainArray.length; i ++) {
				if (startNode == obj.whole.plainNode[i]) {
					startNodeIndex = i;
					break;
				}
			}
			var startPos = obj.whole.plainStart[startNodeIndex] + startNodeOffset;

textareaObj.value = '';
textareaObj.value += 'startPos: ' + startPos + '\n';

// prepare find regexp for find and replace
			var regExpFlags = 'g';

// format the find and replace texts as regexp or plain text
			if (regExpButton.checked == false) {
				findText = findText.replace(/([\\^\$\*\+\?\.\(\)\[\]\{\}\:\=\!\|\,\-])/g, '\\$1');
			}
			else {

// replace \n with newline character
				replaceText = replaceText.replace(/((^|[^\\])(\\\\)*)\\n/g, '$1\n');
			}


textareaObj.value += 'findText: ' + findText + '\n';
textareaObj.value += 'replaceText: ' + replaceText + '\n';

// set regexp flag i
			if ( ! caseSensitiveButton.checked ) {
				regExpFlags += 'i';
			}

textareaObj.value += 'regExpFlags: ' + regExpFlags + '\n';

// new regular expression
			var regExpFind = new RegExp(findText, regExpFlags);
			var result = [];

// find next, search to the right
			if ( (what == 'findnext') || (what == 'findprev') ) {

// set start position for search to right
				var indexStart = startPos;
				if (obj.selection.plain.length > 0) {
					indexStart ++;
				}
textareaObj.value += 'indexStart: ' + indexStart + '\n';

// execute the regexp search to the right
				regExpFind.lastIndex = indexStart;
				result = regExpFind.exec(obj.whole.plain);
			}

// find previous, search to the left
			else if ( (what == 'findprev') || (what == 'replaceprev') ) {

// set start position for search to left
				indexStart = startPos - 1;
				if ( (obj.selection.plain.length > 0) && (what == 'replaceprev') ) {
					indexStart ++;
				}

// cycle through the matches to the left
				var resultNext;
				do {
					result = resultNext;
					resultNext = regExpFind.exec(obj.whole.plain);
					if (resultNext == null) {
						break;
					}
				} while (resultNext.index <= indexStart);
			}

// get the matched string
			var matched;
			var matchedStart;
			var matchedLength;
			if (result != null) {
				matched = result[0];
				matchedStart = result.index;
				matchedLength = matched.length;
				matchedEnd = matchedStart + matchedLength;

// get current pos
				var startNode;
				var startNodeOffset;
				var startNodeIndex;
				var endNode;
				var endNodeOffset;
				var endNodeIndex;
				for (i = 1; i < obj.whole.plainArray.length; i ++) {
					if (obj.whole.plainStart[i] > matchedStart) {
						break;
					}
				}

//textareaObj.value += '' + i + ': ' + obj.whole.plainStart[i] + '\n';
				for (j = i; j < obj.whole.plainArray.length; j ++) {
					if (obj.whole.plainStart[j] > matchedEnd) {
						break;
					}
				}
				startNodeIndex = i - 1;
				startNode = obj.whole.plainNode[startNodeIndex];
				startNodeOffset = matchedStart - obj.whole.plainStart[startNodeIndex];

				endNodeIndex = j - 1;
				endNode = obj.whole.plainNode[endNodeIndex];
				endNodeOffset = matchedEnd - obj.whole.plainStart[endNodeIndex];

textareaObj.value += 'matchedStart: ' + matchedStart + '\n';
textareaObj.value += 'matchedLength: ' + matchedLength + '\n';
textareaObj.value += 'matchedEnd: ' + matchedEnd + '\n';
textareaObj.value += 'startNode.nodeName: ' + startNode.nodeName + '\n';
textareaObj.value += 'startNode.nodeValue: ' + startNode.nodeValue + '\n';
textareaObj.value += 'startNode: ' + startNode + '\n';
textareaObj.value += 'endNode: ' + endNode + '\n';
textareaObj.value += 'startNodeOffset: ' + startNodeOffset + '\n';
textareaObj.value += 'endNodeOffset: ' + endNodeOffset + '\n';

textareaObj.value += 'startNodeIndex: ' + startNodeIndex + '\n';
textareaObj.value += 'endNodeIndex: ' + endNodeIndex + '\n';
/**/

// set selection range
				var range = document.createRange();
				if (startNode.nodeName == 'BR') {
					range.setStartBefore(startNode);
				}
				else {
					range.setStart(startNode, startNodeOffset);
				}

				if (endNode.nodeName == 'BR') {
					range.setEndAfter(endNode);
				}
				else {
					range.setEnd(endNode, endNodeOffset);
				}
				obj.sel.removeAllRanges();
				obj.sel.addRange(range);
			}


			obj.changed = null;
			break;
		case 'updown':

			obj.changed = null;
			break;
		case 'lastchangepos':
			break;
		case 'nextchangepos':
			break;

// fixbasic: fix characters, spaces, empty lines, certain headings, needed for all fixing functions
// to do: only certain changes in multiline tags: comments, tables, subst
		case 'fixbasic':
			FixBasic(obj.changed);
			break;

// fixpipes: add spaces around pipes in wikilinks and templates (good for link lists)
		case 'fixpipes':
			FixPipes(obj.changed);
			break;
		case 'fixpunct':
			FixPunct(obj.changed);
			break;
		case 'fixmath':
			FixMath(obj.changed);
			break;
		case 'fixunits':
			FixUnits(obj.changed);
			break;
		case 'fixdashes':
			FixDashes(obj.changed);
			break;
		case 'fixhtml':
			FixHTML(obj.changed);
			break;
		case 'fixcaps':
			FixCaps(obj.changed);
			break;
		case 'fixall':
			FixAll(obj.changed);
			break;

// source on
		case 'sourceon':
		  obj.changed.plain = obj.changed.plain.replace(/&lt;br&gt;/g, '');
		  obj.changed.plain = obj.changed.plain.replace(/&/g, '&amp;');
		  obj.changed.plain = obj.changed.plain.replace(/\n/g, '&lt;br&gt;\n');
		  highlightSyntaxEdit = false;
			break;

// source off
		case 'sourceoff':
		  obj.changed.plain = obj.changed.plain.replace(/&lt;br&gt;\n/gi, '\n');
		  obj.changed.plain = obj.changed.plain.replace(/&amp;/g, '&');
			break;

		default:
		  alert('Unknown edit function \'' + what + '\'');
	}


/*


// prepare find regexp for find and replace

// find / replace
	if ('findnext replacenext findprev replaceprev'.indexOf(what) >= 0) {
		if (find.value != '') {

// create regexp
			var regExpFind = new RegExp(findText, regExpFlags + 'g');

// set start position for search to right
			var indexStart;
			var result;
			if ('findnext replacenext'.indexOf(what) >= 0) {
				indexStart = startPos;
				if ( (selected.length > 0) && ('findnext'.indexOf(what) >= 0) ) {
					indexStart = startPos + 1;
				}

// execute the regexp search to the right
				regExpFind.lastIndex = indexStart;
				result = regExpFind.exec(textareaObj.value);
			}

// prepare search to the left
			else {

// set start position for search to left
				indexStart = startPos - 1;
				if ( (selected.length > 0) && ('replaceprev'.indexOf(what) >= 0) ) {
					indexStart = startPos;
				}

// cycle through the matches to the left
				var resultNext;
				do {
					result = resultNext;
					resultNext = regExpFind.exec(textareaObj.value);
					if (resultNext == null) {
						break;
					}
				} while (resultNext.index <= indexStart);
			}

// get the matched string
			var matched;
			var matchedStart;
			var matchedLength;
			if (result != null) {
				matched = result[0];
				matchedStart = result.index;
				matchedLength = matched.length;

// replace only if the next match was already selected
				if ('replacenext replaceprev'.indexOf(what) >= 0) {
					if (selected == matched) {
						var replace = selected.replace(regExpFind, replaceText);
						textNew = textareaObj.value.substr(0, matchedStart) + replace + textareaObj.value.substr(matchedStart + matched.length);
						matchedLength = replace.length;
						textChanged = true;
					}
				}

// select the found match in the textarea
				startPosNew = matchedStart;
				endPosNew = matchedStart + matchedLength;
			}
			else {
				if ('findprev replaceprev'.indexOf(what) >= 0) {
					indexStart = startPos;
				}
				startPosNew = indexStart;
				endPosNew = indexStart;
			}
			posChanged = true;
		}
	}

// replace all
	if ('replaceall'.indexOf(what) >= 0) {
		if (findText != '') {

// create regexp
			var regExpFind = new RegExp(findText, regExpFlags + 'g');


// replace all in whole text
			if (selected == '') {

// get the new cursorposition
				textNew = textareaObj.value.replace(regExpFind, replaceText);
				var textbefore = textNew.substr(0, startPos);
				textbefore = textbefore.replace(regExpFind, replaceText);
				startPosNew = textbefore.length;
				endPosNew = startPosNew;
				posChanged = true;
			}

// replace all in selection
			else {
				var replace = selected.replace(regExpFind, replaceText);
				startPosNew = startPos;
				endPosNew = startPos + replace.length;
				textNew = textareaObj.value.substr(0, startPos) + replace + textareaObj.value.substr(endPos);
			}
			textChanged = true;
		}
	}

// save search history to cookie
	if ('findnext findprev'.indexOf(what) >= 0) {
		AddToHistory('find');
	}
	if ('replacenext replaceprev replaceall'.indexOf(what) >= 0) {
		AddToHistory('find');
		AddToHistory('replace');
	}

// get the find field from the selection or the current word
	if ('find#next find#prev replac#enext replace#prev get#find'.indexOf(what) >= 0) {
		if ( ('getfind'.indexOf(what) >= 0) || (find.value == '') ) {

// get from the selection
			var newFind = '';
			if (selected != '') {
			  newFind = selected;
				startPosNew = startPos;
				endPosNew = endPos;
			}

// get from the current word
			else {

// get until next nonword char to the right
				endPosNew = endPos;
			  var pos = startPos;
			  while (pos < textLength) {
			    var character = textareaObj.value.substr(pos ++, 1);
			    if ( character.match(/\W/) ) {
					  endPosNew = pos - 1;
			      break;
			    }
					newFind += character;
			  }

// get until next nonword char to the left
				startPosNew = startPos;
			  pos = startPos - 1;
			  while (pos >= 0) {
			    var character = textareaObj.value.substr(pos --, 1);
			    if ( character.match(/\W/) ) {
				  	startPosNew = pos + 2;
			      break;
			    }
			    newFind = character + newFind;
			  }
			}

// replace newlines in find field
			if (regExp.checked) {
				find.value = newFind.replace(/\n/g, '\\n');
			}
			else {
				find.value = newFind.replace(/\n.*                                          /, '');
			}
		}
	}

*/

// apply changes
	if (obj.changed != null) {

// save last version for redo all
		switch (what) {
			case 'undo':
			case 'redo':
			case 'undoall':
			case 'redoall':
				if (editformLast == null) {
					editformLast = obj.changed.plain;
				}
				break;
			default:
			  editformLast = null; /////////////////  also after every key click
		}

// highlight syntax
		obj.changed.plain = obj.changed.plain.replace(/\n/g, '<br>');
		obj.html = obj.changed.plain;
		if (highlightSyntaxEdit == true) {
			HighlightSyntax(obj, true);
		}

// make changed range text the current selection
		obj.sel.removeAllRanges();
		obj.sel.addRange(obj.changed.range);

		if (obj.html != '') {
		  FrameExecCommand('inserthtml', obj.html);
		}
		else {
		  FrameExecCommand('delete');
		}
	}

// focus the frame
	frameWindow.focus();


return;



// scroll the textarea to the selected text or cursor position
	if (posChanged || textChanged) {
		ParseRows();
	}
	if (posChanged) {
  	ScrollTextarea(textRows.selStartRow, textRows.selEndRow, 0, findMargin, scrollTopPx);
	}
	else {
		textareaObj.scrollTop = scrollTopPx;
	}
	return;
}


//
// FixBasic: fix characters, spaces, empty lines, certain headings, needed for all fixing functions
//

function FixBasic(obj) {

// non-breaking space character to normal space
	obj.plain = obj.plain.replace(/(\u00a0)/g, ' ');

// remove trailing spaces
	obj.plain = obj.plain.replace(/( |&nbsp;)+\n/g, '\n');

// empty line before and after headings, spaces around word (lookahead)
	obj.plain = obj.plain.replace(/\n+(={2,}) *([^\n]*?) *(={2,})(?=\n)\n*/g, '\n\n$1 $2 $3\n\n');

// uppercase well known headings
	obj.plain = obj.plain.replace(/\n== external links? ==\n/ig, '\n== External links ==\n');
	obj.plain = obj.plain.replace(/\n== see also ==\n/ig, '\n== See also ==\n');
	obj.plain = obj.plain.replace(/\n== references? ==\n/ig, '\n== References ==\n');

// add space after * # : ; (list) and after {| |- | (table)
	obj.plain = obj.plain.replace(/(^|\n)([\*\#\:\;]+|\{\||\|\-|\|\}|\|) */g, '$1$2 ');
	obj.plain = obj.plain.replace(/ +\n/g, '\n');

// empty line before and after tables
	obj.plain = obj.plain.replace(/\n+(\{\|)/g, '\n\n$1');
	obj.plain = obj.plain.replace(/(\n\|\}) *([^\n]*)[\n|$]+/g, '$1\n\n$2\n\n');

// empty line before and after lists
	obj.plain = obj.plain.replace(/(^|\n)([^\*\#\:\;].*?)\n+([\*\#\:\;])/g, '$1$2\n\n$3');
	obj.plain = obj.plain.replace(/(^|\n)([\*\#\:\;].*?)\n+([^\*\#\:\;])/g, '$1$2\n\n$3');

// split into lines and change single lines, used to handle tables
	var lines = obj.plain.split('\n');
	obj.plain = '';
	var tableflag = false;
	for (var i = 0; i < lines.length; i++) {
		var line = lines[i];

// do not change lines starting with a blank
		if ( ! line.match(/^ /) ) {

// detect table
			if ( line.match(/^(\{\||\!|\|[^}])/) ) {
				tableflag = true;
			}
			else if ( line.match(/^\|\}/) ) {
				tableflag = false;
			}

// changes only to be done in tables
			if (tableflag) {

// add spaces around ||
				line = line.replace(/ *\|\| */g, ' || ');
			}

// changes not to be done in tables
			if ( ! tableflag) {

// empty line before and after images
				line = line.replace(/^(\[\[image:.*?\]\])/ig, '\n$1');
				line = line.replace(/(\[\[image:.*?(\[\[.*?\]\].*?)*\]\])$/ig, '$1\n');

// empty line before and after includes
				line = line.replace(/^(\{\{.*?\}\})/g, '\n$1');
				line = line.replace(/(\{\{.*?\}\})$/g, '$1\n');

// to be done: convert single newlines into spaces
//      line = line.replace(/(\n[^\n \*\#\:\;\|\{].*?)\n([^\n \*\#\:\;\|\{])/g, '$1 $2');
			}
		}

// concatenate the lines
		obj.plain += line;
		if (i < lines.length - 1) {
			obj.plain += '\n';
		}
	}

// remove spaces in wikilinks
	obj.plain = obj.plain.replace(/\[\[ *([^\n]*?) *\]\]/g, '[[$1]]');

// remove spaces in external links
	obj.plain = obj.plain.replace(/\[ *([^\n]*?) *\]/g, '[$1]');

// no space around pipes before brackets
	obj.plain = obj.plain.replace(/ +\| +\]\]/g, '|]]');

// no space around pipes before curly brackets
	obj.plain = obj.plain.replace(/ +\| +\}\}/g, '|}}');

// no empty line between headings and includes
	obj.plain = obj.plain.replace(/\n(==+ [^\n]*? ==+\n)\n+(\{\{.*?\}\})/g, '\n$1$2');

// spaces in comments
	obj.plain = obj.plain.replace(/(<!--) *([^\n]*?) *(-->)/g, '$1 $2 $3');

// empty lines around html comments
	obj.plain = obj.plain.replace(/\n+(<!--.*?-->)\n+/g, '\n$1\n\n');
	obj.plain = obj.plain.replace(/^(<!--.*?-->)\n+/g, '$1\n');
	obj.plain = obj.plain.replace(/\n+(<!--.*?-->)$/g, '\n$1');

// empty line before and after categories
	obj.plain = obj.plain.replace(/( |\n)*(\[\[category:[^\n]*?\]\])( |\n)*/gi, '\n\n$2\n\n');

// categories not separated by empty lines (lookahead)
	obj.plain = obj.plain.replace(/(\[\[category:[^\n]*?\]\])\n*(?=\[\[category:[^\n]*?\]\])/gi, '$1\n');

// single empty lines only
	obj.plain = obj.plain.replace(/\n{3,}/g, '\n\n');

// remove leading and trailing newlines
	obj.plain = obj.plain.replace(/^\n+/, '');
	obj.plain = obj.plain.replace(/\n{2,}$/, '\n');

	return;
}


//
// FixPipes: add spaces around pipes in wikilinks and templates (good for link lists)
//

function FixPipes(obj) {
	FixBasic(obj);
	obj.plain = obj.plain.replace(/(\[\[(?!image:)[^\n]+?) *\| *(.*?\]\])/ig, '$1 | $2');
	obj.plain = obj.plain.replace(/(\{\{)([^\n]+?)(\}\})/g,
		function (p, p1, p2, p3) {
			p2 = p2.replace(/ *(\|) */g, ' | ');
			return(p1 + p2 + p3);
		}
	);
	return;
}


//
// FixPunct: remove space before .,:
//

function FixPunct(obj) {
	FixBasic(obj);

// ; could be a definition
	obj.plain = obj.plain.replace(/([a-zA-Z\'\"\”\]\}\)]) +([\.\,\:])/g, '$1$2');

	return;
}


//
// FixMath: math character fixer, originally from User:Omegatron
// DO NOT USE ON WHOLE DOCUMENT OR <math> </math> WIKICODE!
//

function FixMath(obj) {
	FixBasic(obj);

// convert html entities into actual dash characters
	obj.plain = obj.plain.replace(/&minus;/g, '\u2212');
	obj.plain = obj.plain.replace(/&middot;/g, '·');

// convert dash next to a number into a minus sign character
	obj.plain = obj.plain.replace(/([^a-zA-Z0-9\,\_\{])-(\d)/g, '$1\u2212$2');

// changes 2x3 to 2×3
	obj.plain = obj.plain.replace(/(\d ?)x( ?\d)/g, '$1×$2');

// changes 10^3 to 10<sup>3</sup>
	obj.plain = obj.plain.replace(/(\d*\.?\d+)\^(\u2212?\d+\.?\d*)/g, '$1<sup>$2</sup>');

// change x^3 to x<sup>3</sup>
	obj.plain = obj.plain.replace(/([0-9a-zA-Z])\^(\u2212?\d+\.?\d*) /g, '$1<sup>$2</sup>');

// change +/- to ±
	obj.plain = obj.plain.replace(/( |\d)\+\/(-|\u2212)( |\d)/g, '$1±$3');

	return;
}


//
// FixUnits: unit formatter - new tab adds spaces between number and units, makes units consistent
// originally from User:Omegatron
//

function FixUnits(obj) {
	FixBasic(obj);

// convert all &deg; into actual ° symbol
	obj.plain = obj.plain.replace(/&deg;/g, '°');

// convert the word ohm(s) or the html entity into the actual Omega symbol (not the actual ohm symbol &#8486;) and make sure it's spaced
	obj.plain = obj.plain.replace(/(\d) ?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|&mu;|µ|&micro;|n|p|f|a|z|y)? ?(&Omega;|&#8486;|(ohm|Ohm)s?)([\s.,:;\'\"\/\)])/g, '$1 $2\u03a9$5');

// convert various micro symbols into the actual micro symbol, make sure it's spaced
	obj.plain = obj.plain.replace(/(\d) ?(&mu;|&micro;)(g|s|m|A|K|mol|cd|rad|sr|Hz|N|J|W|Pa|lm|lx|C|V|O|F|Wb|T|H|S|Bq|Gy|Sv|kat|°C|M)([\s.,:;\'\"\/\)])/g, '$1 µ$3$4');

// convert capital K to lowercase k in units
	obj.plain = obj.plain.replace(/(\d) ?K(g|s|m|A|K|mol|cd|rad|sr|Hz|N|J|W|Pa|lm|lx|C|V|O|F|Wb|T|H|S|Bq|Gy|Sv|kat|°C|M)([\s.,:;\'\"\/\)])/g, '$1 k$2$3');

// capitalize units correctly
	obj.plain = obj.plain.replace(/(\d) ?(khz)([ ,.])/gi, '$1 kHz$3');
	obj.plain = obj.plain.replace(/(\d) ?(mhz)([ ,.])/gi, '$1 MHz$3');
	obj.plain = obj.plain.replace(/(\d) ?(ghz)([ ,.])/gi, '$1 GHz$3');
	obj.plain = obj.plain.replace(/(\d) ?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|&mu;|µ|&micro;|n|p|f|a|z|y)?(hz|HZ)([\s.,:;\'\"\/\)])/g, '$1 $2Hz$4');
	obj.plain = obj.plain.replace(/(\d) ?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|&mu;|µ|&micro;|n|p|f|a|z|y)?(pa|PA)([\s.,:;\'\"\/\)])/g, '$1 $2Pa$4');

// add a space before dB
	obj.plain = obj.plain.replace(/(\d) ?(dB)([\s.,:;\'\"\/\)])/g, '$1 $2$3');

// add a space before any units that were missed before
	obj.plain = obj.plain.replace(/(\d) ?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|&mu;|µ|&micro;|n|p|f|a|z|y)?(g|m|A|K|mol|cd|rad|sr|Hz|N|J|W|Pa|lm|lx|C|V|O|F|Wb|T|H|S|Bq|Gy|Sv|kat|°C|M)([\s.,:;\'\"\/\)])/g, '$1 $2$3$4');

// separate one for seconds since they give a lot of false positives like "1970s". Only difference is mandatory prefix.
	obj.plain = obj.plain.replace(/(\d) ?(Y|Z|E|P|T|G|M|k|K|h|da|d|c|m|&mu;|µ|&micro;|n|p|f|a|z|y)(s)([\s.,:;\'\"\/\)])/g, '$1 $2$3$4');

// bps or b/s or bits/s --> bit/s
	obj.plain = obj.plain.replace(/([KkMmGgTtPpEeYyZz])(bps|bits?\/s|b\/s)/g, '$1bit/s');

// Bps or byte/s or bytes/s --> B/s
	obj.plain = obj.plain.replace(/([KkMmGgTtPpEeYyZz])(Bps|bytes?\/s)/g, '$1B/s');

// after that, make capitalization correct
	obj.plain = obj.plain.replace(/K(bit|B)\/s/g, 'k$1/s');
	obj.plain = obj.plain.replace(/m(bit|B)\/s/g, 'M$1/s');
	obj.plain = obj.plain.replace(/g(bit|B)\/s/g, 'G$1/s');
	obj.plain = obj.plain.replace(/t(bit|B)\/s/g, 'T$1/s');
	obj.plain = obj.plain.replace(/e(bit|B)\/s/g, 'E$1/s');
	obj.plain = obj.plain.replace(/y(bit|B)\/s/g, 'Y$1/s');
	obj.plain = obj.plain.replace(/z(bit|B)\/s/g, 'Z$1/s');

// fix a common error
	obj.plain = obj.plain.replace(/mibi(bit|byte)/g, 'mebi$1');

	return;
}


//
// FixDashes: dash fixer - adds a tab that fixes several obvious en/em dash, minus sign, and such special characters.
// originally from User:Omegatron
//

function FixDashes(obj) {
	FixBasic(obj);

// convert html entities into actual dash characters
	obj.plain = obj.plain.replace(/&mdash;/g, '—');
	obj.plain = obj.plain.replace(/&ndash;/g, '–');
	obj.plain = obj.plain.replace(/&minus;/g, '\u2212');

// convert -- and em dashes with or without spaces to em dash surrounded by spaces
	obj.plain = obj.plain.replace(/([a-zA-Z\'\"”\]\}\)]) *(--|—|&mdash;) *([a-zA-Z\'\"“\[\{\(])/g, '$1 — $3');

// convert - or en dashes with spaces to em dash character surrounded by spaces
	obj.plain = obj.plain.replace(/([a-zA-Z\'\"”\]\}])( |&nbsp;)+(\u2212|–|&ndash;) +([a-zA-Z\'\"“\[\{])/g, '$1$2— $4');

// convert hyphen next to lone number into a minus sign character
	obj.plain = obj.plain.replace(/([a-zA-Z\'\"”\]\>] )-(\d)/g, '$1\u2212$2');

// convert dashes to en dashes in dates
	obj.plain = obj.plain.replace(/([ \(][12]\d\d\d) ?(--?|—|&mdash;) ?([12]\d\d\d|\d\d)([ \),.;])/g, '$1–$3$4');

	return;
}


//
// FixHTML: fix html to wikicode ///////////////// obsolete, use function below
//

function FixHTML(obj) {
	FixBasic(obj);

// remove syntax highlighting
  obj.html = obj.plain;
	RemoveHighlighting(obj);

// turn visible html code into real html
	obj.html = obj.html.replace(/&lt;/g, '<');
	obj.html = obj.html.replace(/&gt;/g, '>');

// wikify
	WikifyHTML(obj);

// turn real html into visible html code
	obj.html = obj.html.replace(/<br>/g, '\n');
	obj.html = obj.html.replace(/</g, '&lt;');
	obj.html = obj.html.replace(/>/g, '&gt;');
  obj.plain = obj.html;

	return;
}


//
// FixCaps: fix capitalizing of lists, linklists, images, headings
//

function FixCaps(obj) {
	FixBasic(obj);

// uppercase lists
// start (listcode (char-ent|tag|category..|digit|non-word,non-ret))(word,non-digit..) end
	obj.plain = obj.plain.replace(/^([\*\#\:\;]+ (\&\w+\;|\<[^\n]*?\>|\{\{.*?\}\}[^\n]*|\d|[^\w\n])*)([^\W\d].*?)?$/gm,
		function (p, p1, p2, p3) {
			if ( ! p3.match(/^(http|ftp|alpha|beta|gamma|delta|epsilon|kappa|lambda|$)/) ) {
				p3 = p3.substr(0, 1).toUpperCase() + p3.substr(1);
			}
			return(p1 + p3);
		}
	);

// uppercase link lists (link)
	obj.plain = obj.plain.replace(/^([\*\#\:\;]+ \[\[)([^\n]*?)(\]\])/gm,
		function (p, p1, p2, p3) {

// uppercase link
			p2 = p2.replace(/^((\&\w+\;|\W|\d)*)([^\W\d].*)$/,
				function (p, p1, p2, p3) {
					if ( ! p3.match(/^(http|ftp|alpha|beta|gamma|delta|epsilon|kappa|lambda)/) ) {
						p3 = p3.substr(0, 1).toUpperCase() + p3.substr(1);
					}
					return(p1 + p3);
				}
			);

// uppercase comment
			p2 = p2.replace(/(\| *(\&\w+\;|<.*?>|\W|\d)*)([^\W\d].*)$/,
				function (p, p1, p2, p3) {
					if ( ! p3.match(/^(http|ftp|alpha|beta|gamma|delta|epsilon|kappa|lambda)/) ) {
						p3 = p3.substr(0, 1).toUpperCase() + p3.substr(1);
					}
					return(p1 + p3);
				}
			);
			return(p1 + p2 + p3);
		}
	);

// uppercase headings
	obj.plain = obj.plain.replace(/^(==+ (\&\w+\;|<.*?>|\d|[^\w\n])*)([^\W\d].*? ==+)$/gm,
		function (p, p1, p2, p3) {
			if ( ! p3.match(/^(http|ftp|alpha|beta|gamma|delta|epsilon|kappa|lambda)/) ) {
				p3 = p3.substr(0, 1).toUpperCase() + p3.substr(1);
			}
			return(p1 + p3);
		}
	);

// uppercase images
	obj.plain = obj.plain.replace(/(\[\[)image:(\w)([^\n]*\]\])/igm,
		function (p, p1, p2, p3) {
			return(p1 + 'Image:' + p2.toUpperCase() + p3);
		}
	);

	return;
}

//
// FixAll:
//

function FixAll(obj) {
	FixBasic(obj);
	FixUnits(obj);
	FixDashes(obj);
	FixHTML(obj);
	FixCaps(obj);
	return;
}



//
// FindBoundaries: find word boundaries and line boundaries starting from selection.range
//

function FindBoundaries(word, line, whole, selection) {

/*
textareaObj.value = '';
*/

// get the start node and offset
	var startNode = selection.range.startContainer;
	var startNodeOffset = selection.range.startOffset;

// get the end node and offset
	var endNode = selection.range.endContainer;
	var endNodeOffset = selection.range.endOffset;

	if (startNode.nodeType == 1) {
		startNode = startNode.childNodes[startNodeOffset];
		startNodeOffset = 0;
	}
	if (endNode.nodeType == 1) {
		endNode = endNode.childNodes[endNodeOffset];
		endNodeOffset = 0;
	}

// find the start and end nodes in the whole plain text arrays
	var startNodeIndex;
	var endNodeIndex;
	for (i = 0; i < whole.plainArray.length; i ++) {
		if (startNode == whole.plainNode[i]) {
			startNodeIndex = i;
		}
		if (endNode == whole.plainNode[i]) {
			endNodeIndex = i;
			break;
		}
	}

// find last previous word and line boundary
	var foundWord = false;
	var foundLine = false;
	var regExp = new RegExp('.*[^\\w\\-]', 'g');

// check text nodes left-wise for a boundary
	for (i = startNodeIndex; i >= 0; i --) {
		var plain = whole.plainArray[i];

// boundary is a newline
		if (plain == '\n') {

// current newline is the start node
			if (i == startNodeIndex) {
				if (! foundWord) {
					word.range.setStartBefore(whole.plainNode[i]);
					foundWord = true;
				}
				line.range.setStartBefore(whole.plainNode[i]);
			}
			else {
				if (! foundWord) {
					word.range.setStartAfter(whole.plainNode[i]);
					foundWord = true;
				}
				line.range.setStartAfter(whole.plainNode[i]);
			}
			foundLine = true;
			break;
		}

// check text node for a word boundary
		else if (! foundWord) {
			if (i == startNodeIndex) {
				plain = plain.substr(0, startNodeOffset);
			}
			regExp.lastIndex = 0;
			if (regExp.exec(plain) != null) {
				word.range.setStart(whole.plainNode[i], regExp.lastIndex);
				foundWord = true;
			}
		}
	}

// boundary is start of text
	if (! foundLine) {
		line.range.setStartBefore(whole.plainNode[0]);
		if (! foundWord) {
			word.range.setStartBefore(whole.plainNode[0]);
		}
	}

// find next word and line boundary
	regExp = new RegExp('[^\\w\\-]', 'g');
	foundWord = false;
	foundLine = false;

// check text nodes right-wise for a boundary
	for (i = endNodeIndex; i < whole.plainArray.length; i ++) {
		var plain = whole.plainArray[i];

// boundary is a newline
		if (plain == '\n') {
			if (! foundWord) {
				word.range.setEndBefore(whole.plainNode[i]);
				foundWord = true;
			}
			line.range.setEndBefore(whole.plainNode[i]);
			foundLine = true;
			break;
		}

// check text node for a word boundary
		else if (! foundWord) {
			if (i == endNodeIndex) {
				regExp.lastIndex = endNodeOffset;
			}
			else {
				regExp.lastIndex = 0;
			}
			var regExpArray = regExp.exec(plain);
			if (regExpArray != null) {
/*
textareaObj.value = '';
textareaObj.value += 'i: ' + i + '\n';
textareaObj.value += 'whole.plainNode[i]: ' + whole.plainNode[i] + '\n';
textareaObj.value += 'whole.plainNode[i].type: ' + whole.plainNode[i].type + '\n';
textareaObj.value += 'whole.plainNode[i].name: ' + whole.plainNode[i].name + '\n';
textareaObj.value += 'whole.plainNode[i].value: ' + whole.plainNode[i].value + '\n';
textareaObj.value += 'regExpArray.index: ' + regExpArray.index + '\n';
*/
				word.range.setEnd(whole.plainNode[i], regExpArray.index);///////////////////////// error on sourceoff ///////////////////////////////////////

// Error: uncaught exception: [Exception... "Index or size is negative or greater than the allowed amount"
// code: "1" nsresult: "0x80530001 (NS_ERROR_DOM_INDEX_SIZE_ERR)"  location: "file:///C:/editor.js Line: 1713"]

				foundWord = true;
			}
		}
	}

// boundary is end of text
	if (! foundLine) {
		line.range.setEndAfter(whole.plainNode[whole.plainArray.length - 1]);
		if (! foundWord) {
			word.range.setEndAfter(whole.plainNode[whole.plainArray.length - 1]);
		}
	}
  return;
}


//
// remove syntax highlighting and wikify
//

function RemoveHighlightingWikify(obj) {

	if (obj.html != '') {

// remove syntax highlighting
		RemoveHighlighting(obj);

// wikify
	  if (obj.htmlCode == true) {
			WikifyHTML(obj);
		}
	}
	return;
}


//
// scroll the textarea if the selected text is outside the viewport
//

function ScrollTextarea(rowStart, rowEnd, lines, margin, scrollTopPx) {

// get top row
  var scrollHeightPx = textareaObj.scrollHeight;
  var scrollTopRow = scrollTopPx / scrollHeightPx * textRows.rowTotal;

// cusor direction: up
	if (lines <= 0) {
	 	if (scrollTopRow > (rowStart + lines) - margin) {
			scrollTopRow = (rowStart + lines) - margin;
			if (scrollTopRow < 0) {
			  scrollTopRow = 0;
			}
		}
	}

// cusor direction: down
	if (lines >= 0) {
	 	if (scrollTopRow < (rowEnd + 1 + lines) + margin - textRows.rows) {
			scrollTopRow = (rowEnd + 1 + lines) + margin - textRows.rows;
			if (scrollTopRow > textRows.rowTotal + 1 - textRows.rows) {
			  scrollTopRow = textRows.rowTotal + 1 - textRows.rows;
			}
		}
	}

// set scroll position
	textareaObj.scrollTop = scrollTopRow / textRows.rowTotal * scrollHeightPx;

	return;
}


//
// ParseRows: get row structure of textarea
//

function ParseRows() {

	textRows.selStart = textareaObj.selectionStart;
	textRows.selEnd = textareaObj.selectionEnd;

// if the text has not changed we don't need to parse lines and rows
	if (textRows.changed != true) {
		if (textRows.textarea == null) {
			textRows.changed = true;
		}
		else if (textRows.textarea.length != textareaObj.value.length) {
			textRows.changed = true;
		}
		else if (textRows.textarea != textareaObj.value) {
			textRows.changed = true;
		}
	}
  if (textRows.changed) {
		textRows.changed = false
		textRows.textarea = textareaObj.value;
		textRows.cols = textareaObj.cols;
		textRows.rows = textareaObj.rows;

// parse lines
		textRows.lineStart = [];
		textRows.lineLength = [];
		var pos;
		var posNext = 0;
		var line = 0;
		do {
			pos = posNext;
			textRows.lineStart[line] = pos;
			posNext = textRows.textarea.indexOf('\n', pos) + 1;
			textRows.lineLength[line] = posNext - pos - 1;
			line ++;
		} while (posNext > 0);
		textRows.lineLength[line - 1] = textRows.textarea.length - pos;
	  textRows.lineTotal = line;

// parse rows
		textRows.rowStart = [];
		textRows.rowLength = [];
		var lineTotal = textRows.lineTotal;
		var row = 0;
		for (line = 0; line < lineTotal; line ++) {
			var rowStart;
			var rowStartNext = textRows.lineStart[line];
			var lineEnd = rowStartNext + textRows.lineLength[line];

// cycle row by row to the end of the line
			do {
				rowStart = rowStartNext;
				pos = 0;
				posNext = rowStart;
				if (rowStart + textRows.cols >= lineEnd) {
					rowStartNext = lineEnd;
				}

// find last space before or first after right border
				else {
					do {
						pos = posNext;
						posNext = textRows.textarea.indexOf(' ', pos + 1);
					} while ( (posNext >= 0) && (posNext <= rowStart + textRows.cols) && (posNext < lineEnd) );
					if (pos > rowStart) {
						rowStartNext = pos + 1;
					}
					else if ( (posNext >= 0) && (posNext < lineEnd) ) {
						rowStartNext = posNext + 1;
					}
					else {
						rowStartNext = lineEnd;
					}
				}

// jump over trailing spaces
				while (textRows.textarea.charAt(rowStartNext) == ' ') {
					rowStartNext ++;
				}

// set row start and length
				textRows.rowStart[row] = rowStart;
				textRows.rowLength[row] = rowStartNext - rowStart;
				row ++;
			} while (rowStartNext < lineEnd);
		}
  	textRows.rowTotal = row;
	}

// get text selection rows by stepwise approximation
	var rowTotal = textRows.rowTotal;
	var selStart = textRows.selStart;
	var selEnd = textRows.selEnd;

// find the largest 2^n < rows
	var add = 1;
	while (add < rowTotal) {
	  add = add * 2;
	}
  add = add / 2;

// approximate with decreasing add
	var selStartRow = add;
	var selEndRow = add;
	while (add >= 1) {

// approximate selection start
		if (selStartRow >= rowTotal) {
			selStartRow -= add;
		}
		else if (textRows.rowStart[selStartRow] > selStart) {
			selStartRow -= add;
		}
		else {
			selStartRow += add;
		}

// approximate selection end
		if (selEndRow >= rowTotal) {
			selEndRow -= add;
		}
		else if (textRows.rowStart[selEndRow] > selEnd) {
			selEndRow -= add;
		}
		else {
			selEndRow += add;
		}
	  add = add / 2;
	}
	if (textRows.rowStart[selStartRow] > selStart) {
		selStartRow --;
	}
	if (textRows.rowStart[selEndRow] > selEnd) {
		selEndRow --;
	}
	textRows.selStartRow = selStartRow;
	textRows.selEndRow = selEndRow;

	return;
}


//
// resize textarea to ~100% by adapting cols
//

function ResizeTextarea() {

	var textareaClone = document.getElementById('textareaClone');
	var scrollTopPx = textareaObj.scrollTop;
	textareaClone.style.width = '100%';
	textareaClone.style.display = 'block';
	var widthMax = textareaClone.offsetWidth;
	textareaClone.style.width = 'auto';

// find optimal width
	textareaClone.cols = 20;
	for (var i = 64; i >= 1; i = i / 2) {
		while (textareaClone.offsetWidth < widthMax) {
			textareaClone.cols = textareaClone.cols + i;
		}
		textareaClone.cols = textareaClone.cols - i;
	}
	textareaClone.style.display = 'none';
	textareaObj.cols = textareaClone.cols;
	textareaObj.style.width = 'auto';
	textareaObj.scrollTop = scrollTopPx;

// parse rows
	textRows.changed = true;
  ParseRows();

	return;
}


//
// convert strange spaces, remove non-\n linebreak characters
//

function ConvertStrangeSpaces() {

	var text = textareaObj.value;
//	text = text.replace(/[\t\v\u00a0\u2028\u2029]+/g, ' '); // \u00a0 = &nbsp;
	text = text.replace(/[\t\v\u2028\u2029]+/g, ' ');
	text = text.replace(/[\r\f]/g, '');
	textareaObj.value = text;

	return;
}



//
// wikifyHTML, obj.html contains the text to be wikified
//

/*
	allowed and converted tags:
			br|p
			h1|h2|h3|h4|h5|h6
			hr
			i|dfn|cite|em|var
			b|strong
			table|caption|col|thead|tfoot|tbody|tr|td|th
			dl|dt|dd|li|ol|ul
			a
	not allowed yet, converted to span:
			bdo|q|kbd|samp|abbr|acronym|label
	other allowed tags:
			big|blockquote|colgroup|center|code|del|div|font|ins|pre|s|small|span|strike|sub|sup|tt|u|rb|rp|rt|ruby
	mediawiki tags:
			nowiki|math|gallery|noinclude|includeonly|ref|references
*/

function WikifyHTML(obj) {

// standardize newline, remove multiple spaces
  obj.html = obj.html.replace(/<br(\b[^>]*)?>/gi, '\n');
  obj.html = obj.html.replace(/\n?\r/g, '\n');
  obj.html = obj.html.replace(/ +/g, ' ');

// escape character entities
  obj.html = obj.html.replace(/&(?!(amp;|lt;|gt;))/g, '&amp;');

// remove comments
	obj.html = obj.html.replace(/<!--.*?-->/g, '');

// <hr> horizontal rule
	obj.html = obj.html.replace(/\s*<hr(\b.*?)>\s*/gi, '\n----\n');

// <i> <em> <dfn> <var> <cite> italic
	obj.html = obj.html.replace(/<(i|em|dfn|var|cite)\b.*?>/gi, '\'\'');
	obj.html = obj.html.replace(/<\/(i|em|dfn|var|cite)\b.*?>/gi, '\'\'');

// <b> <strong> bold
	obj.html = obj.html.replace(/<(b|strong)\b.*?>/gi, '\'\'\'');
	obj.html = obj.html.replace(/<\/(b|strong)\b.*?>/gi, '\'\'\'');

// <h1> .. <h6> headings
	obj.html = obj.html.replace(/\s*<h1\b.*?>(.*?)<\/h1\b.*?>\s*/gi, '\n\n= $1 =\n\n');
	obj.html = obj.html.replace(/\s*<h2\b.*?>(.*?)<\/h2\b.*?>\s*/gi, '\n\n== $1 ==\n\n');
	obj.html = obj.html.replace(/\s*<h3\b.*?>(.*?)<\/h3\b.*?>\s*/gi, '\n\n=== $1 ===\n\n');
	obj.html = obj.html.replace(/\s*<h4\b.*?>(.*?)<\/h4\b.*?>\s*/gi, '\n\n==== $1 ====\n\n');
	obj.html = obj.html.replace(/\s*<h5\b.*?>(.*?)<\/h5\b.*?>\s*/gi, '\n\n===== $1 =====\n\n');
	obj.html = obj.html.replace(/\s*<h6\b.*?>(.*?)<\/h6\b.*?>\s*/gi, '\n\n====== $1 ======\n\n');

// <span> <div>
	obj.html = obj.html.replace(/<(span|div)\s+([^<]*)>/gi,
		function (p, p1, p2) {
			return('<' + p1 + SanitizeAttributes(p1, p2) +  '>');
		}
	);

// <td> <p> table cell, remove first paragraph
	obj.html = obj.html.replace(/(<td\b.*?>)\s*<p\b.*?>/gi, '$1');

// <thead> <tbody> <tfoot>
	obj.html = obj.html.replace(/<\/?(thead|tbody|tfoot)\b.*?>/gi, '');

// <td> table cells
	obj.html = obj.html.replace(/\s*<td\s*>\s*/gi, '\n| ');
	obj.html = obj.html.replace(/\s*<(td)\s+([^>]*?)>\s*/gi,
		function (p, p1, p2) {
			return('\n|' + SanitizeAttributes(p1, p2) + ' | ');
		}
	);

// <th> table cells
	obj.html = obj.html.replace(/\s*<th\s*>\s*/gi, '\n| ');
	obj.html = obj.html.replace(/\s*<(th)\s+([^>]*?)>\s*/gi,
		function (p, p1, p2) {
			return('\n!' + SanitizeAttributes(p1, p2) + ' | ');
		}
	);

// <tr> table rows
	obj.html = obj.html.replace(/\s*<tr\s*>\s*/gi, '\n|-\n');
	obj.html = obj.html.replace(/\s*<(tr)\s+([^>]*?)>\s*/gi,
		function (p, p1, p2) {
			return('\n|-' + SanitizeAttributes(p1, p2) + '\n');
		}
	);

// <caption> captionCaption
	obj.html = obj.html.replace(/\s*<caption\s*>\s*/gi, '\n|+ ');
	obj.html = obj.html.replace(/\s*<(caption)\s+([^>]*?)>\s*/gi,
		function (p, p1, p2) {
			return('\n|+' + SanitizeAttributes(p1, p2) + ' | ');
		}
	);

// <table> tables
	obj.html = obj.html.replace(/\s*<table\s*>\s*(\|-\n)?/gi, '\n\n{|\n');
	obj.html = obj.html.replace(/\s*<(table)\s+([^>]*?)>\s*(\|-\n)?/gi,
		function (p, p1, p2) {
			return('\n{|' + SanitizeAttributes(p1, p2) + '\n');
		}
	);
	obj.html = obj.html.replace(/\s*<\/table>\s*/gi, '\n|}\n\n');

/*
	div style="width:172px;">
	a href="/wiki/Image:Sangreal.jpg" class="internal" title="The Holy Grail, illustration by Arthur Rackham, 1917">
	img src="http://upload.wikimedia.org/wikipedia/en/thumb/1/19/Sangreal.jpg/170px-Sangreal.jpg" alt="The Holy Grail, illustration by Arthur Rackham, 1917" width="170" height="246" longdesc="/wiki/Image:Sangreal.jpg" />
	/a>

	[[Image:Sangreal.jpg|thumb|170px|left|The [[Holy Grail]], illustration by [[Arthur Rackham]], 1917]]
*/

// convert images
	obj.html = obj.html.replace(/<img\b([^>]*)>/gi,
		function (p, p1) {

// get and format parameters
			var address = '';
			var regExpMatch = /\bsrc\s*=\s*(\'|\")\s*(.*?)\s*(\'|\")/i.exec(p1);
			if (regExpMatch != null) {
				address = regExpMatch[2];
			}

			var alt = '';
			regExpMatch = /\balt\s*=\s*(\'|\")\s*(.*?)\s*(\'|\")/i.exec(p1);
			if (regExpMatch != null) {
				alt = regExpMatch[2];
				alt = alt.replace(/&amp;nbsp;|\n/g, ' ');
				alt = alt.replace(/ {2,}/g, ' ');
				alt = alt.replace(/^ | $/g, '');
				if (alt != '') {
					alt = '|' + alt;
				}
			}

			var imgWidth = '';
			regExpMatch = /\bwidth\s*=\s*(\'|\")\s*(.*?)\s*(\'|\")/i.exec(p1);
			if (regExpMatch != null) {
				imgWidth = '|' + regExpMatch[2] + 'px';
			}

			var longDesc = '';;
			regExpMatch = /\blongdesc\s*=\s*(\'|\")\s*(.*?)\s*(\'|\")/i.exec(p1);
			if (regExpMatch != null) {
				longDesc = regExpMatch[2];
			}

// wiki image
			var image = '';
			regExpMatch = /^\/wiki\/Image:(.*)$/.exec(longDesc);
			if (regExpMatch != null) {
				image = regExpMatch[1];
			}

// external image
			else {
				regExpMatch = /([^\/]+)$/.exec(address);
				if (regExpMatch != null) {
					image = regExpMatch[1];
				}
			}

			if (image != '') {
				return('[[Image:' + image + imgWidth + alt + ']]');
			}
			return('');
		}
	);

// convert links
	obj.html = obj.html.replace(/<a\b([^>]*)>(.*?)<\/a>/gi,
		function (p, p1, p2) {

// get and format parameters
			var address = '';;
			var regExpMatch = /\bhref\s*=\s*(\'|\")\s*(.*?)\s*(\'|\")/i.exec(p1);
			if (regExpMatch != null) {
				address = regExpMatch[2];
			}
			if (address == '') {
				return('');
			}

			var linkText = p2;
			linkText = linkText.replace(/&amp;nbsp;|\n/g, ' ');
			linkText = linkText.replace(/ {2,}/g, ' ');
			linkText = linkText.replace(/^ | $/g, '');
			if ( /\[image:(.*?)\]/i.exec(linkText) ) {
				return(linkText);
			}

// wikilink
			regExpMatch = /^(https?|file):\/\/[\w\.\/]*\/wiki\/(.*)$/.exec(address);
			if (regExpMatch != null) {
				var article = regExpMatch[2].replace(/_/g, ' ');
				article = decodeURIComponent(article);
				article = article.replace(/&/g, '&amp;');
				if (linkText == '') {
					return('[[' + article + ']]');
				}
				if ( ( (article.substr(0, 1).toLowerCase() + article.substr(1)) == linkText )
					|| ( (article.substr(0, 1).toUpperCase() + article.substr(1)) == linkText ) ) {
					return('[[' + linkText + ']]');
				}
				if ( ( (article.substr(0, 1).toLowerCase() + article.substr(1)) == linkText.substr(0, linkText.length - 1) )
					|| ( (article.substr(0, 1).toUpperCase() + article.substr(1)) == linkText.substr(0, linkText.length - 1) ) ) {
					return( '[[' + linkText.substr(0, linkText.length - 1) + ']]' + linkText.substr(linkText.length - 1) );
				}
				return('[[' + article + '|' + linkText + ']]');
			}

// normal link
			if ( (linkText == '') || ( (address.substr(0, 1).toLowerCase() + address.substr(1)) == linkText ) || ( (address.substr(0, 1).toUpperCase() + address.substr(1)) == linkText ) ) {
				return('[' + address + ']');
			}
			return('[' + address + ' ' + linkText + ']');
		}
	);

// convert lists(.*?)<(\/?(ol|ul|li))>/gi
	var listObj = {};
	listObj.prefix = '';
	obj.html = obj.html.replace(/(.*?) *<(\/?(ol|ul|li))(\b[^>]*)> */gi,
		function (p, p1, p2, p3, p4) {
			switch (p2.toLowerCase()) {
				case 'ol':
					listObj.prefix += '#';
					return('\n\n');
				case 'ul':
					listObj.prefix += '*';
					return('\n\n');
				case '/ol':
				case '/ul':
					listObj.prefix = listObj.prefix.substr(0, listObj.prefix.length - 1);
					return('\n\n');
				case 'li':
					return(p1 + '\n' + listObj.prefix + ' ');
				case '/li':
					return(p1);
			}
		}
	);

// <p> paragraph
  obj.html = obj.html.replace(/(.)<p\b.*?>/gi, '$1\n\n');
  obj.html = obj.html.replace(/<\/p\b.*?>/gi, '');

// <> remove not allowed tags
	obj.html = obj.html.replace(/(<\/?)(\/?)(\w+)([^>]*>)/g,
		function (p, p1, p2, p3, p4) {
			if ( /^(big|blockquote|colgroup|center|code|del|div|font|ins|pre|s|small|span|strike|sub|sup|tt|u|rb|rp|rt|ruby|nowiki|math|gallery|noinclude|includeonly|ref|references)$/i.test(p3) ) {
				return(p1 + p2 + p3 + p4);
			}
			else {
				return('');
			}
		}
	);

// show html tags
  obj.html = obj.html.replace(/</g, '&lt;');
  obj.html = obj.html.replace(/>/g, '&gt;');

// newlines to <br>
  obj.html = obj.html.replace(/\n{3,}/g, '\n\n');
  obj.html = obj.html.replace(/\n/g, '<br>');

	return;
}


//
// SanitizeAttributes: see Sanitizer.php
//

function SanitizeAttributes(tag, attributes) {

	var common = 'lang|dir|style|class'; // not needed: id|title
	var tablealign = '|align|char|charoff|valign';
	var tablecell = '|abbr|axis|headers|scope|rowspan|colspan|nowrap|width|height|bgcolor';
	tag = tag.toLowerCase();
	var sanitized = '';
	var regExp = /(\w+)\s*=\s*(\'|\")(.*?)(\'|\")/g;
	var regExpMatch;
	while (regExpMatch = regExp.exec(attributes)) {
	  var attrib = regExpMatch[1];
	  var attribValue = regExpMatch[3];
	  if (attribValue == '') {
	  	continue;
	  }
		var valid = false;
		if ('center|em|strong|cite|code|var|sub|supdl|dd|dt|tt|b|i|big|small|strike|s|u|rb|rp|ruby'.indexOf(tag) >= 0) {
			if (common.indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('div|span|h1|h2|h3|h4|h5|h6|p'.indexOf(tag) >= 0) {
			if ((common + '|align').indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('blockquote'.indexOf(tag) >= 0) {
			if ((common + '|cite').indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('br'.indexOf(tag) >= 0) {
			if ('style|clear'.indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('pre'.indexOf(tag) >= 0) {
			if ((common + '|width').indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('ins|del'.indexOf(tag) >= 0) {
			if ((common + '|cite|datetime').indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('ul'.indexOf(tag) >= 0) {
			if ((common + '|type').indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('ol'.indexOf(tag) >= 0) {
			if ((common + '|type|start').indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('li'.indexOf(tag) >= 0) {
			if ((common + '|type|value').indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('table'.indexOf(tag) >= 0) {
			if ((common + '|summary|width|border|frame|rules|cellspacing|cellpadding|align|bgcolor|frame|rules|border').indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('caption'.indexOf(tag) >= 0) {
			if ((common + '|align').indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('thead|tfoot|tbody'.indexOf(tag) >= 0) {
			if ((common + tablealign).indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('colgroup|col'.indexOf(tag) >= 0) {
			if ((common + '|span|width' + tablealign).indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('tr'.indexOf(tag) >= 0) {
			if ((common + '|bgcolor' + tablealign).indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('td|th'.indexOf(tag) >= 0) {
			if ((common + tablecell + tablealign).indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('font'.indexOf(tag) >= 0) {
			if ((common + '|size|color|face').indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('hr'.indexOf(tag) >= 0) {
			if ((common + '|noshade|size|width').indexOf(attrib) >= 0) { valid = true; }
		}
		else if ('rt'.indexOf(tag) >= 0) {
			if ((common + '|rbspan').indexOf(attrib) >= 0) { valid = true; }
		}
		if (valid == true) {

// remove non-standard styles and clean up
			if (attrib == 'style') {
				attribValue = attribValue.replace(/ *(\-moz[\w\-]+|windowtext) */g, '');
				attribValue = attribValue.replace(/\b0(%|in|cm|mm|em|ex|pt|pc|px)\b/g, '0');
				attribValue = attribValue.replace(/[\w\-]+ *\: *\; */g, '');
				attribValue = attribValue.replace(/ *(;|:) */g, '$1 ');
				attribValue = attribValue.replace(/( |;)+$/g, ';');
			}
			sanitized += ' ' + attrib + '="' + attribValue + '"';
		}
	}
	return(sanitized);
}


//
//
// RemoveHighlighting: remove syntax highlighting in obj.plain; sets obj.htmlCode if text contains html code
//

function RemoveHighlighting(obj) {

// remove highlighting-only div and span
  var isRemove = [];
  obj.html = obj.html.replace(/(<(\/?)(div|span)(.*?)>)/g,
  	function (p, p1, p2, p3, p4) {
			if (p2 == '') {
				if ( p4.match(/\bclass=\"rte\w+\"/) ) {
					isRemove.push(true);
					return('');
				}
				else if (p4 == '') {
					isRemove.push(true);
					return('');
				}
				isRemove.push(false);
				return(p1);
			}
			if (isRemove.pop() == true) {
				return('');
			}
			return(p1);
		}
	);
  obj.html = obj.html.replace(/<!--rte\w+-->/g, '');
  obj.html = obj.html.replace(/\n|\r/g, '');
  if (obj.html.match(/<(?!br)/)) {
  	obj.htmlCode = true;
  }
  else {
  	obj.htmlCode = false;
	}
  return;
}


//
//
// HighlightSyntax: highlight syntax in obj.html; if singleLine is set, no block syntax will be highlighted; call RemoveHighlighting first
//

function HighlightSyntax(obj, singleLine) {

// convert newlines
  obj.html = obj.html.replace(/\n|\r/g, '');
  obj.html = obj.html.replace(/<br(\b[^>]*)?>/g, '\n');

// escape character entities
  obj.html = obj.html.replace(/&(?!(amp;|lt;|gt;))/g, '&amp;');

// <sup> </sub> <ins> <del>
	obj.html = obj.html.replace(/((&lt;)sup\b.*?(&gt;)(.*?)(&lt;)\/sup\b.*?(&gt;))/gi, '<span class="rteSup">$1</span><!--rteSup-->');
	obj.html = obj.html.replace(/((&lt;)sub\b.*?(&gt;)(.*?)(&lt;)\/sub\b.*?(&gt;))/gi, '<span class="rteSub">$1</span><!--rteSub-->');
	obj.html = obj.html.replace(/((&lt;)(ins|u)\b.*?(&gt;)(.*?)(&lt;)\/(ins|u)\b.*?(&gt;))/gi, '<span class="rteIns">$1</span><!--rteIns-->');
	obj.html = obj.html.replace(/((&lt;)(del|s|strike)\b.*?(&gt;)(.*?)(&lt;)\/(del|s|strike)\b.*?(&gt;))/gi, '<span class="rteDel">$1</span><!--rteDel-->');

// various inlines
	obj.html = obj.html.replace(/(&lt;\/?(sub|sup|ins|u|del|s|strike|big|br|colgroup|code|font|small|span|tt|rb|rp|rt|ruby)\b.*?&gt;)/gi, '<span class="rteInline">$1</span><!--rteInline-->');

// unsupported or not needed <> tags
	obj.html = obj.html.replace(/(&lt;\/?)(\w+)(.*?\/?&gt;)/g,
		function (p, p1, p2, p3) {
			if ( ! /^(col|thead|tfoot|tbody|big|br|blockquote|colgroup|center|code|del|div|font|ins|pre|s|small|span|strike|sub|sup|tt|u|rb|rp|rt|ruby|nowiki|math|gallery|noinclude|includeonly|ref|references)$/i.test(p2) ) {
				p1 = '<span class="rteUnknown">' + p1;
				p3 = p3 + '</span><!--rteUnknown-->';
			}
			return(p1 + p2 + p3);
		}
	);

// comments
	obj.html = obj.html.replace(/(&lt;!--.*?--&gt;)/g, '<span class="rteComment">$1</span><!--rteComment-->');

// <nowiki> <math> <noinclude> <includeonly> <ref> <references> wiki markup
	obj.html = obj.html.replace(/((&lt;)(nowiki|math|gallery|noinclude|includeonly|ref|references)\b.*?(&gt;)(.*?)(&lt;)\/(nowiki|math|gallery|noinclude|includeonly|ref|references)\b.*?(&gt;))/gi, '<span class="rteWiki">$1</span><!--rteWiki-->');

// [] URL links
	obj.html = obj.html.replace(/(\[?)((http:\/\/|https:\/\/|ftp:\/\/|irc:\/\/|gopher:\/\/|news:|mailto:)\S*[\w\/])([^\]*?)([^\n\]]*)(.)/gi,
		function (p, p1, p2, p3, p4, p5) {

// link URL, text
			p2 = p2.replace(/(.*)/, '<span class="rteURLLink">$1</span><!--rteURLLink-->');
			p4 = p4.replace(/^(\s*)(.*?)(\s*)$/, '$1<span class="rteURLText">$2</span><!--rteURLText-->$3');

// link tags
			if ( (/\[/.test(p1)) && (/\]/.test(p5)) ) {
				p1 = p1.replace(/(\[)/, '<span class="rteLinkTag">$1</span><!--rteLinkTag-->');
				p5 = p5.replace(/(\])/, '<span class="rteLinkTag">$1</span><!--rteLinkTag-->');
			}
			return(p1 + p2 + p4 + p5);
		}
	);

// [[ ]] links, images, categories
	obj.html = obj.html.replace(/(\[\[)([^\]]*)(\]\])/g,
		function (p, p1, p2, p3) {

// image
			if ( /^\s*image:/i.test(p2) ) {
				if (p2.match(/^(\s*)(.*:)+/)) {
					p1 = '<span class="rteImageInter">' + p1;
					p3 = p3 + '</span><!--rteImageInter-->';
				}
				else {
					p1 = '<span class="rteImage">' + p1;
					p3 = p3 + '</span><!--rteImage-->';
				}
				p2 = p2.replace(/^(\s*)(.*:)+/, '$1<span class="rteInter">$2</span><!--rteInter-->');
				p2 = p2.replace(/(\s*)([^>:\|]+)(\s*\|\s*|$)/, '$1<span class="rteImageName">$2</span><!--rteImageName-->$3');
				p2 = p2.replace(/(\|\s*)(.*)/,
					function (p, p1, p2) {
						p2 = p2.replace(/(.*?)(\s*(\||$))/g, '<span class="rteImageText">$1</span><!--rteImageText-->$2');
						return(p1 + p2);
					}
				);
			}

// category
			else if ( /^\s*category:/i.test(p2) ) {
				if (p2.match(/^(\s*)(.*:)+/)) {
					p1 = '<span class="rteCatInter">' + p1;
					p3 = p3 + '</span><!--rteCatInter-->';
				}
				else {
					p1 = '<span class="rteCat">' + p1;
					p3 = p3 + '</span><!--rteCat-->';
				}
				p2 = p2.replace(/^(\s*)(.*:)+/, '$1<span class="rteInter">$2</span><!--rteInter-->');
				p2 = p2.replace(/(\s*)([^>:\|]+)(\s*\|\s*|$)/, '$1<span class="rteCatName">$2</span><!--rteCatName-->$3');
				p2 = p2.replace(/(\|\s*)(.*)/,
					function (p, p1, p2) {
						p2 = p2.replace(/(.*?)(\s*(\||$))/g, '<span class="rteCatText">$1</span><!--rteCatText-->$2');
						return(p1 + p2);
					}
				);
			}

// wikilink
			else {
				if (p2.match(/^(\s*)(.*:)+/)) {
					p1 = '<span class="rteLinkInter">' + p1;
					p3 = p3 + '</span><!--rteLinkInter-->';
				}
				else {
					p1 = '<span class="rteLink">' + p1;
					p3 = p3 + '</span><!--rteLink-->';
				}
				p2 = p2.replace(/^(\s*)(.*:)+/, '$1<span class="rteInter">$2</span><!--rteInter-->');
				p2 = p2.replace(/(\s*)([^>:\|]+)(\s*\|\s*|$)/, '$1<span class="rteLinkName">$2</span><!--rteLinkName-->$3');
				p2 = p2.replace(/(\|\s*)(.*)/,
					function (p, p1, p2) {
						p2 = p2.replace(/(.*?)(\s*(\||$))/g, '<span class="rteLinkText">$1</span><!--rteLinkText-->$2');
						return(p1 + p2);
					}
				);
			}

// link tags
			p1 = p1.replace(/(\[+)/, '<span class="rteLinkTag">$1</span><!--rteLinkTag-->');
			p2 = p2.replace(/(\|)/gi, '<span class="rteLinkTag">$1</span><!--rteLinkTag-->');
			p3 = p3.replace(/(\]+)/, '<span class="rteLinkTag">$1</span><!--rteLinkTag-->');
			return(p1 + p2 + p3);
		}
	);

// {{ }}, {{{ }}} templates
	obj.html = obj.html.replace(/(\{\{+)([^\}]*)(\}\}+)/g,
		function (p, p1, p2, p3) {
			if (p2.match(/^(\s*)(.*:)+/)) {
				p1 = '<span class="rteTemplInter">' + p1;
				p3 = p3 + '</span><!--rteTemplInter-->';
			}
			else {
				p1 = '<span class="rteTempl">' + p1;
				p3 = p3 + '</span><!--rteTempl-->';
			}
			p2 = p2.replace(/^(\s*)(.*:)+/, '$1<span class="rteInter">$2</span><!--rteInter-->');
			p2 = p2.replace(/(\s*)([^>:\|]+)(\s*\|\s*|$)/, '$1<span class="rteTemplName">$2</span><!--rteTemplName-->$3');
			p2 = p2.replace(/(\|\s*)(.*)/,
				function (p, p1, p2) {
					p2 = p2.replace(/(.*?)(\s*(\||$))/g, '<span class="rteTemplText">$1</span><!--rteTemplText-->$2');
					return(p1 + p2);
				}
			);

// template tags
			p1 = p1.replace(/(\{+)/, '<span class="rteLinkTag">$1</span><!--rteLinkTag-->');
			p2 = p2.replace(/(\|)/g, '<span class="rteLinkTag">$1</span><!--rteLinkTag-->');
			p3 = p3.replace(/(\}+)/, '<span class="rteLinkTag">$1</span><!--rteLinkTag-->');
			return(p1 + p2 + p3);
		}
	);

// #redirect
	obj.html = obj.html.replace(/(^|\n)(#)(redirect\b)/g, '$1<span class="rteWikiRedir">$3</span><!--rteWikiRedir-->');

// various blocks
	if (singleLine != true) {
		obj.html = obj.html.replace(/(&lt;(blockquote|center|div|pre)\b.*?&gt;)/gi, '<div class="rteBlock">$1');
		obj.html = obj.html.replace(/(&lt;\/(blockquote|center|div|pre)\b.*?&gt;)/gi, '$1</div><!--rteBlock-->');
	}

// lists * # : ;
	obj.html = obj.html.replace(/^([\*\#\:\;]+)(.*?)$/gm, '<span class="rteListLine"><span class="rteListTag">$1</span><!--rteListTag-->$2</span><!--rteListLine-->');
	if (singleLine != true) {
		obj.html = obj.html.replace(/((<span class=\"rteListLine\">.*?\n)+)/g, '<div class="rteListBlock">$1');
		obj.html = obj.html.replace(/(<span class=\"rteListLine\">.*?)(\n)(?!<span class=\"rteListLine\">)/g, '$1</div><!--rteListBlock-->$2');
	}

// #redirect (finish)
	obj.html = obj.html.replace(/(<span class=\"rteWikiRedir\">)(.*?<\/span><!--rteWikiRedir-->)/g, '$1#$2');

// space-pre
	if (singleLine != true) {
		obj.html = obj.html.replace(/^( +)(.*?)$/gm, '<span class="rteSpaceLine"><span class="rteSpaceTag">$1</span><!--rteSpaceTag-->$2</span><!--rteSpaceLine-->');
		obj.html = obj.html.replace(/((<span class=\"rteSpaceLine\">.*?\n)+)/g, '<div class="rteSpaceBlock">$1');
		obj.html = obj.html.replace(/(<span class=\"rteSpaceLine\">.*?)(\n)(?!<span class="rteSpaceLine">)/g, '$1</div><!--rteSpaceBlock-->$2');
	}

// ---- <hr> horizontal rule
	obj.html = obj.html.replace(/(^|\n|<[^>]*?>)(----)(\n|<[^>]*?>|$)/g, '$1<div class="rteHR">$2</div><!--rteHR-->$3');
	obj.html = obj.html.replace(/(&lt;hr&gt;)/g, '<span class="rteHRInline">$1</span><!--rteHRInline-->');

// == headings
	obj.html = obj.html.replace(/(^|\n|<[^>].*?>)(=+ *)([^\n]*?)( *=+ *)(\n|<[^>]*?>|$)/g,
		function (p, p1, p2, p3, p4, p5) {
			p2 = p2.replace(/(=+)/g, '<span class="rteWiki">$1</span><!--rteWiki-->');
			p4 = p4.replace(/(=+)/g, '<span class="rteWiki">$1</span><!--rteWiki-->');
			if ( /^(external links?|see also|references?)$/i.test(p3) ) {
				p1 = p1 + '<div class="rteHeadingWp">';
				p5 = '</div><!--rteHeadingWp-->' + p5;
			}
			else {
				p1 = p1 + '<div class="rteHeading">';
				p5 = '</div><!--rteHeading-->' + p5;
			}
			return(p1 + p2 + p3 + p4 + p5);
		}
	);

// tables                       (    |    |    |  |    |  )
	obj.html = obj.html.replace(/^(\{\||\|\+|\|\-|\!|\|\}|\|)(.*?)$/gm, '<span class="rteTableLine"><span class="rteTableTag">$1</span><!--rteTableTag-->$2</span><!--rteTableLine-->');
	if (singleLine != true) {
		obj.html = obj.html.replace(/(^|\n)((<[^>]*?>)*\{\|)/g, '$1<div class="rteTableBlock">$2');
		obj.html = obj.html.replace(/(^|\n)((<[^>]*?>)*\|\}(<[^>]*?>)*)/g, '$1$2</div><!--rteTableBlock-->');
		obj.html = obj.html.replace(/(&lt;table\b.*?&gt;)/gi, '<div class="rteTableBlock">$1');
		obj.html = obj.html.replace(/(&lt;\/table\b.*?&gt;)/gi, '$1</div><!--rteTableBlock-->');
	}

// <gallery> wiki markup
	if (singleLine != true) {
		obj.html = obj.html.replace(/(&lt;(gallery)\b.*?&gt;)/gi, '$1<div class="rteWiki">$1');
		obj.html = obj.html.replace(/(&lt;\/(gallery)\b.*?&gt;)/gi, '$1</div><!--rteWiki-->');
	}

// <b> <i>
	obj.html = obj.html.replace(/(\'\'\')(\'*)(.*?)(\'*)(\'\'\')/g, '<span class="rteBold">$2$3$4</span><!--rteBold-->');
	obj.html = obj.html.replace(/(\'\')(.*?)(\'\')/g, '<span class="rteItalic">$1$2$3</span><!--rteItalic-->');
	obj.html = obj.html.replace(/(<span class=\"rteBold\">)/g, '$1\'\'\'');
	obj.html = obj.html.replace(/(<\/span><!--rteBold-->)/g, '\'\'\'$1');
	obj.html = obj.html.replace(/(\'{2,})/g, '<span class="rteWiki">$1</span><!--rteWiki-->');

// named colors
  obj.html = obj.html.replace(/(\b(red|orange|yellow|fuchsia|white|lime|aqua|silver)\b)/g, '<span style="background-color: $1;" class="rteColors">$1</span><!--rteColors-->');
  obj.html = obj.html.replace(/(\b(maroon|olive|purple|green|navy|teal|blue|black|gray)\b)/g, '<span style="color: white; background-color: $1;" class="rteColors">$1</span><!--rteColors-->');

// RGB colors
  obj.html = obj.html.replace(/(#[0-9a-fA-F]{6})([\s\'\";])/g, '<span style="background-color: $1;" class="rteColors">$1</span><!--rteColors-->$2');
  obj.html = obj.html.replace(/(rgb\(\s*\d+,\s*\d+,\s*\d+\s*\))/gi, '<span style="background-color: $1;" class="rteColors">$1</span><!--rteColors-->');

// newlines to <br>
  obj.html = obj.html.replace(/\n/g, '<br>');

// remove comments ///////
  obj.html = obj.html.replace(/<!--rte\w+-->/g, ''); ///////

// remove <span> ... </span>
  var isRemove = [];
  obj.html = obj.html.replace(/(&lt;(\/?)span(.*?)&gt;)/g,
  	function (p, p1, p2, p3) {
			if (p2 == '') {
				if (p3 == '') {
					isRemove.push(true);
					return('');
				}
				isRemove.push(false);
				return(p1);
			}
			if (isRemove.pop() == true) {
				return('');
			}
			return(p1);
		}
	);

	return;
}


//
// HighlightSyntaxFrame: highlight syntax in frame
//

function HighlightSyntaxFrame() {

	if (highlightSyntax != true) {
		return;
	}

// get the selection
  var obj = {};
	obj.sel = frameWindow.getSelection();

// highlight the whole frame
  if (obj.sel.isCollapsed) {
  	obj.html = frameBody.innerHTML;

// remove syntax highlighting
		RemoveHighlighting(obj);

// wikify, highlight, and replace
	  if (obj.htmlCode == true) {
			WikifyHTML(obj);
		}
		HighlightSyntax(obj);
	  FrameExecCommand('selectall');
		if (obj.html != '') {
		  FrameExecCommand('inserthtml', obj.html);
		}
		else {
		  FrameExecCommand('delete');
		}
	}

// highlight a selection
	else {
		obj.selRange = obj.sel.getRangeAt(obj.sel.rangeCount - 1);
		var documentFragment = obj.selRange.cloneContents();

// get the inner HTML of the document fragment
		GetInnerHTML(obj, documentFragment);
		if (obj.html != '') {

// remove syntax highlighting
			RemoveHighlighting(obj);

// wikify, highlight, and replace selection
		  if (obj.htmlCode == true) {
				WikifyHTML(obj);
			}
			HighlightSyntax(obj);
			if (obj.html != '') {
			  FrameExecCommand('inserthtml', obj.html);
			}
			else {
			  FrameExecCommand('delete');
			}
		}
	}

// get new cursor container and offset
	obj.startPos = obj.startContainerPos + obj.selRange.startOffset;
	obj.endPos = obj.endContainerPos + obj.selRange.endOffset;
	obj.selRange = null;
	GetTextPosContainer(obj, frameBody);
/*
// set the new cursor position
	var range = document.createRange();
	range.setStart(obj.startContainer, obj.startOffset);
	range.setEnd(obj.endContainer, obj.endOffset);
	obj.sel.removeAllRanges();
	obj.sel.addRange(range);
*/
// focus the frame
	frameWindow.focus();

  return;
}


//
// UpdateTextarea: copy frame content to textarea
//

function UpdateTextarea() {

// reset frame timeout
	window.clearTimeout(timeoutIdUpdateTextarea);
	timeoutIdUpdateTextarea = null;

// remove syntax highlighting
  var text = frameBody.innerHTML;
  text = text.replace(/<br\b.*?>/gi, '\n');
  text = text.replace(/<(!--.*?--)>;/g, '&lt;$1&gt;');
  text = text.replace(/<.*?>/g, '');
  text = text.replace(/&nbsp;/g, ' ');
  text = text.replace(/&lt;/g, '<');
  text = text.replace(/&gt;/g, '>');
  textareaObj.value = text;
	ParseRows();
}


//
// KeyFrame: event handler for keypress in frame
//

function KeyFrame(event) {

	return;
}


//
// SetFrameSelection: wrapper for execCommand method
//

function FrameExecCommand(command, option) {

	frameDocument.execCommand(command, false, option);
  return;
}


//
// FindAhead: find non-regexp text as you type, event handler for find field
//

function FindAhead() {

  if (document.getElementById('findAhead').checked == true) {
    if (document.getElementById('regExp').checked != true) {

// get the find text
			var find = document.getElementById('findText');
			var findText = find.value;

// get checkboxes
			var caseSensitive = document.getElementById('caseSensitive');
			var regExp = document.getElementById('regExp');

// replace special characters for regexp search
			findText = findText.replace(/([\\^\$\*\+\?\.\(\)\[\]\{\}\:\=\!\|\,\-])/g, '\\$1');
			if (findText != '') {

// get whole plain text
				var obj = {};
			 	obj.sel = frameWindow.getSelection();
				ParseDOM(obj, frameBody);

// set regexp flags
				var regExpFlags = 'g';
				if ( ! caseSensitive.checked ) {
					regExpFlags += 'i';
				}

// create regexp
				var regExpFind = new RegExp(findText, regExpFlags);

// set start position for search to right
				regExpFind.lastIndex = obj.plainAnchor;

// execute the regexp search to the right
				var result = regExpFind.exec(obj.plain);

// search from start
				if (result == null) {
					regExpFind.lastIndex = 0;
					result = regExpFind.exec(obj.plain);
				}

// collapse the selection for no find
				if (result == null) {
					var range = obj.sel.getRangeAt(obj.sel.rangeCount - 1);
					obj.sel.collapse(range.startContainer, range.startOffset);
				}

// select the find
				else {

// make changed range text the current selection
					i = 0;
					while ( (obj.plainStart[i + 1] <= result.index) && (obj.plainStart[i + 1] != null) ) {
						i ++;
					}
					j = i;
					while ( (obj.plainStart[j + 1] <= result.index + result[0].length) && (obj.plainStart[j + 1] != null) ) {
						j ++;
					}
/*
textareaObj.value = 'i: ' + i + '\n';
textareaObj.value += 'j: ' + j + '\n';
textareaObj.value += 'obj.plainStart[i]: ' + obj.plainStart[i] + '\n';
textareaObj.value += 'obj.plainArray[i]: ' + obj.plainArray[i] + '\n';
textareaObj.value += 'result.index: ' + result.index + '\n';
textareaObj.value += 'obj.plainStart[i+1]: ' + obj.plainStart[i+1] + '\n';
textareaObj.value += 'obj.plainArray[i+1]: ' + obj.plainArray[i+1] + '\n';

textareaObj.value += 'obj.plainStart[j+1]: ' + obj.plainStart[j+1] + '\n';
textareaObj.value += 'obj.plainArray[j+1]: ' + obj.plainArray[j+1] + '\n';
textareaObj.value += 'result[0]: ' + result[0] + '\n';
textareaObj.value += 'obj.plain: ' + obj.plain + '\n';
*/

					obj.sel.removeAllRanges();
					var range = document.createRange();
					range.setStart(obj.plainNode[i], result.index - obj.plainStart[i]);
					range.setEnd  (obj.plainNode[j], result.index + result[0].length - obj.plainStart[j]);
					obj.sel.addRange(range);

// var range = obj.sel.getRangeAt(obj.sel.rangeCount - 1);
// obj.sel.collapse(range.startContainer, range.startOffset);

//textareaObj.value = obj.plainNode[i].offsetLeft;
//FrameExecCommand('inserthtml', '-');


/*
// scroll the textarea to the selected text or cursor position
					ParseRows();
					if (textRows.selStartRow >= textRows.rows) {
					  ScrollTextarea(textRows.selStartRow, textRows.selEndRow, 0, 0, 0);
					}
					else {
					  ScrollTextarea(textRows.selStartRow, textRows.selEndRow, 0, 0, textareaObj.scrollHeight);
					}
*/
				}
			}
		}
	}
	return;
}


//
// ClickTextArea: event handler for textarea clicks
//

function ClickTextArea(event) {

// reset cursor memory
  textRows.cursorMemory = null;
	return;
}


//
// KeyTextArea: event handler for textarea keypresses
//

function KeyTextArea(event) {

// 'jump to last change' function
	if (event.type == 'keyup') {

// left, right, up, down, page up, page down;
		switch (event.keyCode) {
			case 37: ; case 39: ; case 38: ; case 33: ; case 40: ; case 34: break;
			default:
			if (event.charCode != null) {
				lastChangePos = textareaObj.selectionStart;
			}
  	}
 	}

// cursor memory function
	else if (event.type == 'keypress') {

// check if cursor memory has been enabled
		if (cursorMemory != true) {
			return;
		}

// left, right
		if ( (event.keyCode == 37) || (event.keyCode == 39) ) {
			textRows.cursorMemory = null;
		}

// up, down, page up, page down; contains a workaround for a bug that misplaces cusor in empty lines
		else if ( (event.keyCode == 38) || (event.keyCode == 40) || (event.keyCode == 33) || (event.keyCode == 34) ) {
			ParseRows();
			var row = textRows.selStartRow;
			var col;
			if (textRows.cursorMemory != null) {
				col = textRows.cursorMemory;
			}
			else {
				col = textRows.selEnd - textRows.rowStart[row];
				textRows.cursorMemory = col;
			}
			var lines;

// up, down, page up, page down
			switch (event.keyCode) {
				case 38: lines = -1; break;
				case 33: lines = scrollMargin - textRows.rows; break;
				case 40: lines = 1; break;
				case 34: lines = textRows.rows - scrollMargin;
			}
			if ( ( (lines < 0) && (row > 0) ) || ( (lines > 0) && (row < textRows.rowTotal) ) ) {
				row = row + lines;
				if (row < 0) {
					row = 0;
				}
				else if (row > textRows.rowTotal) {
					row = textRows.rowTotal;
				}
				var pos;
				if (textRows.rowLength[row] >= col) {
					pos = textRows.rowStart[row] + col;
					if (!event.metaKey && !event.shiftKey && !event.ctrlKey) {
						textareaObj.setSelectionRange(pos, pos);
						event.preventDefault();
					}
				}
				else {
					pos = textRows.rowStart[row] + textRows.rowLength[row];
				}
				ScrollTextarea(textRows.selStartRow, textRows.selEndRow, lines, scrollMargin, textareaObj.scrollTop);
			}
		}
		else {
			textRows.changed = true;
		}
	}
	return;
}


//
// ScrollToTop: event handler for scroll to textarea top button
//




//
// FullScreen: change to fullscreen input area; event handler for fullscreen buttons
//

function FullScreen(event) {

	fullScreenMode = true;

// save window scroll position
	normalPageYOffset = window.pageYOffset;
	normalPageXOffset = window.pageXOffset;

// get fullscreen button coordinates
	var buttonOffsetLeft = event.pageX - window.pageXOffset;
	var buttonOffsetTop = event.pageY - window.pageYOffset;

// move the input area up in the tree
	var inputWrapper = document.getElementById('inputWrapper');
	var globalWrapper = document.getElementById('globalWrapper');
	var subGlobalWrapper = document.getElementById('subGlobalWrapper');
	globalWrapper.insertBefore(inputWrapper, subGlobalWrapper);

// set input area to fullscreen
	inputWrapper.style.position = 'fixed';
	inputWrapper.style.top = '0';
	inputWrapper.style.left = '0';
	inputWrapper.style.right = '0';
	inputWrapper.style.bottom = '0';
	var content = document.getElementById('content');
	inputWrapper.style.backgroundColor = GetStyle(content, 'background-color');
	var buttonsWrapper = document.getElementById('buttonsWrapper');
	buttonsWrapper.style.paddingLeft = '0.5em'
	buttonsWrapper.style.paddingBottom = '0.5em'

// set textarea size
	textareaObj.style.margin = '0';

// set the textarea to maximal height
	var textareaWrapper = document.getElementById('textareaWrapper');
	textareaObj.style.height = (window.innerHeight - buttonsWrapper.offsetHeight - 4) + 'px';

// hide the rest of the page
	subGlobalWrapper.style.display = 'none';

// set floating 'back to normal' button
	var floatButton = document.getElementById('fullScreenButtonFloat');
	floatButton.style.right = '';
	floatButton.style.bottom = '';
	floatButton.style.display = 'inline';
	floatButton.style.left = (buttonOffsetLeft - floatButton.offsetWidth / 2) + 'px';
	floatButton.style.top = (buttonOffsetTop - floatButton.offsetHeight / 2) + 'px';
	floatButton.focus();

// change fullscreen button text and handler
	var fullScreenButton = document.getElementById('fullScreenButton');
	fullScreenButton.value = normalButtonValue;
	fullScreenButton.title = normalButtonTitle;
	fullScreenButton.onclick = NormalScreen;

// set rows
	var textareaClone = document.getElementById('textareaClone');
	textareaClone.style.display = 'block';
  var rows = textareaObj.clientHeight / textareaClone.clientHeight * textareaClone.rows;
	textareaClone.style.display = 'none';
	textareaObj.rows = rows;

// resize textarea to defined cols number and parse rows
	ResizeTextarea();

	return;
}


//
// NormalScreen: change back to normal page view; event handler for fullscreen buttons
//

function NormalScreen() {

// check if we are in fullscreen mode
	if (fullScreenMode != true) {
		return;
	}
	fullScreenMode = false;

// hide floating 'back to normal' button
	var floatButton = document.getElementById('fullScreenButtonFloat').style.display = 'none';

// show the rest of the page
	document.getElementById('subGlobalWrapper').style.display = 'block';

// set input area back to the original position
	var inputWrapper = document.getElementById('inputWrapper');
	normalTreePos.parentNode.insertBefore(inputWrapper, normalTreePos);
	inputWrapper.style.position = 'static';
	inputWrapper.style.height = '';
	inputWrapper.style.backgroundColor = '';

// reset textarea settings
	textareaObj.style.width = normalTextareaWidth;
	textareaObj.style.height = normalTextareaHeight;
	textareaObj.style.margin = normalTextareaMargin;
	textareaObj.rows = normalTextareaRows;
	document.getElementById('buttonsWrapper').style.padding = '';

// change fullscreen button text and handler
	var fullScreenButton = document.getElementById('fullScreenButton');
	fullScreenButton.value = fullButtonValue;
	fullScreenButton.title = fullButtonTitle;
	fullScreenButton.onclick = FullScreen;

// reset window scroll position
	window.scrollTo(normalPageXOffset, normalPageYOffset);

// resize textarea to defined cols number
	ResizeTextarea();

	return;
}


//
// ResizeComboInput: set the size of the background select boxes so that the button is visible
//

function ResizeComboInput(field) {

// add a dummy option
	var dummy;
	if (selectElement[field].options.length == 0) {
		selectElement[field].options[0] = new Option('');
		dummy = true;
	}

// set option widths to 0
	for (i = 0; i < selectElement[field].options.length; i ++) {
		selectElement[field].options[i].style.width = '0';
	}

// calculate select width
	var inputWidth = inputElement[field].clientWidth;
	var selectWidth = selectElement[field].clientWidth;
	var optionWidth = selectElement[field].options[0].offsetWidth;
	var border = inputElement[field].offsetWidth - inputElement[field].clientWidth;
	selectElement[field].style.width = (selectWidth - optionWidth + inputWidth - border) + 'px';

// delete dummy option
	if (dummy) {
		selectElement[field].options[0] = null;
	}

// set option widths to auto
	for (i = 0; i < selectElement[field].options.length; i ++) {
		selectElement[field].options[i].style.width = 'auto';
	}
	return;
}


//
// ChangeComboInput: set the input value to selected option; onchange event handler for select boxes
//

function ChangeComboInput(field) {

// get selection index (-1 for unselected)
	var selected = selectElement[field].selectedIndex;
	if (selected >= 0) {

// get selected option
		var option = selectElement[field].options[selected];
		if (option.text != '') {

// add a tag to the summary box
			if (field == 'summary') {
				var text = inputElement[field].value;
				if (text.match(/ \*\/ $/)) {
					text += ' ';
				}
				else if (text != '') {
					text += '; ';
				}
				text += option.text;
				if (text.indexOf(summaryUsing) >= 0) {
					var summaryUsingEscape = summaryUsing.replace(/([^\w ])/g, '\\$1');
					var regExp = new RegExp(' *' + summaryUsingEscape + ' *', 'g');
					text = text.replace(regExp, '');
					text = text.replace(/ *$/, ' ' + summaryUsing);
				}
				inputElement[field].value = text;
			}

// add case and regexp checkboxes to find / replace fields
			else if (option.value == 'setcheck') {
				document.getElementById('caseSensitive').checked
					= ( option.text.charAt(0) == checkMarker[true] );
				document.getElementById('regExp').checked
					= ( option.text.charAt(1) == checkMarker[true] );
				inputElement[field].value = option.text.substr(3);
			}

// add option text
			else {
				inputElement[field].value = option.text;
			}
		}
	}
	return;
}


//
// AddToHistory: add an input value to the cookie history
//

function AddToHistory(field) {

	if (inputElement[field].value != '') {

// load history from cookie
	LoadHistoryFromCookie(field);

// add current value to history
		fieldHist[field].unshift(inputElement[field].value);

// add case and regexp checkboxes to find / replace value
		if ( (field == 'find') || (field == 'replace') ) {
			fieldHist[field][0] =
				checkMarker[ document.getElementById('caseSensitive').checked ] +
				checkMarker[ document.getElementById('regExp').checked ] +
				' ' + fieldHist[field][0];
		}

// remove multiple old copies from history
		i = 1;
		while (i < fieldHist[field].length) {
			if (fieldHist[field][i] == fieldHist[field][0]) {
				fieldHist[field].splice(i, 1);
			}
			else {
				i ++;
			}
		}

// remove new value if it is a preset value
		i = 0;
		if (presetOptions[field] != null) {
			while (i < presetOptions[field].length) {
				if (presetOptions[field][i] == fieldHist[field][0]) {
					fieldHist[field].shift;
					break;
				}
				else {
					i ++;
				}
			}
		}

// cut history to maximal history length
		fieldHist[field] = fieldHist[field].slice(0, findHistoryLength);

// saved history to cookie
		SaveHistoryToCookie(field);
	}
	return;
}


//
// SetComboOptions: generate the select options from cookie history; onfocus handler for select box
//

function SetComboOptions(field) {

// load history from cookie
	LoadHistoryFromCookie(field);

	var option = {};
	var selected = null;
	j = 0;

// delete options
	var options = selectElement[field].options;
	for (i = 0; i > options.length; i ++) {
		selectElement[field].remove(i);
	}

// delete optgroup
	option = document.getElementById(field + 'Optgroup');
	if (option != null) {
		selectElement[field].removeChild(option);
	}

// workaround for onchange not firing when selecting first option from unselected dropdown
	option = document.createElement('option');
	option.style.display = 'none';
	selectElement[field].options[j++] = option;

// add history entries
	for (i = 0; i < fieldHist[field].length; i ++) {
		if (fieldHist[field][i] != null) {
			if (fieldHist[field][i] == inputElement[field].value) {
				selected = j;
			}
			option = document.createElement('option');
			option.text = fieldHist[field][i];
			if ( (field == 'find') || (field == 'replace') ) {
				option.value = 'setcheck';
			}
			selectElement[field].options[j++] = option;
		}
	}

// add preset entries
	if (presetOptions[field] != null) {
		var startPreset = j;
		for (i = 0; i < presetOptions[field].length; i ++) {
			if (presetOptions[field][i] != null) {
				if (presetOptions[field][i] == inputElement[field].value) {
					selected = j;
				}
				option = document.createElement('option');
				option.text = presetOptions[field][i];
				if (field == 'summary') {
					option.text = option.text.replace(/\{using\}/g, summaryUsing);
				}
				selectElement[field].options[j++] = option;
			}
		}

// add a blank separator
		if (startPreset > 1) {
			option = document.createElement('optgroup');
			option.label = '\u00a0';
			option.id = field + 'Optgroup';
			selectElement[field].insertBefore(option, selectElement[field].options[startPreset]);
		}
	}

// set the selection
	selectElement[field].selectedIndex = selected;
	return;
}


//
// LoadHistoryFromCookie: get the input box history from the respective cookie
//

function LoadHistoryFromCookie(field) {
	var cookie = GetCookie(cookieName[field]);
	if (cookie != null) {
		cookie = decodeURIComponent(cookie);
		fieldHist[field] = cookie.split('\n');
	}
	return;
}


//
// SaveHistoryToCookie: save the input box history to the respective cookie
//

function SaveHistoryToCookie(field) {
	var cookieExpire = new Date();
	cookieExpire.setTime( cookieExpire.getTime() + cookieExpireSec * 1000 );
	var cookie = '';
	cookie = fieldHist[field].join('\n')
	cookie = encodeURIComponent(cookie);
	SetCookie(cookieName[field], cookie, cookieExpire.toGMTString());
	return;
}


// GetStyle: get computed style properties for non-inline css definitions
function GetStyle(element, styleProperty) {
	var style;
	if (element != null) {
		style = document.defaultView.getComputedStyle(element, null).getPropertyValue(styleProperty);
	}
	return(style);
}


//
// GetCookie
//

function GetCookie(name) {
	var cookie = ' ' + document.cookie;
	var search = ' ' + name + '=';
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(';', offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = cookie.substring(offset, end);
			setStr = setStr.replace(/\\+/g, ' ');
			setStr = decodeURIComponent(setStr);
		}
	}
	return(setStr);
}


//
// SetCookie
//

function SetCookie(name, value, expires, path, domain, secure) {
	var cookie = '';
	cookie += name + '=' + encodeURIComponent(value);
	if (expires != null) {
		cookie += '; expires=' + expires
	}
	if (path != null) {
		cookie += '; path=' + path;
	}
	if (domain != null)  {
		cookie += '; domain=' + domain;
	}
	if (secure != null) {
		cookie += '; secure';
	}
	document.cookie = cookie;
}


//
// GetOffsetTop: get element offset relative to left window border
//

function GetOffsetTop(element) {
	var offset = 0;
	do {
		offset += element.offsetTop;
	} while ( (element = element.offsetParent) != null );
	return(offset);
}


//
// GetOffsetLeft: get element offset relative to left window border
//

function GetOffsetLeft (element) {
	var offset = 0;
	do {
		offset += element.offsetLeft;
	} while ( (element = element.offsetParent) != null );
	return(offset);
}

//
// GetCursorTextPos
//

function GetCursorTextPos(obj, currentNode) {

	for (var i = 0; i < currentNode.childNodes.length; i ++) {
		var childNode = currentNode.childNodes.item(i);

// get the position for a given container
		if (childNode == obj.selRange.startContainer) {
			obj.startContainerPos = obj.plain.length;
		}
		if (childNode == obj.selRange.endContainer) {
			obj.endContainerPos = obj.plain.length;
			return;
		}

		switch (childNode.nodeType) {
			case 1:
				if ( (childNode.childNodes.length == 0) && leafElements[childNode.nodeName] ) {
					if (childNode.nodeName == 'BR') {
						obj.plain += '\n';
					}
				}
				else {
					GetCursorTextPos(obj, childNode);
				}
				break;
			case 3:
				var value = childNode.nodeValue;
				value = value.replace(/</g, '&lt;');
				value = value.replace(/>/g, '&gt;');
				obj.plain += value;
				break;
			case 5:
				var value = '&' + childNode.nodeName + ';';
				obj.plain += value;
				break;
		}
  }
	return;
}


//
// GetTextPosContainer
// sets obj.startContainerm, obj.startOffset, obj.endContainer, obj.endOffset for obj.startPos, obj.endPos



function GetTextPosContainer(obj, currentNode) {

	for (var i = 0; i < currentNode.childNodes.length; i ++) {
		var childNode = currentNode.childNodes.item(i);
		var textLength = obj.plain.length;
		switch (childNode.nodeType) {
			case 1:
				if ( (childNode.childNodes.length == 0) && leafElements[childNode.nodeName] ) {
					if (childNode.nodeName == 'BR') {
						obj.plain += '\n';
					}
				}
				else {
					GetTextPosContainer(obj, childNode);
					if (obj.endOffset != null) {
				  	return;
					}
				}
				break;
			case 3:
				var value = childNode.nodeValue;
				value = value.replace(/</g, '&lt;');
				value = value.replace(/>/g, '&gt;');
				obj.plain += value;
				break;
			case 5:
				var value = '&' + childNode.nodeName + ';';
				obj.plain += value;
				break;
		}

// get the container for a given position
		if (obj.startOffset == null) {
		  if (obj.plain.length >= obj.startPos) {
		   	obj.startContainer = childNode;
		   	obj.startOffset = obj.startPos - textLength;
		  }
	  }
	  if (obj.plain.length >= obj.endPos) {
	   	obj.endContainer = childNode;
	   	obj.endOffset = obj.endPos - textLength;
  		return;
		}
  }
	return;
}


// define leaf elements for GetInnerHTML
var leafElements = [];
leafElements['IMG'] = true;
leafElements['HR'] = true;
leafElements['BR'] = true;
leafElements['INPUT'] = true;



//
// ParseDOM:
//

function ParseDOM(obj, topNode) {

	obj.plainLength = 0;
	obj.plainArray = [];
	obj.plainNode = [];
	obj.plainStart = [];
	obj.plainPos = [];
	ParseDOMRecursive(obj, topNode);
	obj.plain = obj.plainArray.join('');

	return;
}


//
// ParseDOMRecursive:
//

function ParseDOMRecursive(obj, currentNode) {

// cycle through the child nodes of currentNode
	for each (var childNode in currentNode.childNodes) {

// check for selection
		if (childNode == obj.sel.focusNode) {
			obj.plainFocus = obj.plainLength + obj.sel.focusOffset;
		}
		if (childNode == obj.sel.anchorNode) {
			obj.plainAnchor = obj.plainLength + obj.sel.anchorOffset;
		}
		var value = null;

// get text of child node
		switch (childNode.nodeType) {
			case 1:
				if ( (childNode.childNodes.length == 0) && (leafElements[childNode.nodeName] == true) ) {
					if (childNode.nodeName == 'BR') {
						value = '\n';
					}
				}
				else {
					ParseDOMRecursive(obj, childNode);
				}
				break;
			case 3:
				value = childNode.nodeValue;
				break;
			case 5:
				value = '&' + childNode.nodeName + ';';
				break;
		}

// add text to text object
		if (value != null) {

// array of text fragments
			obj.plainArray.push(value);

// array of text fragment node references
			obj.plainNode.push(childNode);

// array of text fragment text positions
			obj.plainStart.push(obj.plainLength);

// node references containing text positions
 			obj.plainPos[childNode] = obj.plainLength;

// current text length
 			obj.plainLength += value.length;
		}
	}
	return;
}


//
// GetInnerHTML: get innerHTML from document fragment
//

function GetInnerHTML(obj, currentNode) {

// initialize string
	if (obj.html == null) {
		obj.html = '';
	}
	if (obj.plain == null) {
		obj.plain = '';
	}
	if (obj.plainArray == null) {
		obj.plainArray = [];
		obj.plainNode = [];
		obj.plainStart = [];
	}

	for (var i = 0; i < currentNode.childNodes.length; i ++) {
		var childNode = currentNode.childNodes.item(i);
		switch (childNode.nodeType) {
			case 1:
				obj.html += '<' + childNode.nodeName.toLowerCase();
				for (var j = 0; j < childNode.attributes.length; j ++) {
					if (childNode.attributes.item(j).nodeValue != null) {
						obj.html += ' ' + childNode.attributes.item(j).nodeName + '="' + childNode.attributes.item(j).nodeValue + '"';
					}
				}
				if ( (childNode.childNodes.length == 0) && leafElements[childNode.nodeName] ) {
					obj.html += '>';
					if (childNode.nodeName == 'BR') {
						obj.plainArray.push('\n');
						obj.plainNode.push(childNode);
						obj.plainStart.push(obj.plain.length);
						obj.plain += '\n';
					}
				}
				else {
					obj.html += '>';
					GetInnerHTML(obj, childNode);
					obj.html += '</' + childNode.nodeName.toLowerCase() + '>'
				}
				break;
			case 3:
				var value = childNode.nodeValue;
				value = value.replace(/</g, '&lt;');
				value = value.replace(/>/g, '&gt;');
				obj.html += value;
				obj.plainArray.push(value);
				obj.plainNode.push(childNode);
				obj.plainStart.push(obj.plain.length);
				obj.plain += value;
				break;
			case 4: obj.html += '<![CDATA[' + childNode.nodeValue + ']]>';
				break;
			case 5:
				var value = '&' + childNode.nodeName + ';';
				obj.html += value;
				obj.plainArray.push(value);
				obj.plainNode.push(childNode);
				obj.plainStart.push(obj.plain.length);
				obj.plain += value;
				break;
			case 8: obj.html += '<!--' + childNode.nodeValue + '-->';
				break;
		}
	}
	return;
}


// StyleSheet: create a new style sheet object
function StyleSheet(documentObject) {

	this.styleElement = null;
	if (documentObject == null) {
		documentObject = document;
	}

// IE
	if (documentObject.createStyleSheet) {
		this.styleElement = documentObject.createStyleSheet();
	}

// standards compliant browsers
	else {
		this.styleElement = documentObject.createElement('style');
		this.styleElement.from = 'text/css';
		var insert = documentObject.getElementsByTagName('head')[0];
		if (insert != null) {
			insert.appendChild(this.styleElement);
		}
	}

// add-a-rule method

// IE
	this.addRule = function(selector, declaration) {
		if (this.styleElement.addRule) {
			this.styleElement.addRule(selector, declaration);
		}

// standards compliant browsers
		else {
			if (this.styleElement.sheet != null) {
				if (this.styleElement.sheet.insertRule != null) {
					this.styleElement.sheet.insertRule(selector + ' { ' + declaration + ' } ', 0);
				}
			}
		}
	};
}


/* </nowiki></pre> */