var objMainEditor;

function FCKeditor_OnComplete( editorInstance )
{
  //if (!fckIsRunning) { var fckIsRunning = []; }
  //fckIsRunning[editorInstance.Name] = true ;
  //fckLaunchedTextareaId.push(editorInstance.Config['TextareaID']) ;
  //fckLaunchedJsId.push(editorInstance.Name) ;
  //fckFirstrun[editorInstance.Name] = true;

  // Enable the switch button. It is disabled at startup, waiting the editor to be loaded.
  //var oElem = document.getElementById('switch_' + editorInstance.Name);
  //if (oElem != null) {
    //oElem.style.display = '';
  //}

  // If the textarea isn't visible update the content from the editor.
  //$(editorInstance.LinkedField.form).submit(DoFCKeditorTeaserStuff);

  //editorInstance.Events.AttachEvent( 'OnAfterLinkedFieldUpdate', DoFCKeditorTeaserStuff ) ;


  // editorInstance.Events.AttachEvent( 'OnBlur', cleanHTML) ;

  //editorInstance.Events.AttachEvent( 'OnPaste', myCustomPaste ) ;


  //var teaser = false;
  //var teaserCheckbox = false;

  //for (var k in Drupal.settings.teaser) {
    //if (Drupal.settings.teaser[k] == editorInstance.Config['TextareaID']) {
      //teaser = k;
      //teaserCheckbox = Drupal.settings.teaserCheckbox[k];
    //}
  //}

  //if (teaser) {
    //$('#' + teaser).attr('disabled', '');
    //$('div[@class=teaser-button-wrapper]').hide();
    //$('#' + teaser).parent().hide();
    //$('#' + teaserCheckbox).parent().show();
  //}

  //Img_Assist integration
  //IntegrateWithImgAssist();


objMainEditor = FCKeditorAPI.GetInstance(editorInstance.Name);
 editorInstance.Events.AttachEvent( 'OnAfterLinkedFieldUpdate', cleanHTML ) ;

}

function cleanHTML() {

var objTxtHTML = objMainEditor.GetHTML();

objTxtHTML = objTxtHTML.replace(/<!--break-->/gi, '');
objTxtHTML = objTxtHTML.replace(/&ldquo;/gi, '"');
objTxtHTML = objTxtHTML.replace(/&rdquo;/gi, '"');
objTxtHTML = objTxtHTML.replace(/&amp;/gi, '&');
objTxtHTML = objTxtHTML.replace(/&ndash;/gi, '-');
objTxtHTML = objTxtHTML.replace(/&lsquo;/gi, "'");
objTxtHTML = objTxtHTML.replace(/&rsquo;/gi, "'");
objTxtHTML = objTxtHTML.replace(/&nbsp;/gi, ' ');
objTxtHTML = objTxtHTML.replace(/&hellip;/gi, '...');
objTxtHTML = objTxtHTML.replace(/&bull;/gi, '-');

tmp = objMainEditor.SetHTML(objTxtHTML);

// Make sure the actual form field gets updated before the form is submitted
document.getElementById('edit-body').value = objTxtHTML;

//alert("Content checked.\r\nPress Enter to continue.");

return true;

}

function sleep(delay) 
{ 
    var start = new Date().getTime(); 
    while (new Date().getTime() < start + delay); 

    return;
}  


function myCustomPaste ( editorInstance )
{

	var textHtml = editorInstance.GetClipboardHTML() ;
	
	textHtml = CleanWord(textHtml);

	editorInstance.InsertHtml(textHtml) ;

	return false;
}




function CleanWord(text)
{
	var html = text;

	html = html.replace(/<o:p>\s*<\/o:p>/g, '') ;
	html = html.replace(/<o:p>.*?<\/o:p>/g, '&nbsp;') ;

	// Remove mso-xxx styles.
	html = html.replace( /\s*mso-[^:]+:[^;"]+;?/gi, '' ) ;

	// Remove margin styles.
	html = html.replace( /\s*MARGIN: 0cm 0cm 0pt\s*;/gi, '' ) ;
	html = html.replace( /\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"" ) ;

	html = html.replace( /\s*TEXT-INDENT: 0cm\s*;/gi, '' ) ;
	html = html.replace( /\s*TEXT-INDENT: 0cm\s*"/gi, "\"" ) ;

	html = html.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"" ) ;

	html = html.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"" ) ;

	html = html.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"" ) ;

	html = html.replace( /\s*tab-stops:[^;"]*;?/gi, '' ) ;
	html = html.replace( /\s*tab-stops:[^"]*/gi, '' ) ;

	// Remove FONT face attributes.
		html = html.replace( /\s*face="[^"]*"/gi, '' ) ;
		html = html.replace( /\s*face=[^ >]*/gi, '' ) ;

		html = html.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, '' ) ;

	// Remove Class attributes
	html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;

	// Remove styles.
	html = html.replace( /<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3" ) ;

	// Remove empty styles.
	html =  html.replace( /\s*style="\s*"/gi, '' ) ;

	html = html.replace( /<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi, '&nbsp;' ) ;

	html = html.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' ) ;

	// Remove Lang attributes
	html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;

	html = html.replace( /<SPAN\s*>(.*?)<\/SPAN>/gi, '$1' ) ;

	html = html.replace( /<FONT\s*>(.*?)<\/FONT>/gi, '$1' ) ;

	// Remove XML elements and declarations
	html = html.replace(/<\\?\?xml[^>]*>/gi, '' ) ;

	// Remove Tags with XML namespace declarations: <o:p><\/o:p>
	html = html.replace(/<\/?\w+:[^>]*>/gi, '' ) ;

	// Remove comments [SF BUG-1481861].
	html = html.replace(/<\!--.*?-->/g, '' ) ;

	html = html.replace( /<(U|I|STRIKE)>&nbsp;<\/\1>/g, '&nbsp;' ) ;

	html = html.replace( /<H\d>\s*<\/H\d>/gi, '' ) ;

	// Remove "display:none" tags.
	html = html.replace( /<(\w+)[^>]*\sstyle="[^"]*DISPLAY\s?:\s?none(.*?)<\/\1>/ig, '' ) ;

	// Remove language tags
	html = html.replace( /<(\w[^>]*) language=([^ |>]*)([^>]*)/gi, "<$1$3") ;

	// Remove onmouseover and onmouseout events (from MS Word comments effect)
	html = html.replace( /<(\w[^>]*) onmouseover="([^\"]*)"([^>]*)/gi, "<$1$3") ;
	html = html.replace( /<(\w[^>]*) onmouseout="([^\"]*)"([^>]*)/gi, "<$1$3") ;




		html = html.replace( /<H1([^>]*)>/gi, '<div$1><b><font size="6">' ) ;
		html = html.replace( /<H2([^>]*)>/gi, '<div$1><b><font size="5">' ) ;
		html = html.replace( /<H3([^>]*)>/gi, '<div$1><b><font size="4">' ) ;
		html = html.replace( /<H4([^>]*)>/gi, '<div$1><b><font size="3">' ) ;
		html = html.replace( /<H5([^>]*)>/gi, '<div$1><b><font size="2">' ) ;
		html = html.replace( /<H6([^>]*)>/gi, '<div$1><b><font size="1">' ) ;

		html = html.replace( /<\/H\d>/gi, '<\/font><\/b><\/div>' ) ;

		// Transform <P> to <DIV>
		var re = new RegExp( '(<P)([^>]*>.*?)(<\/P>)', 'gi' ) ;	// Different because of a IE 5.0 error
		html = html.replace( re, '<div$2<\/div>' ) ;

		// Remove empty tags (three times, just to be sure).
		// This also removes any empty anchor
		html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;
		html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;
		html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;


// CUSTOM

html = html.replace(/&ldquo;/gi, '"');
html = html.replace(/&rdquo;/gi, '"');
html = html.replace(/&amp;/gi, '&');
html = html.replace(/&ndash;/gi, '-');
html = html.replace(/&rsquo;/gi, "'");
html = html.replace(/&amp;/gi, "&");
html = html.replace(/&nbsp;/gi, " ");



	return html ;
}





