﻿// RichEdit.js
// NOTE: global variables and functions in this file should
// be prefixed with "re"

//function reOnClientLoad(editor)
//{
//    alert("here:" + editor);
//	// nothing
//}


function reOnClientLoad(sender, args) {

    // Temp bug fix for http://www.telerik.com/account/support-tickets/view-ticket.aspx?threadid=223768   
    // Temp Bug fixes makes focus on last control which doesn't work. 
//    window.setTimeout(function() {
//        sender.set_mode(2);
//        sender.set_mode(1);
//    }, 100);
    
} 
// Custom filter for cleaning HTML
function reCustomHtmlFilter()
{  
	this.GetDesignContent = function (content) {
			return content;
		};

	this.GetHtmlContent = function (content) {
			return content;
		};

	this.GetCleanContent = function (content) {  
			
			// Convert block element tags to <br />
			var blockElementRegex = /<(\/?(h\d|table)|\/tr)\b((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gi;
			content = content.replace(blockElementRegex, '<br />');

			// Remove script and object tags
			var scriptTagsRegex = /<\s*(script|object)((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>(.|\s)*?<\/\s*\1\s*>/gi;
			content = content.replace(scriptTagsRegex, '');
			
			// Remove javascript from hrefs
			var removeHrefJSRegex = /(\s+href\s*=\s*)((?:'|"))\s*javascript:.*?\2/gi;
			content = content.replace(removeHrefJSRegex, '$1$2#$2');

			// Remove tags (except allowed ones)
			// NOTE: add tags here that are ALLOWED (uses a negative look-ahead)
			var removeTagsRegex = /<\/?\s*(?!(ul|ol|li|b|i|strong|em|u|p|div|span|br|a)\b)(\w+)((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gi;
			content = content.replace(removeTagsRegex, ' ');

			// Remove disallowed attributes
			var removeAttributesRegex = /\s+(on\w+|id|class|style|align)\s*=\s*(?:".*?"|'.*?'|[^'">\s]+)/gi;
			content = content.replace(removeAttributesRegex, '');

			// Remove comments
			var commentRegex = /<\!--(.|\s)*?-->/gi;
			content = content.replace(commentRegex, '');

			// Remove all empty tags
			var emptyCleanerRegex = /<\s*(\w+)((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)>\s*<\/\s*\1\s*>\s*/gi;
			content = content.replace(emptyCleanerRegex, '');

			// Clean up excess white space
			content = content.replace(/([ \t]{2,})/g, ' '); // replaces two or more spaces/tabs with a single space
			content = content.replace(/^\s+$/g, ''); // replaces lines of all whitespace with nothing
			content = content.replace(/(\n){2,}/g, '\n'); // replaces two or more line breaks with a single line break
			content = content.replace(/(<\/?)\s*(\w+)((?:\s+\w+(?:\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)*)\s+((?:\s?\/)?>)/g, '$1$2$3$4'); // cleans whitespace inside tags

			return content;
		};
}  

var reHtmlFilter = new reCustomHtmlFilter();

// Disables editing in the specified editor instance.
function reDisableTelerikEditor(editor)
{
	editor["OnClientCommandExecuting"] = function()
	{
		return false;
	}
	
	var oFun = function(e)
	{
		if (document.all) 
		{
			e = (e == null)? window.event : e;				
			e.returnValue = false;
			e.cancelBubble = true; 
			return false;
		}
		else 
		{
			e.preventDefault();
			e.preventBubble();
			e.stopPropagation();
		}

	};
				
	editor.AttachEventHandler ('onkeydown', oFun);
	editor.AttachEventHandler ('onpaste', oFun);
	
}

// This method is hooked to the editor and is called before
// an editor command is executed
function reOnClientCommandExecuting(editor, commandName, oTool)
{
    //alert('reOnClientCommandExecuting');
	// do nothing
}

// This method is hooked to the editor and is called after
// an editor command is executed
function reOnClientCommandExecuted(editor, commandName, oTool)
{
    //alert('reOnClientCommandExecuted');
	// NOTE: The PasteFromWord processing only works in IE
	if(commandName=='Paste' || commandName=='PasteFromWord')
	{
		window.setTimeout(
			function()
			{

				 // Run the custom HTML filter
         var html = editor.GetHtml();
         var cleanedHtml = reHtmlFilter.GetCleanContent(html);
         editor.SetHtml(cleanedHtml, "Cleaned text");

				 //call the FormatStripper tool to clean any remaining MSWord formatting
				 var oTool = {};
				 oTool.GetSelectedValue = function(){ return 'WORD_ALL';}
				 editor.Fire('FormatStripper', oTool);
			   
			}
			, 50);
	}
}
