//------------------------------------------------------------------------------
// HTML Editor
//------------------------------------------------------------------------------

var _html=
{
  
  init: function(objEditor)
  {
    // We put the content of our DIV into the htmContent variable which we later
	  // restore in the newly build editor
  	var htmContent=objEditor.innerHTML;
  	var strEditorName=objEditor.id;

    intH=objEditor.clientHeight-_BorderTW(objEditor)-_BorderBW(objEditor);
    intW=objEditor.clientWidth -_BorderLW(objEditor)-_BorderRW(objEditor);
    intH_Toolbar=parseInt(500/intW+0.99)*16+1;
    intH_Frame=intH-intH_Toolbar;
 	
	  // building the editor
  	var htmEditor="";
  	
  	htmEditor+="<DIV CLASS=_ToolBar STYLE='width:"+(intW-2)+"px;'>";
  
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','bold');><IMG SRC=img/icons/text_bold.png></A>";
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','italic');><IMG SRC=img/icons/text_italic.png></A>";
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','underline');><IMG SRC=img/icons/text_underline.png></A>";
  	htmEditor+="<IMG SRC=img/seperate.gif>";
  
  	htmEditor+="<A HREF=javascript:_html.blockFormat('"+strEditorName+"','<H1&gt;');><IMG SRC=img/icons/text_heading_1.png></A>";
    htmEditor+="<A HREF=javascript:_html.blockFormat('"+strEditorName+"','<H2&gt;');><IMG SRC=img/icons/text_heading_2.png></A>";
    htmEditor+="<A HREF=javascript:_html.blockFormat('"+strEditorName+"','<P&gt;');><IMG SRC=img/icons/heading_erase.png></A>";
    htmEditor+="<IMG SRC=img/seperate.gif>";
  
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','justifyleft');><IMG SRC=img/icons/text_align_left.png></A>";
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','justifycenter');><IMG SRC=img/icons/text_align_center.png></A>";
    htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','justifyright');><IMG SRC=img/icons/text_align_right.png></A>";
  	htmEditor+="<IMG SRC=img/seperate.gif>";       
  
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','indent');><IMG SRC=img/icons/text_indent.png></A>";
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','outdent');><IMG SRC=img/icons/text_indent_remove.png></A>";
    htmEditor+="<IMG SRC=img/seperate.gif>";
  
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','copy');><IMG SRC=img/icons/page_copy.png></A>";
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','cut');><IMG SRC=img/icons/cut.png></A>";
    htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','paste');><IMG SRC=img/icons/page_paste.png></A>";
    htmEditor+="<IMG SRC=img/seperate.gif>";
  
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','undo');><IMG SRC=img/icons/arrow_undo.png></A>";
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','redo');><IMG SRC=img/icons/arrow_redo.png></A>";
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','RemoveFormat');><IMG SRC=img/icons/edit-clear.png></A>";
    htmEditor+="<IMG SRC=img/seperate.gif>";   
  
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','insertunorderedlist');><IMG SRC=img/icons/text_list_bullets.png></A>";
  	htmEditor+="<A HREF=javascript:_html.format('"+strEditorName+"','insertorderedlist');><IMG SRC=img/icons/text_list_numbers.png></A>";
    htmEditor+="<IMG SRC=img/seperate.gif>";
  
    htmEditor+="<A HREF=javascript:_html.insertImg('"+strEditorName+"');><IMG SRC=img/icons/image_add.png></A>";
    htmEditor+="<A HREF=javascript:_html.insertObj('"+strEditorName+"','');><IMG SRC=img/icons/youtube.gif></A>";	
    htmEditor+="<A HREF=javascript:_html.cleanUp('"+strEditorName+"')><IMG SRC=img/icons/cut.png></A>";
    htmEditor+="<A HREF=javascript:alert(document.getElementById('html_"+strEditorName+"').contentWindow.document.body.innerHTML)><IMG SRC=img/icons/cut.png></A>";
    htmEditor+="<IMG SRC=img/nodot.gif ID=EndToolBar_"+strEditorName+">";
  	htmEditor+="</DIV>";
  	 	

  	htmEditor+="<IFRAME FRAMEBORDER=0 ID=html_"+strEditorName+" STYLE='position:absolute;left:0;top:"+intH_Toolbar+"px;width:"+(intW-2)+"px;height:"+intH_Frame+"px;' CLASS=_Control></IFRAME>";
   
  	//Editor in DIV-element plaatsen.
  	objEditor.innerHTML=htmEditor;
  
  	//Inhoud in de editor plaatsen
  	var objEditorFrame=document.getElementById('html_' + strEditorName);
  	objEditorFrame.contentWindow.document.open();
  	objEditorFrame.contentWindow.document.write("<HTML><HEAD><link rel=stylesheet href="+_StyleSheet+" type=text/css><META NAME=\"Content-Type\" CONTENT=\"text/html; charset=windows-1252\"></HEAD><BODY style='padding:4px;'>");
  	objEditorFrame.contentWindow.document.write(htmContent);
  	objEditorFrame.contentWindow.document.write("</BODY></HTML>");
  	objEditorFrame.contentWindow.document.close();
  
  	//Editor activeren
    objEditorFrame.contentWindow.document.body.contentEditable = true;
    _SetDesignMode(objEditorFrame);
    
	//Attach the getter for this controls value
    objEditor.getValue=_html.getValue;
		objEditor.setValue=_html.setValue;
  },
  
  cleanUp: function(strEditorName)
  {
    document.getElementById('html_' + strEditorName).contentWindow.document.body.innerHTML=_html.cleanWord(document.getElementById('html_' + strEditorName).contentWindow.document.body.innerHTML);
  },
  
  format: function(strEditorName,strCommand)
  {
  	// Simpele format commando's
    document.getElementById('html_' + strEditorName).contentWindow.document.execCommand(strCommand, false, null);
  },
  
  //----------------------------------------------------------------------------
  // File Pick
	// InsertImg shows the file-pick dialog, callBackInsertImage 
  strCurrentEditorName:'',
  objCurrentRange:null,
    
  insertImg: function(strEditorName,strCommand)
  {
    _html.strCurrentEditorName=strEditorName;
	
	  if (document.all) 
	  {
		  // for IE we need to save the current cursor position/selection
		  var objEditorFrame = document.getElementById('html_' + strEditorName);
		  objEditorFrame.contentWindow.focus();
		  _html.objCurrentRange = objEditorFrame.contentWindow.document.selection.createRange();
	  }
  	filePopUpFileBrowser(_html.callBackInsertImage);

  },
  callBackInsertImage: function(strImageName)
  {
    var objEditorFrame=document.getElementById('html_'+_html.strCurrentEditorName);

    htmImage='<IMG SRC="'+strImageName+'">';

    if (document.all) 
    {
        _html.objCurrentRange.pasteHTML(htmImage);
        _html.objCurrentRange.collapse(false);
        _html.objCurrentRange.select();
    } 
    else 
    {
		  var objEditorFrame=document.getElementById('html_'+_html.strCurrentEditorName);
      objEditorFrame.contentWindow.document.execCommand('inserthtml', false, htmImage);
    }
	objEditorFrame.contentWindow.focus();
	
  },
  
  insertObj: function(strEditorName,strCommand)
  {
  	// Simpele format commando's
  	var htmCode=prompt("Voer htmlcode in voor youtube- ('insluiten') of ander mediaobject.");
    if (document.all) 
		{
      _html.objCurrentRange.pasteHTML(htmCode);
      _html.objCurrentRange.collapse(false);
      _html.objCurrentRange.select();
    } 
		else 
		{
      document.getElementById('html_' + strEditorName).contentWindow.document.execCommand('inserthtml',false, htmCode);
    }
  
  },
  insertTxt: function(strEditorName,htmTxt)
  {
    if (document.all) 
	  {
      _html.objCurrentRange.pasteHTML(htmTxt);
      _html.objCurrentRange.collapse(false);
      _html.objCurrentRange.select();
    } 
	  else 
	  {
      objEditorFrame.contentWindow.document.execCommand('inserthtml', false, htmTxt);
    }
  },
  
  blockFormat: function(strEditorName,strTagName)
  {
  	//formateer een blok door er de aangegeven tag omheen te zetten.
  	document.getElementById('html_' + strEditorName).contentWindow.document.execCommand('formatblock', false, strTagName);
  },
  
  getValue: function()
  {
  	var htmValue=document.getElementById('html_' + this.id).contentWindow.document.body.innerHTML;
  	return htmValue;
  },
	setValue: function(sValue)
  {
    document.getElementById('html_' + this.id).contentWindow.document.body.innerHTML=sValue;
  },
  
  cleanWord: function( htmText )
  {
    htmText = htmText.replace(/<o:p>\s*<\/o:p>/g, "") ;
    htmText = htmText.replace(/<o:p>.*?<\/o:p>/g, "&nbsp;") ;
    htmText = htmText.replace( /\s*mso-[^:]+:[^;"]+;?/gi, "" ) ;
    htmText = htmText.replace( /\s*MARGIN: 0cm 0cm 0pt\s*;/gi, "" ) ;
    htmText = htmText.replace( /\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"" ) ;
    htmText = htmText.replace( /\s*TEXT-INDENT: 0cm\s*;/gi, "" ) ;
    htmText = htmText.replace( /\s*TEXT-INDENT: 0cm\s*"/gi, "\"" ) ;
    htmText = htmText.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"" ) ;
    htmText = htmText.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"" ) ;
    htmText = htmText.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"" ) ;
    htmText = htmText.replace( /\s*tab-stops:[^;"]*;?/gi, "" ) ;
    htmText = htmText.replace( /\s*tab-stops:[^"]*/gi, "" ) ;
    htmText = htmText.replace( /\s*face="[^"]*"/gi, "" ) ;
    htmText = htmText.replace( /\s*face=[^ >]*/gi, "" ) ;
    htmText = htmText.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, "" ) ;
    htmText = htmText.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
    htmText = htmText.replace( /<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3" ) ;
    htmText = htmText.replace( /\s*style="\s*"/gi, '' ) ; 
    htmText = htmText.replace( /<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi, '&nbsp;' ) ; 
    htmText = htmText.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' ) ; 
    htmText = htmText.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ; 
    htmText = htmText.replace( /<SPAN\s*>(.*?)<\/SPAN>/gi, '$1' ) ; 
    htmText = htmText.replace( /<FONT\s*>(.*?)<\/FONT>/gi, '$1' ) ;
    htmText = htmText.replace(/<\\?\?xml[^>]*>/gi, "") ; 
    htmText = htmText.replace(/<\/?\w+:[^>]*>/gi, "") ; 
    htmText = htmText.replace( /<H\d>\s*<\/H\d>/gi, '' ) ;
    htmText = htmText.replace( /<H1([^>]*)>/gi, '' ) ;
    htmText = htmText.replace( /<H2([^>]*)>/gi, '' ) ;
    htmText = htmText.replace( /<H3([^>]*)>/gi, '' ) ;
    htmText = htmText.replace( /<H4([^>]*)>/gi, '' ) ;
    htmText = htmText.replace( /<H5([^>]*)>/gi, '' ) ;
    htmText = htmText.replace( /<H6([^>]*)>/gi, '' ) ;
    htmText = htmText.replace( /<\/H\d>/gi, '<br>' ) ; //remove this to take out breaks where Heading tags were 
    htmText = htmText.replace( /<(U|I|STRIKE)>&nbsp;<\/\1>/g, '&nbsp;' ) ;
    htmText = htmText.replace( /<(B|b)>&nbsp;<\/\b|B>/g, '' ) ;
    htmText = htmText.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
    htmText = htmText.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
    htmText = htmText.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' ) ;
    //some RegEx code for the picky browsers
    var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi") ;
    htmText = htmText.replace( re, "<div$2</div>" ) ;
    var re2 = new RegExp("(<font|<FONT)([^*>]*>.*?)(<\/FONT>|<\/font>)","gi") ; 
    htmText = htmText.replace( re2, "<div$2</div>") ;
    htmText = htmText.replace( /size|SIZE = ([\d]{1})/g, '' ) ;
    
    return htmText ;
  }

}


