function Editor(edit_name, text_name) {
    this.editor = edit_name;
    this.store = text_name;
    this.viewHTML = false;

    this.load_html = Editor_load_html;
    this.save_html = Editor_save_html;
    this.switch_view = Editor_switch_view;
    this.exec_command = Editor_exec_command;
    this.bold = Editor_bold;
    this.italic = Editor_italic;
    this.underline = Editor_underline;
    this.ordered_list = Editor_ordered_list;
    this.unordered_list = Editor_unordered_list;
    this.justify_left = Editor_justify_left;
    this.justify_center = Editor_justify_center;
    this.justify_right = Editor_justify_right;
    this.font_size = Editor_font_size;
}

function Editor_load_html() {
    this.editor.document.open();
    if (this.store.value == '')
        this.editor.document.write('<!-- -->');
    else
        this.editor.document.write(this.store.value);
    this.editor.document.close();
    this.editor.document.designMode = 'On';
}

function Editor_save_html() {
    if (this.editor.document.body.getAttribute('viewAsText') == 'No')
        this.store.value = this.editor.document.body.innerText;
    else
        this.store.value = this.editor.document.body.innerHTML;
}

function Editor_switch_view(options) {
    if(options.selectedIndex == 0) {
        this.editor.document.body.innerHTML = this.editor.document.body.innerText;
        this.editor.document.body.setAttribute('viewAsText', 'Yes');
        this.viewHTML = false;
    } else {
        this.editor.document.body.innerText = this.editor.document.body.innerHTML;
        this.editor.document.body.setAttribute('viewAsText', 'No');
        this.viewHTML = true;
    }
    this.editor.focus();
}

function Editor_exec_command(command, parameter) {
    if (this.viewHTML)
        alert('Leave HTML mode to permform this action');
    else {
        if (parameter)
            this.editor.document.execCommand(command, "false", parameter);
        else
            this.editor.document.execCommand(command);
        this.editor.focus();
    }
}

function Editor_bold() {
    this.exec_command('bold');
}

function Editor_italic() {
    this.exec_command('italic');
}

function Editor_underline() {
    this.exec_command('underline');
}

function Editor_ordered_list() {
    this.exec_command('insertOrderedList');
}

function Editor_unordered_list() {
    this.exec_command('insertUnorderedList');
}

function Editor_justify_left() {
    this.exec_command('justifyLeft');
}

function Editor_justify_right() {
    this.exec_command('justifyRight');
}

function Editor_justify_center() {
    this.exec_command('justifyCenter');
}

function Editor_font_size(size) {
    this.exec_command('fontSize', size);
}
