var html = 'this is the copy';
var currentview=0;
var table_dialog, color_dialog, properties_dialog;
var pp;
var enabletoolbar=true;
var n=0;
var object_name = '';

function InitEditor() {
    var obj = document.getElementById(object_name);
    obj.contentEditable ="true";
    WriteDefaultPage();
    obj.focus();
    document.execCommand("fontname", false, 'Arial');
    document.execCommand("fontsize", false, 2);
    EditorView(0);
}

function EditorView(view){
    var obj = document.getElementById(object_name);
    if(view==0){
        EnableToolbar(true);
        obj.contentEditable = 'true';
        obj.innerHTML = html;
        obj.focus();
    }
    if(view==1){
        EnableToolbar(false);
        obj.innerHTML = html;
    }
    if(view==2){
        EnableToolbar(false);
        obj.innerHTML = html;
    }
    currentview=view;
}

function EnableToolbar(enable){
    var obj = document.getElementById(object_name);
    enabletoolbar=enable;
}

function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function saveHTML(sobj) {
    var obj = document.getElementById(object_name);
    html = obj.innerHTML;
    var obj2 = document.getElementById(sobj);
    obj2.value = html;
}

function WriteDefaultPage(){
    var obj = document.getElementById(object_name);
    obj.innerHTML = '';
}

function TableOn(table, on){
    var obj = document.getElementById(object_name);
    if(on) table.style.backgroundColor="#FFDF71";
    else table.style.backgroundColor="#FED23B";
}

function ToolbarOn(toolon){
    var obj = document.getElementById(object_name);
    var tool=event.srcElement;
    if (toolon){
        tool.className = 'cms_editor_button_over';
    } else {
        tool.className = 'cms_editor_button';
    }
}
    
function FindInPage(showdialog){
    var obj = document.getElementById(object_name);
    var display="visible";
    if (showdialog==false) 
        display="hidden";
    find_dialog.style.visibility=display;
    if (showdialog!=false)
        document.find.findwhat.focus();
}

function InsertHTML(newhtml){
    var obj = document.getElementById(object_name);
    obj.focus();
    var selpoint=document.selection.createRange();
    selpoint.pasteHTML(newhtml);
}

function GetSelectedText(){
    var obj = document.getElementById(object_name);
    obj.focus();
    var selpoint=document.selection.createRange();
    var seltext=selpoint.text;
    return seltext;
}

function InsertNewImage(){
    var obj = document.getElementById(object_name);
    if(enabletoolbar==false) return;
    obj.focus();
    document.execCommand("insertimage", true, null);
}

function InsertForm(){
    var obj = document.getElementById(object_name);
    var formmethod=window.prompt("Choose form method: GET | POST", "POST");
    var formaction=window.prompt("Choose form action:", "http://");
    InsertHTML("<div style=\"background-Color:#C0C0C0\"><form method=\"" + formmethod + "\" action=\"" + formaction + "\">\n<p>&nbsp;</p></form></div>");
}

function InsertFormControl(control){
    var obj = document.getElementById(object_name);
    obj.focus();
    //Inserts a form control
    document.execCommand(control, true, null);
}

function CreateNewLink(){
    var obj = document.getElementById(object_name);
//Inserts a link in the selected text
    if(enabletoolbar==false) return;
    var linktext=GetSelectedText();
    if(linktext=="") return;
    var url=window.prompt("Enter a URL for the new link:", "http://");
    if(url!=null) {
        InsertHTML("<a href=\"" + url + "\">" + linktext + "</a>");
    }
    obj.focus();
}

function InsertTable(){
    var obj = document.getElementById(object_name);
    table_dialog=window.open("table.htm", "newtable", "top=100,left=100,height=300,width=400,scrollbars=no");
}

function CreateTable(tr, tc, ta, tb, tp, ts, tw, tt){
    var obj = document.getElementById(object_name);
    var tablewidth="";
    if(tw!=""){
        tablewidth=" width=\"" + tw + tt + "\"";
    }
    var thtml="<table border=\"" + tb + "\" cellpadding=\"" + tp + "\" cellspacing=\"" + ts + "\"" + tablewidth + ">";
    tr=parseInt(tr);
    tc=parseInt(tc);
    for (r=0;r<tr;r++) {
        thtml+="<tr>";
        for (c=0;c<tc;c++) {
            thtml+="<td></td>";
        }
        thtml+="</tr>";
    }
    thtml+="</table>";
    InsertHTML(thtml);
    table_dialog.close();
}

function EditPage(){
    var obj = document.getElementById(object_name);
    properties_dialog=window.open("properties.htm", "editpage", "top=100,left=100,height=275,width=387,scrollbars=no");
}

function ChangeForeColor(){
    var obj = document.getElementById(object_name);
    if (GetSelectedText()!="") {
        pp = "EditForeColor";
        color_dialog = window.open("/plugins/color.html", "colorpicker", "top=100,left=100,height=215,width=480,scrollbars=no");
    }
}

function EditForeColor(thecolor){
    var obj = document.getElementById(object_name);
    obj.focus();
    document.execCommand("forecolor", false, thecolor);
    if (color_dialog)
        color_dialog.close();
}

function ChangeBackColor(){
    var obj = document.getElementById(object_name);
    if(GetSelectedText()!=""){
        pp="EditBackColor";
        color_dialog=window.open("/plugins/color.html?p=EditBackColor", "colorpicker", "top=100,left=100,height=260,width=460,scrollbars=no");
    }
}

function EditBackColor(thecolor){
    var obj = document.getElementById(object_name);
    obj.focus();
    document.execCommand("backcolor", false, thecolor);
    color_dialog.close();
}

function ChangeFont(font){
    var obj = document.getElementById(object_name);
    obj.focus();
    document.execCommand("fontname", false, font);
}

function ChangeFontSize(size){
    var obj = document.getElementById(object_name);
    obj.focus();
    document.execCommand("fontsize", false, size);
}

function DesignTools(tool){
    var obj = document.getElementById(object_name);
    if (enabletoolbar==false) {
        window.alert("You must switch into HTML Editor to do this");
        return;
    }
    obj.focus();
    document.execCommand(tool, true, null);
}

function formatBlockCustom(cmd1,cmd2) {
    var range = document.selection.createRange();
    range.pasteHTML (cmd1 + range.text + cmd2);
    //range.execCommand('FormatBlock', true, cmd);
    return;
}
function replace_all(str1, str2, str3) {
    while(str1.indexOf(str2) != -1) {
        str1 = str1.replace(str2, str3);
    }
    return str1;
}

function stripHTML(sobj) {
    if ( window.confirm(" You are about to strip all formatting from the content in the editor.\n Please note that the process is permanent.\n Do you want to continue.") ) {
        var x = window.frames[sobj].document.body.innerHTML;
        x = x.replace(/<p .*?>/gi, "<p>");
        x = x.replace(/<b .*?>/gi, "<b>");
        x = x.replace(/<i .*?>/gi, "<i>");
        x = x.replace(/<strong.*?>/gi, "<strong>");
        
        x = x.replace(/<p>/gi, "(ZXCVB)");
        x = x.replace(/<b>/gi, "(BVCXZ)");
        x = x.replace(/<i>/gi, "(ASDFG)");
        x = x.replace(/<strong>/gi, "(GFDSA)");
        x = x.replace(/<br>/gi, "(TREWQ)");
        
        x = x.replace(/<\/p>/gi, "(A0ZXCVB)");
        x = x.replace(/<\/b>/gi, "(A0BVCXZ)");
        x = x.replace(/<\/i>/gi, "(A0ASDFG)");
        x = x.replace(/<\/strong>/gi, "(A0GFDSA)");
        
        x = x.replace(/<(.*?)>/gi, "");
        
        x = replace_all(x, "(ZXCVB)", "<p>");
        x = replace_all(x, "(BVCXZ)", "<b>");
        x = replace_all(x, "(ASDFG)", "<i>");
        x = replace_all(x, "(GFDSA)", "<strong>");
        x = replace_all(x, "(TREWQ)", "<br>");
        
        x = replace_all(x, "(A0ZXCVB)", "</p>");
        x = replace_all(x, "(A0BVCXZ)", "</b>");
        x = replace_all(x, "(A0ASDFG)", "</i>");
        x = replace_all(x, "(A0GFDSA)", "</strong>");
        window.frames[sobj].document.body.innerHTML = x;
    }   
}

