//Variables for controlling opening and closing tags (function tag)

var b = 2;
var i = 2;
var u = 2;
var q = 2;
var c = 2;
var url = 2;
var img = 2;

//Function for creating non-font tags

function bbCode(bbopen, bbclose) {
    var comment = document.editform.comment;
    if (document.selection) { // IE
        sel = document.selection.createRange();
        if (sel.text == '') {
            comment.value+= bbopen + bbclose;
        } else {
            sel.text = bbopen + sel.text + bbclose;
        }
    } else if (comment.selectionStart || comment.selectionStart == 0) { // Firefox
        var startPos = comment.selectionStart;
        var endPos = comment.selectionEnd;
        comment.value = comment.value.substring(0, startPos) + bbopen + comment.value.substring(startPos, endPos) + bbclose
        + comment.value.substring(endPos, comment.value.length);
    } else {
        comment.value += bbopen + bbclose;
    }
    comment.focus();
    return;
}

//Function for adding smilies

function smilie (smilie) {
    var comment = document.editform.comment;
    if (document.selection) {
        sel = document.selection.createRange();
        sel.text = comment + smilie;
    }
    else if (comment.selectionStart || comment.selectionStart == 0) {
        var startPos = comment.selectionStart;
        var endPos = comment.selectionEnd;
        comment.value = comment.value.substring(0, startPos)
        + smilie + comment.value.substring(endPos, comment.value.length);
    } else {
        comment.value += smilie;
    }
    comment.focus();
    return;
}

//Function for adding font color and size tags

function font(bbopen, bbclose) {
        var comment = document.editform.comment;
        comment.value += bbopen + bbclose;
        comment.focus();
        return;
}

//Function for adding smilies

function smilie (smilie) {
        var comment = document.editform.comment;
        comment.value += smilie;
        comment.focus();
        return;
}

//Helpbox messages
bold_help = "Bold text: [b]text[/b]";
italic_help = "Italic text: [i]text[/i]";
underline_help = "Underline text: [u]text[/u]";
quote_help = "Quote text: [quote]text[/quote] or [quote=name]text[/quote]";
code_help = "Code display: [code]code[/code]";
img_help = "Insert image: [img]http://image_url[/img]";
url_help = "Insert URL: [url]http://url[/url] or [url=http://url]URL text[/url]";
fontcolor_help = "Font color: [color=red]text[/color]  Tip: you can also use color=#FF0000";
fontsize_help = "Font size: [size=50%]small text[/size]";

//Function for displaying help information

// Shows the help messages in the helpline window
function helpline(help) {
        var helpbox = document.editform.helpbox;
        helpbox.value = eval(help + "_help");
}

//Check the form submission for errors

function checkForm() {
    var comment = document.editform.comment;

    //Check to make sure post lengths are sensible

    if (comment.value.length < 2) {
        alert("We require that each post  \nbe at least 2 characters long. \n \nGo back and try again.");
        return false;
    } else {
        return true;
    }
}
