var MENU_SEPARATOR     = "";
var ContextMenu        = new Array();
var GeneralContextMenu = new Array();

function ContextMenuItem(string, cmdId) {
  this.string = string;
  this.cmdId = cmdId;
}

function window_onload() {
  GeneralContextMenu[0] = new ContextMenuItem("Cut", DECMD_CUT);
  GeneralContextMenu[1] = new ContextMenuItem("Copy", DECMD_COPY);
  GeneralContextMenu[2] = new ContextMenuItem("Paste", DECMD_PASTE);
}

function edit_ShowContextMenu() {
  var menuStrings = new Array();
  var menuStates = new Array();
  var state;
  var idx = 0;
  var i   = 0;

  ContextMenu.length = 0;

  for (i=0; i<GeneralContextMenu.length; i++) {
    ContextMenu[idx++] = GeneralContextMenu[i];
  }

  for (i=0; i<ContextMenu.length; i++) {
    menuStrings[i] = ContextMenu[i].string;
    if (menuStrings[i] != MENU_SEPARATOR) {
      state = document.poster.edit.QueryStatus(ContextMenu[i].cmdId);
    } else {
      state = DECMDF_ENABLED;
    }
    if (state == DECMDF_DISABLED || state == DECMDF_NOTSUPPORTED) {
      menuStates[i] = OLE_TRISTATE_GRAY;
    } else if (state == DECMDF_ENABLED || state == DECMDF_NINCHED) {
      menuStates[i] = OLE_TRISTATE_UNCHECKED;
    } else { // DECMDF_LATCHED
      menuStates[i] = OLE_TRISTATE_CHECKED;
    }
  }

  document.poster.edit.SetContextMenu(menuStrings, menuStates);
}

function edit_ContextMenuAction(itemIndex) {
  document.poster.edit.ExecCommand(ContextMenu[itemIndex].cmdId, OLECMDEXECOPT_DODEFAULT);
}

function insert_reference() {
    if (document.poster.reference.selectedIndex > -1)
        insert_text('['+(document.poster.reference.selectedIndex+1)+']');
}

function insertSChar(schr) {
    insert_text(schr);
}

function insert_text(txt) {
    var doc;
    var sel;
    var tr;

    doc = document.edit.DOM;
    sel = doc.selection;
    tr  = sel.createRange();
    tr.pasteHTML(txt);
    doc = null;
    sel = null;
    tr  = null;
}

function revolveTop(rad, OBJ) {
    if (rad == 1) {
        OBJ.style.border                = "thin outset";
    }
    else {
        OBJ.style.border                = "thin solid";
        OBJ.style.borderLeftColor        = "buttonface";
        OBJ.style.borderRightColor        = "buttonface";
        OBJ.style.borderTopColor        = "buttonface";
        OBJ.style.borderBottomColor        = "buttonface";
        OBJ.style.backgroundColor        = "buttonface";
    }
}

function poster_view(id, nocache,uid) {
    document.poster._popup.value = 1;
    var options = "width=750,height=650,";
    options += "resizable=no,scrollbars=yes,status=yes,";
    options += "menubar=no,toolbar=no,location=no,directories=no";
    var newWin = window.open('poster.preview.php?id='+id+'&uid='+uid+'&nocache='+nocache, 'posterWin', options);
    document.poster._popup.value = 0;
    newWin.focus();
}

function send_message(uid, nocache) {
    var options = "width=500,height=400,";
    options += "resizable=no,scrollbars=yes,status=yes,";
    options += "menubar=no,toolbar=no,location=no,directories=no";
    var newWin = window.open('send.message.php?uid='+uid+'&nocache='+nocache, 'messageWin', options);
    newWin.focus();
}


function poster_del(id, nocache,uid) {
    if (confirm(poster_confirm)) {
        document.location = 'user.home.php?del='+id+'&uid='+uid+'&nocache='+nocache;
    }
}

function add_author() {
    document.poster.cmd.value = 'add_author';
    document.poster.submit();
}

function del_author() {
    document.poster.cmd.value = 'del_author';
    document.poster.submit();
}

function copy_institution(i) {
    if (i > 0) {
        iprev = eval('document.poster.institution_'+(i-1));
        iself = eval('document.poster.institution_'+i);
        iself.value = iprev.value;
    }
}

function add_ref() {
    document.poster.cmd.value = 'add_ref';
    document.poster.submit();
}

function del_ref() {
    document.poster.cmd.value = 'del_ref';
    document.poster.submit();
}

function file_change() {
    switch (document.poster.type.selectedIndex) {
        case 1:
            document.all['count'].style.position   = 'relative';
            document.all['count'].style.visibility = 'visible';
            break;

        case 2:
            document.all['count'].style.position   = 'relative';
            document.all['count'].style.visibility = 'visible';
            break;

        case 3:
            document.all['count'].style.position   = 'relative';
            document.all['count'].style.visibility = 'visible';
            break;

        case 4:
            document.all['count'].style.position   = 'relative';
            document.all['count'].style.visibility = 'visible';
            break;

        default:
            document.all['count'].style.position   = 'absolute';
            document.all['count'].style.visibility = 'hidden';
            break;
    }
}

function file_upload() {
    document.poster.cmd.value = 'upload';
    document.poster.submit();
}

function file_del(id) {
    if (confirm(file_confirm)) {
        document.poster.cmd.value = "delete";
        document.poster.del.value = id;
        document.poster.submit();
    }
}

function new_table() {
    document.poster.cmd.value = "new";
    document.poster.submit();
}

function save_table() {
    document.poster.cmd.value = "save";
    document.poster.submit();
}

function table_del(id) {
    if (confirm(table_confirm)) {
        document.poster.cmd.value = "delete";
        document.poster.del.value = id;
        document.poster.submit();
    }
}

function table_view(id, nocache) {
    document.poster._popup.value = 1;
    var url = 'table.preview.php?id='+id+'&nocache='+nocache;
    var options = "width=500,height=350,";
    options += "resizable=no,scrollbars=yes,status=yes,";
    options += "menubar=no,toolbar=no,location=no,directories=no";
    var newWin = window.open(url, 'tableWin', options);
    document.poster._popup.value = 0;
    newWin.focus();
}

function do_bold() {
    document.edit.ExecCommand(DECMD_BOLD, OLECMDEXECOPT_DODEFAULT);
}

function do_copy() {
    document.edit.ExecCommand(DECMD_COPY, OLECMDEXECOPT_DODEFAULT);
}

function do_cut() {
    document.edit.ExecCommand(DECMD_CUT, OLECMDEXECOPT_DODEFAULT);
}

function do_paste() {
    document.edit.ExecCommand(DECMD_PASTE,OLECMDEXECOPT_DODEFAULT);
}

function do_italic() {
    document.edit.ExecCommand(DECMD_ITALIC, OLECMDEXECOPT_DODEFAULT);
}

function do_underline() {
    document.edit.ExecCommand(DECMD_UNDERLINE, OLECMDEXECOPT_DODEFAULT);
}

function do_sup() {
    var sel = document.edit.DOM.selection.createRange();
    sel.pasteHTML("<sup>" + sel.htmlText + "</sup>");
}

function do_sub() {
    var sel = document.edit.DOM.selection.createRange();
    sel.pasteHTML("<sub>" + sel.htmlText + "</sub>");
}

function do_ordered_list() {
}

function do_unordered_list() {
}

function strip_tags() {
    var str = document.edit.DocumentHTML;
    reg = new RegExp('</?a[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?font[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?span[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?div[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?pre[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('<h[0-9][^>]*>', 'gi');
    str = str.replace(reg, '<b>');
    reg = new RegExp('</h[0-9][^>]*>', 'gi');
    str = str.replace(reg, '</b>');
    reg = new RegExp('</?code[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?span[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?table[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?tbody[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?th[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?tr[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?td[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?body[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?head[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?meta[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?param[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?html[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?title[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('</?[a-z]\:[^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('<\![^>]*>', 'gi');
    str = str.replace(reg, '');
    reg = new RegExp('<li [^>]*>', 'gi');
    str = str.replace(reg, '<li>');
    reg = new RegExp('<ul [^>]*>', 'gi');
    str = str.replace(reg, '<ul>');
    reg = new RegExp('<ol [^>]*>', 'gi');
    str = str.replace(reg, '<ol>');
    reg = new RegExp('<p [^>]*>', 'gi');
    str = str.replace(reg, '<p>');
    reg = new RegExp('<strong[^>]*>', 'gi');
    str = str.replace(reg, '<b>');
    reg = new RegExp('</strong[^>]*>', 'gi');
    str = str.replace(reg, '</b>');
    reg = new RegExp('<em[^>]*>', 'gi');
    str = str.replace(reg, '<i>');
    reg = new RegExp('</em[^>]*>', 'gi');
    str = str.replace(reg, '</i>');
    reg = new RegExp('&nbsp;', 'gi');
    str = str.replace(reg, ' ');
    reg = new RegExp('>\s+<', 'gi');
    str = str.replace(reg, '><');
    return str;
}


function strip_tagss(str) {
  //  var str = document.edit.DocumentHTML;
    reg = new RegExp('\n', 'gi');
    str = str.replace(reg, '');

    reg = new RegExp('\t', 'gi');
    str = str.replace(reg, '');

    reg = new RegExp('\r', 'gi');
    str = str.replace(reg, '');


    return str;
}

function form_submit_content() {
    var str = strip_tags();
    document.poster.content.value = str;
    document.poster.submit();
}
function form_save_content() {
    var str = strip_tags();
    document.poster.content.value = str;
	document.poster.subcmd.value = 'save';
    document.poster.submit();
}

function form_submit_title() {
    var str = strip_tags();
    document.poster.title.value = str;
    document.poster.submit();
}
