/*
	Use:
	$('#BBCodeEditor').ruiEditor();
*/

(function( $ ) {
  $.fn.ruiEditor = function( options ) {
  
  
  
	// Create some defaults, extending them with any options that were provided
    var settings = $.extend( {
      Bold				: true,
      Italics 			: true,
      Underline 		: true,
      Strikethrough 	: true,
      Indent 			: true,
      Size 				: true,
      Color 			: true,
      OrderedList 		: true,
      UnorderedList		: true
    }, options);
	
	
	
	return this.each(function(){
		$(this).attr('class', '');
		$(this).addClass('ui-corner-all');
		$(this).addClass('rui-editor-container');
		var textareaEditor = $(this).find('textarea').addClass('rui-editor-textarea')[0];
		var menu = $('<ul id="icons" class="ui-widget ui-helper-clearfix"></ul>');
		/*BOLD*/
		if (settings.Bold)
			$(menu).append($('<li><a id="' + $(textareaEditor).attr('id') + '_b" href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-b"></span></a></li>').bind('mouseover', function(){
					$(this).find('a').addClass('ui-state-hover');
				}).bind('mouseout', function(){
					$(this).find('a').removeClass('ui-state-hover');
				}).bind('mousedown', function(){
					$(this).find('a').addClass('ui-state-active');
				}).bind('mouseup', function(){
					$(this).find('a').removeClass('ui-state-active');
				}).bind('click', function(){
					CreateBBCode(textareaEditor, 'B', null);
					return false;
				}));
		/*ITALIC*/
		if (settings.Italics)
			$(menu).append($('<li><a id="' + $(textareaEditor).attr('id') + '_i" href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-i"></span></a></li>').bind('mouseover', function(){
					$(this).find('a').addClass('ui-state-hover');
				}).bind('mouseout', function(){
					$(this).find('a').removeClass('ui-state-hover');
				}).bind('mousedown', function(){
					$(this).find('a').addClass('ui-state-active');
				}).bind('mouseup', function(){
					$(this).find('a').removeClass('ui-state-active');
				}).bind('click', function(){
					CreateBBCode(textareaEditor, 'I', null);
					return false;
				}));
		/*UNDERLINE*/
		if (settings.Underline)
			$(menu).append($('<li><a id="' + $(textareaEditor).attr('id') + '_u" href="#u" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-u"></span></a></li>').bind('mouseover', function(){
					$(this).find('a').addClass('ui-state-hover');
				}).bind('mouseout', function(){
					$(this).find('a').removeClass('ui-state-hover');
				}).bind('mousedown', function(){
					$(this).find('a').addClass('ui-state-active');
				}).bind('mouseup', function(){
					$(this).find('a').removeClass('ui-state-active');
				}).bind('click', function(){
					CreateBBCode(textareaEditor, 'U', null);
					return false;
				}));
		/*STRIKETHROUGH*/
		if (settings.Strikethrough)
			$(menu).append($('<li><a href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-s"></span></a></li>').bind('mouseover', function(){
					$(this).find('a').addClass('ui-state-hover');
				}).bind('mouseout', function(){
					$(this).find('a').removeClass('ui-state-hover');
				}).bind('mousedown', function(){
					$(this).find('a').addClass('ui-state-active');
				}).bind('mouseup', function(){
					$(this).find('a').removeClass('ui-state-active');
				}).bind('click', function(){
					CreateBBCode(textareaEditor, 'S', null);
					return false;
				}));
		/*INDENT*/
		if (settings.Indent)
			$(menu).append($('<li><a href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-indent"></span></a></li>').bind('mouseover', function(){
					$(this).find('a').addClass('ui-state-hover');
				}).bind('mouseout', function(){
					$(this).find('a').removeClass('ui-state-hover');
				}).bind('mousedown', function(){
					$(this).find('a').addClass('ui-state-active');
				}).bind('mouseup', function(){
					$(this).find('a').removeClass('ui-state-active');
				}).bind('click', function(){
					CreateBBCode(textareaEditor, 'INDENT', null);
					return false;
				}));
		/*UNORDEREDLIST*/
		if (settings.UnorderedList)
			$(menu).append($('<li><a href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-ul"></span></a></li>').bind('mouseover', function(){
					$(this).find('a').addClass('ui-state-hover');
				}).bind('mouseout', function(){
					$(this).find('a').removeClass('ui-state-hover');
				}).bind('mousedown', function(){
					$(this).find('a').addClass('ui-state-active');
				}).bind('mouseup', function(){
					$(this).find('a').removeClass('ui-state-active');
				}).bind('click', function(){
					CreateBBCode(textareaEditor, 'UL', null);
					return false;
				}));
		/*ORDEREDLIST*/
		if (settings.OrderedList)
			$(menu).append($('<li><a href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-ol"></span></a></li>').bind('mouseover', function(){
					$(this).find('a').addClass('ui-state-hover');
				}).bind('mouseout', function(){
					$(this).find('a').removeClass('ui-state-hover');
				}).bind('mousedown', function(){
					$(this).find('a').addClass('ui-state-active');
				}).bind('mouseup', function(){
					$(this).find('a').removeClass('ui-state-active');
				}).bind('click', function(){
					CreateBBCode(textareaEditor, 'OL', null);
					return false;
				}));
		/*COLOR*/
		if (settings.Color)
			$(menu).append($('<li></li>').append($('<a href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-c"></span></a>').bind('mouseover', function(){
					$(this).addClass('ui-state-hover');
				}).bind('mouseout', function(){
					$(this).removeClass('ui-state-hover');
				}).bind('mousedown', function(){
					$(this).addClass('ui-state-active');
				}).bind('mouseup', function(){
					$(this).removeClass('ui-state-active');
				}).bind('click', function(){
					CreateBBCode(textareaEditor, 'COLOR', 'VALUE="WHITE"');
					return false;
				})).append($('<ul></ul>')
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-c-white"></span></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'COLOR', 'VALUE="WHITE"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-c-red"></span></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'COLOR', 'VALUE="RED"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-c-green"></span></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'COLOR', 'VALUE="GREEN"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-c-blue"></span></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'COLOR', 'VALUE="BLUE"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-c-orange"></span></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'COLOR', 'VALUE="ORANGE"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-c-purple"></span></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'COLOR', 'VALUE="PURPLE"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-c-gray"></span></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'COLOR', 'VALUE="GRAY"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-c-yellow"></span></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'COLOR', 'VALUE="YELLOW"');
								return false;
							})))
				));
		/*SIZE*/
		if (settings.Size)
			$(menu).append($('<li></li>').append($('<a href="javascript:void(0);" class="ui-state-default ui-corner-all"><span class="rui-icon rui-icon-size"></span></a>').bind('mouseover', function(){
					$(this).addClass('ui-state-hover');
				}).bind('mouseout', function(){
					$(this).removeClass('ui-state-hover');
				}).bind('mousedown', function(){
					$(this).addClass('ui-state-active');
				}).bind('mouseup', function(){
					$(this).removeClass('ui-state-active');
				}).bind('click', function(){
					CreateBBCode(textareaEditor, 'SIZE', 'VALUE="NONE"');
					return false;
				})).append($('<ul></ul>')
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><div class="rui-icon-size-div"><h1>T</h1></div></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'SIZE', 'VALUE="1"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><div class="rui-icon-size-div"><h2>T</h2></div></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'SIZE', 'VALUE="2"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><div class="rui-icon-size-div"><h3>T</h3></div></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'SIZE', 'VALUE="3"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><div class="rui-icon-size-div"><h4>T</h4></div></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'SIZE', 'VALUE="4"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><div class="rui-icon-size-div"><h5>T</h5></div></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'SIZE', 'VALUE="5"');
								return false;
							})))
					.append($('<li></li>').append($('<a id="ac1" href="javascript:void(0);" class="ui-state-default ui-corner-all"><div class="rui-icon-size-div"><h6>T</h6></div></a>').bind('mouseover', function(){
								$(this).addClass('ui-state-hover');
							}).bind('mouseout', function(){
								$(this).removeClass('ui-state-hover');
							}).bind('mousedown', function(){
								$(this).addClass('ui-state-active');
							}).bind('mouseup', function(){
								$(this).removeClass('ui-state-active');
							}).bind('click', function(){
								CreateBBCode(textareaEditor, 'SIZE', 'VALUE="6"');
								return false;
							})))
				));
		$(this).prepend(menu);// hotkeys 
		
		$(this).prepend( $('<a id="' + $(textareaEditor).attr('id') + '_apreview" href="javascript:void(0);">Preview</a>').button().bind('click', function(){
				$('#' + $(textareaEditor).attr('id') + '_divPreview').html($(textareaEditor).val()
					.replace(/\[INDENT\]/g, '<div style="margin-left:15px;">')
					.replace(/\[\/INDENT\]/g, '</div>')
					.replace(/\[B\]/g, '<b>')
					.replace(/\[\/B\]/g, '</b>')
					.replace(/\[I\]/g, '<i>')
					.replace(/\[\/I\]/g, '</i>')
					.replace(/\[U\]/g, '<u>')
					.replace(/\[\/U\]/g, '</u>')
					.replace(/\[S\]/g, '<s>')
					.replace(/\[\/S\]/g, '</s>')
					.replace(/\[UL\]/g, '<ul>')
					.replace(/\[\/UL\]/g, '</ul>')
					.replace(/\[OL\]/g, '<ol>')
					.replace(/\[\/OL\]/g, '</ol>')
					.replace(/\[LI\]/g, '<li>')
					.replace(/\[\/LI\]/g, '</li>')
					.replace(/\[COLOR VALUE=\"WHITE\"\]/g, '<span style="color:white;">')
					.replace(/\[\/COLOR\]/g, '</span>')
					.replace(/\[COLOR VALUE=\"RED\"\]/g, '<span style="color:red;">')
					.replace(/\[\/COLOR\]/g, '</span>')
					.replace(/\[COLOR VALUE=\"GREEN\"\]/g, '<span style="color:green;">')
					.replace(/\[\/COLOR\]/g, '</span>')
					.replace(/\[COLOR VALUE=\"BLUE\"\]/g, '<span style="color:blue;">')
					.replace(/\[\/COLOR\]/g, '</span>')
					.replace(/\[COLOR VALUE=\"ORANGE\"\]/g, '<span style="color:orange;">')
					.replace(/\[\/COLOR\]/g, '</span>')
					.replace(/\[COLOR VALUE=\"PURPLE\"\]/g, '<span style="color:purple;">')
					.replace(/\[\/COLOR\]/g, '</span>')
					.replace(/\[COLOR VALUE=\"GRAY\"\]/g, '<span style="color:gray;">')
					.replace(/\[\/COLOR\]/g, '</span>')
					.replace(/\[COLOR VALUE=\"YELLOW\"\]/g, '<span style="color:yellow;">')
					.replace(/\[\/COLOR\]/g, '</span>')
					.replace(/\[SIZE VALUE=\"1\"\]/g, '<h1>')
					.replace(/\[\/SIZE\]/g, '</h1>')
					.replace(/\[SIZE VALUE=\"2\"\]/g, '<h2>')
					.replace(/\[\/SIZE\]/g, '</h2>')
					.replace(/\[SIZE VALUE=\"3\"\]/g, '<h3>')
					.replace(/\[\/SIZE\]/g, '</h3>')
					.replace(/\[SIZE VALUE=\"4\"\]/g, '<h4>')
					.replace(/\[\/SIZE\]/g, '</h4>')
					.replace(/\[SIZE VALUE=\"5\"\]/g, '<h5>')
					.replace(/\[\/SIZE\]/g, '</h5>')
					.replace(/\[SIZE VALUE=\"6\"\]/g, '<h6>')
					.replace(/\[\/SIZE\]/g, '</h6>')
					.replace(/\n/g, '<br>')
				).show();
				$(textareaEditor).hide();
			})
		);
		$(this).prepend( $('<a id="' + $(textareaEditor).attr('id') + '_aedit" href="javascript:void(0);">Edit</a>').button().bind('click', function(){
				$('#' + $(textareaEditor).attr('id') + '_divPreview').hide();
				$(textareaEditor).show();
			})
		);
		
		$(this).append( $('<div id="' + $(textareaEditor).attr('id') + '_divPreview" style="display:none;width:' + $(textareaEditor).width() + 'px;height:' + $(textareaEditor).height() + 'px;overflow:auto;color:white;"></div>') );
		
		var isCtrl = false;
		$(textareaEditor).keyup(function (e) { 
			if(e.which == 17) isCtrl=false; }).keydown(function (e) { 
				if(e.which == 17) isCtrl=true; 
				if (e.which == 66 && isCtrl == true) // CTRL + B, bold
				{ 
					$('#' + $(textareaEditor).attr('id') + '_b').click();
					return false;
				} 
				else if (e.which == 73 && isCtrl == true) // CTRL + I, italic
				{ 
					$('#' + $(textareaEditor).attr('id') + '_i').click();
					return false;
				} 
				else if (e.which == 85 && isCtrl == true) // CTRL + U, underline
				{ 
					$('#' + $(textareaEditor).attr('id') + '_u').click();
					return false;
				}
				else if (e.which == 90 && isCtrl == true) // CTRL + Z, undo
				{
					$(this).val(backUp.pop());
					return false;
				}
				else if (isCtrl == false)
					backUp.push($(this).val());
			}
		);
	});
  };
})( jQuery );

var backUp = new Array();backUp.push('');
function CreateBBCode (element, tag, args) {
	if (tag == null)
		return;
	var attributes = '';
	if (args != null)
		attributes = ' ' + args;
	var bbCodeOpen = '[' + tag + attributes + ']';
	var bbCodeClose = '[/' + tag + ']';
	var createListItems = false;
	var changeColor = false;
	var changeSize = false;
	if(tag == 'UL' || tag == 'OL') /*If we are creating a UL or OL, we need to create list items deliminated by the newline character*/
	{
		createListItems = true;
		bbCodeOpen = bbCodeOpen + '\n[LI]';
		bbCodeClose = '[/LI]\n' + bbCodeClose;
	}
	if (tag == 'COLOR' && args != null) /*If we are creating a color tag, we may be able to just change the color out*/
		changeColor = true;
	if (tag == 'SIZE' && args != null) /*If we are creating a size tag, we may be able to just change the size out*/
		changeSize = true;
    if (document.selection) {
		element.focus();
		backUp.push(element.value);
		sel = document.selection.createRange();
		if (createListItems)
			sel.text = bbCodeOpen + sel.text.replace(/\[LI\]/g, '')
											.replace(/\[\/LI\]/g, '')
											.replace(/\[UL\]\n/g, '')
											.replace(/\n\[\/UL\]/g, '')
											.replace(/\[OL\]\n/g, '')
											.replace(/\n\[\/OL\]/g, '')
											.replace(/\n/g, '[/LI]\n[LI]') + bbCodeClose;
		else if (sel.text.indexOf('[COLOR VALUE="WHITE"]') == 0 && changeColor)
			sel.text = bbCodeOpen + sel.text.replace('[COLOR VALUE="WHITE"]', '');
		else if (sel.text.indexOf('[COLOR VALUE="RED"]') == 0 && changeColor)
			sel.text = bbCodeOpen + sel.text.replace('[COLOR VALUE="RED"]', '');
		else if (sel.text.indexOf('[COLOR VALUE="GREEN"]') == 0 && changeColor)
			sel.text = bbCodeOpen + sel.text.replace('[COLOR VALUE="GREEN"]', '');
		else if (sel.text.indexOf('[COLOR VALUE="BLUE"]') == 0 && changeColor)
			sel.text = bbCodeOpen + sel.text.replace('[COLOR VALUE="BLUE"]', '');
		else if (sel.text.indexOf('[COLOR VALUE="ORANGE"]') == 0 && changeColor)
			sel.text = bbCodeOpen + sel.text.replace('[COLOR VALUE="ORANGE"]', '');
		else if (sel.text.indexOf('[COLOR VALUE="PURPLE"]') == 0 && changeColor)
			sel.text = bbCodeOpen + sel.text.replace('[COLOR VALUE="PURPLE"]', '');
		else if (sel.text.indexOf('[COLOR VALUE="GRAY"]') == 0 && changeColor)
			sel.text = bbCodeOpen + sel.text.replace('[COLOR VALUE="GRAY"]', '');
		else if (sel.text.indexOf('[COLOR VALUE="YELLOW"]') == 0 && changeColor)
			sel.text = bbCodeOpen + sel.text.replace('[COLOR VALUE="YELLOW"]', '');
		else if (sel.text.indexOf('[SIZE VALUE="1"]') == 0 && changeSize)
			sel.text = bbCodeOpen + sel.text.replace('[SIZE VALUE="1"]', '');
		else if (sel.text.indexOf('[SIZE VALUE="2"]') == 0 && changeSize)
			sel.text = bbCodeOpen + sel.text.replace('[SIZE VALUE="2"]', '');
		else if (sel.text.indexOf('[SIZE VALUE="3"]') == 0 && changeSize)
			sel.text = bbCodeOpen + sel.text.replace('[SIZE VALUE="3"]', '');
		else if (sel.text.indexOf('[SIZE VALUE="4"]') == 0 && changeSize)
			sel.text = bbCodeOpen + sel.text.replace('[SIZE VALUE="4"]', '');
		else if (sel.text.indexOf('[SIZE VALUE="5"]') == 0 && changeSize)
			sel.text = bbCodeOpen + sel.text.replace('[SIZE VALUE="5"]', '');
		else if (sel.text.indexOf('[SIZE VALUE="6"]') == 0 && changeSize)
			sel.text = bbCodeOpen + sel.text.replace('[SIZE VALUE="6"]', '');
		else if (sel.text.indexOf(bbCodeOpen) != 0)
			sel.text = bbCodeOpen + sel.text + bbCodeClose;
    } else if (element.selectionStart || element.selectionStart == '0') {
		element.focus();
		backUp.push(element.value);
		var startPos = element.selectionStart;
		var endPos = element.selectionEnd;
		if (createListItems)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace(/\[LI\]/g, '')
																															.replace(/\[\/LI\]/g, '')
																															.replace(/\[UL\]\n/g, '')
																															.replace(/\n\[\/UL\]/g, '')
																															.replace(/\[OL\]\n/g, '')
																															.replace(/\n\[\/OL\]/g, '')
																															.replace(/\n/g, '[/LI]\n[LI]') + bbCodeClose + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[COLOR VALUE="WHITE"]') == 0 && changeColor)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[COLOR VALUE="WHITE"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[COLOR VALUE="RED"]') == 0 && changeColor)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[COLOR VALUE="RED"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[COLOR VALUE="GREEN"]') == 0 && changeColor)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[COLOR VALUE="GREEN"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[COLOR VALUE="BLUE"]') == 0 && changeColor)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[COLOR VALUE="BLUE"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[COLOR VALUE="ORANGE"]') == 0 && changeColor)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[COLOR VALUE="ORANGE"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[COLOR VALUE="PURPLE"]') == 0 && changeColor)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[COLOR VALUE="PURPLE"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[COLOR VALUE="GRAY"]') == 0 && changeColor)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[COLOR VALUE="GRAY"]', '');
		else if (element.value.substring(startPos, endPos).indexOf('[COLOR VALUE="YELLOW"]') == 0 && changeColor)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[COLOR VALUE="YELLOW"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[SIZE VALUE="1"]') == 0 && changeSize)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[SIZE VALUE="1"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[SIZE VALUE="2"]') == 0 && changeSize)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[SIZE VALUE="2"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[SIZE VALUE="3"]') == 0 && changeSize)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[SIZE VALUE="3"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[SIZE VALUE="4"]') == 0 && changeSize)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[SIZE VALUE="4"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[SIZE VALUE="5"]') == 0 && changeSize)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[SIZE VALUE="5"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf('[SIZE VALUE="6"]') == 0 && changeSize)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos).replace('[SIZE VALUE="6"]', '') + element.value.substring(endPos, element.value.length);
		else if (element.value.substring(startPos, endPos).indexOf(bbCodeOpen) != 0)
			element.value = element.value.substring(0, startPos) + bbCodeOpen + element.value.substring(startPos, endPos) + bbCodeClose + element.value.substring(endPos, element.value.length);
		
		/*reselect the selection including the newly created tags*/
		element.selectionStart = startPos;
		element.selectionEnd = endPos + bbCodeOpen.length + bbCodeClose.length;
    } else {
      element.value += bbCodeOpen + bbCodeClose;
    }
}

function bbCodeHelperGetBBCode(bbCodeText){
    return bbCodeText
        .replace(/<div style=\"margin-left:15px;\">/g, '[INDENT]').replace(/<\/div>/g, '[/INDENT]')
					.replace(/<b>/g, '[B]').replace(/<\/b>/g, '[/B]')
					.replace(/<i>/g, '[I]').replace(/<\/i>/g, '[/I]')
					.replace(/<u>/g, '[U]').replace(/<\/u>/g, '[/U]')
					.replace(/<s>/g, '[S]').replace(/<\/s>/g, '[/S]')
					.replace(/<ul>/g, '[UL]').replace(/<\/ul>/g, '[/UL]')
					.replace(/<ol>/g, '[OL]').replace(/<\/ol>/g, '[/OL]')
					.replace(/<li>/g, '[LI]').replace(/<\/li>/g, '[/LI]')
					.replace(/<span style=\"color:white;\">/g, '[COLOR VALUE="WHITE"]').replace(/<\/span>/g, '[/COLOR]')
					.replace(/<span style=\"color:red;\">/g, '[COLOR VALUE="RED"]').replace(/<\/span>/g, '[/COLOR]')
					.replace(/<span style=\"color:green;\">/g, '[COLOR VALUE="GREEN"]').replace(/<\/span>/g, '[/COLOR]')
					.replace(/<span style=\"color:blue;\">/g, '[COLOR VALUE="BLUE"]').replace(/<\/span>/g, '[/COLOR]')
					.replace(/<span style=\"color:orange;\">/g, '[COLOR VALUE="ORANGE"]').replace(/<\/span>/g, '[/COLOR]')
					.replace(/<span style=\"color:purple;\">/g, '[COLOR VALUE="PURPLE"]').replace(/<\/span>/g, '[/COLOR]')
					.replace(/<span style=\"color:gray;\">/g, '[COLOR VALUE="GRAY"]').replace(/<\/span>/g, '[/COLOR]')
					.replace(/<span style=\"color:yellow;\">/g, '[COLOR VALUE="YELLOW"]').replace(/<\/span>/g, '[/COLOR]')
					.replace(/<h1>/g, '[SIZE VALUE="1"]').replace(/<\/h1>/g, '[/SIZE]')
					.replace(/<h2>/g, '[SIZE VALUE="2"]').replace(/<\/h2>/g, '[/SIZE]')
					.replace(/<h3>/g, '[SIZE VALUE="3"]').replace(/<\/h3>/g, '[/SIZE]')
					.replace(/<h4>/g, '[SIZE VALUE="4"]').replace(/<\/h4>/g, '[/SIZE]')
					.replace(/<h5>/g, '[SIZE VALUE="5"]').replace(/<\/h5>/g, '[/SIZE]')
					.replace(/<h6>/g, '[SIZE VALUE="6"]').replace(/<\/h6>/g, '[/SIZE]');
}
