function SF_RichTextEditor(name, ocontainer){

this.name=name;
this.container=ocontainer;
// Private
this.grass = false;
this.sott = false;
this.colleg = false;
this.italic = false;
this.leftal = false;
this.centeral = false;
this.rightal = false;
this.acapo='<br>';
this.t_link='';
this.hasfocus=false;

this.editor=null;

this.button_bgcolor="#EEEEEE";
this.button_forecolor="#4E4E4E";
this.button_width="24px";// to be implemented

this.width="100%";

this.colorpicker=null;
this.imgbrowser=null;

this.img_path="./";

this.preview=false;
this.returnObject=null;

this.browser_str="document.frames('" + this.name + "_editor').";

if(navigator.appName != 'Microsoft Internet Explorer')
 this.browser_str = "document.getElementById('" + this.name + "_editor').contentWindow.";

this.renderFrame = function()
{
 var str="<div id=\"" + this.name + "_colorpick\" style=\"position:absolute;z-index:120;\"></div>";
 str += "<div id=\"" + this.name + "_fbrows\" style=\"position:absolute;z-index:110;\"></div>";
 str += "<table width=\"" + this.width + "\" border=\"0\" ><tr>";
                    
 str += "<td colspan=\"2\"> <input name=\"s\" type=\"button\" class=\"SFED_button\" id=\"s\" style=\"font-weight: bolder;width=24px;\" onclick=\"" + this.browser_str + "document.execCommand('bold', false, null); " + this.name + ".grass=(!" + this.name + ".grass);if(" + this.name + ".grass){this.style.backgroundColor='#00ff66';this.style.color='#000033';}else{this.style.backgroundColor='" + this.button_bgcolor + "';this.style.color='" + this.button_forecolor + "';} document.getElementById('" + this.name + "_editor').contentWindow.focus();\" value=\"G\">"; 
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-style: italic;width=24px;\" value=\"I\"  onclick=\"" + this.browser_str + "document.execCommand('italic', false, null); " + this.name + ".italic=(!" + this.name + ".italic);if(" + this.name + ".italic){this.style.backgroundColor='#00ff66';this.style.color='#000033';}else{this.style.backgroundColor='" + this.button_bgcolor + "';this.style.color='" + this.button_forecolor + "';}document.getElementById('" + this.name + "_editor').contentWindow.focus();\">";  
 str += "<input type=\"button\" class=\"SFED_button\" style=\"text-decoration: underline;width=24px;\" value=\"S\"  onclick=\"" + this.browser_str + "document.execCommand('underline', false, null); " + this.name + ".sott=(!" + this.name + ".sott);if(" + this.name + ".sott){this.style.backgroundColor='#00ff66';this.style.color='#000033';}else{this.style.backgroundColor='" + this.button_bgcolor + "';this.style.color='" + this.button_forecolor + "';};document.getElementById('" + this.name + "_editor').contentWindow.focus();\">";  
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "left.gif);\" onclick=\"" + this.browser_str + "document.execCommand('justifyleft', false, null);document.getElementById('" + this.name + "_editor').contentWindow.focus();\">"; 
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "center.gif);\" onclick=\"" + this.browser_str + "document.execCommand('justifycenter', false, null);document.getElementById('" + this.name + "_editor').contentWindow.focus();\">"; 
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "right.gif);\" onclick=\"" + this.browser_str + "document.execCommand('justifyright', false, null);document.getElementById('" + this.name + "_editor').contentWindow.focus();\">"; 
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "ordered_list.gif);\" onclick=\"" + this.browser_str + "document.execCommand('insertorderedlist', false, null);document.getElementById('" + this.name + "_editor').contentWindow.focus();\">";
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "unordered_list.gif);\" onclick=\"" + this.browser_str + "document.execCommand('insertunorderedlist', false, null);document.getElementById('" + this.name + "_editor').contentWindow.focus();\">";
 var undo_args = "";
 if(navigator.appName != 'Microsoft Internet Explorer')
  undo_args = ", false, null";
 //str+="<button onclick=\"document.frames('" + this.name + "_editor').focus();document.frames('" + this.name + "_editor').document.execCommand('undo');\">UNDO</button>";
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "copy.gif);\" onclick=\"document.getElementById('" + this.name + "_editor').contentWindow.focus();" + this.browser_str + "document.execCommand('copy'" + undo_args + ");\">";
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "cut.gif);\" onclick=\"document.getElementById('" + this.name + "_editor').contentWindow.focus();" + this.browser_str + "document.execCommand('cut'" + undo_args + ");\">";
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "paste.gif);\" onclick=\"document.getElementById('" + this.name + "_editor').contentWindow.focus();" + this.browser_str + "document.execCommand('paste'" + undo_args + ");\">";
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "undo.gif);\" onclick=\"document.getElementById('" + this.name + "_editor').contentWindow.focus();" + this.browser_str + "document.execCommand('undo'" + undo_args + ");\">"; 
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "redo.gif);\" onclick=\"" + this.browser_str + "document.execCommand('redo'" + undo_args + ");document.getElementById('" + this.name + "_editor').contentWindow.focus();\">"; 
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "fore_color.gif);\" onClick=\"" + this.name + ".colorpicker.show();\"> ";
 str += "<select name=\"fontname\" id=\"fontname\" class=\"cbox2\"  onchange=\"document.getElementById('" + this.name + "_editor').contentWindow.focus();" + this.browser_str + "document.execCommand('fontname', false, this.value);\">";
 str += "<option value=\"Arial\">Arial</option>";
 str += "<option value=\"Courier\">Courier</option>";
 str += "<option value=\"Geneva\">Geneva</option>";
 str += "<option value=\"Times New Roman\" selected>Times New Roman</option>";
 str += "<option value=\"Verdana\">Verdana</option>";
 str += "</select> <select name=\"size1\" class=\"cbox2\" id=\"size1\" onchange=\"document.getElementById('" + this.name + "_editor').contentWindow.focus();" + this.browser_str + "document.execCommand('fontsize', false, this.value);\">";
 var s;
 for(s=0;s<=7;s++)
  str += "<option value=\"" + s.toString() + "\">" + s.toString() + "</option>";
 str += " </select> </td></tr><tr> ";
 str += "<td colspan=\"2\"> <input type=\"button\" class=\"SFED_button\"  style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "imgins.gif);\" onclick=\"if(" + this.name  + ".imgbrowser != null)" + this.name  + ".imgbrowser.show();\">"; 
 //<input name=\"Submit72\" type=\"button\" class=\"SFED_button\"  style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "imgdb.gif);\" onclick=\"document.getElementById('insimgdb').style.visibility='visible';\">";  
 //<input name=\"Submit73\" type=\"button\" class=\"SFED_button\"  style=\"font-weight: bolder; width=24px; background: url(" + this.img_path + "tabledb.gif);\" onclick=\"document.getElementById('inslink').style.visibility='visible';\"> 
 str += "<input name=\"" + this.name + "_linkfield\" type=\"text\" class=\"logfields\" id=\"" + this.name + "_linkfield\" value=\"http://\">";
 str += "<input type=\"button\" class=\"SFED_button\" style=\"font-weight: bolder; width=21px; background: url(" + this.img_path + "hyperlink.gif);\" onclick=\"" + this.browser_str + "document.execCommand('createlink', false, document.getElementById('" + this.name + "_linkfield').value);\">"; 
 str += "</td></tr><tr>"; 
 str += "<td colspan=\"2\"><div id=\"" + this.name + "_editor_container\">";
 str += "<iframe id=\"" + this.name + "_editor\" width=\"100%\" height=\"200\" onfocus=\"" + this.name + ".UpdatePreview(); SF_Ctrl('" + this.returnObject.id + "').value=" + this.name + ".GetHTML();\" onblur=\"" + this.name + ".UpdatePreview(); SF_Ctrl('" + this.returnObject.id + "').value=" + this.name + ".GetHTML();\"></iframe>";
 str += "</div></td></tr>"; 
 if(this.preview) {
   str += "<tr><td colspan=\"2\"><span>Anteprima:</span><br>";  
   str += "<table width=\"100%\" height=\"80\" border=0 bgcolor=white>"; 
   str += "<tr> "; 
   str += "<td valign=\"top\"><span id=\"" + this.name  + "_preview\"></span></td>"; 
   str += "</tr>"; 
   str += "</table></td></tr>"; 
 }                
                  
                    
                  
 str += "</table><br>";
 var upd_str = this.name + ".UpdatePreview();";
 if(this.returnObject)
  upd_str += "SF_Ctrl('" + this.returnObject.id + "').value=" + this.name + ".GetHTML();";
 var tm = setInterval(upd_str, 100);
 this.container.innerHTML=str;
 //this.initEditor();
 
 this.colorpicker = new SF_ColorPicker(this.name + '.colorpicker', document.getElementById(this.name + '_colorpick'), this.selColor_CB, this.selColor_CB);
 //this.colorpicker.clickFunction=this.selColor;
 //this.colorpicker.dblClickFunction=this.selColor;
 this.colorpicker.hide();
 this.colorpicker.resizeContainer();
 this.colorpicker.render();
 if(this.imgbrowser!=null)
 //this.filebrowser.addFilter('Tutte le immagini', 'jpg,jpeg,gif,bmp,png');
  this.imgbrowser.show;

 
}



this.initEditor = function ()
{

 if(navigator.appName == 'Microsoft Internet Explorer')
  this.editor=document.frames(this.name + '_editor').document;
 else 
  this.editor=document.getElementById(this.name + '_editor').contentWindow.document;
 
 this.editor.open();
 this.editor.close();
 
 this.editor.designMode='On';
}

this.AddImg = function(imgpath)
{

 document.getElementById(this.name + '_editor').contentWindow.focus();
 document.getElementById(this.name + '_editor').contentWindow.document.execCommand('insertimage', false, imgpath);

}

this.AddLink = function (linkpath, linktext)
{
 document.getElementById(this.name + '_editor').contentWindow.focus();
 var a=document.getElementById(this.name + '_editor').contentWindow.document.createElement('A'); a.href=linkpath; a.innerHTML=linktext; document.getElementById(this.name + '_editor').contentWindow.document.selection.createRange().pasteHTML(a.outerHTML);
}

this.GetHTML = function ()
{
 return document.getElementById(this.name + '_editor').contentWindow.document.body.innerHTML;
}

this.setHTML = function(htmlcode)
{
 // NON FUNZA BENE
 document.getElementById(this.name + '_editor').contentWindow.document.body.innerHTML=htmlcode;
}

this.UpdatePreview = function ()
{
 if(this.preview)
  SF_Ctrl(this.name + '_preview').innerHTML=this.GetHTML();
}

this.selColor = function (colorid)
{

 document.getElementById(this.name + '_editor').contentWindow.focus();
 document.getElementById(this.name + '_editor').contentWindow.document.execCommand('forecolor', false, colorid);

}

this.selColor_CB = function (colorid)
{
 
 var nm_arr = this.name.split('.');

 document.getElementById(nm_arr[0] + '_editor').contentWindow.focus();
 document.getElementById(nm_arr[0] + '_editor').contentWindow.document.execCommand('forecolor', false, colorid);
 eval(this.name + ".hide();");
 
}
/*
this.returnColor = function (colorid)
{document.getElementById(this.name + '_editor').contentWindow.focus();
 selColor(colorid);
 Document.getElementById('colorpick').style.visibility='hidden';
}*/

return this;
}


