var oWizard_id = {
    'wizard_sections_from' : '',
    'wizard_sections_to'   : '',
    '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 aWizard_id = new Array(
    'wizard_sections_from',
    'wizard_sections_to',
    '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',
    'wizard_char_format'
);

function AjaxRequest(request, funcName) {
    var http_request = false;
    if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            http_request = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (e) {
            try {
                http_request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (e) {}
        }
    }

    if (!http_request) {
        return false;
    }

    http_request.onreadystatechange = function() { OnReadyState(http_request, funcName); };
    http_request.open('POST', '/catalog/xml.php', true);
    http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_request.send(request);
}

function OnReadyState(http_request, funcName) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            eval(funcName + '(http_request.responseXML);');
        }
    }
}

function WizardRequest() {
    var sPost = 'page=wizard&wizard_rubric=' + document.getElementById('wizard_rubric_hidden').value;
    for (var i = 0; i < aWizard_id.length; i++) {
        sPost += '&' + aWizard_id[i] + '=' + document.getElementById(aWizard_id[i] + '_hidden').value;
    }
    AjaxRequest(sPost, 'Wizard_InsertRequest');
}

function L_strByInt(i, str) {
    var aStr = {
        'safe' : ['сейф', 'сейфа', 'сейфов'],
        'box'  : ['шкаф', 'шкафа', 'шкафов'],
        'any'  : ['сейф и шкаф', 'сейфа и шкафа', 'сейфов и шкафов']
    };
    var r;
    var rx = /(\d?(\d))$/;
    var aI = rx.exec(i);
    if (aI[1] >= 5 && aI[1] <= 20) {
        r = '<span style="font-size: 14px;">Найдено</span> ' + i + ' ' + aStr[str][2];
    } else {
        if (aI[2] == 1) {
            r = '<span style="font-size: 14px;">Найден</span> ' + i + ' ' + aStr[str][0];
        } else if (aI[2] >= 2 && aI[2] <= 4) {
            r = '<span style="font-size: 14px;">Найдено</span> ' + i + ' ' + aStr[str][1];
        } else {
            r = '<span style="font-size: 14px;">Найдено</span> ' + i + ' ' + aStr[str][2];
        }
    }
    return r;
}

function Wizard_count(count) {
    var rubric = '';
    var oCount = document.getElementById('wizard_count');
    var oRubric = document.getElementById('wizard_rubric_hidden');
    var oSafe = document.getElementById('wizard_rubric_safe');
    var oBox = document.getElementById('wizard_rubric_box');
    if (oRubric.value == 'any') {
        rubric = L_strByInt(count, 'any');
        oSafe.checked = true;
        oBox.checked = true;
    } else if (oRubric.value == 'safe') {
        rubric = L_strByInt(count, 'safe');
        oSafe.checked = true;
    } else if (oRubric.value == 'box') {
        rubric = L_strByInt(count, 'box');
        oBox.checked = true;
    }
    oCount.innerHTML = rubric;
}

function Wizard_InsertRequest(oXMLDoc) {
    var i, oA, oTD, oSpan;
    var aPairs = new Array('sections',
                           'price',
                           'height',
                           'width',
                           'depth',
                           'weight');
    var oRootNode = oXMLDoc.getElementsByTagName('root').item(0);

    document.getElementById('wizard_rubric_safe').disabled = false;
    document.getElementById('wizard_rubric_box').disabled = false;
    for (i = 0; i < aPairs.length; i++) {
        document.getElementById('wizard_' + aPairs[i] + '_to_td').innerHTML = '';
    }

    if (oRootNode.hasChildNodes()) {
        var oXML_count = oRootNode.getElementsByTagName('count').item(0);
        var oXML_rubric = oRootNode.getElementsByTagName('rubric').item(0);
        var oXML_type = oRootNode.getElementsByTagName('type').item(0);
        var oXML_producer = oRootNode.getElementsByTagName('producer').item(0);
        var oXML_char_format = oRootNode.getElementsByTagName('char_format').item(0);
        if (oXML_count) Wizard_count(oXML_count.firstChild.data);
        if (oXML_rubric && oXML_rubric.firstChild.data == 'false') {
            document.getElementById('wizard_rubric_safe').disabled = true;
            document.getElementById('wizard_rubric_box').disabled = true;
        }
        if (oXML_char_format) {
            if (oXML_char_format.getElementsByTagName('option').length > 0) {
                Wizard_select(oXML_char_format, 'wizard_char_format');
            } else {
                Wizard_select_class('wizard_char_format', 'Формат');
            }
        }
        if (oXML_type) Wizard_select(oXML_type, 'wizard_type');
        if (oXML_producer) Wizard_select(oXML_producer, 'wizard_producer');
        for (i = 0; i < aPairs.length; i++) {
            var oXML_from = oRootNode.getElementsByTagName(aPairs[i] + '_from').item(0);
            var oXML_to   = oRootNode.getElementsByTagName(aPairs[i] + '_to').item(0);
            if (oXML_from && oXML_to) {
                oWizard_id['wizard_' + aPairs[i] + '_from'] = oXML_from.firstChild.data;
                oWizard_id['wizard_' + aPairs[i] + '_to']   = oXML_to.firstChild.data;
                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.innerHTML = G_number_format(oXML_from.firstChild.data);
                oA.href = 'javascript:Wizard_value(\'' + aPairs[i] + '_from\', ' + oXML_from.firstChild.data + ');';
                oA.className = 'wizard_from_to';
                oA.title = 'Вставить минимальное значение';

                oSpan = document.createElement('span');
                oTD.appendChild(oSpan);
                oSpan.innerHTML = ' &mdash; ';

                oA = document.createElement('a');
                oTD.appendChild(oA);
                oA.innerHTML = G_number_format(oXML_to.firstChild.data);
                oA.href = 'javascript:Wizard_value(\'' + aPairs[i] + '_to\', ' + oXML_to.firstChild.data + ');';
                oA.className = 'wizard_from_to';
                oA.title = 'Вставить максимальное значение';

                oSpan = document.createElement('span');
                oTD.appendChild(oSpan);
                oSpan.innerHTML = ')';
            }
        }
    }
    WizardInsert();
}

function Wizard_value(sStr, sValue) {
    document.getElementById('wizard_' + sStr).value = sValue;
    document.getElementById('wizard_' + sStr).style.color = 'black';
    Wizard_event(false);
}

function Wizard_select_class(sID, sText) {
    var oEl = document.getElementById(sID);
    oEl.innerHTML = '';

    var oOption = document.createElement('option');
    oEl.appendChild(oOption);
    oOption.text = sText;
    oOption.value = 'any';
    oOption.selected = true;
    oEl.disabled = true;
}

function Wizard_select(oParent, sID) {
    document.getElementById(sID).innerHTML = '';
    var oXMLOptions = oParent.getElementsByTagName('option');
    for (var i = 0; i < oXMLOptions.length; i++) {
        var oOption = document.createElement('option');
        document.getElementById(sID).appendChild(oOption);
        oOption.text = oXMLOptions.item(i).firstChild.data;
        oOption.value = oXMLOptions.item(i).getAttribute('value');
    }
    document.getElementById(sID).disabled = false;
}

function WizardForm_onreset() {
    document.getElementById('wizard_rubric_hidden').value = 'any';
    for (var i = 0; i < aWizard_id.length; i++) {
        switch (aWizard_id[i]) {
            case 'wizard_char_format':
                document.getElementById('wizard_char_format_hidden').value = 'any';
                break;
            case 'wizard_type':
                document.getElementById('wizard_type_hidden').value = 'wizard_type_any';
                break;
            case 'wizard_producer':
                document.getElementById('wizard_producer_hidden').value = 0;
                break;
            default :
                document.getElementById(aWizard_id[i] + '_hidden').value = '';
                document.getElementById(aWizard_id[i]).style.color = 'gray';
        }
    }
    WizardRequest();
}

function Wizard_sort(sField, sSort) {
    document.getElementById('wizard_sort_field_hidden').value = sField;
    document.getElementById('wizard_sort_hidden').value = (sSort == 'ASC') ? 'DESC' : 'ASC';
    Wizard_event('submit');
}

function Wizard_page(page) {
    document.getElementById('wizard_page').value = page;
    Wizard_event('submit');
}

function Wizard_event(sSubmit) {
    var rxInt = /[^\d]+/g;
    var rxID  = /^wizard_(sections|price|height|width|depth|weight)_(from|to)$/i;
    if (document.getElementById('wizard_rubric_safe').checked && document.getElementById('wizard_rubric_box').checked) {
        document.getElementById('wizard_rubric_hidden').value = 'any';
    } else if (document.getElementById('wizard_rubric_safe').checked) {
        document.getElementById('wizard_rubric_hidden').value = 'safe';
    } else if (document.getElementById('wizard_rubric_box').checked) {
        document.getElementById('wizard_rubric_hidden').value = 'box';
    }

    for (var i = 0; i < aWizard_id.length; i++) {
        var oID = document.getElementById(aWizard_id[i]);
        switch (aWizard_id[i]) {
            case 'wizard_char_format':
                document.getElementById('wizard_char_format_hidden').value = oID.value;
                break;
            case 'wizard_type':
                document.getElementById('wizard_type_hidden').value = oID.value;
                break;
            case 'wizard_producer':
                document.getElementById('wizard_producer_hidden').value = oID.value;
                break;
            default :
                var aID = rxID.exec(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, '')) < oWizard_id['wizard_' + aID[1] + '_from']) {
                        oTo.value = oWizard_id['wizard_' + aID[1] + '_from'];
                        oTo.style.color = 'red';
                    }
                    if (parseInt(oFrom.value.replace(rxInt, '')) > oWizard_id['wizard_' + aID[1] + '_to']) {
                        oFrom.value = oWizard_id['wizard_' + aID[1] + '_to'];
                        oFrom.style.color = 'red';
                    }
                }

                var sValue = oID.value.replace(rxInt, '');
                oID.value = sValue;
                if (sValue == oWizard_id['wizard_' + aID[1] + '_' + aID[2]] && WL_is_gray(oID.style.color)) {
                    sValue = '';
                } else if (sValue == '') {
                    oID.style.color = 'gray';
                    document.getElementById(aWizard_id[i] + '_hidden').value = '';
                } else {
                    document.getElementById(aWizard_id[i] + '_hidden').value = sValue;
                }

                if (oWizard_id['wizard_' + aID[1] + '_' + aID[2]] && sValue) {
                    if (aID[2] == 'from' && parseInt(sValue) > oWizard_id['wizard_' + aID[1] + '_to']) {
                        oFrom.value = oWizard_id['wizard_' + aID[1] + '_from'];
                        oID.style.color = 'red';
                    }
                    if (aID[2] == 'to' && parseInt(sValue) < oWizard_id['wizard_' + aID[1] + '_from']) {
                        oTo.value = oWizard_id['wizard_' + aID[1] + '_to'];
                        oID.style.color = 'red';
                    }
                }
        }
    }

    if (sSubmit == 'submit') {
        document.getElementById('wizard_from').value = GetCookie('order_from');
        document.getElementById('wizard_form_hidden').submit();
    } else {
        WizardRequest();
    }
}

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 == oWizard_id['wizard_' + sID_part + '_from']) && WL_is_gray(oFrom.style.color)) {
        oFrom.value = oWizard_id['wizard_' + sID_part + '_from'];
        sFrom = '';
    } else {
        sFrom = oFrom.value;
    }
    if ((oTo.value == '' || oTo.value == oWizard_id['wizard_' + sID_part + '_to']) && WL_is_gray(oTo.style.color)) {
        oTo.value = oWizard_id['wizard_' + sID_part + '_to'];
        sTo = '';
    } else {
        sTo = oTo.value;
    }

    switch (sID_part) {
        case 'sections': sStr = 'Кол-во секций: '; sStr_post = '';      break;
        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 + 'от ' + sFrom + ' до ' + sTo + sStr_post + '</div>';
    } else if (sFrom.length > 0) {
        sReturn = '<div>' + sStr + 'от ' + sFrom + sStr_post + '</div>';
    } else if (sTo.length > 0) {
        sReturn = '<div>' + sStr + 'до ' + sTo + sStr_post + '</div>';
    }
    return sReturn;
}

function WizardInsert() {
    var sWizardFilled = '';
    var rxWizard = /^wizard_(sections|price|height|width|depth|weight)_(to)$/;
    document.getElementById('wizard_fieldset').style.visibility = 'hidden';

    for (var i = 0; i < aWizard_id.length; i++) {
        switch (aWizard_id[i]) {
            case 'wizard_char_format':
                var oFormat = document.getElementById('wizard_char_format');
                Global_SelectOption(document.getElementById(aWizard_id[i] + '_hidden').value, aWizard_id[i]);
                if (oFormat.value != 'any') {
                    sWizardFilled += '<div>Формат документов: '
                        + oFormat.options[oFormat.selectedIndex].text + '</div>';
                }
                break;
            case 'wizard_producer':
                var oProducer = document.getElementById('wizard_producer');
                Global_SelectOption(document.getElementById(aWizard_id[i] + '_hidden').value, aWizard_id[i]);
                if (oProducer.value != 0) {
                    sWizardFilled += '<div>Торговая марка: '
                        + oProducer.options[oProducer.selectedIndex].text + '</div>';
                }
                break;
            case 'wizard_type':
                var oType = document.getElementById('wizard_type');
                Global_SelectOption(document.getElementById(aWizard_id[i] + '_hidden').value, aWizard_id[i]);
                if (oType.value != 'wizard_type_any') {
                    sWizardFilled += '<div>Тип замка: '
                        + oType.options[oType.selectedIndex].text + '</div>';
                }
                break;
            default :
                var aWizard_exec = rxWizard.exec(aWizard_id[i]);
                document.getElementById(aWizard_id[i]).value
                    = document.getElementById(aWizard_id[i] + '_hidden').value;
                if (aWizard_exec) sWizardFilled += WizardFilled(aWizard_exec[1]);
        }
    }

    document.getElementById('wizard_filled').innerHTML = sWizardFilled;
    document.getElementById('wizard_fieldset').style.visibility = 'visible';
}

function WizardPart(part) {
    if (part == 'part_1') {
        document.getElementById('wizard_tab1').className = 'current';
        document.getElementById('wizard_tab2').className = 'next';
        document.getElementById('wizard_form2').style.display = 'none';
        document.getElementById('wizard_form1').style.display = 'block';
    } else {
        document.getElementById('wizard_tab1').className = 'next';
        document.getElementById('wizard_tab2').className = 'current';
        document.getElementById('wizard_form1').style.display = 'none';
        document.getElementById('wizard_form2').style.display = 'block';
    }
}

function Page_onkeydown(event) {
    var id = false;
    event = event || window.event;
    var key = event.keyCode;
    if(event.ctrlKey) {
        var tag = (event.target || event.srcElement).tagName;
        if(tag != 'INPUT' && tag != 'TEXTAREA') {
            if(key == 37) {
                id = document.getElementById('page_previous');
            }
            if(key == 39) {
                id = document.getElementById('page_next');
            }
            if(id) {
                location.href = id.href;
            }
        }
    }
}

