function if_opera()
    {
    if (is_opera)
        {
        alerter('opera_not_supported');
        return;
        }
    }


function resize_textarea(C, B)
    {
    var A = fetch_object(B);

    if (parseInt(A.offsetHeight) < 100 && C < 0)
        {
        return false;
        }
    A.style.height = parseInt(A.offsetHeight) + (C < 0 ? -100 : 100) + "px";
    return false;
    }

function chempty(str, c)
    {
    if (str == "" && c == 0)
        {
        alerter('');
        return false;
        }
    else
        {
        return true;
        }
    }

function bbcodeclean(bbcodetext)
    {
    bbcodetext = bbcodetext.replace(/<br \/>\s*?|&nbsp;<br \/>/g, '');
    bbcodetext = bbcodetext.replace(/<br>/gi, "\n");
    bbcodetext = bbcodetext.replace(/<br \/>/gi, "\n");
    bbcodetext = bbcodetext.replace(/<p>&nbsp;<\/p>/g, '');
    return bbcodetext;
    }

function wysiwygclean(wysiwyg)
    {
    wysiwyg = wysiwyg.replace(/\r\n|\r|\n/g, '<br \/>');
    wysiwyg = wysiwyg.replace(/\n+/g, '<br \/>');
    wysiwyg = wysiwyg.replace(/<br \/><br \/>/g, '<br \/>');
    wysiwyg = wysiwyg.replace(/<br \/>\s*?<br \/>/g, '<br \/>');
    return wysiwyg;
    }

function showDialog(dUrl, vars, dh, dw, dt, dl, s, scr, rsiz)
    {
    var dFeatures =
        'dialogHeight: ' + dh + 'px; dialogWidth: ' + dw + 'px; dialogTop: ' + dt + 'px; dialogLeft: ' + dl + 'px; status: ' + s + '; scroll: ' + scr
        + ';resizable: ' + rsiz + ';edge: sunken;unadorned: yes;',ret;
    ret = window.showModalDialog(dUrl, vars, dFeatures);
    if (ret)
        {
        return ret;
        }
    }

function in_array(needle, haystack, argStrict)
    {
    var key = '';

    for (key in haystack)
        {
        if (haystack[key] === needle)
            {
            return true;
            }
        }
    return false;
    }

function cltype(ltype)
    {
    if (ltype == "")
        {
        ltype = "d";
        }
    else
        {
        if (!in_array(ltype, ['1', 's', 'a', 'A', 'i', 'I', 'c', 'd']))
            {
            ltype = "d";
            }
        }
    return ltype;
    }

var stored;

function Editor_store(str)
    {
    stored = str;
    }

function Editor_getSelText()
    {
    var field = fetch_object('bbtextarea');

    if (document.selection)
        {
        field.focus();
        var selected = document.selection.createRange().text;
        var sel = document.selection.createRange();
        }
    else
        {
        if (field.selectionStart || field.selectionStart == 0)
            {
            var a = field.selectionStart;
            var z = field.selectionEnd;
            var selected = field.value.substring(a, z);
            sel = '';
            field.focus();
            }
        }
    return {
    selected: selected,
    sel: sel,
    a: a,
    z: z,
    field: field
    };
    }

function Dimofinf_Editor_add(first, second, cond)
    {
    var Dim_textrange = Editor_getSelText();

    if (chempty(Dim_textrange.selected, cond) == false)
        {
        return;
        }

   if (is_ie || is_saf && !is_chrome)
        {
        if (second)
            {
            Dim_textrange.sel.text = first + Dim_textrange.selected + second;
            }
        else
            {
            Dim_textrange.sel.text = first;
            }
        countclik();
        }
    else
        {
        if (second)
            {
            Dim_textrange.field.value = Dim_textrange.field.value.substring(0, Dim_textrange.a) + first + Dim_textrange.selected + second
                + Dim_textrange.field.value.substring(Dim_textrange.z, Dim_textrange.field.value.length);
            }
        else
            {
            Dim_textrange.field.value = Dim_textrange.field.value.substring(0, Dim_textrange.a) + first
                + Dim_textrange.field.value.substring(Dim_textrange.z, Dim_textrange.field.value.length);
            }
        countclik();
        }
    }

function Dimofinf_Editor(Dim_bbtag)
    {
    var Dim_textrange = Editor_getSelText();

    if (Dim_bbtag.indexOf("face") == 0)
        {
        Dimofinf_Editor_add("[FONT=" + Dim_bbtag.substr(5) + "]", "[/FONT]", 0);
        }
    else if (Dim_bbtag.indexOf("size") == 0)
        {
        Dimofinf_Editor_add("[SIZE=" + Dim_bbtag.substr(5) + "]", "[/SIZE]", 0);
        }
    else if (Dim_bbtag == "color" || Dim_bbtag == "highlight")
        {
        if_opera();
        var colr = showDialog(script_url + "/editor/color.php", "", '230', '234', '10', '240', 'no', 'no', 'no');

        if (colr != "" && colr != "undefined")
            {
            Dimofinf_Editor_add("[" + Dim_bbtag.toUpperCase() + "=" + colr + "]", "[/" + Dim_bbtag.toUpperCase() + "]", 0);
            }
        }
    else if (Dim_bbtag == "left" || Dim_bbtag == "center" || Dim_bbtag == "right" || Dim_bbtag == "justify" || Dim_bbtag == "indent")
        {
        Dimofinf_Editor_add("[" + Dim_bbtag.toUpperCase() + "]", "[/" + Dim_bbtag.toUpperCase() + "]", 0);
        }
    else if (Dim_bbtag == "rtl" || Dim_bbtag == "ltr")
        {
        fetch_object('bbtextarea').dir = Dim_bbtag;
        }
    else if (Dim_bbtag == "bold")
        {
        Dimofinf_Editor_add("[B]", "[/B]", 0);
        }
    else if (Dim_bbtag == "italic")
        {
        Dimofinf_Editor_add("[I]", "[/I]", 0);
        }
    else if (Dim_bbtag == "underline")
        {
        Dimofinf_Editor_add("[U]", "[/U]", 0);
        }
    else if (Dim_bbtag == "code")
        {
        Dimofinf_Editor_add("[CODE]", "[/CODE]", 0);
        }
    else if (Dim_bbtag == "strike")
        {
        Dimofinf_Editor_add("[STRIKE]", "[/STRIKE]", 0);
        }
    else if (Dim_bbtag == "cut")
        {
        chempty(Dim_textrange.selected, 0);
        Editor_store(Dim_textrange.selected);
        var code = '';
        Dimofinf_Editor_add(code, "", 1);
        }
    else if (Dim_bbtag == "paste")
        {
        Dimofinf_Editor_add(stored, "", 1);
        }
    else if (Dim_bbtag == "copy")
        {
        Editor_store(Dim_textrange.selected);
        }
    else if (Dim_bbtag == "selectall")
        {
        Dim_textrange.field.focus();
        Dim_textrange.field.select();
        }
    else if (Dim_bbtag == "numlist" || Dim_bbtag == "squarelist" || Dim_bbtag == "circlelist" || Dim_bbtag == "romanlist" || Dim_bbtag == "alfalist")
        {
        switch (Dim_bbtag)
            {
            case "numlist":
                ltype = 1;
                break;

            case "squarelist":
                ltype = "s";
                break;

            case "circlelist":
                ltype = prompter('enter_clist_type', '');
                break;

            case "romanlist":
                ltype = prompter('enter_rlist_type', '');
                break;

            case "alfalist":
                ltype = prompter('enter_alist_type', '');
                break;
            }
        ltype = cltype(ltype);
        liststart = '[list="' + ltype + '"]\n';
        listend = "[/list]\n";
        listentry = "initial";

        while ((listentry != "") && (listentry != null))
            {
            listentry = prompter('enter_list_item', '');

            if ((listentry != "") && (listentry != null))
                liststart = liststart + "[*]" + listentry + "\n";
            }

        if ((liststart + listend) != '[list="' + ltype + '"]\n[/list]\n')
            {
            Dimofinf_Editor_add(liststart + listend, "", 1);
            }
        }
    else if (Dim_bbtag == "poem")
        {
        if_opera();
        code = showDialog(script_url + "/editor/poem.php", '', '510', '694', '10', '240', 'no', 'no', 'yes');

        if (code)
            {
            Dimofinf_Editor_add('[poem="' + code + '"]\n', "[/poem]", 0);
            }
        }
    else if (Dim_bbtag == "smilie")
        {
        code = showDialog(script_url + "/editor/smilie.php", Dim_textrange.field, '250', '120', '10', '240', 'no', 'no', 'yes');
        }
    else if (Dim_bbtag == "up")
        {
        code = showDialog(script_url + "/editor/upld.php", Dim_textrange.field, '260', '560', '10', '240', 'no', 'no', 'no');

        if (!code)
            {
            return;
            }
        Dimofinf_Editor_add(code, "", 1);
        }
    else if (Dim_bbtag == "frame")
        {
        if_opera();
        var code = showDialog(script_url + "/editor/frame.php", '', '400', '470', '10', '240', 'no', 'no', 'no');

        if (!code)
            {
            return;
            }
        Dimofinf_Editor_add('[frame="' + code + '"]\n', "[/frame]", 0);
        }
    else if (Dim_bbtag == "plain")
        {
        var code = Dim_textrange.selected.replace(/\[[^\]]*\]/gi, "");
        Dimofinf_Editor_add(code, "", 0);
        }
    else if (Dim_bbtag == "php")
        {
        Dimofinf_Editor_add("[PHP]\n", "[/PHP]", 0);
        }
    else if (Dim_bbtag == "html")
        {
        Dimofinf_Editor_add("[HTML]\n", "\n[/HTML]", 0);
        }
    else if (Dim_bbtag == "preview")
        {
        showDialog(script_url + "/editor/preview.php", Dim_textrange.field, 'auto', 'auto', '1', '0', 'no', 'yes', 'yes');
        }
    else if (Dim_bbtag == "addtextcanned")
        {
        if (Dim_textrange.selected == "")
            {
            alerter('empty_code_selection');
            return false;
            }
        showDialog(script_url + "/editor/canned.php?type=text", Dim_textrange.field, '200', '300', '1', '0', 'no', 'yes', 'yes');
        }
    else if (Dim_bbtag == "addmboxcanned")
        {
        if (Dim_textrange.selected == "")
            {
            alerter('empty_code_selection');
            return false;
            }
        showDialog(script_url + "/editor/canned.php?type=mbox", Dim_textrange.field, '200', '300', '1', '0', 'yes', 'yes', 'yes');
        }
    else if (Dim_bbtag == "getmboxcanned")
        {
        if (Dim_textrange.selected == "")
            {
            alerter('');
            return false;
            }
        code = showDialog(script_url + "/editor/canneds.php?do=set&type=mbox", Dim_textrange.field, '100', '350', '1', '0', 'no', 'yes', 'yes');

        if (code)
            {
            var code = code.replace("dimofinfhere", "\n"+Dim_textrange.selected+"\n");
            Dimofinf_Editor_add(code, "", 0);
            }
        }
    else if (Dim_bbtag == "gettextcanned")
        {
        code = showDialog(script_url + "/editor/canneds.php?do=set&type=text", Dim_textrange.field, '100', '350', '1', '0', 'no', 'yes', 'yes');

        if (code)
            {
            Dimofinf_Editor_add(code, "", 1);
            }
        }
    else if (Dim_bbtag == "link")
        {
        var lnk = prompter('enter_link_url', '');

        if (lnk)
            {
            if (Dim_textrange.selected == "")
                {
                Dimofinf_Editor_add("[URL]" + lnk + "[/URL]", "", 1);
                }
            else
                {
                Dimofinf_Editor_add("[URL=" + lnk + "]\n", "\n[/URL]", 0);
                }
            }
        }
    else if (Dim_bbtag == "email")
        {
        var lnk = prompter('enter_email_link', '');

        if (lnk)
            {
            if (Dim_textrange.selected == "")
                {
                Dimofinf_Editor_add("[EMAIL]" + lnk + "[/EMAIL]", "", 1);
                }
            else
                {
                Dimofinf_Editor_add("[EMAIL=" + lnk + "]\n", "\n[/EMAIL]", 0);
                }
            }
        }
    else if (Dim_bbtag == "image")
        {
        var lnk = prompter('enter_image_url', '');

        if (lnk)
            {
            Dimofinf_Editor_add("[IMG]" + lnk + "[/IMG]", "", 1);
            }
        }
    else if (Dim_bbtag == "multimedia")
        {
        if (Dim_textrange.selected == "")
            {
            alerter('');
            return false;
            }
        var lnk = prompter('enter_media_url', '');

        if (lnk)
            {
            Dimofinf_Editor_add("[MULTIMEDIA=\"" + lnk + "\"]", "[/MULTIMEDIA]", 0);
            }
        }
    else if (Dim_bbtag == "sitecode")
        {
        if (Dim_textrange.selected == "")
            {
            alerter('');
            return false;
            }
        var site = showDialog(script_url + "/editor/media_content.php?action=sitecode", '', '103', '400', '10', '240', 'no', 'no', 'yes');

        if (site)
            {
            var code = prompter('enter_media_code', '');
            }

        if (site && code)
            {
            Dimofinf_Editor_add("[SITECODE=\"" + site + " " + code + "\"]", "[/SITECODE]", 0);
            }
        }
    else if (Dim_bbtag == "comment")
        {
        if (Dim_textrange.selected == "")
            {
            alerter('');
            return false;
            }
        var lnk = prompter('enter_comment_id', '');

        if (lnk && !isNaN(lnk * 1))
            {
            Dimofinf_Editor_add("[COMMENT=\"" + lnk + "\"]", "[/COMMENT]", 0);
            }
        }
    else if (Dim_bbtag == "content")
        {
        if (Dim_textrange.selected == "")
            {
            alerter('');
            return false;
            }
        var mdl = showDialog(script_url + "/editor/media_content.php?action=contentmodule", '', '103', '420', '10', '240', 'no', 'no', 'yes');
        var lnk = prompter('enter_content_id', '');

        if (mdl && lnk && !isNaN(lnk * 1))
            {
            Dimofinf_Editor_add("[CONTENT=\"" + mdl + " " + lnk + "\"]", "[/CONTENT]", 0);
            }
        }
    else if (Dim_bbtag == "contentlink")
        {
        if (Dim_textrange.selected == "")
            {
            alerter('');
            return false;
            }
        var mdl = showDialog(script_url + "/editor/media_content.php?action=contentlinkmodule", '', '103', '420', '10', '240', 'no', 'no', 'yes');
        var lnk = prompter('enter_content_id', '');

        if (mdl && lnk && !isNaN(lnk * 1))
            {
            Dimofinf_Editor_add("[CONTENTLINK=\"" + mdl + " " + lnk + "\"]", "[/CONTENTLINK]", 0);
            }
        }
        else{
        	if(Dim_bbtag.indexOf("|") != -1){
        var option = prompter('enter_content_option', '');
        		if(option)
        		{
        		Dim_bbtag = Dim_bbtag.replace("|","");
        		Dimofinf_Editor_add("["+Dim_bbtag+"=\"" + option + "\"]", "[/"+Dim_bbtag+"]", 0);
	        	}
        	}else{
        	Dimofinf_Editor_add("["+Dim_bbtag+"]\n", "[/"+Dim_bbtag+"]", 0);
        }
         }
    } // function Dimofinf_Editor

function Set_button_over(eButton)
    {
    eButton.className = 'bbcodOver';
    eButton.style.cursor = pointer_cursor;
    }

function Set_button_out(eButton)
    {
    eButton.className = 'bbcodButton';
    eButton.style.cursor = pointer_cursor;
    }

function Set_button_down(eButton)
    {
    eButton.className = 'bbcodDown';
    eButton.style.cursor = pointer_cursor;
    }

function Set_button_up(eButton)
    {
    eButton.className = 'bbcodBorder';
    eButton = null;
    }
