var G_location = {
    'timeout' : false,
    'jump'    : true
};
var G_set = {
    'rubric'       : [],
    'rubric_m'     : [],
    'class_theft'  : [],
    'class_fire'   : [],
    'price_from'   : false,
    'price_to'     : false,
    'type'         : [],
    'height_from'  : false,
    'height_to'    : false,
    'width_from'   : false,
    'width_to'     : false,
    'depth_from'   : false,
    'depth_to'     : false,
    'weight_from'  : false,
    'weight_to'    : false,
    'producer'     : [],
    'price_sort'   : []
};
var oType = {
    'wizard_type_key'  : 'Ключевой',
    'wizard_type_el'   : 'Кодовый электронный',
    'wizard_type_code' : 'Кодовый механический',
    'wizard_type_bio'  : 'Биометрический'
};

function WL_page(page) {
    var oDIV, oA, oSpan;
    var div = document.getElementById('page_div');
    var pages = Math.ceil(G_set['price_sort'].length / parseInt(document.getElementById('top_items_on_page').value));

    oDIV = document.createElement('div');
    oDIV.style.fontSize = '14px';
    div.appendChild(oDIV);

    oSpan = document.createElement('span');
    oSpan.innerHTML = 'Страницы';
    oDIV.appendChild(oSpan);

    if (page > 1) {
        oA = document.createElement('a');
        oA.className = 'page_previous_button';
        oA.href = 'javascript:WL_submit(' + (page - 1) + ');';
        oDIV.appendChild(oA);

        oSpan = document.createElement('span');
        oSpan.innerHTML = '&larr;&nbsp;';
        oDIV.appendChild(oSpan);

        oA = document.createElement('a');
        oA.id = 'page_previous';
        oA.className = 'page_previous';
        oA.href = 'javascript:WL_submit(' + (page - 1) + ');';
        oA.innerHTML = 'предыдущая';
        oDIV.appendChild(oA);
    } else {
        oSpan = document.createElement('span');
        oSpan.className = 'page_previous';
        oSpan.innerHTML = '&larr; предыдущая';
        oDIV.appendChild(oSpan);
    }

    if (pages > page) {
        oA = document.createElement('a');
        oA.id = 'page_next';
        oA.className = 'page_next';
        oA.href = 'javascript:WL_submit(' + (page + 1) + ');';
        oA.innerHTML = 'следующая';
        oDIV.appendChild(oA);

        oSpan = document.createElement('span');
        oSpan.innerHTML = '&nbsp;&rarr;';
        oDIV.appendChild(oSpan);

        oA = document.createElement('a');
        oA.className = 'page_next_button';
        oA.href = 'javascript:WL_submit(' + (page + 1) + ');';
        oDIV.appendChild(oA);
    } else {
        oSpan = document.createElement('span');
        oSpan.className = 'page_next';
        oSpan.innerHTML = 'следующая &rarr;';
        oDIV.appendChild(oSpan);
    }

    oDIV = document.createElement('div');
    oDIV.style.marginTop = '5px';
    div.appendChild(oDIV);
    var start = (page > 7) ? page - 6 : 1;
    for (var i = start; i <= pages; i++) {
        if (page > 7 && i == page - 6) {
            oA = document.createElement('a');
            oA.innerHTML = '1';
            oA.href = 'javascript:WL_submit(1);';
            oA.className = 'page_a';
            oDIV.appendChild(oA);

            oSpan = document.createElement('span');
            oSpan.innerHTML = '...';
            oDIV.appendChild(oSpan);
            continue;
        }

        if (i > page + 5 && i < pages) {
            oSpan = document.createElement('span');
            oSpan.innerHTML = '...';
            oDIV.appendChild(oSpan);

            oA = document.createElement('a');
            oA.innerHTML = pages;
            oA.href = 'javascript:WL_submit(' + pages + ');';
            oA.className = 'page_a';
            oDIV.appendChild(oA);
            break;
        }

        if (i == page) {
            oSpan = document.createElement('span');
            oDIV.appendChild(oSpan);
            oSpan.innerHTML = i;
            oSpan.className = 'page_now';
        } else {
            oA = document.createElement('a');
            oA.innerHTML = i;
            oA.href = 'javascript:WL_submit(' + i + ');';
            oA.className = 'page_a';
            oDIV.appendChild(oA);
        }
    }
}

function WL_submit_cell(value, cell_main) {
    var i, table, row, cell, div, a, img, span, sort_by, input, div_child;
    for (i in oArticle) {
        sort_by = parseFloat(oArticle[i]['price']) + parseInt(oArticle[i]['char'][8]) / 1000000;

        if (!oArticle[i]['print']['m'] || oArticle[i]['print']['d'] || sort_by != value) continue;
        oArticle[i]['print']['d'] = true;

        table = document.createElement('table');
        table.width = '100%';
        table.border = '0';
        cell_main.appendChild(table);
        row = table.insertRow(-1);

        cell = row.insertCell(-1);
        cell.align = 'center';
        cell.style.width = '120px';
        div = document.createElement('div');
        cell.appendChild(div);
        a = document.createElement('a');
        a.href = '/catalog/gd-' + i + '.html';
        div.appendChild(a);
        img = document.createElement('img');
        img.id = 'label-' + i;
        img.src = '/img/catalog/' + i + '-basket.jpg';
        img.border = '0';
        img.alt = oArticle[i].name;
        a.appendChild(img);

        div = document.createElement('div');
        div.innerHTML = '<input type="checkbox" onclick="javascript:ParentChecked(this);" id="checkbox-' + i + '" value="' + i + '" />'
            + ' <span id="checkbox_label_' + i + '">В сравнение</span>';
        cell.appendChild(div);

        cell = row.insertCell(-1);
        cell.vAlign = 'top';
        div = document.createElement('div');
        cell.appendChild(div);
        if (oArticle[i].custom > 0) {
            span = document.createElement('span');
            span.innerHTML = '* ';
            span.style.color = '#9E0607';
            span.style.fontWeight = 'bold';
            span.style.fontSize = '14px';
            div.appendChild(span);
        }
        a = document.createElement('a');
        a.className = 'art_name';
        a.href = '/catalog/gd-' + i + '.html';
        span = document.createElement('span');
        span.innerHTML = oArticle[i].name;
        span.style.color = '#000000';
        a.appendChild(span);
        div.appendChild(a);

        div = document.createElement('div');
        div.style.margin = '5px 0 1em';
        div.innerHTML = oArticle[i].desc;
        if (oArticle[i].custom > 0) {
            div_child = document.createElement('div');
            div_child.innerHTML = '*модель под заказ';
            div_child.style.marginTop = '5px';
            div_child.style.fontSize = '14px';
            div.appendChild(div_child);
        }
        cell.appendChild(div);

        div = document.createElement('div');
        div.style.backgroundImage = 'url("/img/coins.gif")';
        div.style.backgroundRepeat = 'no-repeat';
        div.style.height = '28px';
        cell.appendChild(div);
        span = document.createElement('span');
        span.innerHTML = oArticle[i]['price_format'] + ' руб.';
        span.style.color = '#9E0607';
        span.style.display = 'block';
        span.style.fontFamily = 'Arial';
        span.style.fontSize = '15px';
        span.style.fontWeight = 'bold';
        span.style.margin = '0';
        span.style.padding = '7px 0 0 45px';
        div.appendChild(span);

        div = document.createElement('div');
        cell.appendChild(div);
        input = document.createElement('input');
        input.type = 'hidden';
        input.id = 'input_article_number-' + i;
        input.value = '1';
        div.appendChild(input);
        a = document.createElement('a');
        a.className = 'bag_in';
        a.href = 'javascript:IntoBasket_new(' + i + ', \'' + oArticle[i]['price'] + '\', \'' + oArticle[i]['char'][2] + '\', 0, true);';
        a.id = 'bag_in_' + i;
        div.appendChild(a);

        break;
    }
}

function WL_display_false() {
    var i;
    for (i in oArticle) {
        oArticle[i]['print']['d'] = false;
    }
}

function WL_submit(page) {
    var i, table, row, cell, aSort;
    GVarOnPage['wizard'] = page;
    var col_num = 1;
    var items_on_page = document.getElementById('top_items_on_page').value;
    var items_on_page_counter = items_on_page;
    WL_display_false();
    switch (document.getElementById('sort_by').value) {
        case 'price_asc':
            aSort = G_set['price_sort'].sort(WL_order_asc);
            break;
        case 'price_desc':
            aSort = G_set['price_sort'].sort(WL_order_desc);
            break;
    }
    var main_div = document.getElementById('main_div');
    var page_div = document.getElementById('page_div');
    page_div.style.display = 'none';
    page_div.innerHTML = '';
    main_div.style.display = 'none';
    main_div.innerHTML = '';
    table = document.createElement('table');
    main_div.appendChild(table);
    table.width = '100%';
    for (i = 0; i < aSort.length; i++) {
        if (i < (parseInt(page) - 1) * items_on_page) continue;
        items_on_page_counter -= 1;
        if (col_num == 1) row = table.insertRow(-1);
        cell = row.insertCell(-1);
        cell.width = '50%';
        cell.vAlign = 'top';
        cell.style.padding = '10px 0';
        WL_submit_cell(aSort[i], cell);
        col_num = (col_num == 1) ? 2 : 1;
        if (items_on_page_counter <= 0) break;
    }
    if (col_num == 2) row.insertCell(-1);
    if (G_set['price_sort'].length > items_on_page) {
        WL_page(page);
        page_div.style.display = 'block';
    }
    SelectedCompare();
    main_div.style.display = 'block';
    if (G_location['jump']) {
        window.clearTimeout(G_location['timeout']);
        G_location['timeout'] = window.setTimeout('document.location = "#top_div"', 10);
    } else G_location['jump'] = true;
}

function WL_order_desc(a, b) {
    return b - a;
}

function WL_order_asc(a, b) {
    return a - b;
}

function Wizard_value(sStr, sValue) {
    document.getElementById('wizard_' + sStr).value = sValue;
    document.getElementById('wizard_' + sStr).style.color = 'black';
    WL_event();
}

function WL_is_gray(color) {
    var i;
    var a = ['gray', '#808080'];
    for (i = 0; i < a.length; i++) {
        if (color == a[i]) return true;
    }
    return false;
}

function WizardFilled(sID_part) {
    var sStr, sStr_post, sFrom, sTo;
    var sReturn = '';
    var oFrom = document.getElementById('wizard_' + sID_part + '_from');
    var oTo = document.getElementById('wizard_' + sID_part + '_to');
    if ((oFrom.value == '' || oFrom.value == G_set[sID_part + '_from']) && WL_is_gray(oFrom.style.color)) {
        oFrom.value = G_set[sID_part + '_from'];
        sFrom = '';
    } else {
        sFrom = oFrom.value;
    }
    if ((oTo.value == '' || oTo.value == G_set[sID_part + '_to']) && WL_is_gray(oTo.style.color)) {
        oTo.value = G_set[sID_part + '_to'];
        sTo = '';
    } else {
        sTo = oTo.value;
    }
    switch (sID_part) {
        case 'price'   : sStr = 'Цена: ';         sStr_post = ' руб.'; break;
        case 'height'  : sStr = 'Высота: ';       sStr_post = ' мм.';  break;
        case 'width'   : sStr = 'Ширина: ';       sStr_post = ' мм.';  break;
        case 'depth'   : sStr = 'Глубина: ';      sStr_post = ' мм.';  break;
        case 'weight'  : sStr = 'Вес: ';          sStr_post = ' кг.';  break;
    }
    if (sFrom.length > 0 && sTo.length > 0) {
        sReturn = '<div>' + sStr + 'от ' + G_number_format(sFrom) + ' до ' + G_number_format(sTo) + sStr_post + '</div>';
    } else if (sFrom.length > 0) {
        sReturn = '<div>' + sStr + 'от ' + G_number_format(sFrom) + sStr_post + '</div>';
    } else if (sTo.length > 0) {
        sReturn = '<div>' + sStr + 'до ' + G_number_format(sTo) + sStr_post + '</div>';
    }
    return sReturn;
}

function WL_insert() {
    var el, sReturn;
    var aWizard_id = [
        'wizard_class_theft',
        'wizard_class_fire',
        'wizard_price_from',
        'wizard_price_to',
        'wizard_type',
        'wizard_height_from',
        'wizard_height_to',
        'wizard_width_from',
        'wizard_width_to',
        'wizard_depth_from',
        'wizard_depth_to',
        'wizard_weight_from',
        'wizard_weight_to',
        'wizard_producer'
    ];
    var aWizardFilled = [];
    var rxWizard = /^wizard_(price|height|width|depth|weight)_(to)$/;
    var wizard_rubric_theft = document.getElementById('wizard_rubric_theft');
    var wizard_rubric_fire = document.getElementById('wizard_rubric_fire');

    if (wizard_rubric_fire.checked && wizard_rubric_theft.checked) {
        aWizardFilled.push('<div>Для защиты любого вида</div>');
    } else if (wizard_rubric_fire.checked) {
        aWizardFilled.push('<div>Для защиты от пожара</div>');
    } else if (wizard_rubric_theft.checked) {
        aWizardFilled.push('<div>Для защиты от кражи</div>');
    }

    for (var i = 0; i < aWizard_id.length; i++) {
        switch (aWizard_id[i]) {
            case 'wizard_class_theft':
                el = document.getElementById('wizard_class_theft');
                if (el.value != 'any') {
                    aWizardFilled.push('<div>Класс взломостойкости: '
                        + el.value + '</div>');
                }
                break;
            case 'wizard_class_fire':
                el = document.getElementById('wizard_class_fire');
                if (el.value != 'any') {
                    aWizardFilled.push('<div>Класс огнестойкости: '
                        + el.value + '</div>');
                }
                break;
            case 'wizard_producer':
                el = document.getElementById('wizard_producer');
                if (el.value != 0) {
                    aWizardFilled.push('<div>Торговая марка: '
                        + oProducer['id_' + el.value]['name'] + '</div>');
                }
                break;
            case 'wizard_type':
                el = document.getElementById('wizard_type');
                if (el.value != 'wizard_type_any') {
                    aWizardFilled.push('<div>Тип замка: '
                        + oType[el.value] + '</div>');
                }
                break;
            default :
                var aWizard_exec = rxWizard.exec(aWizard_id[i]);
                if (aWizard_exec) {
                    sReturn = WizardFilled(aWizard_exec[1]);
                    if (sReturn) aWizardFilled.push(sReturn);
                }
        }
    }

    if (aWizardFilled.length > 5) {
        document.getElementById('wizard_form1').style.marginBottom = ((aWizardFilled.length - 5) * 14) + 'px';
    } else {
        document.getElementById('wizard_form1').style.marginBottom = 0;
    }
    document.getElementById('wizard_filled').innerHTML = aWizardFilled.join('');
    G_location['jump'] = false;
    WL_submit(1);
}

function WL_label() {
    var i, oA, oSpan;
    var wizard_rubric_theft = document.getElementById('wizard_rubric_theft');
    var wizard_rubric_fire = document.getElementById('wizard_rubric_fire');
    var aPairs = [
        'price',
        'height',
        'width',
        'depth',
        'weight'
    ];
	var oCount = document.getElementById('wizard_count');
    for (i = 0; i < aPairs.length; i++) {
        document.getElementById('wizard_' + aPairs[i] + '_to_td').innerHTML = '';
    }
    oCount.innerHTML = 'Моделей найдено: ' + G_set['price_sort'].length;
    if (G_set['rubric'].length > 1) {
        wizard_rubric_fire.disabled = false;
        wizard_rubric_theft.disabled = false;
    } else {
        wizard_rubric_fire.disabled = true;
        wizard_rubric_theft.disabled = true;
    }
    Wizard_select_theft();
    Wizard_select_fire();
    Wizard_select_type();
    Wizard_select_producer();
    for (i = 0; i < aPairs.length; i++) {
        var oFrom = G_set[aPairs[i] + '_from'];
        var oTo   = G_set[aPairs[i] + '_to'];
        if (oFrom && oTo) {
            document.getElementById('wizard_' + aPairs[i] + '_from').disabled = false;
            document.getElementById('wizard_' + aPairs[i] + '_to').disabled = false;
            var oTD = document.getElementById('wizard_' + aPairs[i] + '_to_td');

            oSpan = document.createElement('span');
            oTD.appendChild(oSpan);
            oSpan.innerHTML = '(min-max: ';

            oA = document.createElement('a');
            oTD.appendChild(oA);
            oA.className = 'wizard_from_to';
            oA.innerHTML = G_number_format(oFrom);
            oA.href = 'javascript:Wizard_value(\'' + aPairs[i] + '_from\', ' + oFrom + ');';
            oA.title = 'Вставить минимальное значение';

            oSpan = document.createElement('span');
            oTD.appendChild(oSpan);
            oSpan.innerHTML = ' &mdash; ';

            oA = document.createElement('a');
            oTD.appendChild(oA);
            oA.className = 'wizard_from_to';
            oA.innerHTML = G_number_format(oTo);
            oA.href = 'javascript:Wizard_value(\'' + aPairs[i] + '_to\', ' + oTo + ');';
            oA.title = 'Вставить максимальное значение';

            oSpan = document.createElement('span');
            oTD.appendChild(oSpan);
            oSpan.innerHTML = ')';
        } else {
            document.getElementById('wizard_' + aPairs[i] + '_from').disabled = true;
            document.getElementById('wizard_' + aPairs[i] + '_to').disabled = true;
        }
    }
    WL_insert();
}

function Wizard_select_producer() {
    var oOption, i;
    var oSelect = document.getElementById('wizard_producer');
    var sValue = oSelect.value;
    oSelect.innerHTML = '';
    oOption = document.createElement('option');
    oSelect.appendChild(oOption);
    oOption.text = 'Любая';
    oOption.value = 0;
    for (i in oProducer) {
        if (!WL_in_array(oProducer[i]['id'], G_set['producer'])) continue;
    	oOption = document.createElement('option');
    	oSelect.appendChild(oOption);
    	oOption.text = oProducer[i]['name'];
    	oOption.value = oProducer[i]['id'];
    }
    Global_SelectOption(sValue, 'wizard_producer');
}

function Wizard_select_type() {
    var oOption, i;
    var oSelect = document.getElementById('wizard_type');
    var sValue = oSelect.value;
    oSelect.innerHTML = '';
    oOption = document.createElement('option');
    oSelect.appendChild(oOption);
    oOption.text = 'Любой';
    oOption.value = 'wizard_type_any';
    for (i in oType) {
        if (!WL_in_array(i, G_set['type'])) continue;
    	oOption = document.createElement('option');
    	oSelect.appendChild(oOption);
    	oOption.text = oType[i];
    	oOption.value = i;
    }
    Global_SelectOption(sValue, 'wizard_type');
}

function Wizard_select_theft() {
    var oOption, i;
    var oSelect = document.getElementById('wizard_class_theft');
    var sValue = oSelect.value;
    oSelect.innerHTML = '';
    oOption = document.createElement('option');
    oSelect.appendChild(oOption);
    oOption.text = 'взломостойкость';
    oOption.value = 'any';
    for (i = 0; i < oClassTheft.length; i++) {
        if (!WL_in_array(oClassTheft[i], G_set['class_theft'])) continue;
    	oOption = document.createElement('option');
    	oSelect.appendChild(oOption);
    	oOption.text = oClassTheft[i];
    	oOption.value = oClassTheft[i];
    }
    Global_SelectOption(sValue, 'wizard_class_theft');
    oSelect.disabled = (G_set['class_theft'].length > 0) ? false : true;
}

function Wizard_select_fire() {
    var oOption, i;
    var oSelect = document.getElementById('wizard_class_fire');
    var sValue = oSelect.value;
    oSelect.innerHTML = '';
    oOption = document.createElement('option');
    oSelect.appendChild(oOption);
    oOption.text = 'огнестойкость';
    oOption.value = 'any';
    for (i = 0; i < oClassFire.length; i++) {
        if (!WL_in_array(oClassFire[i], G_set['class_fire'])) continue;
    	oOption = document.createElement('option');
    	oSelect.appendChild(oOption);
    	oOption.text = oClassFire[i];
    	oOption.value = oClassFire[i];
    }
    Global_SelectOption(sValue, 'wizard_class_fire');
    oSelect.disabled = (G_set['class_fire'].length > 0) ? false : true;
}

function WL_in_array(s, a) {
    var bReturn = false;
    for (var i = 0; i < a.length; i++) {
        if (s == a[i]) {
            bReturn = true;
            break;
        }
    }
    return bReturn;
}

function WL_from_char(id, subStr, char_id) {
    var char_sub;
    if (oArticle[id]['char'][char_id]) {
        char_sub = oArticle[id]['char'][char_id];
        if (G_set[subStr + '_from'] == false || G_set[subStr + '_from'] > parseInt(char_sub)) {
            G_set[subStr + '_from'] = parseInt(char_sub);
        }
    }
    if (oArticle[id]['char'][char_id]) {
        char_sub = oArticle[id]['char'][char_id];
        if (G_set[subStr + '_to'] == false || G_set[subStr + '_to'] < parseInt(char_sub)) {
            G_set[subStr + '_to'] = parseInt(char_sub);
        }
    }
}

function WL_from_to() {
    var id;
    G_set = {
        'rubric'       : [],
        'rubric_m'     : [],
        'class_theft'  : [],
        'class_fire'   : [],
        'price_from'   : false,
        'price_to'     : false,
        'type'         : [],
        'height_from'  : false,
        'height_to'    : false,
        'width_from'   : false,
        'width_to'     : false,
        'depth_from'   : false,
        'depth_to'     : false,
        'weight_from'  : false,
        'weight_to'    : false,
        'producer'     : [],
        'price_sort'   : []
    };
    for (id in oArticle) {
        if (oArticle[id]['print']['r']) {
            if (!WL_in_array(oArticle[id]['rubric'], G_set['rubric'])) {
                G_set['rubric'].push(oArticle[id]['rubric']);
            }
        }
        if (oArticle[id]['print']['theft'] && oArticle[id]['char'][14].length > 0) {
            if (!WL_in_array(oArticle[id]['char'][14], G_set['class_theft'])) {
                G_set['class_theft'].push(oArticle[id]['char'][14]);
            }
        }
        if (oArticle[id]['print']['fire'] && oArticle[id]['char'][11].length > 0) {
            if (!WL_in_array(oArticle[id]['char'][11], G_set['class_fire'])) {
                G_set['class_fire'].push(oArticle[id]['char'][11]);
            }
        }
        if (oArticle[id]['print']['t']) {
            if (!WL_in_array(oArticle[id]['char'][15], G_set['type'])) {
                G_set['type'].push(oArticle[id]['char'][15]);
            }
        }
        if (oArticle[id]['print']['p']) {
            if (!WL_in_array(oArticle[id]['producer_id'], G_set['producer'])) {
                G_set['producer'].push(oArticle[id]['producer_id']);
            }
        }
        if (!oArticle[id]['print']['m']) continue;

        G_set['price_sort'].push(parseInt(oArticle[id]['price']) + parseInt(oArticle[id]['char'][8]) / 1000000);

        if (!WL_in_array(oArticle[id]['rubric'], G_set['rubric'])) {
            G_set['rubric'].push(oArticle[id]['rubric']);
        }
        if (!WL_in_array(oArticle[id]['rubric'], G_set['rubric_m'])) {
            G_set['rubric_m'].push(oArticle[id]['rubric']);
        }
        if (G_set['price_from'] == false || G_set['price_from'] > parseInt(oArticle[id]['price'])) {
            G_set['price_from'] = parseInt(oArticle[id]['price']);
        }
        if (G_set['price_to'] == false || G_set['price_to'] < parseInt(oArticle[id]['price'])) {
            G_set['price_to'] = parseInt(oArticle[id]['price']);
        }
        if (oArticle[id]['char'][14].length > 0 && !WL_in_array(oArticle[id]['char'][14], G_set['class_theft'])) {
            G_set['class_theft'].push(oArticle[id]['char'][14]);
        }
        if (oArticle[id]['char'][11].length > 0 && !WL_in_array(oArticle[id]['char'][11], G_set['class_fire'])) {
            G_set['class_fire'].push(oArticle[id]['char'][11]);
        }
        if (!WL_in_array(oArticle[id]['char'][15], G_set['type'])) {
            G_set['type'].push(oArticle[id]['char'][15]);
        }
        WL_from_char(id, 'height', 8);
        WL_from_char(id, 'width', 7);
        WL_from_char(id, 'depth', 9);
        WL_from_char(id, 'weight', 2);
        if (!WL_in_array(oArticle[id]['producer_id'], G_set['producer'])) {
            G_set['producer'].push(oArticle[id]['producer_id']);
        }
    }
    if (G_set['price_sort'].length > 0) WL_label();
}

function WL_event() {
    var aWizard_id = [
        'wizard_price_from',
        'wizard_price_to',
        'wizard_height_from',
        'wizard_height_to',
        'wizard_width_from',
        'wizard_width_to',
        'wizard_depth_from',
        'wizard_depth_to',
        'wizard_weight_from',
        'wizard_weight_to'
    ];
    var rxInt = /[^\d]+/g;
    var rxID  = /^wizard_(price|height|width|depth|weight)_(from|to)$/i;
    var wizard_rubric_theft = document.getElementById('wizard_rubric_theft');
    var wizard_rubric_fire = document.getElementById('wizard_rubric_fire');
    if (!wizard_rubric_theft.checked && !wizard_rubric_fire.checked) {
        if (WL_in_array('theft', G_set['rubric_m'])) {
            wizard_rubric_fire.checked = true;
        } else if (WL_in_array('fire', G_set['rubric_m'])) {
            wizard_rubric_theft.checked = true;
        } else {
            wizard_rubric_fire.checked = true;
            wizard_rubric_theft.checked = true;
        }
    }

    for (var i = 0; i < aWizard_id.length; i++) {
        var aID = rxID.exec(aWizard_id[i]);
        var oID = document.getElementById(aWizard_id[i]);
        var oFrom = document.getElementById('wizard_' + aID[1] + '_from');
        var oTo   = document.getElementById('wizard_' + aID[1] + '_to');
        if (oFrom.value.length > 0 && oTo.value.length > 0 && parseInt(oFrom.value) > parseInt(oTo.value)) {
            if (parseInt(oTo.value.replace(rxInt, '')) < G_set[aID[1] + '_from']) {
                oTo.value = G_set[aID[1] + '_from'];
                oTo.style.color = 'red';
            }
            if (parseInt(oFrom.value.replace(rxInt, '')) > G_set[aID[1] + '_to']) {
                oFrom.value = G_set[aID[1] + '_to'];
                oFrom.style.color = 'red';
            }
        }
        var sValue = oID.value.replace(rxInt, '');
        if (sValue == G_set[aID[1] + '_' + aID[2]] && WL_is_gray(oID.style.color)) {
            sValue = '';
        } else if (sValue == '') {
            oID.style.color = 'gray';
        }
        oID.value = sValue;
        if (G_set[aID[1] + '_' + aID[2]] && sValue) {
            if (aID[2] == 'from' && parseInt(sValue) > G_set[aID[1] + '_to']) {
                oFrom.value = G_set[aID[1] + '_from'];
                oID.style.color = 'red';
            }
            if (aID[2] == 'to' && parseInt(sValue) < G_set[aID[1] + '_from']) {
                oTo.value = G_set[aID[1] + '_to'];
                oID.style.color = 'red';
            }
        }
    }

    WL_print();
}

function WL_onreset() {
    var aWizard_id = [
        'wizard_price_from',
        'wizard_price_to',
        'wizard_class_theft',
        'wizard_class_fire',
        'wizard_type',
        'wizard_height_from',
        'wizard_height_to',
        'wizard_width_from',
        'wizard_width_to',
        'wizard_depth_from',
        'wizard_depth_to',
        'wizard_weight_from',
        'wizard_weight_to',
        'wizard_producer'
    ];
    var wizard_rubric_theft = document.getElementById('wizard_rubric_theft');
    var wizard_rubric_fire = document.getElementById('wizard_rubric_fire');
    wizard_rubric_theft.disabled = false;
    wizard_rubric_fire.disabled = false;
    wizard_rubric_theft.checked = true;
    wizard_rubric_fire.checked = true;
    for (var i = 0; i < aWizard_id.length; i++) {
        switch (aWizard_id[i]) {
            case 'wizard_class_theft':
                Global_SelectOption('any', 'wizard_class_theft');
                break;
            case 'wizard_class_fire':
                Global_SelectOption('any', 'wizard_class_fire');
                break;
            case 'wizard_type':
                Global_SelectOption('wizard_type_any', 'wizard_type');
                break;
            case 'wizard_producer':
                Global_SelectOption('0', 'wizard_producer');
                break;
            default :
                document.getElementById(aWizard_id[i]).value = '';
                document.getElementById(aWizard_id[i]).style.color = 'gray';
        }
    }
    WL_print();
}

function WL_print() {
    var id, el, i;
    var aPrint = ['m', 'r', 't', 'p', 'theft', 'fire'];
    for (id in oArticle) {
        oArticle[id]['print']['r'] = true;
        if (oArticle[id]['rubric'] == 'both' || document.getElementById('wizard_rubric_' + oArticle[id]['rubric']).checked) {
            oArticle[id]['print']['m'] = true;
            oArticle[id]['print']['t'] = true;
            oArticle[id]['print']['p'] = true;
            oArticle[id]['print']['theft'] = true;
            oArticle[id]['print']['fire']  = true;
        } else {
            oArticle[id]['print']['m'] = false;
            oArticle[id]['print']['t'] = false;
            oArticle[id]['print']['p'] = false;
            oArticle[id]['print']['theft'] = false;
            oArticle[id]['print']['fire']  = false;
        }
        for (i = 0; i < aPrint.length; i++) {
            if (aPrint[i] == 'theft') continue;
            if (oArticle[id]['print'][aPrint[i]]) {
                el = document.getElementById('wizard_class_theft');
                if (el.value != 'any' && (el.value != oArticle[id]['char'][14])) {
                    oArticle[id]['print'][aPrint[i]] = false;
                }
            }
        }
        for (i = 0; i < aPrint.length; i++) {
            if (aPrint[i] == 'fire') continue;
            if (oArticle[id]['print'][aPrint[i]]) {
                el = document.getElementById('wizard_class_fire');
                if (el.value != 'any' && (el.value != oArticle[id]['char'][11])) {
                    oArticle[id]['print'][aPrint[i]] = false;
                }
            }
        }
        WL_print_price(id);
        for (i = 0; i < aPrint.length; i++) {
            if (aPrint[i] == 't') continue;
            if (oArticle[id]['print'][aPrint[i]]) {
                el = document.getElementById('wizard_type');
                if (el.value != 'wizard_type_any' && el.value != oArticle[id]['char'][15]) {
                    oArticle[id]['print'][aPrint[i]] = false;
                }
            }
        }
        WL_print_char(id, 'height', 8);
        WL_print_char(id, 'width', 7);
        WL_print_char(id, 'depth', 9);
        WL_print_char(id, 'weight', 2);
        for (i = 0; i < aPrint.length; i++) {
            if (aPrint[i] == 'p') continue;
            if (oArticle[id]['print'][aPrint[i]]) {
                el = document.getElementById('wizard_producer');
                if (el.value != '0' && el.value != oArticle[id]['producer_id']) {
                    oArticle[id]['print'][aPrint[i]] = false;
                }
            }
        }
    }
    WL_from_to();
}

function WL_print_price(id) {
    var el, i;
    var aPrint = ['m', 'r', 't', 'p', 'theft', 'fire'];
    for (i = 0; i < aPrint.length; i++) {
        if (oArticle[id]['print'][aPrint[i]]) {
            el = document.getElementById('wizard_price_from');
            if (el.value.length > 0 && parseInt(oArticle[id]['price']) < parseInt(el.value)) {
                oArticle[id]['print'][aPrint[i]] = false;
            }
        }
        if (oArticle[id]['print'][aPrint[i]]) {
            el = document.getElementById('wizard_price_to');
            if (el.value.length > 0 && parseInt(oArticle[id]['price']) > parseInt(el.value)) {
                oArticle[id]['print'][aPrint[i]] = false;
            }
        }
    }
}

function WL_print_char(id, subStr, char_id) {
    var el, char_sub, i;
    var aPrint = ['m', 'r', 't', 'p', 'theft', 'fire'];
    for (i = 0; i < aPrint.length; i++) {
        if (oArticle[id]['print'][aPrint[i]] && oArticle[id]['char'][char_id]) {
            el = document.getElementById('wizard_' + subStr + '_from');
            char_sub = oArticle[id]['char'][char_id];
            if (el.value.length > 0 && parseInt(char_sub) < parseInt(el.value)) {
                oArticle[id]['print'][aPrint[i]] = false;
            }
        }
        if (oArticle[id]['print'][aPrint[i]] && oArticle[id]['char'][char_id]) {
            el = document.getElementById('wizard_' + subStr + '_to');
            char_sub = oArticle[id]['char'][char_id];
            if (el.value.length > 0 && parseInt(char_sub) > parseInt(el.value)) {
                oArticle[id]['print'][aPrint[i]] = false;
            }
        }
    }
}

function L_tabs(num, sClass, sStr) {
    var oTD = document.getElementById('wizard_tab' + num);
    var oForm = document.getElementById('wizard_form' + num);
    oTD.innerHTML = '';
    if (sClass == 'current') {
        var oSpan = document.createElement('span');
        oTD.appendChild(oSpan);
        oSpan.innerHTML = sStr;
        oSpan.className = sClass;

        oForm.style.display = 'block';
        oTD.className = 'current';
    } else {
        var oA = document.createElement('a');
        oTD.appendChild(oA);
        oA.href = "javascript:WizardPart('part_" + num + "')";
        oA.className = sClass;
        oA.innerHTML = sStr;

        oForm.style.display = 'none';
        oTD.className = 'next';
    }
}

function WizardPart(part) {
    if (part == 'part_1') {
        L_tabs(1, 'current', 'Основные&nbsp;характеристики');
        L_tabs(2, 'next', 'Дополнительные');
        document.getElementById('wizard_button').value = 'Дополнительные »';
    } else if (part == 'part_2') {
        L_tabs(2, 'current', 'Дополнительные');
        L_tabs(1, 'next', 'Основные&nbsp;характеристики');
        document.getElementById('wizard_button').value = '« Основные';
    }
}

function WizardAdvChars_onclick() {
    if (document.getElementById('wizard_form1').style.display == 'none') {
        WizardPart('part_1');
    } else {
        WizardPart('part_2');
    }
}

