JS_HELPOFF = false;
/* indentify the browser */
DOM = (document.getElementById) ? 1 : 0;
NS4 = (document.layers) ? 1 : 0;
IE4 = (document.all) ? 1 : 0;
OPERA = navigator.userAgent.indexOf("Opera") > -1 ? 1 : 0;
MAC = navigator.userAgent.indexOf("Mac") > -1 ? 1 : 0;
var OBJ_TMP;

/* edit box stuff */
function insertTag(obj, stag, etag) {
    if (navigator.userAgent.indexOf("MSIE") > -1 && !OPERA) {
            insertTagIE(obj, stag, etag);
    } else if (window.getSelection && navigator.userAgent.indexOf("Safari") == -1) {
        insertTagMoz(obj, stag, etag);
    } else {
        insertTagNS(obj, stag, etag);
    }
    obj.focus(); 
}

function insertTagNS(obj, stag, etag) {
    obj.value = obj.value+stag+etag;
}

function insertTagMoz(obj, stag, etag) {
    txt = window.getSelection();

    if (!txt || txt == '') {
        //t = document.getElementById('txtb');
        t = obj;
        h = document.getElementsByTagName('textarea')[0];
        if (t.selectionStart == t.selectionEnd) {
            t.value = t.value.substring(0, t.selectionStart) + stag + etag +  t.value.substring(t.selectionEnd, t.value.length);
            return;
        }
        txt = t.value.substring(t.selectionStart, t.selectionEnd);
        if (txt) {
            t.value = t.value.substring(0, t.selectionStart) + stag + txt + etag +  t.value.substring(t.selectionEnd, t.value.length);
            return;
        }
    }
    obj.value = obj.value+stag+etag;
}

function insertTagIE(obj, stag, etag, mode) {
    r=document.selection.createRange();
    if( document.selection.type == 'Text' && (obj.value.indexOf(r.text) != -1) ) {
        a = r.text;
        if (mode == 1) {
            r.text = stag+etag;
        } else {
            r.text = stag+r.text+etag;
    }
        if ( obj.value.indexOf(document.selection.createRange().text) == -1 ) {
            document.selection.createRange().text = a;
        }
    }
    else insertAtCaret(obj, stag+etag); 
}

function dialogTag(obj, qst, def, stag, etag, mode) {
    var q;
    q = prompt(qst, def);
    if ( !q ) return;
    stag = stag.replace(/%s/i, q);
    insertTag(obj, stag, etag, mode);
}

function url_insert(obj) {
    var h;
    if (h = check_selection(obj) )
        dialogTag(obj, 'Location:', 'http://'+h, '[url=%s]', '[/url]');
    else
        dialogTag(obj, 'Location:', 'http://', '[url]%s[/url]', '');
}

function insert_custom(obj,t) {
    var h;
    var show = t.substring(0,1).toUpperCase() + t.substring(1,t.length).toLowerCase();
    if (h = check_selection(obj) )
        dialogTag(obj, show+':', h, '['+t+' %s]', '', 1);
    else
        dialogTag(obj, show+':', '', '['+t+' %s]', '', 1);
}


function check_selection(obj) {
    var txt = '';
    if (typeof(obj.selectionStart) != 'undefined') {
        txt = obj.value.substring(obj.selectionStart,obj.selectionEnd);
    } else if (document.selection) {
        txt = document.selection.createRange().text;
    } else return;
    return txt;
}

function storeCaret(textEl) {
     if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

function insertAtCaret(textEl, text) {
    if (textEl.createTextRange && textEl.caretPos) {
        var caretPos = textEl.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
    }
    else 
        textEl.value  =  textEl.value + text;
}

function insertParentTagIE(obj,stag, etag) {
    r=window.opener.document.selection.createRange();
    obj = window.opener.obj;
    
    if( window.opener.document.selection.type == 'Text' && (obj.value.indexOf(r.text) != -1) ) {
        a = r.text;
        r.text = stag+r.text+etag;
        if ( obj.value.indexOf(window.opener.document.selection.createRange().text) == -1 ) {
            window.opener.document.selection.createRange().text = a;
        }
    }
    else insertAtCaret(obj, stag+etag);
}

function insertParentTagNS(obj, stag, etag) {
    window.opener.obj.value = window.opener.document.post_form.msg_body.value + stag + etag;
}

function insertParentTag(obj, stag, etag) {
    if ( document.all ) 
        insertParentTagIE(obj, stag, etag);
    else
        insertParentTagNS(obj, stag, etag);
    
}

function show_preview(obj) {
    var t = obj.value;
    
    var p = window.open('about:blank','preview','width=400,height=400,statusbar=no,locationbar=no,toolbar=no,scrollbars=yes');
    p.document.open();
    p.document.write('<html><head><title>Preview</title></head><body><form method=post action="'+preview_url+'" name=preview>');
    p.document.write('<textarea style="position:relative;visibility:hidden" name=render_me></textarea>');
    p.document.write('</form></body></html>');
    p.document.forms['preview'].elements['render_me'].value = t;
    p.document.forms['preview'].submit();
    
    //document.forms['preview'].submit();
}

function show_tag_help() {
    window.open("/p/page.cgi?p=blog_help", "taghelp", "height=420,width=500,scrollbars=1" );
}

function popup_choices(obj,t,lang) {
    if (advanced_options[t]) {
        OBJ_TMP = obj;
        if (popupWin) {
            if (!popupWin.closed) {
                 popupWin.close();
            }
        }
        delete popupWin;
        popupWin = window.open('/p/page.cgi?p=wysiwyg_editor_options&lang='+lang,'add_stuff','width=500,height=230,statusbar=no,scrollbars=no,locationbar=no,toolbar=no');
        //p.parent = window.top;
        popupWin.opener = window.top;
        popupWin.loadMe = advanced_options[t];
    }
}

function insert_from_popup(m,tid,opt_txt) {
    var str = (opt_txt != '') ? '['+m+' '+tid+' '+opt_txt+']' : '['+m+' '+tid+']';
    if (m == 'photo') {
        str = (opt_txt != '') ? '['+m+opt_txt+' '+tid+']' : '['+m+' '+tid+']';
    }
    insertTag(OBJ_TMP,str,'');
}
