var currentTags  = "";
var startTags    = "";
var endTags      = "";
var constructingTag = false;

function textdec(tag, type) {
    disable(tag);
    addTag("\[" + tag + "]", "\[/" + tag + "]", type);
}

function hex() {
    var hexVal = prompt("Enter a hex value, or one of the following:\n\nred, blue, green, yellow, orange, purple, pink, white, black\n\npost, mail, sig, profilecomment, journalentry, journalcomment, gangannouncement");
    hexVal = String(hexVal).toLowerCase();
    switch(hexVal) {
        case "red"              : hexVal = "ff0000"; break;
        case "blue"             : hexVal = "0000ff"; break;
        case "green"            : hexVal = "00ff00"; break;
        case "yellow"           : hexVal = "ffff00"; break;
        case "orange"           : hexVal = "ff6600"; break;
        case "purple"           : hexVal = "aa00aa"; break;
        case "pink"             : hexVal = "ff3399"; break;
        case "white"            : hexVal = "ffffff"; break;
        case "black"            : hexVal = "000000"; break;

        case "post"             : hexVal = "f8f8f8"; break;
        case "mail"             : hexVal = "ffffff"; break;
        case "sig"              : hexVal = "ffffff"; break;
        case "profilecomment"   : hexVal = "efefef"; break;
        case "journalentry"     : hexVal = "d6e1e9"; break;
        case "journalcomment"   : hexVal = "f4f4f4"; break;
        case "gangannouncement" : hexVal = "f7f7f7"; break;
    }
    hexVal = hexVal.replace(/[^A-Fa-f0-9]/, "").toUpperCase();
    if(hexVal.length == 6 && hexVal.match(/[A-Fa-f0-9]{6}/)) {
        addTag("\[HEX=" + hexVal.toUpperCase() + "]", "\[/HEX]", "colored (" + hexVal.toUpperCase() + ")");
        disable("HEX");
    }
}

function img() {
    var imgURL = prompt("Enter image URL: ", "");
    if(imgURL) { nontag("\[IMG]" + imgURL + "\[/IMG]"); }
}

function link(tag, type, contents) {
    var temp1 = prompt("Enter " + type + ". ", "");
    var temp2 = prompt("Enter link name (can be blank). ", "");
    if(temp1) {
        if(temp2) { nontag("[" + tag + "=" + temp1 + "]" + temp2 + "[/" + tag + "]"); }
        else      { nontag("[" + tag + "=" + temp1 + "]" + temp1 + "[/" + tag + "]"); }
    }
}

function nontag(value) {
    value = " " + value + " ";

    var msg = document.getElementById(constructingTag ? "affected_text" : "hwmessage");
    if(document.selection) {
        msg.focus();
        var range = document.selection.createRange();
        range.text = value;
        range.select();
    }
    else if(msg.selectionStart) {
        var position = msg.selectionStart + 1;
        msg.value = msg.value.substr(0, msg.selectionStart) + value + msg.value.substr(msg.selectionEnd, msg.value.length);
        msg.focus();
        msg.setSelectionRange(position, position);
    }
    else {
        msg.value += value;
    }
}

function addTag(start, end, type) {
    constructingTag = true;
    startTags      += start;
    endTags         = end + endTags;
    if(currentTags) { currentTags += ", "; }
    currentTags    += type;

    document.getElementById("current_tags").innerHTML = currentTags;
    document.getElementById("tag_info").style.visibility = "visible";
}

function createTag() {
    document.getElementById("hwmessage").value += startTags + document.getElementById("affected_text").value + endTags;
    resetForm();
}

function disable(hbtmlButton) {
    document.getElementById(hbtmlButton).disabled = true;
}

function resetForm() {
    currentTags  = "";
    startTags    = "";
    endTags      = "";
    constructingTag = false;

    var buttons = document.getElementById("tag_buttons").getElementsByTagName("BUTTON");
    for(var i = 0; i < buttons.length; ++i) {
        if(buttons[i].disabled) {
            buttons[i].disabled = false;
        }
    }

    document.getElementById("affected_text").value = "";
    document.getElementById("tag_info").style.visibility = "hidden";
}

function message_replace() {
    var toReplace   = String(prompt("Enter text to replace (case-sensitive). ", ""));
    if(!toReplace.length) { return; }
    var replacement = String(prompt("Enter replacement text. Leaving this blank will erase the text you entered at the previous prompt. ", ""));
    if(replacement == null) { return; }

    var toReplaceRegex = RegExp(toReplace.replace(/([\\^$*+?.([{|])/, "\\$1"), "g");
    if(confirm("Replace \"" + toReplace + "\" with \"" + replacement + "\"\n\nAre you sure?")) {
        var currentText = document.getElementById("hwmessage").value.replace(toReplaceRegex, replacement);
        document.getElementById("hwmessage").value = currentText;
    }
}
