
var BBtags = new Array();

BBtags['b']     = new Array('[b]', '[/b]', false);
BBtags['i']     = new Array('[i]', '[/i]', false);
BBtags['u']     = new Array('[u]', '[/u]', false);
BBtags['small'] = new Array('[small]', '[/small]', false);
//BBtags['code']  = new Array('[code]', '[/code]', false);
BBtags['url']   = new Array('[url]', '[/url]', false);
BBtags['url=']  = new Array('[url=]', '[/url]', false);
BBtags['quote'] = new Array('[quote]', '[/quote]', false);

function addtags(id)
{
    var str_tag;
    var isIE = navigator.userAgent.toLowerCase().indexOf("msie") != -1;
    var sel_text = '';

/*    if (isIE) {
        if (document.selection.createRange())
            var range = document.selection.createRange(); var sel_text = range.text;
        if (sel_text) sel_text = sel_text.replace(/\ +/g, " ");
    }

    if (sel_text) {
        eval("document.forms['addmess'].answer.value = document.forms['addmess'].answer.value.replace(/" + sel_text + "/g, '" + BBtags[id][0] + sel_text + BBtags[id][1] + "')");
        document.forms['addmess'].answer.focus();
        preview();

        return true;
    }
  */
    button = document.getElementById(id);
    if (!button || !BBtags[id]) return false;

    if (BBtags[id][2]) {
        BBtags[id][2] = false;
        str_tag = BBtags[id][1]
    }
    else {
        BBtags[id][2] = true;
        str_tag = BBtags[id][0]
    }


    (BBtags[id][2]) ? button.value = BBtags[id][1] : button.value = BBtags[id][0];
    

    document.forms['addmess'].answer.value += str_tag;
    document.forms['addmess'].answer.focus();
    preview();

    return false;
}

function open_calendar(date, field)
{
    window.open('html/calendar.html?date='+date+'&insert=' + field,'mywindow','width=300,height=280');
}

/*
    void quote(int id)
    The function sets quote to answer textarea
    Input: int id - id quoting answer
    Version: 2.7.7
    Last update: 30 august 2005
*/
function quote(id)
{
    var text     = document.getElementById('message'+id);
    var login    = document.getElementById('login'+id);
    var postdate = document.getElementById('postdate'+id);

    var str = text.innerHTML;

    str = str.replace(/&quot;/g, "\"");
    str = str.replace(/&gt;/g, ">");
    str = str.replace(/&lt;/g,"<");
    str = str.replace(/\"/ig, "\'");
    str = str.replace(/<br>/ig,"\n");
    str = str.replace(/<b>/ig, "[b]");
    str = str.replace(/<i>/ig, "[i]");
    str = str.replace(/<\/i>/ig, "[/i]");
    str = str.replace(/<\/b>/ig, "[/b]");
    str = str.replace(/<small>/ig, "[small]");
    str = str.replace(/<\/small>/ig, "[/small]");
    str = str.replace(/<blockquote>/ig, "[quote]");
    str = str.replace(/<\/blockquote>/ig, "[/quote]");
    str = str.replace(/<hr(.+?)>/ig, "");
    str = str.replace(/<img(.+?)src='images\/smiles\/(.+?)\.gif'(.+?)>/ig, "[:$2:]");

    var quoting = '[quote][b][small]' + login.innerHTML + '[/b] (' + postdate.innerHTML + ")[/small]\n" + str + '[/quote]';

    document.forms['addmess'].answer.value += quoting;
    document.forms['addmess'].answer.focus();
    preview();
}


/*
    void check_all(string id, string name_check)
    The function sets selected or unselected for all name_check elements
    Input: string id - id form
           string name_check - name of elments array
    Version: 2.7.8
    Last update: 9 september 2005
*/
function check_all(id, name_check, down_class) {  
    var element = document.getElementById(id).elements;
    var num = 0;

    name_check = name_check + "[]";

    this.isIE = navigator.userAgent.toLowerCase().indexOf("msie") != -1;
    this.down_class = down_class;
    attr = this.isIE ? 'className' : 'class';

    for (i = 0; i < element.length; i++) {
        if (element[i].name == name_check) {
            myRow = document.getElementById('row' + i);
            if ( myRow.getAttribute(attr) != this.down_class)
                myRow.setAttribute(attr, this.down_class); 
            else
                myRow.setAttribute(attr, this.over_class); 
            element[i].checked = !element[i].checked;
        }
    }
}

/*
    void display_view(string id)
    The function sets attribute "display" as true for element with id
    Input: string id - id element
    Version: 2.7.8
    Last update: 9 september 2005
*/
function display_view(id) {//, id_string, string1, string2) {
    var element = document.getElementById(id);

    if (element.style.display == 'none') {
        element.style.display = '';
//        if (id_string && string1) {
//alert(id_string);
//alert(document.getElementById('string_s').innerText);
//            document.getElementById(id_string).innerText = string1;
//        }
    }
    else {
        element.style.display = 'none';
//        if (id_string && string2) {
//            document.getElementById(id_string).innerText = string2;
//        }
    }
}


/*
Класс для подсветки строк таблицы.
Конструктор принимает следующие аргументы:
    name - необходимо передать физическое имя создаваемого объекта
    table_id - id целевой таблицы
    default_class - стиль для отображения по умолчанию
    over_class - стиль при наведенном курсоре
    down_class - стиль, если строка выбрана
    checkbox_name - подразумевается, что чекбоксы нумеруются так: checkbox_name[]; если отсутствует, 
            то происходит только подсветка строк при наведении курсора
*/

var TableMark = function(name, table_id, default_class, over_class, down_class, checkbox_name)
{

    this.name = name;
    this.tableid = table_id;
    this.default_class = default_class;
    this.over_class = over_class;   
    this.down_class = down_class;
    this.ch_name = checkbox_name;

    this.isIE = navigator.userAgent.toLowerCase().indexOf("msie") != -1;

    rows = document.getElementById( this.tableid ).getElementsByTagName("TR");
    for (i=0; i<rows.length; i++) {
        if (this.isIE) {
            rows[i].setAttribute('className', this.default_class);
            rows[i].setAttribute('onmouseover', new Function(this.name + '.' + "ChangeColor(\'over\', this, \'"+ checkbox_name +"\')"));
            rows[i].setAttribute('onmouseout', new Function(this.name + '.' + "ChangeColor(\'out\', this, \'"+ checkbox_name +"\')"));   
            rows[i].setAttribute('onmousedown', new Function(this.name + '.' + "ChangeColor(\'down\', this, \'"+ checkbox_name +"\')")); 
        } else {
            rows[i].setAttribute('class', this.default_class);
            rows[i].setAttribute('onmouseover', this.name + '.' + "ChangeColor(\'over\', this, \'"+ checkbox_name +"\')");
            rows[i].setAttribute('onmouseout', this.name + '.' + "ChangeColor(\'out\', this, \'"+ checkbox_name +"\')");
            rows[i].setAttribute('onmousedown', this.name + '.' + "ChangeColor(\'down\', this, \'"+ checkbox_name +"\')");
        }
    }
    elements = document.getElementsByTagName("INPUT");
    if (this.ch_name) {
        func = this.isIE ? new Function('return false') : 'return false';
        for (j=0; j<elements.length; j++) {
            if (elements[j].getAttribute('name')==this.ch_name + '[]') elements[j].setAttribute('onclick', func);
        }
    }
}

TableMark.prototype.ChangeColor = function(act, tr, checkbox_name)
{

    attr = this.isIE ? 'className' : 'class';

    ch = tr.getElementsByTagName("INPUT")[0];


    if (ch && ch.name == (checkbox_name + "[]")) {
        switch (act) {
            case 'over':
                if ( tr.getAttribute(attr) != this.down_class) tr.setAttribute(attr, this.over_class);
            break;
            case 'out':
                if ( tr.getAttribute(attr) != this.down_class) tr.setAttribute(attr, this.default_class);   
            break;
            case 'down':
                if (this.ch_name) {
                    if ( tr.getAttribute(attr) != this.down_class)
                        tr.setAttribute(attr, this.down_class); 
                    else
                        tr.setAttribute(attr, this.over_class);
                    ch.checked = !ch.checked;
                }
        }
    }
} 