defmode = "normalmode"; // default mode. either normalmode, advmode, or helpmode

if(defmode == "advmode") {
    helpmode = false;
    normalmode = false;
    advmode = true;
}else if(defmode == "helpmode") {
    helpmode = true;
    normalmode = false;
    advmode = false;
}else {
    helpmode = false;
    normalmode = true;
    advmode = false;
}

function chmode(swtch) {
    if (swtch == 1)    {
        advmode = false;
        normalmode = false;
        helpmode = true;
        alert(bbcode_helpmode);
    }else if (swtch == 0) {
        helpmode = false;
        normalmode = false;
        advmode = true;
        alert(bbcode_advmode);
    }else if (swtch == 2) {
        helpmode = false;
        advmode = false;
        normalmode = true;
        alert(bbcode_normode);
    }
}

function AddText(text) {
    if (document.input.message.createTextRange && document.input.message.caretPos) {
        var caretPos = document.input.message.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
        setfocus();
    }else {
        document.input.message.value  += text;
        setfocus();
    }
}

function email() {
    if (helpmode) {
        alert(bbcode_help_email);
    }else if (advmode) {
        AddTxt="[email] [/email]";
        AddText(AddTxt);
    }else {
        txt2=prompt(bbcode_prompt_email,"");
        if (txt2!=null) {
            txt=prompt(bbcode_prompt_emailerror,"name@domain.com");
            if (txt!=null) {
                if (txt2=="") {
                    AddTxt="[email]"+txt+"[/email]";

                }else {
                    AddTxt="[email="+txt+"]"+txt2+"[/email]";
                }

                AddText(AddTxt);
            }
        }
    }
}

function chsize(size) {
    if (helpmode) {
        alert(bbcode_help_size);
    }else if (advmode) {
        AddTxt="[size="+size+"] [/size]";
        AddText(AddTxt);
    }else {
        txt=prompt(bbcode_prompt_size+size,"Text");
        if (txt!=null) {
            AddTxt="[size="+size+"]"+txt+"[/size]";
            AddText(AddTxt);
        }
    }
}

function chfont(font) {
    if (helpmode) {
        alert(bbcode_help_font);
    }else if (advmode) {
        AddTxt="[font="+font+"] [/font]";
        AddText(AddTxt);
    }else {
        txt=prompt(bbcode_prompt_font+font,"Text");
        if (txt!=null) {
            AddTxt="[font="+font+"]"+txt+"[/font]";
            AddText(AddTxt);
        }
    }
}

function bold() {
    if (helpmode) {
        alert(bbcode_help_bold);
    }else if (advmode) {
        AddTxt="[b] [/b]";
        AddText(AddTxt);
    }else {
        txt=prompt(bbcode_prompt_bold,"Text");
        if (txt!=null) {
            AddTxt="[b]"+txt+"[/b]";
            AddText(AddTxt);
        }
    }
}

function italicize() {
    if (helpmode) {
        alert(bbcode_help_italic);
    }else if (advmode) {
        AddTxt="[i] [/i]";
        AddText(AddTxt);
    }else {
        txt=prompt(bbcode_prompt_italic,"Text");
        if (txt!=null) {
            AddTxt="[i]"+txt+"[/i]";
            AddText(AddTxt);
        }
    }
}

function quote() {
    if (helpmode) {
        alert(bbcode_help_quote);
    }else if (advmode) {
        AddTxt="\r[quote]\r[/quote]";
        AddText(AddTxt);
    }else {
        txt=prompt(bbcode_prompt_quote,"Text");
        if(txt!=null) {
            AddTxt="\r[quote]\r"+txt+"\r[/quote]";
            AddText(AddTxt);
        }
    }
}

function chcolor(color) {
    if (helpmode) {
        alert(bbcode_help_color);
    }else if (advmode) {
        AddTxt="[color="+color+"] [/color]";
        AddText(AddTxt);
    }else {
         txt=prompt(bbcode_prompt_color+color,"Text");
        if(txt!=null) {
            AddTxt="[color="+color+"]"+txt+"[/color]";
            AddText(AddTxt);
        }
    }
}

function center() {
    if (helpmode) {
        alert(bbcode_help_center);
    }else if (advmode) {
        AddTxt="[align=center] [/align]";
        AddText(AddTxt);
    }else {
        txt=prompt(bbcode_prompt_center,"Text");
        if (txt!=null) {
            AddTxt="\r[align=center]"+txt+"[/align]";
            AddText(AddTxt);
        }
    }
}

function hyperlink() {
    if (helpmode) {
        alert(bbcode_help_link);
    }else if (advmode) {
        AddTxt="[url] [/url]";
        AddText(AddTxt);
    }else {
        txt2=prompt(bbcode_prompt_link1,"");
        if (txt2!=null)    {
            txt=prompt(bbcode_prompt_link2,"http://");
            if (txt!=null)    {
                if (txt2=="") {
                    AddTxt="[url]"+txt+"[/url]";
                    AddText(AddTxt);
                }else {
                    AddTxt="[url="+txt+"]"+txt2+"[/url]";
                    AddText(AddTxt);
                }
            }
        }
    }
}

function image() {
    if (helpmode) {
        alert(bbcode_help_image);
    }else if (advmode) {
        AddTxt="[img] [/img]";
        AddText(AddTxt);
    }else {
        txt=prompt(bbcode_prompt_image,"http://");
        if(txt!=null) {
            AddTxt="\r[img]"+txt+"[/img]";
            AddText(AddTxt);
        }
    }
}

function code() {
    if (helpmode) {
        alert(bbcode_help_code);
    }else if (advmode) {
        AddTxt="\r[code]\r[/code]";
        AddText(AddTxt);
    }else {
        txt=prompt(bbcode_prompt_code,"");
        if (txt!=null) {
            AddTxt="\r[code]"+txt+"[/code]";
            AddText(AddTxt);
        }
    }
}

function list() {
    if (helpmode) {
        alert(bbcode_help_list);
    }else if (advmode) {
        AddTxt="\r[list]\r[*]\r[*]\r[*]\r[/list]";
        AddText(AddTxt);
    }else {
        st=prompt(bbcode_prompt_list_start,"");
        while ((st!="") && (st!="A") && (st!="a") && (st!="1") && (st!=null)) {
            st = prompt(bbcode_prompt_list_error,"");
        }

        if (st != null) {
            if (st == "") {
                AddTxt="\r[list]\r\n";
            }else {
                AddTxt="\r[list="+st+"]\r";
            }

            txt = "1";
            while ((txt!="") && (txt!=null)) {
                txt = prompt(bbcode_prompt_list_end, "");
                if (txt!="") {
                    AddTxt+="[*]"+txt+"\r";
                }
            }
            if ((st!="") && (st!=null)) {
                AddTxt+="[/list="+st+"]\r\n";
            }else {
                AddTxt+="[/list]\r\n";
            }
            AddText(AddTxt);
        }
    }
}

function underline() {
      if (helpmode) {
        alert(bbcode_help_underline);
    }else if (advmode) {
        AddTxt="[u] [/u]";
        AddText(AddTxt);
    }else {
        txt=prompt(bbcode_prompt_underline,"Text");
        if (txt!=null) {
            AddTxt="[u]"+txt+"[/u]";
            AddText(AddTxt);
        }
    }
}

function storeCaret(textEl) {
    if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

function setfocus() {
    document.input.message.focus();
}
