    function encimaTrim(input) {
        var a = input.replace(/^\s+/, '');
        return a.replace(/\s+$/, '');
    };

    function encimaRemoveWordTags(content){

        var result = encimaReplaceFunction(content,/style="/i,
        [
                /(mso-[^:]*:[^;]*[;"])+/gi,
//                /(FONT-SIZE:[^;]*[;"])+/gi,
                /(FONT-FAMILY:[^;]*[;"])+/gi,
                /(TEXT-ALIGN:[^;]*[;"])+/gi,
                /(LINE-HEIGHT:[^;]*[;"])+/gi,
                /(MARGIN:[^;]*[;"])+/gi,
                /(MARGIN-[^:]*:[^;]*[;"])+/gi,
                /(PAGE-BREAK-[^:]*:[^;]*[;"])+/gi,
                /(BORDER-[^:]*:[^;]*[;"])+/gi,
                /(PADDING-[^:]*:[^;]*[;"])+/gi,
                /(TAB-STOPS:[^;]*[;"])+/gi,
                /(TEXT-INDENT:[^;]*[;"])+/gi,
        ],/style=""/gi);
        // alle Mso classes verwijderen, western is ook zo'n populaire bij microsoft word
        result = encimaReplaceFunction(result,/class="/i,[/(Mso[\w]*")|(western)/g],/class=""/gi);
        // replace font size
        result = result.replace(/<font([^>]*)size="?\d"?/gi,"<font $1");
        // replace font color black by no specification
        result = result.replace(/<font([^>]*)color="?#000000"?/gi,"<font $1");
        //alert("na class " + result);
        // alle font faces verwijderen
        result = encimaReplaceFunction(result,/face="/i,[/[^"]*"/g],/face=""/gi);
        // double and single empty font tags and font tags without attributes
        result = result.replace(/<font[ ]*>(.*)<\/font>/gi, "$1");
        // surrounding font tags verwijderen die leeg zijn
        result = result.replace(/<font[ ]*>(.*<font[^>]*>.*<\/font>.*)<\/font>/gi, "$1");
        return result;
    }

    function encimaReplaceFunction(content, regexp,replaceArray,replaceEmptyRegexp){
        var resultArray = [];
        var remaining = content;
        var arraycounter  = 0;
        //var currentpos = 0;
        while (remaining.length>0){
            // first lookup the style tags, global ignore case
            var index = remaining.search(regexp);
            //alert(index);
            if (index==-1){
                resultArray[arraycounter] = remaining;
                break;
            }
            else {
                resultArray[arraycounter++] = remaining.substring(0,index);
                // getting remaining parts without the style=" tag
                remaining = remaining.substring(index);
                var match = remaining.match(regexp);
                var innerPartIndex = 0;
                if (match.length>0){
                    resultArray[arraycounter++] = match[0];
                    innerPartIndex = match[0].length;
                }
                remaining = remaining.substring(innerPartIndex);
                // now look for the first "
                var quoteindex = remaining.indexOf("\"");
                if (quoteindex!=-1){
                    var stylePart = remaining.substring(0,quoteindex + 1);
                    // now replace mso tags
                    for (var rc=0;rc<replaceArray.length;rc++){
                       var rxp = replaceArray[rc];
                       stylePart = stylePart.replace(rxp,'');
                    }
                    if (stylePart.length>0 && stylePart.charAt(stylePart.length-1)=="\""){
                        stylePart = stylePart.substring(0,stylePart.length-1);
                    }
                    stylePart = encimaTrim(stylePart);
                    resultArray[arraycounter++] = stylePart;
                    // remaining is the rest after the style
                    remaining = remaining.substring(quoteindex);
                    //currentpos=index;
                }
            }
        }
        var result = '';
        for (var counter=0;counter<resultArray.length;counter++){
            result += resultArray[counter];
        }
        // alle empty style tags eruit gooien
        result = result.replace(replaceEmptyRegexp,'');
        return result;
    }

// remove format
webeditor.removeformatAllHTML = false; // set to true to remove all HTML tags
webeditor.removeformatAllXML = false; // set to true to remove all XML tags
webeditor.removeformatAllNamespace = true; // set to true to remove all namespace attributes
webeditor.removeformatAllLang = true; // set to true to remove all lang attributes
webeditor.removeformatAllClass = false; // set to true to remove all class attributes
webeditor.removeformatAllStyle = true; // set to true to remove all style attributes
webeditor.removeformatEmptyStyle = true; // set to true to remove empty style attributes
webeditor.removeformatEmptySpan = true; // set to true to remove empty span attributes
webeditor.removeformatAllSpan = false; // set to true to remove all span attributes
webeditor.removeformatEmptyFont = true; // set to true to remove empty font tags
webeditor.removeformatAllFont = true; // set to true to remove all font tags
webeditor.removeformatAllDelIns = false; // set to true to remove all del and ins tags
webeditor.removeformatEmptyPDiv = true; // set to true to remove empty p and div tags
webeditor.removeformatAllFormatTags = true; // set to true to remove all format tags
webeditor.removeformatAllMsoClass = false; // set to true to remove all mso classes
webeditor.removeformatAllMsoStyle = false; // set to true to remove all mso styles
webeditor.removeformatAllMsoConditional = false; // set to true to remove all mso conditional tags
// autoclean
webeditor.autoclean = true; // set to true to automatically clean html code
webeditor.autocleanAllHTML = false; // set to true to remove all HTML tags
webeditor.autocleanAllXML = true; // set to true to remove all XML tags
webeditor.autocleanAllNamespace = true; // set to true to remove all namespace attributes
webeditor.autocleanAllLang = true; // set to true to remove all lang attributes
webeditor.autocleanAllClass = false; // set to true to remove all class attributes
webeditor.autocleanAllStyle = false; // set to true to remove all style attributes
webeditor.autocleanEmptyStyle = true; // set to true to remove empty style attributes
webeditor.autocleanEmptySpan = true; // set to true to remove empty span attributes
webeditor.autocleanAllSpan = false; // set to true to remove all span attributes
webeditor.autocleanEmptyFont = true; // set to true to remove empty font tags
webeditor.autocleanAllFont = false; // set to true to remove all font tags
webeditor.autocleanAllDelIns = true; // set to true to remove all del and ins tags
webeditor.autocleanEmptyPDiv = true; // set to true to remove empty p and div tags
webeditor.autocleanAllFormatTags = false; // set to true to remove all format tags
webeditor.autocleanAllMsoClass = true; // set to true to remove all mso classes
webeditor.autocleanAllMsoStyle = true; // set to true to remove all mso styles
webeditor.autocleanAllMsoConditional = true; // set to true to remove all mso conditional tags
webeditor.nbsp2blank = true; // set to true to convert all &nbsp; to blanks
      
function webeditor_custom_encode(content) {
    //alert('webeditor_custom_encode');
    content = cleanContentSub(content, null, 'all_xml', 'all_namespace', 'all_lang', null, null, 'empty_span', null, 'empty_font', null, null, 'empty_p_div', null, 'mso_class', 'mso_style', 'mso_conditional', 'empty_style');
    content = encimaRemoveWordTags(content);
    //alert(content);
    return content;
}
function webeditor_custom_decode(content) {
   //alert('webeditor_custom_decode');
   // cleanContentSub(content, all_html, all_xml, all_namespace, all_lang, all_class, all_style, empty_span, all_span, empty_font, all_font, all_del_ins, empty_p_div, all_format_tags, mso_class, mso_style, mso_conditional, empty_style)
   content = cleanContentSub(content, null, 'all_xml', 'all_namespace', 'all_lang', null, null, 'empty_span', null, 'empty_font', null, null, 'empty_p_div', null, 'mso_class', 'mso_style', 'mso_conditional', 'empty_style');
   content = encimaRemoveWordTags(content);
   return content;
}
