/*=====================================*\
|| ################################### ||
|| #                                 # ||
|| #     EEB-CMS BB-Code Scripts     # ||
|| #                                 # ||
|| ################################### ||
\*=====================================*/

// INIT
// **********************************************************************
    var fontoptions = new Array("Arial", "Arial Black", "Comic Sans MS", "Courier New", "Fixedsys", "Garamond", "Georgia", "Impact", "Lucida Console", "Lucida Sans Unicode", "Microsoft Sans Serif", "Palatino Linotype", "System", "Tahoma", "Times New Roman", "Trebuchet MS", "Verdana");
    var sizeoptions = new Array(10, 11, 13, 15, 18, 20, 24, 28, 32);
    var buttonstatus = new Array();
    buttonstatus = {
    	"B" : 0,
    	"I" : 0,
    	"U" : 0,
    	"LEFT" : 0,
    	"CENTER" : 0,
    	"RIGHT" : 0,
    	"INDENT" : 0,
    	"IMG" : 0,
    	"CODE" : 0,
    	"PHP" : 0,
    	"QUOTE" : 0 };
    var coloroptions = new Array();
    coloroptions = {
        "#000000" : "Black",
        "#3F3F3F" : "DarkGrey",
        "#7F7F7F" : "MediumGrey",
        "#BFBFBF" : "LightGrey",
        "#FFFFFF" : "White",
        "#7F0000" : "DarkRed",
        "#7F3F00" : "DarkBrown",
        "#7F7F00" : "DarkYellow",
        "#3F7F00" : "DarkLime",
        "#007F00" : "DarkGreen",
        "#007F3F" : "DarkSeaGreen",
        "#007F7F" : "DarkCyan",
        "#003F7F" : "DarkRoyalBlue",
        "#00007F" : "DarkBlue",
        "#3F007F" : "DarkViolett",
        "#7F007F" : "DarkMagenta",
        "#FF0000" : "Red",
        "#FF7F00" : "Orange",
        "#FFFF00" : "Yellow",
        "#7FFF00" : "Lime",
        "#00FF00" : "Green",
        "#00FF7F" : "SeaGreen",
        "#00FFFF" : "Cyan",
        "#007FFF" : "RoyalBlue",
        "#0000FF" : "Blue",
        "#7F00FF" : "Violett",
        "#FF00FF" : "Magenta",
        "#FF7F7F" : "LightRed",
        "#FFBF7F" : "LightOrange",
        "#FFFF7F" : "LightYellow",
        "#BFFF7F" : "LightLime",
        "#7FFF7F" : "LightGreen",
        "#7FFFBF" : "LightSeaGreen",
        "#7FFFFF" : "LightCyan",
        "#7FBFFF" : "LightRoyalBlue",
        "#7F7FFF" : "LightBlue",
        "#BF7FFF" : "LightViolett",
        "#FF7FFF" : "LightMagenta" };

    var opened_BBCodes = new Array();

    var is_OP = (navigator.appName.indexOf('Opera') != -1 ) ? 1 : 0;
    var is_IE = (navigator.appName.indexOf('Microsoft') != -1 ) ? 1 : 0;
    var is_NS = (navigator.appName.indexOf('Netscape') != -1 ) ? 1 : 0; 
    var EMAIL_predefined = "";
    var URL_predefined = "http://";
    var IMGURL_predefined = "http://";
    var IMG_predefined = "http://";
    var attachedForm = document.comment.comment;
// Stupid Opera-Workaround because it has no CSS-Understandings :-/
    if (is_OP) {
        document.writeln("<style type=\"text/css\">");
        document.writeln(".bbcode_hover, .bbcode_click, .bbcode_active {");
        document.writeln("    height: 23px;");
        document.writeln("}");
        document.writeln("</style>");
    }

// **********************************************************************
// Emulated Arrayfunctions. They behave like in PHP !

    function array_search(myarray, string) {
        var array_len = myarray.length;
        for (var i = 0; i < array_len; i ++) {
            if (myarray[i] == string) return i;
        }
        return "n";
    }
 
    function array_push(myarray, string) {
        var array_len = myarray.length;
        myarray[array_len] = string;
        return myarray;
    }

    function array_pop(myarray) {
        var array_len = myarray.length;
        myarray.length = array_len - 1;
        return myarray;
    }

// **********************************************************************
// URL and EMAIL

    function makeurl(code) {
        getText();
        if (code == "URL" || code == "EMAIL" || code == "IMGURL") {
            input1 = prompt(eval(code + "_hint1"), textmiddle);
            input2 = prompt(eval(code + "_hint2"), eval(code + "_predefined"));
                if (input1) {
                    ready_value += "[" + code + "=" + input2 + "]";
                    ready_value += input1;
                } else {
                    ready_value += "[" + code + "]";
                    ready_value += input2;
                }
                ready_value += "[/" + code + "]";
        }
        writeText();
    }
    
// **********************************************************************
// Named and unnamend LIST

    function makelist(code) {
        getText();
        if (code == "LIST") {
            input1 = prompt(eval(code + "_hint1"), textmiddle);
        }
        if (input1) {
            ready_value += "[" + code + "=" + input1 + "]\r\n";
        } else {
	    ready_value += "[" + code + "]\r\n";
	}
        while (input2 = prompt(eval(code + "_hint2"), "")) {
            ready_value += "[*]" + input2 + "[/*]\r\n";
        }
        ready_value += "[/" + code + "]\r\n";
        writeText();
    }



// **********************************************************************
// Main BBCode-Engine. It can handle most of all known BBCodes

    function bbcode(code) {
        getText();
// Opera Hack. I dont know, why it needs shit like this :-/
        mode = mode;
// ------------------
        selector = document.getElementsByName(code)[0];
        if(textmiddle) {
            if ((code != "SIZE") && (code != "FONT") && (code != "COLOR")) {
                ready_value += "[" + code + "]" + textmiddle + "[/" + code + "]";
            } else {
                input1 = selector.value;
                selector.selectedIndex = 0;
                ready_value += "[" + code + "=" + input1 + "]" + textmiddle + "[/" + code + "]";
            }
        } else {
            if (mode == 1) {
                if ((code != "SIZE") && (code != "FONT") && (code != "COLOR")) {
                    input1 = prompt(GENERIC_hint + "\r\n( [" + code + "]xxx[/" + code + "] )", "");
                    if (!input1) input1 = "";
                    ready_value += "[" + code + "]" + input1 + "[/" + code + "]";
                } else {
                    input1 = selector.value;
                    selector.selectedIndex = 0;
                    input2 = prompt(GENERIC_hint + "\r\n( [" + code + "=" + input1 + "]xxx[/" + code + "] )", "");
                    ready_value += "[" + code + "=" + input1 + "]" + input2 + "[/" + code + "]";
                }
            } else {
                index = array_search(opened_BBCodes, code);
                if (index != "n") {
                    array_len = opened_BBCodes.length;
                    if ((code == "SIZE") || (code == "FONT") || (code == "COLOR")) {
                        selector.selectedIndex = 0;
                    }
                    if (index == (array_len - 1)) {
                        ready_value += "[/" + opened_BBCodes[array_len - 1] + "]";
                        buttonstatus[opened_BBCodes[array_len - 1]] = 0;
                        array_pop(opened_BBCodes)
                    } else {
                        for (i = array_len - 1; i >= index; i --) {
                            ready_value += "[/" + opened_BBCodes[i] + "]";
                            buttonstatus[opened_BBCodes[i]] = 0;
                            get_status(opened_BBCodes[i]);
                            array_pop(opened_BBCodes)
                        }
                    }
                } else {
                    opened_BBCodes = array_push(opened_BBCodes, code);
                    buttonstatus[code] = 1;
                    if ((code != "SIZE") && (code != "FONT") && (code != "COLOR")) {
                         ready_value += "[" + code + "]";
                    } else {
                        input1 = selector.value;
                        selector.selectedIndex = 0;
                        ready_value += "[" + code + "=" + input1 + "]";
                    }
                }
            }
        }
        writeText();
    }

// **********************************************************************
// Gets Text or Cursorposition (except Opera)

    function getText() {
        window.status = attachedForm;
        attachedForm.focus();
        input1 = "";
        input2 = "";
        ready_value = "";
        if (is_IE) {
            textform = document.selection.createRange().duplicate();
            textmiddle = textform.text;
        }
        if (is_NS) {
            textform = attachedForm;
            text = textform.value;
            start = textform.selectionStart;
            end = textform.selectionEnd;
            textstart = text.substr(0, start);
            textmiddle = text.substr(start, (end - start));
            textend = text.substring(end, (text.length));
        }
        if (is_OP) textmiddle = "";
    }	

// **********************************************************************
// Writes Text at Cursorposition (except Opera)

    function writeText() {
        if (is_IE) {
            textform.text = ready_value;
        }
        if (is_NS) {
            textform.value = textstart + ready_value + textend;
        }
        if (is_OP) {
            attachedForm.value += ready_value;
        }
        attachedForm.focus();
    }

// **********************************************************************
// Close all opened BB-Tags

    function closeAllTags() {
        getText();
        ready_value += textmiddle;
        for (var i = opened_BBCodes.length - 1; i >= 0; i --) {
            code = opened_BBCodes[i]
            ready_value += "[/" + code + "]";
            array_pop(opened_BBCodes)
            if (code != "FONT" && code != "COLOR" || code != "SIZE") {
                buttonstatus[code] = 0;
                get_status(code);
            }
        }
        writeText();
    }

// **********************************************************************
// Close last opened BB-Tag

    function closeLastTag() {
        getText();
        array_len = opened_BBCodes.length;
        ready_value += textmiddle;
        if (array_len) {
            code = opened_BBCodes[array_len - 1]
            ready_value += "[/" + code + "]";
            array_pop(opened_BBCodes)
            if (code != "FONT" && code != "COLOR" || code != "SIZE") {
                buttonstatus[code] = 0;
                get_status(code);
            }
        }
        writeText();
    }

// **********************************************************************
// Select-Box builder

   function buildFont() {
       document.writeln("<select name=\"FONT\" onchange=\"bbcode('FONT');\">");
       for (var font in fontoptions) {
           document.writeln("<option value=\"" + fontoptions[font] + "\">" + fontoptions[font] + "</option>");
       }
       document.writeln("</select>");
   }

   function buildSize() {
       document.writeln("<select name=\"SIZE\" onchange=\"bbcode('SIZE');\">");
       for (var size in sizeoptions) {
           document.writeln("<option value=\"" + sizeoptions[size] + "\">" + sizeoptions[size] + " Pixel</option>");
       }
       document.writeln("</select>");
   }

   function buildColor() {
       document.writeln("<select name=\"COLOR\" onchange=\"bbcode('COLOR');\">");
       for (var color in coloroptions) {
           document.writeln("<option value=\"" + color + "\" style=\"background-color: " + color + ";\">" + coloroptions[color] + "</option>");
       }
       document.writeln("</select>");
   }
   
   function get_status(element) {
       button = document.getElementsByName(element)[0];
       if (buttonstatus[element] == 1) {
           button.className = "bbcode_active";
       } else {
           button.className = "bbcode";
       }
   }
  
