////////////////////////////////////////////////////////////////////////////
// openWYSIWYG Modification v0.4.3 by Haggi (hagman@gmx.de)
// 
// Complete rewritten WYSIWYG Editor, features a few more functions as the
// orginal one. You can find the changelog within the readme.html
////////////////////////////////////////////////////////////////////////////
// Orginal Source from:
// openWYSIWYG v1.01 Copyright (c) 2006 openWebWare.com
// This copyright notice MUST stay intact for use.
//
// An open source WYSIWYG editor for use in web based applications.
// For full source code and docs, visit http://www.openwebware.com/
//
// This library is free software; you can redistribute it and/or modify 
// it under the terms of the GNU Lesser General Public License as published 
// by the Free Software Foundation; either version 2.1 of the License, or 
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along 
// with this library; if not, write to the Free Software Foundation, Inc., 59 
// Temple Place, Suite 330, Boston, MA 02111-1307 USA 
////////////////////////////////////////////////////////////////////////////
var WYSIWYG = {

	/* ---------------------------------------------------------------------- *\
	  Settings object, holds all customizeable attributes
	\* ---------------------------------------------------------------------- */
	Settings: function() {
	
		// Images Directory
		this.ImagesDir = "opensysiwyg/images/";
		
		// Popups Directory
		this.PopupsDir = "opensysiwyg/popups/";
		
		// CSS Directory File
		this.CSSFile = "opensysiwyg/styles/wysiwyg.css";		
		
		// Default WYSIWYG width and height (use px or %)
		this.Width = "100%";
		this.Height = "300px";
		
		// Default stylesheet of the WYSIWYG editor window
		this.DefaultStyle = "font-family: Arial; font-size: 12px; background-color: #FFFFFF";
		
		// Stylesheet if editor is disabled
		this.DisabledStyle = "font-family: Arial; font-size: 12px; background-color: #EEEEEE";
		
		// Replace line breaks with <br> tags
		this.ReplaceLineBreaks = false;
		
		// Width + Height of the preview window
		this.PreviewWidth = 500;
		this.PreviewHeight = 400;
		
		// Confirmation message if you strip any HTML added by word
		this.RemoveFormatConfMessage = "Clean HTML inserted by MS Word ?";
		
		// Nofication if browser is not supported by openWYSIWYG, leave it blank for no message output.
		this.NoValidBrowserMessage = "openWYSIWYG does not support your browser.";
				
		// Anchor path to strip, leave it blank to ignore
		// or define auto to strip the path where the editor is placed 
		// (only IE)
		this.AnchorPathToStrip = "auto";
		
		// Image path to strip, leave it blank to ignore
		// or define auto to strip the path where the editor is placed 
		// (only IE)
		this.ImagePathToStrip = "auto";
		
		// Enable / Disable the custom context menu
		this.ContextMenu = true;
		
		// Enabled the status bar update. Within the status bar 
		// node tree of the actually selected element will build
		this.StatusBarEnabled = true;
		
		// If enabled than the capability of the IE inserting line breaks will be inverted.
		// Normal: ENTER = <p> , SHIFT + ENTER = <br>
		// Inverted: ENTER = <br>, SHIFT + ENTER = <p>
		this.InvertIELineBreaks = false;
		
		// Holds the available buttons displayed 
		// on the toolbar of the editor
		this.Toolbar = new Array();
		this.Toolbar[0] = new Array(
			"font", 
			"fontsize",	
			"bold", 
			"italic", 
			"underline", 
			/*"strikethrough",*/ 
			"seperator", 
			"forecolor", 
			"backcolor", 
			"seperator", 
			"justifyleft", 
			"justifycenter", 
			"justifyright", 
			"seperator", 
			"unorderedlist", 
			"orderedlist",
			"outdent", 
			"indent"
		);
		this.Toolbar[1] = new Array(
			"subscript", 
			"superscript", 
			"seperator", 
			"cut", 
			"copy", 
			"paste",
			"removeformat",
			"seperator", 
			"undo", 
			"redo", 
			"seperator", 
			"inserttable", 
			"insertimage", 
			"createlink", 
			"seperator", 
			"undo", 
			"redo", 
			"seperator",  
			"preview", 
			"print",
			"seperator", 
			"viewSource", 
			"seperator", 
			"help"
		);
			
		// List of available font types
	    this.Fonts = new Array(
			"Arial", 
			"Sans Serif", 
			"Tahoma", 
			"Verdana", 
			/* "Comment", */ 
			"Courier New", 
			"Georgia", 
			"Times New Roman", 
			"Impact", 
			"Comic Sans MS"
		);
		
		// List of available font sizes 
	    this.Fontsizes = new Array(
			"1", 
			"2", 
			"3", 
			"4", 
			"5", 
			"6", 
			"7"
		);
				
		// Add the given element to the defined toolbar
		// on the defined position
		this.addToolbarElement = function(element, toolbar, position) {
			if(element != "seperator")
				this.removeToolbarElement(element);
			if(this.Toolbar[toolbar-1] == null) {
				this.Toolbar[toolbar-1] = new Array();
			}
			this.Toolbar[toolbar-1].splice(position+1, 1, element);			
		}
		
		// Remove an element from the toolbar
		this.removeToolbarElement = function(element) {
			if(element == "seperator") return; // do not remove seperators
			for(var i=0;i<this.Toolbar.length;i++) {
				if(this.Toolbar[i]) {
					var toolbar = this.Toolbar[i];
					for(var j=0;j<toolbar.length;j++) {
						if(toolbar[j] != null && toolbar[j] == element) {
							this.Toolbar[i].splice(j,1);
						}
					}
				}
			}
		}
		
		// clear all or a given toolbar
		this.clearToolbar = function(toolbar) {
			if(typeof toolbar == "undefined") {
				this.Toolbar = new Array();
			}
			else {
				this.Toolbar[toolbar+1] = new Array();
			}
		}
		
	},
	
	// stores the different settings for each textarea
	// the textarea identifier is used to store the settings object
	config: new Array(),	
		
	/* ---------------------------------------------------------------------- *\
		!! Do not change something below or you know what you are doning !!
	\* ---------------------------------------------------------------------- */	

	// List of available block formats (not in use)
	//BlockFormats: new Array("Address", "Bulleted List", "Definition", "Definition Term", "Directory List", "Formatted", "Heading 1", "Heading 2", "Heading 3", "Heading 4", "Heading 5", "Heading 6", "Menu List", "Normal", "Numbered List"),

	// List of available actions and their respective ID and images
	ToolbarList: {
	//Name              buttonID               buttonTitle           buttonImage               buttonImageRollover
	"bold":           ['Bold',                 'Bold',               'bold.gif',               'bold_on.gif'],
	"italic":         ['Italic',               'Italic',             'italics.gif',            'italics_on.gif'],
	"underline":      ['Underline',            'Underline',          'underline.gif',          'underline_on.gif'],
	"strikethrough":  ['Strikethrough',        'Strikethrough',      'strikethrough.gif',      'strikethrough_on.gif'],
	"seperator":      ['',                     '',                   'seperator.gif',          'seperator.gif'],
	"subscript":      ['Subscript',            'Subscript',          'subscript.gif',          'subscript_on.gif'],
	"superscript":    ['Superscript',          'Superscript',        'superscript.gif',        'superscript_on.gif'],
	"justifyleft":    ['Justifyleft',          'Justifyleft',        'justify_left.gif',       'justify_left_on.gif'],
	"justifycenter":  ['Justifycenter',        'Justifycenter',      'justify_center.gif',     'justify_center_on.gif'],
	"justifyright":   ['Justifyright',         'Justifyright',       'justify_right.gif',      'justify_right_on.gif'],
	"unorderedlist":  ['InsertUnorderedList',  'InsertUnorderedList','list_unordered.gif',     'list_unordered_on.gif'],
	"orderedlist":    ['InsertOrderedList',    'InsertOrderedList',  'list_ordered.gif',       'list_ordered_on.gif'],
	"outdent":        ['Outdent',              'Outdent',            'indent_left.gif',        'indent_left_on.gif'],
	"indent":         ['Indent',               'Indent',             'indent_right.gif',       'indent_right_on.gif'],
	"cut":            ['Cut',                  'Cut',                'cut.gif',                'cut_on.gif'],
	"copy":           ['Copy',                 'Copy',               'copy.gif',               'copy_on.gif'],
	"paste":          ['Paste',                'Paste',              'paste.gif',              'paste_on.gif'],
	"forecolor":      ['ForeColor',            'ForeColor',          'forecolor.gif',          'forecolor_on.gif'],
	"backcolor":      ['BackColor',            'BackColor',          'backcolor.gif',          'backcolor_on.gif'],
	"undo":           ['Undo',                 'Undo',               'undo.gif',               'undo_on.gif'],
	"redo":           ['Redo',                 'Redo',               'redo.gif',               'redo_on.gif'],
	"inserttable":    ['InsertTable',          'InsertTable',        'insert_table.gif',       'insert_table_on.gif'],
	"insertimage":    ['InsertImage',          'InsertImage',        'insert_picture.gif',     'insert_picture_on.gif'],
	"createlink":     ['CreateLink',           'CreateLink',         'insert_hyperlink.gif',   'insert_hyperlink_on.gif'],
	"viewSource":     ['ViewSource',           'ViewSource',         'view_source.gif',        'view_source_on.gif'],
	"viewText":       ['ViewText',             'ViewText',           'view_text.gif',          'view_text_on.gif'],
	"help":           ['Help',                 'Help',               'help.gif',               'help_on.gif'],
	"selectfont":     ['SelectFont',           'SelectFont',         'select_font.gif',        'select_font_on.gif'],
	"selectsize":     ['SelectSize',           'SelectSize',         'select_size.gif',        'select_size_on.gif'],
	"preview":		  ['Preview', 			   'Preview',       	 'preview.gif',			   'preview_on.gif'],
	"print":		  ['Print', 			   'Print',       	 	 'print.gif',			   'print_on.gif'],
	"removeformat":   ['RemoveFormat',         'RemoveFormat',       'remove_format.gif',      'remove_format_on.gif']
	},


	// Create viewTextMode global variable and set to 0
	// enabling all toolbar commands while in HTML mode
	viewTextMode: new Array(),

	/* ---------------------------------------------------------------------- *\
	  Function    : getSelection()
	  Description : Get the selection of the given editor
	  Usage       : WYSIWYG.getSelection(n)
	  Arguments   : n - Identifier of the editor element
	\* ---------------------------------------------------------------------- */
	getSelection: function(n) {
		var ifrm = this.getEditorWindow(n);
		var doc = ifrm.document;
		var sel = null;
		if(ifrm.getSelection)
			sel = ifrm.getSelection();
		else if (doc.getSelection) 
			sel = doc.getSelection();
		else if (doc.selection) 
			sel = doc.selection;
		return sel;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : getRange()
	  Description : Get the range of the given selection
	  Usage       : WYSIWYG.getRange(sel)
	  Arguments   : sel - Selection
	\* ---------------------------------------------------------------------- */
	getRange: function(sel) {
		return sel.createRange ? sel.createRange() : sel.getRangeAt(0);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : getEditor()
	  Description : Get the iframe of the WYSIWYG editor
	  Usage       : WYSIWYG.getEditorWindow(n)
	  Arguments   : n - Identifier of the editor element
	\* ---------------------------------------------------------------------- */
	getEditor: function(n) {
		return $("wysiwyg" + n);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : getEditorWindow()
	  Description : Get the window element of the WYSIWYG editor
	  Usage       : WYSIWYG.getEditorWindow(n)
	  Arguments   : n - Identifier of the editor element
	\* ---------------------------------------------------------------------- */
	getEditorWindow: function(n) {
		return this.getEditor(n).contentWindow;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : isIE()
	  Description : Return true if browser is IE, otherwise false
	  Usage       : WYSIWYG.isIE()
	\* ---------------------------------------------------------------------- */
	isIE: function() {
		return navigator.appName == "Microsoft Internet Explorer" ? true : false;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : isFF()
	  Description : Return true if browser is Firefox (Mozilla)
	  Usage       : WYSIWYG.isFF()
	\* ---------------------------------------------------------------------- */
	isFF: function() {
		return !document.all && document.getElementById && !this.isOpera();
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : isOpera()
	  Description : Return true if browser is Opera, otherwise false
	  Usage       : WYSIWYG.isOpera()
	\* ---------------------------------------------------------------------- */
	isOpera: function() {
		return navigator.appName == "Opera" ? true : false;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : trim()
	  Description : Trim whitespaces of the given string
	  Usage       : WYSIWYG.trim(" This is a test     ");
	  Arguments   : str - String
	\* ---------------------------------------------------------------------- */
	trim: function(str) {
		return str.replace(/^\s*|\s*$/g,"");
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : isBrowserCompatible()
	  Description : Check if openWYSIWYG is running on the current browser
	  Usage       : WYSIWYG.isBrowserCompatible();
	\* ---------------------------------------------------------------------- */
	isBrowserCompatible: function() {
		// Validate browser and compatiblity
		if ((navigator.userAgent.indexOf('Safari') != -1 ) || !document.getElementById || !document.designMode){   
			//no designMode (Safari lies)
	   		return false;
		} 
		return true;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : _setStyleAttribute()
	  Description : Set the style attribute of an element
	  				For internal use, to solve the IE bug while setting the style attribute of an element
	  Usage       : this._setStyleAttribute(element, "font-size:10px;color:#000099;") 
	  Arguments   : element  - The element on which the style attribute is set
	  				style - Stylesheet to be set
	\* ---------------------------------------------------------------------- */
	_setStyleAttribute: function(element, style) {
		var styles = style.split(";");
		for(i=0;i<styles.length;i++) {
			var attributes = styles[i].split(":");
			if(attributes.length == 2) {
				try {
					var attr = this.trim(attributes[0]);
					while((pos = attr.search(/-/)) != -1) {
						var strBefore = attr.substring(0, pos);
						var strToUpperCase = attr.substring(pos + 1, pos + 2);
						var strAfter = attr.substring(pos + 2, attr.length);
						attr = strBefore + strToUpperCase.toUpperCase() + strAfter;
					}
					var value = this.trim(attributes[1]).toLowerCase();
					element.style[attr] = value;
				}
				catch (e) {
					alert(e);
				}
			}
		}
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : setAttribute()
	  Description : Set an attribute on a node
	  Usage       : WYSIWYG.setAttribute(node, "style", "font-size:10px;");
	  Arguments   : node - Node
	  				attr - Attribute to set
					value - Value of the attribute
	\* ---------------------------------------------------------------------- */
	setAttribute: function(node, attr, value) {
		if(value == "") return;
		if(attr.toLowerCase() == "style")
			this._setStyleAttribute(node, value);
		else
			node.setAttribute(attr, value);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : removeNode()
	  Description : Removes the current node of the selection
	  Usage       : WYSIWYG.removeNode(n);
	  Arguments   : n - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	removeNode: function(n) {
		// get editor
		var doc = this.getEditorWindow(n).document;
		// get selection and range
		var sel = this.getSelection(n);
		var range = this.getRange(sel);
		// the current tag of range
		var tag = this.getTag(range);
		var parent = tag.parentNode;
		if(tag == null || parent == null) return;		
		if(tag.nodeName == "HTML" || tag.nodeName == "BODY") return;
		// copy child elements of the node to the parent element before remove the node
		for(var i = 0; i < tag.childNodes.length;i++) {
			parent.insertBefore(tag.childNodes[i], tag);	
		}
		// remove node
		parent.removeChild(tag);
		// validate if parent is a link and the node is only 
		// surrounded by the link, then remove the link too
		if(parent.nodeName == "A" && parent.childNodes.length == 0) {
			if(parent.parentNode) parent.parentNode.removeChild(parent);
		}
		// update the status bar
		this.updateStatusBar(n);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : insertImage()
	  Description : insert an image into WYSIWYG in rich text
	  Usage       : WYSIWYG.insertImage("test.jpg", 500, 200, "center", 0, "Picture Alternativ", 5, 5, "textareaID")
	  Arguments   : src - Source of the image
	  				width - Width
					height - Height
					align - Alignment of the image
					border - border 
					alt - Alternativ Text
					hspace - Horizontal Space
					vspace - Vertical Space
	                n  - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	insertImage: function(src, width, height, align, border, alt, hspace, vspace, n) {
	
		// get editor
		var doc = this.getEditorWindow(n).document;
		// get selection and range
		var sel = this.getSelection(n);
		var range = this.getRange(sel);
		
		// the current tag of range
		var img = this.findParentTag("img", range);
		
		// element is not a link
		var update = img == null ? false : true;
		if(!update)
			img = doc.createElement("img");
		
		// set the attributes
		this.setAttribute(img, "src", src);
		this.setAttribute(img, "style", "width:" + width + ";height:" + height);
		if(align != "") this.setAttribute(img, "align", align); else img.removeAttribute("align");
		this.setAttribute(img, "border", border);
		this.setAttribute(img, "alt", alt);
		this.setAttribute(img, "hspace", hspace);
		this.setAttribute(img, "vspace", vspace);
		img.removeAttribute("width");
		img.removeAttribute("height");
		
		// on update exit here
		if(update) return;   
		
		// Check if IE or Mozilla (other)
		if (this.isIE())
			range.pasteHTML(img.outerHTML);   
		else 
			this.insertNodeAtSelection(img, n);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : insertLink()
	  Description : insert a link into WYSIWYG in rich text
	  Usage       : WYSIWYG.insertLink("http://www.google.de", "_blank", "", "", "", "textareaID")
	  Arguments   : href - The link url
	  				target - Target of the link
					style - Stylesheet of the link
					styleClass - Stylesheet class of the link
					name - Name attribute of the link
	                n  - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	insertLink: function(href, target, style, styleClass, name, n) {
	
		// get editor
		var doc = this.getEditorWindow(n).document;
		// get selection and range
		var sel = this.getSelection(n);
		var range = this.getRange(sel);
		var lin;
		// get element from selection
		if(this.isIE()) {
			if(sel.type == "Control" && range.length == 1) {	
				range = this.getTextRange(range(0));
				range.select();
			}
			if (sel.type == 'Text' || sel.type == 'None') {
				sel = this.getSelection(n);
				range = this.getRange(sel);
				// find a as parent element
				lin = this.findParentTag("a", range);
			}
		}
		else {
			// find a as parent element
			lin = this.findParentTag("a", range);
		}
		
		// check if parent is found
		var update = lin == null ? false : true;
		if(!update) 
			lin = doc.createElement("a");
		
		// set the attributes
		this.setAttribute(lin, "href", href);
		this.setAttribute(lin, "class", styleClass);
		this.setAttribute(lin, "className", styleClass);
		this.setAttribute(lin, "target", target);
		this.setAttribute(lin, "name", name);
		this.setAttribute(lin, "style", style);
		
		// on update exit here
		if(update) return;
		
		// append chield element			
		doc.documentElement.appendChild(lin);    
		
		// Check if IE or Mozilla (other)
		if (this.isIE()) {	
			lin.innerHTML = range.text;
			range.pasteHTML(lin.outerHTML);   
		} 
		else {
		    lin.innerHTML = sel;
			this.insertNodeAtSelection(lin, n);
		}
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : removeFormat()
	  Description : Strip any HTML added by word
	  Usage       : removeFormat(n)
	  Arguments   : n  - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	removeFormat: function(n) {
		
		if ( !confirm (this.config[n].RemoveFormatConfMessage) ) return;
		var doc = this.getEditorWindow(n).document;
		var str = doc.body.innerHTML;
		
		str = str.replace(/<span([^>])*>(&nbsp;)*\s*<\/span>/gi, '');
	    str = str.replace(/<span[^>]*>/gi, '');
	    str = str.replace(/<\/span[^>]*>/gi, '');
	    str = str.replace(/<p([^>])*>(&nbsp;)*\s*<\/p>/gi, '');
	    str = str.replace(/<p[^>]*>/gi, '');
	    str = str.replace(/<\/p[^>]*>/gi, '');
	    str = str.replace(/<h([^>])[0-9]>(&nbsp;)*\s*<\/h>/gi, '');
	    str = str.replace(/<h[^>][0-9]>/gi, '');
	    str = str.replace(/<\/h[^>][0-9]>/gi, ''); 
		str = str.replace (/<B [^>]*>/ig, '<b>');
		
		// var repl_i1 = /<I[^>]*>/ig;
		// str = str.replace (repl_i1, '<i>');
		
		str = str.replace (/<DIV[^>]*>/ig, '');
		str = str.replace (/<\/DIV>/gi, '');
		str = str.replace (/<[\/\w?]+:[^>]*>/ig, '');
		str = str.replace (/(&nbsp;){2,}/ig, '&nbsp;');
		str = str.replace (/<STRONG>/ig, '');
		str = str.replace (/<\/STRONG>/ig, '');
		str = str.replace (/<TT>/ig, '');
		str = str.replace (/<\/TT>/ig, '');
		str = str.replace (/<FONT [^>]*>/ig, '');
		str = str.replace (/<\/FONT>/ig, '');
		str = str.replace (/STYLE=\"[^\"]*\"/ig, '');
		str = str.replace(/<([\w]+) class=([^ |>]*)([^>]*)/gi, '<$1$3');
  	    str = str.replace(/<([\w]+) style="([^"]*)"([^>]*)/gi, '<$1$3'); 
		str = str.replace(/width=([^ |>]*)([^>]*)/gi, '');
	    str = str.replace(/classname=([^ |>]*)([^>]*)/gi, '');
	    str = str.replace(/align=([^ |>]*)([^>]*)/gi, '');
	    str = str.replace(/valign=([^ |>]*)([^>]*)/gi, '');
	    str = str.replace(/<\\?\??xml[^>]>/gi, '');
	    str = str.replace(/<\/?\w+:[^>]*>/gi, '');
	    str = str.replace(/<st1:.*?>/gi, '');
	    str = str.replace(/o:/gi, ''); 
	    
	    str = str.replace(/<!--([^>])*>(&nbsp;)*\s*<\/-->/gi, '');
   		str = str.replace(/<!--[^>]*>/gi, '');
   		str = str.replace(/<\/--[^>]*>/gi, '');
		
		doc.body.innerHTML = str;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : display()
	  Description : Display a WYSIWYG Editor with the content of an textarea (only to display the HTML)
	  Usage       : WYSIWYG.display(textareaID)
	  Arguments   : textareaID - The textarea identifier (the textarea's ID)
	  				width - Width of the editor area
					height - Height of the editor area
	\* ---------------------------------------------------------------------- */
	display: function(textareaID, settings) {
	
		// Validate browser compatiblity
		if(!this.isBrowserCompatible()) {
			if(this.config[n].NoValidBrowserMessage != "") alert(this.config[n].NoValidBrowserMessage);
			return;
		}
		
		// Pass the textareaID to the "n" variable.
	    var n = textareaID;
		// Get the textarea element
		var textarea = $(n);
		// Validate if textarea exists
		if(textarea == null) {
			alert("No textarea found with the given identifier (ID: " + n + ").");
			return;
		}
		
	    // Load settings in config array, use the textarea id as identifier
		if(typeof(settings) != "object") {
			this.config[n] = new this.Settings();
		}
		else {
			this.config[n] = settings;
		}
		
		// Include Style Sheet file
		document.write('<link rel="stylesheet" type="text/css" href="' + this.config[n].CSSFile +'">\n');
		
		// Hide the textarea 
		textarea.style.display = 'none'; 
			
	    // determine the width + height
		var currentWidth = this.config[n].Width;
		var currentHeight = this.config[n].Height;
		if(typeof width != "undefined") currentWidth = width;
	    if(typeof height != "undefined") currentHeight = height;
	 
		// Calculate the width + height of the editor 
		var toolbarWidth = currentWidth;
		var ifrmWidth = "100%";
		var	ifrmHeight = "100%";
		if(currentWidth.search(/%/) == -1) {
			ifrmWidth = currentWidth;
			ifrmHeight = currentHeight;
		}
				
		// Create iframe which will be used for rich text editing
		var iframe = '<table cellpadding="0" cellspacing="0" border="0" style="width:' + currentWidth + '; height:' + currentHeight + ';" class="tableTextareaEditor"><tr><td valign="top">\n'
	    + '<iframe frameborder="0" id="wysiwyg' + n + '" class="iframeText" style="width:' + ifrmWidth + ';height:' + ifrmHeight + ';"></iframe>\n'
	    + '</td></tr></table>\n';
	
	    // Insert after the textArea both toolbar one and two
		textarea.insertAdjacentHTML("afterEnd", iframe);
			
		// Get the iframe (editor) object
		var ifrm = this.getEditor(n);
						
		// Pass the textarea's existing text over to the content variable
	    var content = textarea.value;
		var doc = this.getEditorWindow(n).document;
		
		// Replace all \n with <br> 
		if(this.config[n].ReplaceLineBreaks)
			content = content.replace(/(\r\n)|(\n)/ig, "<br>");
			
		// Write the textarea's content into the iframe
	    doc.open();
	    doc.write(content);
	    doc.close();
	    
	    // Set default style of the editor window
		this.setAttribute(doc.body, "style", this.config[n].DefaultStyle);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : generate()
	  Description : replace textarea with wysiwyg editor
	  Usage       : WYSIWYG.generate(textareaID, width, height);
	  Arguments   : textareaID - ID of textarea to replace
	  			  : width - Width of the wysiwyg
				  : height - Height of the wysiwyg
	\* ---------------------------------------------------------------------- */
	generate: function(textareaID, settings) {
	
		// Validate browser compatiblity
		if(!this.isBrowserCompatible()) {
			if(this.config[n].NoValidBrowserMessage != "") alert(this.config[n].NoValidBrowserMessage);
			return;
		}
		
		// Pass the textareaID to the "n" variable.
	    var n = textareaID;
	    // Get the textarea element
		var textarea = $(n);
		// Validate if textarea exists
		if(textarea == null) {
			alert("No textarea found with the given identifier (ID: " + n + ").");
			return;
		}
		
		// Load settings in config array, use the textarea id as identifier
		if(typeof(settings) != "object") {
			this.config[n] = new this.Settings();
		}
		else {
			this.config[n] = settings;
		}
		
		// Include Style Sheet file
		document.write('<link rel="stylesheet" type="text/css" href="' + this.config[n].CSSFile +'">\n');
		
		// Hide the textarea 
		textarea.style.display = 'none'; 
			
	    // determine the width + height
		var currentWidth = this.config[n].Width;
		var currentHeight = this.config[n].Height;
		if(typeof width != "undefined") currentWidth = width;
	    if(typeof height != "undefined") currentHeight = height;
	 
		// Calculate the width + height of the editor 
		var toolbarWidth = currentWidth;
		var ifrmWidth = "100%";
		var	ifrmHeight = "100%";
		if(currentWidth.search(/%/) == -1) {
			toolbarWidth = currentWidth.replace(/px/gi, "");
			toolbarWidth = (parseFloat(toolbarWidth) + 2) + "px";
			ifrmWidth = currentWidth;
			ifrmHeight = currentHeight;
		}
		
				
	    // Generate the WYSIWYG Table
	    // This table holds the toolbars and the iframe as the editor
	    var editor = "";
	    editor += '<table border="0" cellpadding="0" cellspacing="0" class="tableTextareaEditor" id="wysiwyg_table_' + n + '" style="width:' + currentWidth  + '; height:' + currentHeight + ';">';
	    editor += '<tr><td>';
	    	  
		// Output all command buttons that belong to toolbar one
		for (var j = 0; j < this.config[n].Toolbar.length;j++) { 
			if(this.config[n].Toolbar[j] && this.config[n].Toolbar[j].length > 0) {
				var toolbar = this.config[n].Toolbar[j];
				
				// Generate WYSIWYG toolbar one
			    editor += '<table border="0" cellpadding="0" cellspacing="0" class="toolbar1" style="width:100%;" id="toolbar' + j + '_' + n + '">';
	    		editor += '<tr><td style="width: 6px;"><img src="' + this.config[n].ImagesDir + 'seperator2.gif" alt="" hspace="3"></td>';
				
				for (var i = 0; i < toolbar.length;i++) { 
				    if (toolbar[i]) {
				    	// Font selection
						if (toolbar[i] == "font"){
							editor += '<td style="width: 90px;"><span id="FontSelect' + n + '"></span></td>';
						}
						// Font size selection
						else if (toolbar[i] == "fontsize"){
							editor += '<td style="width: 60px;"><span id="FontSizes'  + n + '"></span></td>';
						}
						// Button print out
						else {
							// Get the values of the Button from the global ToolbarList object
							var buttonObj = this.ToolbarList[toolbar[i]];
							var buttonID = buttonObj[0];
							var buttonTitle = buttonObj[1];
							var buttonImage = this.config[n].ImagesDir + buttonObj[2];
							var buttonImageRollover  = this.config[n].ImagesDir + buttonObj[3];
							    
							if (toolbar[i] == "seperator") {
								editor += '<td style="width: 12px;" align="center"><img src="' + buttonImage + '" border=0 unselectable="on" width="2" height="18" hspace="2" unselectable="on"></td>';
							}
							// View Source button
							else if (toolbar[i] == "viewSource"){
							    editor += '<td style="width: 22px;">';
								editor += '<span id="HTMLMode' + n + '"><a href="javascript:void(0)" onclick="WYSIWYG.formatText(\'' + buttonID + '\',\'' + n + '\');"><img src="'  + buttonImage +  '" border="0" unselectable="on" title="' + buttonTitle + '" id="' + buttonID + '" class="buttonEditor" onmouseover="if(className==\'button\'){className=\'buttonEditorOver\'}; this.src=\'' + buttonImageRollover + '\';" onmouseout="if(className==\'buttonEditorOver\'){className=\'button\'}; this.src=\'' + buttonImage + '\';" unselectable="on"  width="20" height="20"></a></span>';
								editor += '<span id="textMode' + n + '"><a href="javascript:void(0)" onclick="WYSIWYG.formatText(\'ViewText\',\'' + n + '\');"><img src="' + this.config[n].ImagesDir + 'view_text.gif" border="0" unselectable="on" title="viewText" id="ViewText" class="buttonEditor" onmouseover="if(className==\'button\'){className=\'buttonEditorOver\'}; this.src=\'' + this.config[n].ImagesDir + 'view_text_on.gif\';"    onmouseout="if(className==\'buttonEditorOver\'){className=\'button\'}; this.src=\'' + this.config[n].ImagesDir + 'view_text.gif\';" unselectable="on"  width="20" height="20"></a></span>';
						        editor += '</td>';
					        }
							else {
								editor += '<td style="width: 22px;"><a href="javascript:void(0)" onclick="WYSIWYG.formatText(\'' + buttonID + '\',\'' + n + '\');"><img src="' + buttonImage + '" border=0 unselectable="on" title="' + buttonTitle + '" id="' + buttonID + '" class="buttonEditor" onmouseover="if(className==\'button\'){className=\'buttonEditorOver\'}; this.src=\'' + buttonImageRollover + '\';" onmouseout="if(className==\'buttonEditorOver\'){className=\'button\'}; this.src=\'' + buttonImage + '\';" unselectable="on" width="20" height="20"></a></td>';
							}
						}
			  		}
			  	}
			  	editor += '<td>&nbsp;</td></tr></table>';
			}
		}
		
	 	editor += '</td></tr><tr><td valign="top">\n';
		// Create iframe which will be used for rich text editing
		editor += '<iframe frameborder="0" id="wysiwyg' + n + '" class="iframeText" style="width:100%;height:' + currentHeight + ';"></iframe>\n'
	    + '</td></tr>';
	    // Status bar HTML code
	    if(this.config[n].StatusBarEnabled)
		    editor += '<tr><td class="wysiwyg-statusbar" style="height:10px;" id="wysiwyg_statusbar_' + n + '">&nbsp;</td></tr>';    
	    editor += '</table>\n';
	    	
	    // Insert after the textArea both toolbar one and two
	    textarea.insertAdjacentHTML("afterEnd", editor);
	    		
	    // Insert the Font Type and Size drop downs into the toolbar
	    // Hide the dynamic drop down lists for the Font Types and Sizes
    	this.outputFontSelect(n);
	  	this.hideFonts(n);
		this.outputFontSizes(n);
		this.hideFontSizes(n);
			
		// Hide the "Text Mode" button
		// Validate if textMode Elements are prensent
		if($("textMode" + n)) 
			$("textMode" + n).style.display = 'none'; 
					
		// Get the iframe (editor) object
		var ifrm = this.getEditor(n); 
				
		// Pass the textarea's existing text over to the content variable
	    var content = textarea.value;
		var doc = this.getEditorWindow(n).document;		
		

		// Replace all \n with <br> 
		if(this.config[n].ReplaceLineBreaks)
			content = content.replace(/\n\r|\n/ig, "<br>");
				
		// Write the textarea's content into the iframe
	    doc.open();
	    doc.write(content);
	    doc.close();
	    		
		// Make the iframe editable in both Mozilla and IE
		// Improve compatiblity for IE + Mozilla
		if (doc.body.contentEditable) {
			doc.body.contentEditable = true;
		}
		else {
			doc.designMode = "on";	
		}
	
		// Set default font style
		this.setAttribute(doc.body, "style", this.config[n].DefaultStyle);
	    
	  	// Add Event Listener for various actions
	    if (this.isIE()) {
	    	// Update the textarea with content in WYSIWYG when user submits form
		    for (var idx=0; idx < document.forms.length; idx++) {
		      document.forms[idx].attachEvent("onsubmit", function() { WYSIWYG.updateTextArea(n); });
		    }
			
			// close font selection if mouse moves over the editor window
			doc.attachEvent("onmouseover", function() { WYSIWYG.hideFonts(n); });
			// close font size selection if mouse moves over the editor window
			doc.attachEvent("onmouseover", function() { WYSIWYG.hideFontSizes(n); });

			// If it's true invert the line break capability of IE
			if(this.config[n].InvertIELineBreaks)
				doc.attachEvent("onkeypress", function() {WYSIWYG.invertIELineBreakCapability(n)});
			
			// custom context menu	
			if(this.config[n].ContextMenu) {		
				doc.attachEvent("oncontextmenu", function(e) {return WYSIWYG.showContextMenu(e, n);});
				doc.attachEvent("onmousedown", function() {WYSIWYG.closeContextMenu();});
			}
			
			// status bar update
			if(this.config[n].StatusBarEnabled)
				doc.attachEvent("onmouseup", function(e) {WYSIWYG.updateStatusBar(n);});
			
	    }
	    else {
	    	// Update the textarea with content in WYSIWYG when user submits form
		  	for (var idx=0; idx < document.forms.length; idx++) {
		    	document.forms[idx].addEventListener("submit", function OnSumbmit() { WYSIWYG.updateTextArea(n); }, false);
		    }
			
			// close font selection if mouse moves over the editor window
			doc.addEventListener("mouseover", function hideFonts() { WYSIWYG.hideFonts(n); }, false);
			// close font size selection if mouse moves over the editor window
			doc.addEventListener("mouseover", function hideFonts() { WYSIWYG.hideFontSizes(n); }, false);		
			
			// custom context menu
			if(this.config[n].ContextMenu) {		
				doc.addEventListener("mousedown", function context(e) {return WYSIWYG.disableDesignMode(e, n)}, false);
				doc.addEventListener("keydown", function keydown(){ if(doc.designMode == "off") doc.designMode = "on"; }, false);
				doc.addEventListener("contextmenu", function context(e) {WYSIWYG.showContextMenu(e, n); return "return false";}, false);
				doc.addEventListener("mousedown", function click(e) {WYSIWYG.closeContextMenu();}, false);
				doc.oncontextmenu = function(){ return false; };
			}
			
			// status bar update
			if(this.config[n].StatusBarEnabled)
				doc.addEventListener("mouseup", function updateStatusBar(e) {WYSIWYG.updateStatusBar(n);}, false);
	    }
	    						
		// init viewTextMode var
	    this.viewTextMode[n] = false;			
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : disable()
	  Description : Disable the given WYSIWYG Editor Box
	  Usage       : WYSIWYG.disable(textareaID)
	  Arguments   : textareaID - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	disable: function(textareaID) {
		// set n to textareaID
		var n = textareaID;
		
		// get the editor window
		var editor = this.getEditorWindow(n);
	
		// Validate if editor exists
		if(editor == null) {
			alert("No editor found with the given identifier (ID: " + n + ").");
			return;
		}
		
		if(editor) {
			// disable design mode or content editable feature
			if(editor.document.body.contentEditable)
				editor.document.body.contentEditable = false;
			else
				editor.document.designMode = "Off";		
				
			// change the style of the body
			this.setAttribute(editor.document.body, "style", this.config[n].DisabledStyle);
			
			// hide the status bar
			this.hideStatusBar(n)
							
			// hide all toolbars
			this.hideToolbars(n);
		}
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : enable()
	  Description : Enables the given WYSIWYG Editor Box
	  Usage       : WYSIWYG.enable(textareaID)
	  Arguments   : textareaID - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	enable: function(textareaID) {
		// set n to textareaID
		var n = textareaID;
		
		// get the editor window
		var editor = this.getEditorWindow(n);
	
		// Validate if editor exists
		if(editor == null) {
			alert("No editor found with the given identifier (ID: " + n + ").");
			return;
		}
		
		if(editor) {
			// disable design mode or content editable feature
			if(editor.document.body.contentEditable)
				editor.document.body.contentEditable = true;
			else
				editor.document.designMode = "On";		
				
			// change the style of the body
			this.setAttribute(editor.document.body, "style", this.config[n].DefaultStyle);
			
			// hide the status bar
			this.showStatusBar(n)
							
			// hide all toolbars
			this.showToolbars(n);
		}
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : getNodeTree()
	  Description : Returns the node structure of the current selection as array
	  Usage       : WYSIWYG.getNodeTree(n);
	  Arguments   : n  - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	getNodeTree: function(n) {
		
		var sel = this.getSelection(n);
		var range = this.getRange(sel);	
			
		// get element of range
		var tag = this.getTag(range);
		if(tag == null) return;
		// get parent of element
		var node = this.getParent(tag);
		// init the tree as array with the current selected element
		var nodeTree = new Array(tag);
		// get all parent nodes
		var ii = 1;
		
		while(node != null && node.nodeName != "#document") {
			nodeTree[ii] = node;
			node = this.getParent(node);			
			ii++;
		}
		
		return nodeTree;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : updateStatusBar()
	  Description : Updates the status bar with the current node tree
	  Usage       : WYSIWYG.updateStatusBar(n);
	  Arguments   : n  - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	updateStatusBar: function(n) {
		
		// get the node structure
		var nodeTree = this.getNodeTree(n);
		if(nodeTree == null) return;
		// format the output
		var outputTree = "";
		var max = nodeTree.length - 1;
		for(var i=max;i>=0;i--) {
			if(nodeTree[i].nodeName != "HTML" && nodeTree[i].nodeName != "BODY") {
				outputTree += '<a class="wysiwyg-statusbar" href="javascript:void(0)" onclick="WYSIWYG.selectNode(\'' + n + '\',' + i + ');">' + nodeTree[i].nodeName + '</a>';	
			}
			else {
				outputTree += nodeTree[i].nodeName;
			}
			if(i > 0) outputTree += " > ";
		}
			
		// update the status bar 	
		var statusbar = $("wysiwyg_statusbar_" + n);
		if(statusbar)
			statusbar.innerHTML = outputTree;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : disableDesignMode()
	  Description : Disable the design mode if right mouse button is pressed.
	  				It's needed for custom context menus on mozilla (firefox),
	  				because if design mode is on then you can`t diabled the browser
	  				context menu.
	  Usage       : WYSIWYG.disableDesignMode(e, n);
	  Arguments   : event - browser event (like which button pressed)
	  				n  - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	disableDesignMode: function(event, n) {
		var doc = this.getEditorWindow(n).document;
		if(event.which == 3) {
			doc.designMode = "off";
			return false;	
		}
		else if(event.which != 3 && doc.designMode == "off") {
			doc.designMode = "on";
			return true;
		}
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : getEditorOffsetX()
	  Description : Get the y offset of the editor to the body of the page
	  Usage       : WYSIWYG.getEditorOffsetX(n);
	  Arguments   : n  - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	getEditorOffsetX: function(n) {
		var parent;
		var offsetX = 0;
		var elm = $("wysiwyg_table_" + n);
		if(elm) {
			offsetX += elm.offsetLeft;
			while((elm = this.getParent(elm)) != null && elm.nodeName != "HTML") {
				if(elm.nodeName != "TR") {
					offsetX += elm.offsetLeft;
				}
			}
			return offsetX;
		}
		return 0
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : getEditorOffsetY()
	  Description : Get the y offset of the editor to the body of the page
	  Usage       : WYSIWYG.getEditorOffsetY(n);
	  Arguments   : n  - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	getEditorOffsetY: function(n) {
		var parent;
		var offsetY = 0;
		var elm = $("wysiwyg_table_" + n);
		if(elm) {
			offsetY += elm.offsetTop;
			while((elm = this.getParent(elm)) != null && elm.nodeName != "HTML") {
				if(elm.nodeName != "TR") {
					offsetY += elm.offsetTop;
				}
			}
			return offsetY;
		}
		return 0
	},
			
	/* ---------------------------------------------------------------------- *\
	  Function    : contextMenu()
	  Description : Shows a customer context menu
	  Usage       : WYSIWYG.contextMenu(n);
	  Arguments   : n  - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	showContextMenu: function(e, n) {
				
		// get editor doc
		var editor = this.getEditor(n);
		var doc = this.getEditorWindow(n).document;
				
		// get selection
		var sel = this.getSelection(n);
		var range = this.getRange(sel);
		
		// get the context menu offsets
		var offx = 0;
		var offy = this.config[n].Toolbar.length * 25;
		offx += this.getEditorOffsetX(n);
		offy += this.getEditorOffsetY(n);
		
		// get current selected node					
		var tag = this.getTag(range);
		if(tag == null) return;
		
		// Check if the current element is surrounded by an link, show the link properties on the context menu
		var linkProperties = (tag.parentNode && tag.parentNode.nodeName == "A") ? true : false;
		// Check if current element is an image, show the image properties on the context menu
		var imgProperties = (tag.nodeName == "IMG") ? true : false;
		
		// delete image
		var deleteImage = this.config[n].ImagesDir + "delete.gif";
		// image + link button images
		var buttonObj = this.ToolbarList["insertimage"];
		var imageImage = this.config[n].ImagesDir + buttonObj[2];
		var buttonObj = this.ToolbarList["createlink"];
		var linkImage = this.config[n].ImagesDir + buttonObj[2];
		
		var output = '<table class="wysiwyg-context-menu-border" border="0" cellpadding="0" cellspacing="0"><tr><td>';
		output += '<table class="wysiwyg-context-menu" border="0" align="center" cellpadding="2" cellspacing="0">';
		if(imgProperties) {
			
			// get width + height
			//var width = imageTag.attributes.width.value + "px";
			//var height = imageTag.attributes.height.value + "px";
			
			// Image properties
			output += '<tr>';
			output += '<td style="text-align:center;width:20px;"><a href="javascript:void(0)" onclick="WYSIWYG.formatText(\'InsertImage\',\'' + n + '\', \'sel\');"><img src="' + imageImage + '" border="0"></a></td>';
			output += '<td><a href="javascript:void(0)" onclick="WYSIWYG.formatText(\'InsertImage\',\'' + n + '\', \'sel\');">Image properties</a></td>';
			output += '</tr>';
			
			//output += '<tr><td>Width: ' + width + ' / Height: ' + height + '</td></tr>';
		}
		if(linkProperties) {
			output += '<tr>';
			output += '<td style="text-align:center;width:20px;"><a href="javascript:void(0)" onclick="WYSIWYG.formatText(\'CreateLink\',\'' + n + '\', \'sel\');"><img src="' + linkImage + '" border="0"></a></td>';
			output += '<td><a href="javascript:void(0)" onclick="WYSIWYG.formatText(\'CreateLink\',\'' + n + '\', \'sel\');">Link properties</a></td>';
			output += '</tr>';
		}
		
		if(tag != null) {
			if(tag.nodeName == "HTML" || tag.nodeName == "BODY") return;
			output += '<tr>';
			output += '<td style="text-align:center;width:20px;"><a href="javascript:void(0)" onclick="WYSIWYG.formatText(\'RemoveNode\',\'' + n + '\', \'sel\');"><img src="' + deleteImage + '" border="0"></a></td>';
			output += '<td><a href="javascript:void(0)" onclick="WYSIWYG.formatText(\'RemoveNode\',\'' + n + '\', \'sel\');">Remove</a></td>';
			output += '</tr>';
		}
		output += '</table>';
		//output += '<tr><td><hr size="1" color="#C9C9C9"></td></tr>';
		output += '</td></tr></table>';
		
		
		// add table
		var contextmenu = $('wysiwyg_contextmenu');
		contextmenu.innerHTML = output;
		contextmenu.style.left = e.clientX + offx + "px";
		contextmenu.style.top = e.clientY + offy + "px";
		contextmenu.style.visibility = "visible";
		
		return false;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : closeContextMenu()
	  Description : Close the context menu
	  Usage       : WYSIWYG.closeContextMenu(n);
	\* ---------------------------------------------------------------------- */
	closeContextMenu: function() {
		var contextmenu = $('wysiwyg_contextmenu');
		contextmenu.style.visibility = "hidden";
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : formatText() (changed)
	  Description : Format the content within the WYSIWYG Editor
	  Usage       : WYSIWYG.formatText(id, n, selected);
	  Arguments   : id - The execCommand (e.g. Bold)
	                n  - The editor identifier that the command affects (the textarea's ID)
	                selected - The selected value when applicable (e.g. Arial)
	\* ---------------------------------------------------------------------- */
	formatText: function(id, n, selected) {
			
		// When user clicks toolbar button make sure it always targets its respective WYSIWYG
		this.getEditorWindow(n).focus();
		
		// When in Text Mode these execCommands are disabled
		var formatIDs = new Array("FontSize","FontName","Bold","Italic","Underline","Subscript","Superscript","Strikethrough","Justifyleft","Justifyright","Justifycenter","InsertUnorderedList","InsertOrderedList","Indent","Outdent","ForeColor","BackColor","InsertImage","InsertTable","CreateLink", "Preview", "RemoveFormat");
	  
		// Check if button clicked is in disabled list
		for (var i = 0; i < formatIDs.length; i++) {
			if (formatIDs[i] == id) {
				 var disabled_id = 1; 
			}
		}
		
		// Check if in Text Mode and disabled button was clicked
		if (this.viewTextMode[n] == true && disabled_id == 1) {
		  alert ("You are in TEXT Mode. This feature has been disabled.");
		  return;
		}
		
		// FontSize
		if (id == "FontSize") {
	      	this.getEditorWindow(n).document.execCommand("FontSize", false, selected);
		}
		
		// FontName
		else if (id == "FontName") {
	      	this.getEditorWindow(n).document.execCommand("FontName", false, selected);
		}
		
		// ForeColor and BackColor
	    else if (id == 'ForeColor' || id == 'BackColor') {
	      	var w = screen.availWidth;
	      	var h = screen.availHeight;
	      	var popW = 210, popH = 165;
	      	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	      	var currentColor = this._dec_to_rgb(this.getEditorWindow(n).document.queryCommandValue(id));
	   
		  	window.open(this.config[n].PopupsDir + 'select_color.html?color=' + currentColor + '&command=' + id + '&wysiwyg=' + n, 'popup', 'location=0,status=0,scrollbars=0,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos);
	    }
		
		// InsertImage
		else if (id == "InsertImage") {
	    	window.open(this.config[n].PopupsDir + 'insert_image.html?wysiwyg=' + n, 'popup', 'location=0,status=0,scrollbars=0,resizable=0,width=400,height=210');
		}
		
		// Remove Image
		else if (id == "RemoveImage") {
	    	this.removeImage(n);
		}
		
		// Create Link
	  	else if (id == "CreateLink") {
	    	window.open(this.config[n].PopupsDir + 'insert_hyperlink.html?wysiwyg=' + n, 'popup', 'location=0,status=0,scrollbars=0,resizable=0,width=350,height=160');
	  	}
	  	
	  	// Remove Link
	  	else if (id == "RemoveLink") {
	    	this.removeLink(n);
	  	}
	  	
	  	// Remove a Node
	  	else if (id == "RemoveNode") {
	  		this.removeNode(n);
	  	}
	  
		// InsertTable
	  	else if (id == "InsertTable") {
	    	window.open(this.config[n].PopupsDir + 'create_table.html?wysiwyg=' + n, 'popup', 'location=0,status=0,scrollbars=0,resizable=0,width=400,height=360');
	  	}
	  
		// ViewSource
    	else if (id == "ViewSource") {
	    	this.viewSource(n);
	  	}
		
		// ViewText
		else if (id == "ViewText") {
	    	this.viewText(n);
	  	}

		// Help
		else if (id == "Help") {
	    	window.open(this.config[n].PopupsDir + 'about.html?wysiwyg=' + n, 'popup', 'location=0,status=0,scrollbars=0,resizable=0,width=400,height=350');
	  	}
	  	
	  	// Strip any HTML added by word
	  	else if(id == "RemoveFormat") {
	  		this.removeFormat(n);	  	
	  	}
		
		// Preview
		// Thx to Korvo
		else if (id =="Preview") {
			window.open(this.config[n].PopupsDir + 'preview.html?wysiwyg=' + n,'popup', 'location=0,status=0,scrollbars=1,resizable=1,width=' + this.config[n].PreviewWidth + ',height=' + this.config[n].PreviewHeight);
		} 

		// Print
		else if (id =="Print") {
			this.print(n);
		} 

		// Every other command
	  	else {
      		this.getEditorWindow(n).document.execCommand(id, false, null);
	  	}
		
		
		// hide node the font + font size selection
		this.hideFonts(n);
		this.hideFontSizes(n);
		this.closeContextMenu();
		
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : insertHTML()
	  Description : Insert HTML into WYSIWYG in rich text
	  Usage       : WYSIWYG.insertHTML("<b>hello</b>", "textareaID")
	  Arguments   : html - The HTML being inserted (e.g. <b>hello</b>)
	                n  - The editor identifier that the HTML 
									     will be inserted into (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	insertHTML: function(html, n) {	
		if (this.isIE()) {	  
			this.getEditorWindow(n).document.selection.createRange().pasteHTML(html);   
		} 
		else {
			var span = this.getEditorWindow(n).document.createElement("span");
			span.innerHTML = html;
			var node = this.insertNodeAtSelection(span, n);		
		}
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : insertNodeAtSelection()
	  Description : insert HTML into WYSIWYG in rich text (mozilla)
	  Usage       : WYSIWYG.insertNodeAtSelection(insertNode, n)
	  Arguments   : insertNode - The HTML being inserted (must be innerHTML inserted within a div element)
	                n          - The editor identifier that the HTML will be inserted into (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	insertNodeAtSelection: function(insertNode, n) {
	
		// get editor document
		var doc = this.getEditorWindow(n).document;
		// get current selection
		var sel = this.getSelection(n);
		
		// get the first range of the selection
		// (there's almost always only one range)
		var range = sel.getRangeAt(0);
		
		// deselect everything
		sel.removeAllRanges();
		
		// remove content of current selection from document
		range.deleteContents();
		
		// get location of current selection
		var container = range.startContainer;
		var pos = range.startOffset;
		
		// make a new range for the new selection
		range = doc.createRange();
		
		if (container.nodeType==3 && insertNode.nodeType==3) {					
			// if we insert text in a textnode, do optimized insertion
			container.insertData(pos, insertNode.data);
			// put cursor after inserted text
			range.setEnd(container, pos+insertNode.length);
			range.setStart(container, pos+insertNode.length);		
		} 	
		else {
		
			var afterNode;	
			if (container.nodeType==3) {
				// when inserting into a textnode
				// we create 2 new textnodes
				// and put the insertNode in between
				var textNode = container;
				container = textNode.parentNode;
				var text = textNode.nodeValue;
				
				// text before the split
				var textBefore = text.substr(0,pos);
				// text after the split
				var textAfter = text.substr(pos);
				
				var beforeNode = document.createTextNode(textBefore);
				var afterNode = document.createTextNode(textAfter);
				
				// insert the 3 new nodes before the old one
				container.insertBefore(afterNode, textNode);
				container.insertBefore(insertNode, afterNode);
				container.insertBefore(beforeNode, insertNode);
				
				// remove the old node
				container.removeChild(textNode);
			} 
			else {
				// else simply insert the node
				afterNode = container.childNodes[pos];
				container.insertBefore(insertNode, afterNode);
			}
			
			range.setEnd(afterNode, 0);
			range.setStart(afterNode, 0);
		}
		
		sel.addRange(range);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : print()
	  Description : Print out the content of the WYSIWYG editor area
	  Usage       : WYSIWYG.print(n)
	  Arguments   : n - The editor identifier (textarea ID)
	\* ---------------------------------------------------------------------- */
	print: function(n) {
		if(document.all && navigator.appVersion.substring(22,23)==4) {
			var doc = this.getEditorWindow(n).document;
			doc.focus();
			var OLECMDID_PRINT = 6;
			var OLECMDEXECOPT_DONTPROMPTUSER = 2;
			var OLECMDEXECOPT_PROMPTUSER = 1;
			var WebBrowser = '<object id="WebBrowser1" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
			doc.body.insertAdjacentHTML('beforeEnd',WebBrowser);
			WebBrowser1.ExecWB(OLECMDID_PRINT,OLECMDEXECOPT_DONTPROMPTUSER);
			WebBrowser1.outerHTML = '';
		} else {
			this.getEditorWindow(n).print();
		}
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : _dec_to_rgb
	  Description : convert a decimal color value to rgb hexadecimal
	  Usage       : var hex = WYSIWYG._dec_to_rgb('65535');   // returns FFFF00
	  Arguments   : value   - dec value
	\* ---------------------------------------------------------------------- */
	_dec_to_rgb: function(value) {
		var hex_string = "";
		for (var hexpair = 0; hexpair < 3; hexpair++) {
			var myByte = value & 0xFF;            // get low byte
			value >>= 8;                          // drop low byte
			var nybble2 = myByte & 0x0F;          // get low nybble (4 bits)
			var nybble1 = (myByte >> 4) & 0x0F;   // get high nybble
			hex_string += nybble1.toString(16);   // convert nybble to hex
			hex_string += nybble2.toString(16);   // convert nybble to hex
		}
		return hex_string.toUpperCase();
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : outputFontSelect()
	  Description : creates the Font Select drop down and inserts it into the toolbar
	  Usage       : WYSIWYG.outputFontSelect(n)
	  Arguments   : n - The editor identifier that the Font Select will update
		                when making font changes (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	outputFontSelect: function(n) {	
		
		// get the font element 
		var fontDiv = $('FontSelect' + n);
		if(fontDiv == null) return;
		
		var fonts = this.config[n].Fonts;
		var FontSelectObj = this.ToolbarList['selectfont'];
		var FontSelect = this.config[n].ImagesDir + FontSelectObj[2];
		var FontSelectOn  = this.config[n].ImagesDir + FontSelectObj[3];
		fonts.sort();
		
		var FontSelectDropDown = new Array;
		FontSelectDropDown[n] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td onMouseOver="$(\'selectFont' + n + '\').src=\'' + FontSelectOn + '\';" onMouseOut="$(\'selectFont' + n + '\').src=\'' + FontSelect + '\';"><a href="javascript:void(0)" onclick="WYSIWYG.showFonts(\'' + n + '\');"><img src="' + FontSelect + '" id="selectFont' + n + '" width="85" height="20" unselectable="on" border="0"><br>';
		FontSelectDropDown[n] += '<span id="Fonts' + n + '" class="dropdown" style="width: 145px;">';
		for (var i = 0; i < fonts.length;i++) {
		  	if (fonts[i]) {
		  		FontSelectDropDown[n] += '<button type="button" onClick="WYSIWYG.formatText(\'FontName\',\'' + n + '\',\'' + fonts[i] + '\')\;" onMouseOver="this.className=\'mouseOver\'" onMouseOut="this.className=\'mouseOut\'" class="mouseOut" style="width: 120px;"><table cellpadding="0" cellspacing="0" border="0"><tr><td align="left" style="font-family:' + fonts[i] + '; font-size: 12px;">' + fonts[i] + '</td></tr></table></button><br>';	
		  	}
	  	}
		FontSelectDropDown[n] += '</span></td></tr></table>';
		
		fontDiv.insertAdjacentHTML("afterBegin", FontSelectDropDown[n]);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : outputFontSizes()
	  Description : creates the Font Sizes drop down and inserts it into the toolbar
	  Usage       : WYSIWYG.outputFontSelect(n)
	  Arguments   : n   - The editor identifier that the Font Sizes will update
		                    when making font changes (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	outputFontSizes: function(n) {	
	
		var fontSizeDiv = $('FontSizes' + n);
		if(fontSizeDiv == null) return;
		
		var fontSize = this.config[n].Fontsizes;
		var FontSizeObj        = this.ToolbarList['selectsize'];
		var FontSize           = this.config[n].ImagesDir + FontSizeObj[2];
		var FontSizeOn         = this.config[n].ImagesDir + FontSizeObj[3];
		fontSize.sort();
		
		var FontSizesDropDown = new Array;
		FontSizesDropDown[n] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td onMouseOver="$(\'selectSize' + n + '\').src=\'' + FontSizeOn + '\';" onMouseOut="$(\'selectSize' + n + '\').src=\'' + FontSize + '\';"><a href="javascript:void(0)" onclick="WYSIWYG.showFontSizes(\'' + n + '\');"><img src="' + FontSize + '" id="selectSize' + n + '" width="49" height="20" unselectable="on" border="0"><br>';
	  	FontSizesDropDown[n] += '<span id="Sizes' + n + '" class="dropdown" style="width: 170px;">';
	
		for (var i = 0; i < fontSize.length;i++) {
		  if (fontSize[i])
	      	FontSizesDropDown[n] += '<button type="button" onClick="WYSIWYG.formatText(\'FontSize\',\'' + n + '\',\'' + fontSize[i] + '\')\;" onMouseOver="this.className=\'mouseOver\'" onMouseOut="this.className=\'mouseOut\'" class="mouseOut" style="width: 145px;"><table cellpadding="0" cellspacing="0" border="0"><tr><td align="left" style="font-family: arial, verdana, helvetica;"><font size="' + fontSize[i] + '">size ' + fontSize[i] + '</font></td></tr></table></button><br>';	
	  	}
		
		FontSizesDropDown[n] += '</span></td></tr></table>';
		fontSizeDiv.insertAdjacentHTML("afterBegin", FontSizesDropDown[n]);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : hideFonts()
	  Description : Hides the list of font names in the font select drop down
	  Usage       : WYSIWYG.hideFonts(n)
	  Arguments   : n   - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	hideFonts: function(n) {
		if($('Fonts' + n)) $('Fonts' + n).style.display = 'none'; 
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : hideFontSizes()
	  Description : Hides the list of font sizes in the font sizes drop down
	  Usage       : WYSIWYG.hideFontSizes(n)
	  Arguments   : n   - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	hideFontSizes: function(n) {
		if($('Sizes' + n)) $('Sizes' + n).style.display = 'none'; 
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : showFonts()
	  Description : Shows the list of font names in the font select drop down
	  Usage       : WYSIWYG.showFonts(n)
	  Arguments   : n   - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	showFonts: function(n) { 
		if($('Fonts' + n) == null) return;
		if ($('Fonts' + n).style.display == 'block') {
			$('Fonts' + n).style.display = 'none';
		}
		else {
			$('Fonts' + n).style.display = 'block'; 
			$('Fonts' + n).style.position = 'absolute';		
		}
		
		// hide font size selection
		this.hideFontSizes(n);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : showFontSizes()
	  Description : Shows the list of font sizes in the font sizes drop down
	  Usage       : WYSIWYG.showFonts(n)
	  Arguments   : n   - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	showFontSizes: function(n) { 
		if($('Sizes' + n) == null) return;
		if ($('Sizes' + n).style.display == 'block') {
			$('Sizes' + n).style.display = 'none';
		}
		else {
			$('Sizes' + n).style.display = 'block'; 
			$('Sizes' + n).style.position = 'absolute';		
		}
		
		// hide font size selection
		this.hideFonts(n);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : viewSource()
	  Description : Shows the HTML source code generated by the WYSIWYG editor
	  Usage       : WYSIWYG.showFonts(n)
	  Arguments   : n   - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	viewSource: function(n) {
		
		// document
		var doc = this.getEditorWindow(n).document;
			
		// View Source for IE 	 
		if (this.isIE()) {
			var iHTML = doc.body.innerHTML;
			// strip off the absolute urls
			iHTML = this.stripURLPath(n, iHTML);
			// replace all decimal color strings with hex decimal color strings
			iHTML = this.replaceDecWithHexColor(iHTML);
			doc.body.innerText = iHTML;
		}
	  	// View Source for Mozilla/Netscape
	  	else {
	  		// replace all decimal color strings with hex decimal color strings
			var html = this.replaceDecWithHexColor(doc.body.innerHTML);
	    	html = document.createTextNode(html);
	    	doc.body.innerHTML = "";
	    	doc.body.appendChild(html);
	  	}
	  
		// Hide the HTML Mode button and show the Text Mode button
		// Validate if Elements are present
		if($('HTMLMode' + n))
		    $('HTMLMode' + n).style.display = 'none'; 
	    if($('textMode' + n))
		    $('textMode' + n).style.display = 'block';
		
		// set the font values for displaying HTML source
		doc.body.style.fontSize = "12px";
		doc.body.style.fontFamily = "Courier New"; 
		
	  	this.viewTextMode[n] = true;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : viewSource()
	  Description : Shows the HTML source code generated by the WYSIWYG editor
	  Usage       : WYSIWYG.showFonts(n)
	  Arguments   : n   - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	viewText: function(n) { 
		
		// get document
		var doc = this.getEditorWindow(n).document;
		
		// View Text for IE 	  	 
		if (this.isIE()) {
	    	var iText = doc.body.innerText;
	    	// strip off the absolute urls
			iText = this.stripURLPath(n, iText);
			// replace all decimal color strings with hex decimal color strings
			iText = this.replaceDecWithHexColor(iText);
	    	doc.body.innerHTML = iText;
		}
	  
		// View Text for Mozilla/Netscape
	  	else {
	    	var html = doc.body.ownerDocument.createRange();
	    	html.selectNodeContents(doc.body);
	    	// replace all decimal color strings with hex decimal color strings
			html = this.replaceDecWithHexColor(html.toString());
	    	doc.body.innerHTML = html;
		}
					  
		// Hide the Text Mode button and show the HTML Mode button
		// Validate if Elements are present
		if($('textMode' + n))
			$('textMode' + n).style.display = 'none'; 
		if($('HTMLMode' + n))
			$('HTMLMode' + n).style.display = 'block';
		
		// reset the font values (changed)
		this.setAttribute(doc.body, "style", this.config[n].DefaultStyle);
		
		this.viewTextMode[n] = false;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : getDocumentPath()
	  Description : Get the path of the given document
	  Usage       : WYSIWYG.getDocumentPath(doc)
	  Arguments   : doc  - Document of which you get the the path
	\* ---------------------------------------------------------------------- */
	getDocumentPathOfUrl: function(url) {
		var path = null;
		
		// if local file system, convert local url into web url
		url = url.replace(/file:\/\//gi, "file:///");
		url = url.replace(/\\/gi, "\/");
		var pos = url.lastIndexOf("/");
		if(pos != -1) {
			path = url.substring(0, pos + 1);
		}
		return path;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : getDocumentUrl()
	  Description : Get the documents url, convert local urls to web urls
	  Usage       : WYSIWYG.getDocumentUrl(doc)
	  Arguments   : doc  - Document of which you get the the path
	\* ---------------------------------------------------------------------- */
	getDocumentUrl: function(doc) {
		// if local file system, convert local url into web url
		var url = doc.URL;
		url = url.replace(/file:\/\//gi, "file:///");
		url = url.replace(/\\/gi, "\/");
		return url;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : stripURLPath()
	  Description : Strips off the defined image and the anchor urls of the given content.
	  				It also can strip the document URL automatically if you define auto.
	  Usage       : WYSIWYG.stripURLPath(content)
	  Arguments   : content  - Content on which the stripping applies
	\* ---------------------------------------------------------------------- */
	stripURLPath: function(n, content, exact) {
	
		// parameter exact is optional
		if(typeof exact == "undefined")
			exact = true;
	
		var stripImgageUrl = null;
		var stripAnchorUrl = null;
		
		// add url to strip of anchors to array
		if(this.config[n].AnchorPathToStrip == "auto") {
			stripAnchorUrl = this.getDocumentUrl(document);
		}
		else if(this.config[n].AnchorPathToStrip != "") {
			stripAnchorUrl = this.config[n].AnchorPathToStrip;
		}
		
		// add strip url of images to array
		if(this.config[n].ImagePathToStrip == "auto") {
			stripImgageUrl = this.getDocumentUrl(document);
		}
		else if(this.config[n].ImagePathToStrip != "") {
			stripImgageUrl = this.config[n].ImagePathToStrip;
		}
		
		// strip url of image path
		if(stripImgageUrl) {
			var url;
			
			// escape reserved characters to be a valid regex	
			url = this.stringToRegex(this.getDocumentPathOfUrl(stripImgageUrl));	
			
			// exact replacing of url. regex: src="<url>"
			if(exact) {
				var regex = eval("/(src=\")(" + url + ")([^\"]*)/gi");
				content = content.replace(regex, "$1$3");	
			}
			// not exect replacing of url. regex: <url>
			else {
				var regex = eval("/(" + url + ")(.+)/gi");
				content = content.replace(regex, "$2");	
			}
			
			// strip absolute urls without a heading slash ("images/print.gif")	
			var result = this.getDocumentPathOfUrl(stripImgageUrl).match(/.+[\/]{2,3}[^\/]*/,"");
			if(result) {
				url = this.stringToRegex(result[0]);
				
				// exact replacing of url. regex: src="<url>"
				if(exact) {
					var regex = eval("/(src=\")(" + url + ")([^\"]*)/gi");
					content = content.replace(regex, "$1$3");
				}
				// not exect replacing of url. regex: <url>
				else {
					var regex = eval("/(" + url + ")(.+)/gi");
					content = content.replace(regex, "$2");	
				}
			}	
		}
		
		// strip url of image path
		if(stripAnchorUrl) {
		
			var url;
						
			// escape reserved characters to be a valid regex		
			url = this.stringToRegex(this.getDocumentPathOfUrl(stripAnchorUrl));
			
			// strip absolute urls with a heading slash ("/product/index.html")
			// exact replacing of url. regex: src="<url>"
			if(exact) {
				var regex = eval("/(href=\")(" + url + ")([^\"]*)/gi");
				content = content.replace(regex, "$1$3");	
			}
			// not exect replacing of url. regex: <url>
			else {
				var regex = eval("/(" + url + ")(.+)/gi");
				content = content.replace(regex, "$2");	
			}
			
			// strip absolute urls without a heading slash ("product/index.html")	
			var result = this.getDocumentPathOfUrl(stripAnchorUrl).match(/.+[\/]{2,3}[^\/]*/,"");
			if(result) {
				url = this.stringToRegex(result[0]);
				// exact replacing of url. regex: src="<url>"
				if(exact) {
					var regex = eval("/(href=\")(" + url + ")([^\"]*)/gi");
					content = content.replace(regex, "$1$3");	
				}
				// not exect replacing of url. regex: <url>
				else {
					var regex = eval("/(" + url + ")(.+)/gi");
					content = content.replace(regex, "$2");	
				}
				
			}
			
			// stip off anchor links with #name			
			url = this.stringToRegex(stripAnchorUrl);
			// exact replacing of url. regex: src="<url>"
			if(exact) {
				var regex = eval("/(href=\")(" + url + ")(#[^\"]*)/gi");
				content = content.replace(regex, "$1$3");
			}
			// not exect replacing of url. regex: <url>
			else {
				var regex = eval("/(" + url + ")(.+)/gi");
				content = content.replace(regex, "$2");	
			}
			
			
			// stip off anchor links with #name (only for local system)
			url = this.getDocumentUrl(document);
			var pos = url.lastIndexOf("/");
			if(pos != -1) {
				url = url.substring(pos + 1, url.length);
				url = this.stringToRegex(url);
				// exact replacing of url. regex: src="<url>"
				if(exact) {
					var regex = eval("/(href=\")(" + url + ")(#[^\"]*)/gi");
					content = content.replace(regex, "$1$3");
				}
				// not exect replacing of url. regex: <url>
				else {
					var regex = eval("/(" + url + ")(.+)/gi");
					content = content.replace(regex, "$2");	
				}
			}
		}
		
		return content;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : stringToRegex()
	  Description : Parse a given string to a valid regular expression
	  Usage       : WYSIWYG.stringToRegex(string)
	  Arguments   : string - String to be parsed
	\* ---------------------------------------------------------------------- */
	stringToRegex: function(string) {
		
		string = string.replace(/\//gi, "\\/");
		string = string.replace(/\(/gi, "\\(");
		string = string.replace(/\)/gi, "\\)");
		string = string.replace(/\[/gi, "\\[");
		string = string.replace(/\]/gi, "\\]");
		string = string.replace(/\+/gi, "\\+");
		string = string.replace(/\$/gi, "\\$");
		string = string.replace(/\*/gi, "\\*");
		string = string.replace(/\?/gi, "\\?");
		string = string.replace(/\^/gi, "\\^");
		string = string.replace(/\\b/gi, "\\\\b");
		string = string.replace(/\\B/gi, "\\\\B");
		string = string.replace(/\\d/gi, "\\\\d");
		string = string.replace(/\\B/gi, "\\\\B");
		string = string.replace(/\\D/gi, "\\\\D");
		string = string.replace(/\\f/gi, "\\\\f");
		string = string.replace(/\\n/gi, "\\\\n");
		string = string.replace(/\\r/gi, "\\\\r");
		string = string.replace(/\\t/gi, "\\\\t");
		string = string.replace(/\\v/gi, "\\\\v");
		string = string.replace(/\\s/gi, "\\\\s");
		string = string.replace(/\\S/gi, "\\\\S");
		string = string.replace(/\\w/gi, "\\\\w");
		string = string.replace(/\\W/gi, "\\\\W");
		
		return string;			
	},
	
		
	/* ---------------------------------------------------------------------- *\
	  Function    : updateTextArea()
	  Description : Updates the text area value with the HTML source of the WYSIWYG
	  Usage       : WYSIWYG.updateTextArea(n)
	  Arguments   : n   - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	updateTextArea: function(n) {	
		// on update switch editor back to html mode
		if(this.viewTextMode[n]) this.viewText(n);
		// get inner HTML
		var content = this.getEditorWindow(n).document.body.innerHTML;
		// strip off defined URLs on IE
		content = this.stripURLPath(n, content);
		// replace all decimal color strings with hex color strings
		content = this.replaceDecWithHexColor(content);
		// remove line breaks before content will be updated
		if(this.config[n].ReplaceLineBreaks) content = content.replace(/(\r\n)|(\n)/ig, "");
		// set content back in textarea
		$(n).value = content;
	},
		
	/* ---------------------------------------------------------------------- *\
	  Function    : hideToolbars()
	  Description : Hide all toolbars
	  Usage       : WYSIWYG.hideToolbars(n)
	  Arguments   : n - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	hideToolbars: function(n) {
		for(var i=0;i<this.config[n].Toolbar.length;i++) {
			var toolbar = $("toolbar" + i + "_" + n);
			if(toolbar)
				toolbar.style.display = "none";
		}	
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : showToolbars()
	  Description : Display all toolbars
	  Usage       : WYSIWYG.showToolbars(n)
	  Arguments   : n - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	showToolbars: function(n) {
		for(var i=0;i<this.config[n].Toolbar.length;i++) {
			var toolbar = $("toolbar" + i + "_" + n);
			if(toolbar)
				toolbar.style.display = "";
		}	
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : hideStatusBar()
	  Description : Hide the status bar
	  Usage       : WYSIWYG.hideStatusBar(n)
	  Arguments   : n - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	hideStatusBar: function(n) {
		var statusbar = $('wysiwyg_statusbar_' + n);
		if(statusbar)
			statusbar.style.display = "none";
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : showStatusBar()
	  Description : Display the status bar
	  Usage       : WYSIWYG.showStatusBar(n)
	  Arguments   : n - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	showStatusBar: function(n) {
		var statusbar = $('wysiwyg_statusbar_' + n);
		if(statusbar)
			statusbar.style.display = "";
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : findParentTag()
	  Description : Get the given parent tag of a range
	  Usage       : WYSIWYG.findParentTag(parentTagName, range)
	  Arguments   : parentTagName - Parent tag to find
	  				range - Range 
	\* ---------------------------------------------------------------------- */
	findParentTag: function(parentTagName, range){
		parentTagName = parentTagName.toUpperCase();
		try {
			if(!this.isIE()) {
				var node = range.startContainer;	
				var pos = range.startOffset;
				if(node.nodeType != 3) node = node.childNodes[pos];	
				elmWorking = node;
				while (elmWorking.tagName != "HTML") {
			  		if (elmWorking.tagName == parentTagName){
			  			return elmWorking;
			  		} 
			  		elmWorking = elmWorking.parentNode;
			 	}
			 	return null;
			}
			else {
				
				var rangeWorking;
				var index;
				var elmWorking = null;
				if(range.length > 0) {
					elmWorking = range.item(0);
				}
				else {
					elmWorking = range.parentElement();	
				}
				while (elmWorking.tagName != "HTML") {
			  		if (elmWorking.tagName == parentTagName){
			   			return elmWorking;
			  		} else {
			   			elmWorking = elmWorking.parentElement;
			  		}
			 	}
				rangeWorking = range.duplicate();
				rangeWorking.collapse(true);
				rangeWorking.moveEnd("character", 1);
				if (rangeWorking.text.length>0) {
					while (rangeWorking.compareEndPoints("EndToEnd", range) < 0){
			  			rangeWorking.move("Character");
			  			if (null != this.findParentTag(parentTagName, rangeWorking)){
			   				return this.findParentTag(parentTagName, rangeWorking);
			  			}
			 		}
			 	}
			 	return null;
			}
		}
		catch(e) {
			return null;
		}
	},
		
	/* ---------------------------------------------------------------------- *\
	  Function    : getTag()
	  Description : Get the acutally tag of the given range
	  Usage       : WYSIWYG.getTag(range)
	  Arguments   : range - Range
	\* ---------------------------------------------------------------------- */
	getTag: function(range) {
		try {
		    if(!this.isIE()) {
				var node = range.startContainer;	
				var pos = range.startOffset;
				if(node.nodeType != 3) node = node.childNodes[pos];
				if(node.nodeName && node.nodeName.search(/#/) != -1)
					return node.parentNode;
				return node;
			}
			else {
				if(range.length > 0) {
					return range.item(0);
				}
				else if(range.parentElement()) {
					return range.parentElement();
				}
			}
			return null;
		}
		catch(e) {
			return null;
		}
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : getParent()
	  Description : Get the parent node of an node
	  Usage       : WYSIWYG.getParent(node)
	  Arguments   : element - Element which parent will be returned
	\* ---------------------------------------------------------------------- */
	getParent: function(element) {
		if(element.parentNode)
			return element.parentNode;
		return null;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : getTextRange()
	  Description : Get the text range object of the given element
	  Usage       : WYSIWYG.getTextRange(element)
	  Arguments   : element - An element of which you get the text range object
	\* ---------------------------------------------------------------------- */
	getTextRange: function(element){
		var range = element.parentTextEdit.createTextRange();
		range.moveToElementText(element);
		return range;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : toHexColor()
	  Description : Convert RGB color string to hex color string
	  Usage       : WYSIWYG.getTextRange(color)
	  Arguments   : color - RGB color string
	\* ---------------------------------------------------------------------- */
	toHexColor: function(color) {
		color=color.replace(/^rgb/g,'');
		color=color.replace(/\(/g,'');
		color=color.replace(/\)/g,'');
		color=color.replace(/ /g,'');
		color=color.split(',');
		r = parseFloat(color[0]).toString(16).toUpperCase();
		g = parseFloat(color[1]).toString(16).toUpperCase();
		b = parseFloat(color[2]).toString(16).toUpperCase();
		if (r.length<2) r='0'+r;
		if (g.length<2) g='0'+g;
		if (b.length<2) b='0'+b;
		return '#' + r + g + b;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : replaceDecWithHexColor()
	  Description : Replace decimal colors with a hex colors within a string.
	  Usage       : replaceDecWithHexColor(string)
	  Arguments   : str   - string on which the decimal colors should be replaced
	\* ---------------------------------------------------------------------- */
	replaceDecWithHexColor: function(str) {
		// find all decimal color strings
		var matcher = str.match(/rgb\([0-9 ]+,[0-9 ]+,[0-9 ]+\)/gi);
		if(matcher) {
			for(var j=0; j<matcher.length;j++) {
				var regex = eval("/" + this.stringToRegex(matcher[j]) + "/gi");
				// replace the decimal color strings with hex color strings
				str = str.replace(regex, this.toHexColor(matcher[j]));
			}
		}
		return str;
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : invertIELineBreakCapability()
	  Description : Inverts the line break capability of IE (Thx to richyrich)
	  				Normal: ENTER = <p> , SHIFT + ENTER = <br>
	  				Inverted: ENTER = <br>, SHIFT + ENTER = <p>
	  Usage       : WYSIWYG.invertIELineBreakCapability(n)
	  Arguments   : n   - The editor identifier (the textarea's ID)
	\* ---------------------------------------------------------------------- */
	invertIELineBreakCapability: function(n) {
	
		var editor = this.getEditorWindow(n);
		// validate if the press key is the carriage return key
		if (editor.event.keyCode==13) {
	    	if (editor.event.shiftKey==false) {
				var sel = this.getRange(this.getSelection(n));
	            sel.pasteHTML("<br>");
	            editor.event.cancelBubble = true;
	            editor.event.returnValue = false;
	            sel.select();
	            sel.moveEnd("character", 1);
	            sel.moveStart("character", 1);
	            sel.collapse(false);
	            return false;
			}
	        else {
	            var sel = this.getRange(this.getSelection(n));
	            sel.pasteHTML("<p>");
	            editor.event.cancelBubble = true;
	            editor.event.returnValue = false;
	            sel.select();
	            sel.moveEnd("character", 1);
	            sel.moveStart("character", 1);
	            sel.collapse(false);
	            return false;
	    	}
		}  
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : selectNode()
	  Description : Select a node within the current editor
	  Usage       : WYSIWYG.selectNode(n, level)
	  Arguments   : n   - The editor identifier (the textarea's ID)
	  				level - identifies the level of the element which will be selected
	\* ---------------------------------------------------------------------- */
	selectNode: function(n, level) {
		
		var sel = this.getSelection(n);
		var range = this.getRange(sel);
		var parentnode = this.getTag(range);
		var i = 0;
		
		for (var node=parentnode; (node && (node.nodeType == 1)); node=node.parentNode) {
			if (i == level) {
				this.nodeSelection(n, node);
			}
			i++;
		}
		
		this.updateStatusBar(n);
	},
	
	/* ---------------------------------------------------------------------- *\
	  Function    : nodeSelection()
	  Description : Do the node selection
	  Usage       : WYSIWYG.nodeSelection(n, node)
	  Arguments   : n   - The editor identifier (the textarea's ID)
	  				node - The node which will be selected
	\* ---------------------------------------------------------------------- */
	nodeSelection: function(n, node) {
		
		var doc = this.getEditorWindow(n).document;
		var sel = this.getSelection(n);
		var range = this.getRange(sel);
		
		if(!this.isIE()) {
			if (node.nodeName == "BODY") {
				range.selectNodeContents(node);
			} else {
				range.selectNode(node);
			}

			/*
			if (endNode) {
				try {
					range.setStart(node, startOffset);
					range.setEnd(endNode, endOffset);
				} catch(e) {
				}
			}
			*/
			
			if (sel) sel.removeAllRanges();
			if (sel) sel.addRange(range);	
		}
		else {
			// MSIE may not select everything when BODY is selected - 
			// start may be set to first text node instead of first non-text node - 
			// no known workaround
			if ((node.nodeName == "TABLE") || (node.nodeName == "IMG") || (node.nodeName == "INPUT") || (node.nodeName == "SELECT") || (node.nodeName == "TEXTAREA")) {
				try {
					var range = doc.body.createControlRange();
					range.addElement(node);
					range.select();
				} 
				catch(e) { }
			} 
			else {
				var range = doc.body.createTextRange();
				if (range) {
					range.collapse();
					if (range.moveToElementText) {
						try {
							range.moveToElementText(node);
							range.select();
						} catch(e) {
							try {
								range = doc.body.createTextRange();
								range.moveToElementText(node);
								range.select();
							} 
							catch(e) {}
						}
					} else {
						try {
							range = doc.body.createTextRange();
							range.moveToElementText(node);
							range.select();
						} 
						catch(e) {}
					}
				}
			}
		}
	}
};

/* ---------------------------------------------------------------------- *\
	Write context menu div
\* ---------------------------------------------------------------------- */
document.write('<div id="wysiwyg_contextmenu" style="position:absolute; z-index:10; visibility:hidden;"></div>');

/* ---------------------------------------------------------------------- *\
  Function    : $()
  Description : Get an element by Id
  Notes       : prototype framework
\* ---------------------------------------------------------------------- */
function $(id) {
	return document.getElementById(id);
}

/* ---------------------------------------------------------------------- *\
  Function    : insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement()
  Description : Emulates insertAdjacentHTML(), insertAdjacentText() and 
  				insertAdjacentElement() three functions so they work with 
				Netscape 6/Mozilla
  Notes       : by Thor Larholm me@jscript.dk
\* ---------------------------------------------------------------------- */
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){
  HTMLElement.prototype.insertAdjacentElement = function (where,parsedNode) {
	  switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) 
      this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr) {
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML);
	}


	HTMLElement.prototype.insertAdjacentText = function (where,txtStr) {
		var parsedText = document.createTextNode(txtStr);
		this.insertAdjacentElement(where,parsedText);
	}
};




