var imagePath = '/wp-content/themes/oknanew/img2/';
var wwwPath = '/wp-content/themes/oknanew/';

var currWindow = new Array();
var currKomplekt = 2;

// 1 - ширина
// 2 - высота
// 3 - тип створки (1-9)
// 5 - москитная сетка

var leafs = new Array();
leafs[1] = '1gl.jpg'; leafs[2] = '2povr.jpg'; leafs[3] = '3povl.jpg'; leafs[4] = '4povotkr.jpg'; leafs[5] = '5povotkl.jpg';
var leafsCount = 5;

var doors = new Array();
doors[1] = '6dpovr.jpg'; doors[2] = '7dpovl.jpg'; doors[3] = '8dpovotkr.jpg'; doors[4] = '9dpovotkl.jpg';
var doorsCount = 4;


function $(element) {
    element = document.getElementById(element);
    return element;
}

function komplektLoaded(data, id, withParams) {
    //$('winform').innerHTML = data;
    jQuery('#winform').empty();
    jQuery('#winform').prepend(data);
    currWindow = new Array();
    if ((id >=1) && (id <= 4)) {
        for (i = 1; i <= id; i++) {
            currWindow[i] = new Array();
            currWindow[i][1] = ''; currWindow[i][2] = ''; currWindow[i][3] = 1; currWindow[i][5] = 0; currWindow[i][6] = false; currWindow[i][7] = false;
        }
    }
    if (id == 5) {
        currWindow[1] = new Array();
        currWindow[1][1] = ''; currWindow[1][2] = ''; currWindow[1][3] = 6; currWindow[1][5] = 0; currWindow[1][6] = false; currWindow[1][7] = false;
    }
    if (id == 6) {
        currWindow[1] = new Array();
        currWindow[1][1] = ''; currWindow[1][2] = ''; currWindow[1][3] = 1; currWindow[1][5] = 0; currWindow[1][6] = false; currWindow[1][7] = false;
        currWindow[2] = new Array();
        currWindow[2][1] = ''; currWindow[2][2] = ''; currWindow[2][3] = 6; currWindow[2][5] = 0; currWindow[2][6] = false; currWindow[2][7] = false;
    }
    if (id == 7) {
        currWindow[1] = new Array();
        currWindow[1][1] = ''; currWindow[1][2] = ''; currWindow[1][3] = 1; currWindow[1][5] = 0; currWindow[1][6] = false; currWindow[1][7] = false;
        currWindow[2] = new Array();                                                                                                   
        currWindow[2][1] = ''; currWindow[2][2] = ''; currWindow[2][3] = 1; currWindow[2][5] = 0; currWindow[2][6] = false; currWindow[2][7] = false;
        currWindow[3] = new Array();
        currWindow[3][1] = ''; currWindow[3][2] = ''; currWindow[3][3] = 6; currWindow[3][5] = 0; currWindow[3][6] = false; currWindow[3][7] = false;
    }
    if (id == 1 || id == 2) {
        $('ugol').disabled = '';
    } else {
        $('ugol').checked = false;
        $('ugol').disabled = 'disabled';
    }
    $('price').innerHTML = 0;
    $('price2').innerHTML = 0;
    $('iprice').value = 0;
    $('iprice2').value = 0;
    $('submit').disabled = 'disabled';
    // Подстановка значений, переданных по ссылке
    if (withParams) {
        if ((typeof(pstvorka1) != 'undefined') && (typeof(currWindow[1]) != 'undefined')) {
            currWindow[1][3] = pstvorka1;
            if (pstvorka1 <= 5) changeLeaf(1, true);
            else changeDoor(1, true);
        }
        if ((typeof(pstvorka2) != 'undefined') && (typeof(currWindow[2]) != 'undefined')) {
            currWindow[2][3] = pstvorka2;
            if (pstvorka2 <= 5) changeLeaf(2, true);
            else changeDoor(2, true);
        }
        if ((typeof(pstvorka3) != 'undefined') && (typeof(currWindow[3]) != 'undefined')) {
            currWindow[3][3] = pstvorka3;
            if (pstvorka3 <= 5) changeLeaf(3, true);
            else changeDoor(3, true);
        }
        if ((typeof(pstvorka4) != 'undefined') && (typeof(currWindow[4]) != 'undefined')) {
            currWindow[4][3] = pstvorka4;
            if (pstvorka4 <= 5) changeLeaf(4, true);
            else changeDoor(4, true);
        }
        var cont = false;
        if ((typeof(pwidth1) != 'undefined') && (typeof(currWindow[1]) != 'undefined') && ($('width1'))) { currWindow[1][1] = pwidth1; $('width1').value = pwidth1; cont = true; }
        if ((typeof(pwidth2) != 'undefined') && (typeof(currWindow[2]) != 'undefined') && ($('width2'))) { currWindow[2][1] = pwidth1; $('width2').value = pwidth2; cont = true; }
        if ((typeof(pwidth3) != 'undefined') && (typeof(currWindow[3]) != 'undefined') && ($('width3'))) { currWindow[3][1] = pwidth1; $('width3').value = pwidth3; cont = true; }
        if ((typeof(pwidth4) != 'undefined') && (typeof(currWindow[4]) != 'undefined') && ($('width4'))) { currWindow[4][1] = pwidth1; $('width4').value = pwidth4; cont = true; }
        if ((typeof(pheight1) != 'undefined') && (typeof(currWindow[1]) != 'undefined') && ($('height1'))) { currWindow[1][2] = pheight1; $('height1').value = pheight1; cont = true; }
        if ((typeof(pheight2) != 'undefined') && (typeof(currWindow[2]) != 'undefined') && ($('height2'))) { currWindow[2][2] = pheight2; $('height2').value = pheight2; cont = true; }
        if ((typeof(pheight3) != 'undefined') && (typeof(currWindow[3]) != 'undefined') && ($('height3'))) { currWindow[3][2] = pheight3; $('height3').value = pheight3; cont = true; }
        if (cont && $('widthTypeSplit') != null) {
            $('widthTypeSplit').checked = 'checked'; $('widthTypeAll').checked = ''; 
            $('iWidthAll').style.display = 'none'; $('sWidthAll').style.display = '';
            if ($('iWidth1') != null) { $('iWidth1').style.display = ''; $('sWidth1').style.display = 'none'; }
            if ($('iWidth2') != null) { $('iWidth2').style.display = ''; $('sWidth2').style.display = 'none'; }
            if ($('iWidth3') != null) { $('iWidth3').style.display = ''; $('sWidth3').style.display = 'none'; }
            if ($('iWidth4') != null) { $('iWidth4').style.display = ''; $('sWidth4').style.display = 'none'; }
        }
        recalculate();
    }
}

// Изменение комплектации
function changeKomplekt(id, withParams) {
    currKomplekt = id;
    $('komplekt' + id).checked = 'checked';
    jQuery().ajaxStart( function () { jQuery("#loader").show();});
    jQuery().ajaxStop( function () { jQuery("#loader").hide();});
    jQuery.post(wwwPath + 'winforms/komplekt' + id + '.php', function(data) {komplektLoaded(data, id, withParams)});
}

// Смена типа створка
function changeLeaf(id, manualChanged) {
    currType = currWindow[id][3];
    if (!manualChanged) currType++;
    if (currType > leafsCount) currType = 1;
    currWindow[id][3] = currType;
    $('leaf' + id).src = imagePath + leafs[currType];
    //$('sleaf' + id).src = imagePath + 's' + leafs[currType];
    $('ileaf' + id).value = currType;
    currWindow[id][5] = 0; // Снятие флага москитной сетки
    $('moskit' + id).checked = false;
    recalculate();
}

// Смена типа двери
function changeDoor(id, manualChanged) {
    currType = currWindow[id][3] - 5; // offset
    if (!manualChanged) currType++;
    if (currType > doorsCount) currType = 1;
    currWindow[id][3] = currType + 5; // offset
    $('leaf' + id).src = imagePath + doors[currType];
    //$('sleaf' + id).src = imagePath + 's' + doors[currType];
    $('ileaf' + id).value = currType + 5;
    currWindow[id][5] = 0; // Снятие флага москитной сетки
    //$('moskit' + id).checked = false;
    recalculate();
}

// Москитная сетка
function changeMoskit(id) {
    if ($('moskit' + id).checked) {
        if ((currWindow[id][3] == 1) || (currWindow[id][3] >= 6)) {
            alert('Вы можете поставить москитную сетку только на открывающиеся створки окна');
            $('moskit' + id).checked = false;
            currWindow[id][5] = 0;
        }
        else {
            currWindow[id][5] = 1;
        }
    }
    else
        currWindow[id][5] = 0;
    recalculate();
}


// Упросщенная версия калькулятора, для страницы популярных заказов
function calcsimple(currKomplekt, currWindow, profilSel, steklopaketSel, montazhSel) {
    var total = 0;
    var profil = (profilSel - 1) * 2 + steklopaketSel;
    
    if (currKomplekt == 1) {
        if (currWindow[1][6] || currWindow[1][7]) var sum1 = 0;
        else {
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (montazhSel) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
        }
        total = sum1;
    }
    if (currKomplekt == 2) {
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7]) var sum1 = 0;
        else {
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (montazhSel) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7]) var sum2 = 0;
        else {
            var sum2 = calcStvorka(currWindow[2][1], currWindow[2][2], currWindow[2][3], profil); // Stvorka 2
            if (montazhSel) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * montazh);
        }
        total = sum1 + sum2;
        if (total > 0) {
            total = total - (currWindow[1][2] / 1000) * komplekt2_splicing[profilSel];
        }
    }
    if (currKomplekt == 3) {
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum1 = 0;
        else {
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (montazhSel) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum2 = 0;
        else {
            var sum2 = calcStvorka(currWindow[2][1], currWindow[2][2], currWindow[2][3], profil); // Stvorka 2
            if (montazhSel) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * montazh);
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum3 = 0;
        else {
            var sum3 = calcStvorka(currWindow[3][1], currWindow[3][2], currWindow[3][3], profil); // Stvorka 3
            if (montazhSel) sum3 = sum3 + (currWindow[3][1] * currWindow[3][2] / 1000000 * montazh);
        }
        total = sum1 + sum2 + sum3;
        if (total > 0) {
            total = total - (currWindow[1][2] / 1000) * komplekt3_splicing[profilSel] * 2;
        }
    }
    if (currKomplekt == 4) {
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7] || currWindow[4][6] || currWindow[4][7]) var sum1 = 0;
        else {
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (montazhSel) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7] || currWindow[4][6] || currWindow[4][7]) var sum2 = 0;
        else {
            var sum2 = calcStvorka(currWindow[2][1], currWindow[2][2], currWindow[2][3], profil); // Stvorka 2
            if (montazhSel) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * montazh);
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7] || currWindow[4][6] || currWindow[4][7]) var sum3 = 0;
        else {
            var sum3 = calcStvorka(currWindow[3][1], currWindow[3][2], currWindow[3][3], profil); // Stvorka 3
            if (montazhSel) sum3 = sum3 + (currWindow[3][1] * currWindow[3][2] / 1000000 * montazh);
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7] || currWindow[4][6] || currWindow[4][7]) var sum4 = 0;
        else {
            var sum4 = calcStvorka(currWindow[4][1], currWindow[4][2], currWindow[4][3], profil); // Stvorka 4
            if (montazhSel) sum4 = sum4 + (currWindow[4][1] * currWindow[4][2] / 1000000 * montazh);
        }
        total = sum1 + sum2 + sum3 + sum4;
        if (total > 0) {
            total = total - (currWindow[1][2] / 1000) * komplekt4_splicing[profilSel] * 3;
        }
    }
    if (currKomplekt == 5) {
        if (currWindow[1][6] || currWindow[1][7]) var sum1 = 0;
        else {
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (montazhSel) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
        }
        total = sum1;
    }
    if (currKomplekt == 6) {
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7]) var sum1 = 0;
        else {
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (montazhSel) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7]) var sum2 = 0;
        else {
            var sum2 = calcStvorka(currWindow[2][1], currWindow[2][2], currWindow[2][3], profil); // Stvorka 1
            if (montazhSel) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * montazh);
        }
        total = sum1 + sum2;
        if (total > 0) {
            total = total + (currWindow[1][2] / 1000) * 5; // Стык, +5 на каждый метр
        }
    }
    if (currKomplekt == 7) {
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum1 = 0;
        else {
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (montazhSel) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum2 = 0;
        else {
            var sum2 = calcStvorka(currWindow[2][1], currWindow[2][2], currWindow[2][3], profil); // Stvorka 2
            if (montazhSel) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * montazh);
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum3 = 0;
        else {
            var sum3 = calcStvorka(currWindow[3][1], currWindow[3][2], currWindow[3][3], profil); // Stvorka 3
            if (montazhSel) sum3 = sum3 + (currWindow[3][1] * currWindow[3][2] / 1000000 * montazh);
        }
        total = sum1 + sum2 + sum3;
        if (total > 0) {
            total = total - (currWindow[1][2] / 1000) * komplekt2_splicing[profilSel]; // Стык окон
            total = total + (currWindow[1][2] / 1000) * 5; // Стык дверь, +5 на каждый метр
        }
    }
    
    total = Math.round(total);
    total = Math.round(total * exchange);
    return total.toString();
}


// Пересчет суммы
function recalculate() {
    var total = 0;
    profilSel = parseInt($('profil').value);
    steklopaketSel = parseInt($('steklopaket').value);
    var profil = (profilSel - 1) * 2 + steklopaketSel;
    var podokonnikSel = parseInt($('podokonnik').value);
    var otlivSel = parseInt($('otliv').value);
    var kozirekSel = parseInt($('kozirek').value);
    var inCart = true; // Флаг, показывающий возможность заказа с нулевой ценой
    if (currKomplekt == 1) {
        currWindow[1][1] = checkWidth($('width1').value, currWindow[1][3], 1);
        currWindow[1][2] = checkHeight($('height1').value, currWindow[1][3], 1);

        if (currWindow[1][6] || currWindow[1][7]) var sum1 = 0;
        else {
            inCart = false;
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (currWindow[1][5] == 1) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
            if ((currWindow[1][3] != 1) && ($('furnitura').value == 2)) sum1 = sum1 + furnitura;
        }
        total = sum1;
        if (total && $('ugol').checked) total = total + (currWindow[1][2] / 1000 * ugol[profilSel]);
        if (total && ($('otkos').value != 0)) total = total + otkos[$('otkos').value][1];
    }
    if (currKomplekt == 2) {
        if ($('widthTypeAll').checked) {
            $('width1').value = intval(intval($('widthAll').value) / 2); jQuery('#sWidth1').text($('width1').value + ' мм');
            $('width2').value = intval($('widthAll').value)  - intval($('width1').value); jQuery('#sWidth2').text($('width2').value + ' мм');
        } else {
            $('widthAll').value = intval($('width1').value) + intval($('width2').value); jQuery('#sWidthAll').text($('widthAll').value + ' мм');
        }
    
        currWindow[1][1] = checkWidth($('width1').value, currWindow[1][3], 1);
        currWindow[1][2] = checkHeight($('height1').value, currWindow[1][3], 1);
        currWindow[2][1] = checkWidth($('width2').value, currWindow[2][3], 2);
        currWindow[2][2] = currWindow[1][2]; currWindow[2][7] = currWindow[1][7];
        
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7]) var sum1 = 0;
        else {
            inCart = false;
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (currWindow[1][5] == 1) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
            if ((currWindow[1][3] != 1) && ($('furnitura').value == 2)) sum1 = sum1 + furnitura;
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7]) var sum2 = 0;
        else {
            inCart = false;
            var sum2 = calcStvorka(currWindow[2][1], currWindow[2][2], currWindow[2][3], profil); // Stvorka 2
            if (currWindow[2][5] == 1) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * montazh);
            if ((currWindow[2][3] != 1) && ($('furnitura').value == 2)) sum2 = sum2 + furnitura;
        }
        
        total = sum1 + sum2;
        if (total > 0) {
            if ((profil == 1) || (profil == 2)) {
                total = total - 8.84 * currWindow[1][2] / 1000;
            } else if ((profil == 5) || (profil == 6)) {
                total = total - 5.46 * currWindow[1][2] / 1000;
            } else {
                total = total - (currWindow[1][2] / 1000) * komplekt2_splicing[profilSel];
            }
            if ($('ugol').checked) total = total + (currWindow[1][2] / 1000 * ugol[profilSel]);
            if ($('otkos').value != 0) total = total + otkos[$('otkos').value][2];
        }
    }
    if (currKomplekt == 3) {
        if ($('widthTypeAll').checked) {
            $('width1').value = intval(intval($('widthAll').value) / 3); jQuery('#sWidth1').text($('width1').value + ' мм');
            $('width2').value = intval(intval($('widthAll').value) / 3); jQuery('#sWidth2').text($('width2').value + ' мм');
            $('width3').value = intval($('widthAll').value)  - intval($('width1').value) - intval($('width2').value); jQuery('#sWidth3').text($('width3').value + ' мм');
        } else {
            $('widthAll').value = intval($('width1').value) + intval($('width2').value) + intval($('width3').value); jQuery('#sWidthAll').text($('widthAll').value + ' мм');
        }
        
        currWindow[1][1] = checkWidth($('width1').value, currWindow[1][3], 1);
        currWindow[1][2] = checkHeight($('height1').value, currWindow[1][3], 1);
        currWindow[2][1] = checkWidth($('width2').value, currWindow[2][3], 2);
        currWindow[2][2] = currWindow[1][2]; currWindow[2][7] = currWindow[1][7];
        currWindow[3][1] = checkWidth($('width3').value, currWindow[3][3], 3);
        currWindow[3][2] = currWindow[1][2]; currWindow[3][7] = currWindow[1][7];
        
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum1 = 0;
        else {
            inCart = false;
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (currWindow[1][5] == 1) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
            if ((currWindow[1][3] != 1) && ($('furnitura').value == 2)) sum1 = sum1 + furnitura;
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum2 = 0;
        else {
            inCart = false;
            var sum2 = calcStvorka(currWindow[2][1], currWindow[2][2], currWindow[2][3], profil); // Stvorka 2
            if (currWindow[2][5] == 1) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * montazh);
            if ((currWindow[2][3] != 1) && ($('furnitura').value == 2)) sum2 = sum2 + furnitura;
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum3 = 0;
        else {
            inCart = false;
            var sum3 = calcStvorka(currWindow[3][1], currWindow[3][2], currWindow[3][3], profil); // Stvorka 3
            if (currWindow[3][5] == 1) sum3 = sum3 + (currWindow[3][1] * currWindow[3][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum3 = sum3 + (currWindow[3][1] * currWindow[3][2] / 1000000 * montazh);
            if ((currWindow[3][3] != 1) && ($('furnitura').value == 2)) sum3 = sum3 + furnitura;
        }

        total = sum1 + sum2 + sum3;
        if (total > 0) {
            if ((profil == 1) || (profil == 2)) {
                total = total - 8.84 * 2 * currWindow[1][2] / 1000;
            } else if ((profil == 5) || (profil == 6)) {
                total = total - 5.46 * 2 * currWindow[1][2] / 1000;
            } else {
                total = total - (currWindow[1][2] / 1000) * komplekt3_splicing[profilSel] * 2;
            }
            if ($('otkos').value != 0) total = total + otkos[$('otkos').value][3];
        }
    }
    if (currKomplekt == 4) {
        if ($('widthTypeAll').checked) {
            $('width1').value = intval(intval($('widthAll').value) / 4); jQuery('#sWidth1').text($('width1').value + ' мм');
            $('width2').value = intval(intval($('widthAll').value) / 4); jQuery('#sWidth2').text($('width2').value + ' мм');
            $('width3').value = intval(intval($('widthAll').value) / 4); jQuery('#sWidth3').text($('width3').value + ' мм');
            $('width4').value = intval($('widthAll').value)  - intval($('width1').value) - intval($('width2').value) - intval($('width3').value); jQuery('#sWidth4').text($('width4').value + ' мм');
        } else {
            $('widthAll').value = intval($('width1').value) + intval($('width2').value) + intval($('width3').value) + intval($('width4').value); jQuery('#sWidthAll').text($('widthAll').value + ' мм');
        }
        
        currWindow[1][1] = checkWidth($('width1').value, currWindow[1][3], 1);
        currWindow[1][2] = checkHeight($('height1').value, currWindow[1][3], 1);
        currWindow[2][1] = checkWidth($('width2').value, currWindow[2][3], 2);
        currWindow[2][2] = currWindow[1][2]; currWindow[2][7] = currWindow[1][7];
        currWindow[3][1] = checkWidth($('width3').value, currWindow[3][3], 3);
        currWindow[3][2] = currWindow[1][2]; currWindow[3][7] = currWindow[1][7];
        currWindow[4][1] = checkWidth($('width4').value, currWindow[4][3], 4);
        currWindow[4][2] = currWindow[1][2]; currWindow[4][7] = currWindow[1][7];
        
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7] || currWindow[4][6] || currWindow[4][7]) var sum1 = 0;
        else {
            inCart = false;
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (currWindow[1][5] == 1) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
            if ((currWindow[1][3] != 1) && ($('furnitura').value == 2)) sum1 = sum1 + furnitura;
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7] || currWindow[4][6] || currWindow[4][7]) var sum2 = 0;
        else {
            inCart = false;
            var sum2 = calcStvorka(currWindow[2][1], currWindow[2][2], currWindow[2][3], profil); // Stvorka 2
            if (currWindow[2][5] == 1) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * montazh);
            if ((currWindow[2][3] != 1) && ($('furnitura').value == 2)) sum2 = sum2 + furnitura;
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7] || currWindow[4][6] || currWindow[4][7]) var sum3 = 0;
        else {
            inCart = false;
            var sum3 = calcStvorka(currWindow[3][1], currWindow[3][2], currWindow[3][3], profil); // Stvorka 3
            if (currWindow[3][5] == 1) sum3 = sum3 + (currWindow[3][1] * currWindow[3][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum3 = sum3 + (currWindow[3][1] * currWindow[3][2] / 1000000 * montazh);
            if ((currWindow[3][3] != 1) && ($('furnitura').value == 2)) sum3 = sum3 + furnitura;
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7] || currWindow[4][6] || currWindow[4][7]) var sum4 = 0;
        else {
            inCart = false;
            var sum4 = calcStvorka(currWindow[4][1], currWindow[4][2], currWindow[4][3], profil); // Stvorka 4
            if (currWindow[4][5] == 1) sum4 = sum4 + (currWindow[4][1] * currWindow[4][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum4 = sum4 + (currWindow[4][1] * currWindow[4][2] / 1000000 * montazh);
            if ((currWindow[4][3] != 1) && ($('furnitura').value == 2)) sum4 = sum4 + furnitura;
        }

        total = sum1 + sum2 + sum3 + sum4;
        if (total > 0) {
            if ((profil == 1) || (profil == 2)) {
                total = total - 8.84 * 3 * currWindow[1][2] / 1000;
            } else if ((profil == 5) || (profil == 6)) {
                total = total - 5.46 * 3 * currWindow[1][2] / 1000;
            } else {
                total = total - (currWindow[1][2] / 1000) * komplekt4_splicing[profilSel] * 3;
            }
            if ($('otkos').value != 0) total = total + otkos[$('otkos').value][3];
        }
    }
    if (currKomplekt == 5) {
        currWindow[1][1] = checkWidth($('width1').value, currWindow[1][3], 1); // width
        currWindow[1][2] = checkHeight($('height1').value, currWindow[1][3], 1); // height

        if (currWindow[1][6] || currWindow[1][7]) var sum1 = 0;
        else {
            inCart = false;
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (currWindow[1][5] == 1) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
            if ((currWindow[1][3] != 1) && ($('furnitura').value == 2)) sum1 = sum1 + furnitura;
        }
        total = sum1;
        if ($('otkos').value != 0) total = total + otkos[$('otkos').value][4];
    }
    if (currKomplekt == 6) {
        currWindow[1][1] = checkWidth($('width1').value, currWindow[1][3], 1); // win
        currWindow[1][2] = checkHeight($('height1').value, currWindow[1][3], 1); // win
        currWindow[2][1] = checkWidth($('width2').value, currWindow[2][3], 2); // door
        currWindow[2][2] = checkHeight($('height2').value, currWindow[2][3], 2); // door
                                                
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7]) var sum1 = 0;
        else {
            inCart = false;
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (currWindow[1][5] == 1) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
            if ((currWindow[1][3] != 1) && ($('furnitura').value == 2)) sum1 = sum1 + furnitura;
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7]) var sum2 = 0;
        else {
            inCart = false;
            var sum2 = calcStvorka(currWindow[2][1], currWindow[2][2], currWindow[2][3], profil); // Stvorka 1
            if (currWindow[2][5] == 1) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * montazh);
            if ((currWindow[2][3] != 1) && ($('furnitura').value == 2)) sum2 = sum2 + furnitura;
        }

        total = sum1 + sum2;
        if (total > 0) {
            total = total + (currWindow[1][2] / 1000) * 5; // Стык, +5 на каждый метр
            if ($('otkos').value != 0) total = total + otkos[$('otkos').value][4];
        }
    }
    if (currKomplekt == 7) {
        if ($('widthTypeAll').checked) {
            $('width1').value = intval(intval($('widthAll').value) / 2); jQuery('#sWidth1').text($('width1').value + ' мм');
            $('width2').value = intval($('widthAll').value)  - intval($('width1').value); jQuery('#sWidth2').text($('width2').value + ' мм');
        } else {
            $('widthAll').value = intval($('width1').value) + intval($('width2').value); jQuery('#sWidthAll').text($('widthAll').value + ' мм');
        }
    
        currWindow[1][1] = checkWidth($('width1').value, currWindow[1][3], 1); // win
        currWindow[1][2] = checkHeight($('height1').value, currWindow[1][3], 1); // win
        currWindow[2][1] = checkWidth($('width2').value, currWindow[2][3], 2); // win
        currWindow[2][2] = currWindow[1][2]; currWindow[2][7] = currWindow[1][7]; // win
        currWindow[3][1] = checkWidth($('width3').value, currWindow[3][3], 3); // door
        currWindow[3][2] = checkHeight($('height3').value, currWindow[3][3], 3); // door

        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum1 = 0;
        else {
            inCart = false;
            var sum1 = calcStvorka(currWindow[1][1], currWindow[1][2], currWindow[1][3], profil); // Stvorka 1
            if (currWindow[1][5] == 1) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum1 = sum1 + (currWindow[1][1] * currWindow[1][2] / 1000000 * montazh);
            if ((currWindow[1][3] != 1) && ($('furnitura').value == 2)) sum1 = sum1 + furnitura;
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum2 = 0;
        else {
            inCart = false;
            var sum2 = calcStvorka(currWindow[2][1], currWindow[2][2], currWindow[2][3], profil); // Stvorka 2
            if (currWindow[2][5] == 1) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum2 = sum2 + (currWindow[2][1] * currWindow[2][2] / 1000000 * montazh);
            if ((currWindow[2][3] != 1) && ($('furnitura').value == 2)) sum2 = sum2 + furnitura;
        }
        if (currWindow[1][6] || currWindow[1][7] || currWindow[2][6] || currWindow[2][7] || currWindow[3][6] || currWindow[3][7]) var sum3 = 0;
        else {
            inCart = false;
            var sum3 = calcStvorka(currWindow[3][1], currWindow[3][2], currWindow[3][3], profil); // Stvorka 3
            if (currWindow[3][5] == 1) sum3 = sum3 + (currWindow[3][1] * currWindow[3][2] / 1000000 * moskitka);
            if ($('montazh').checked) sum3 = sum3 + (currWindow[3][1] * currWindow[3][2] / 1000000 * montazh);
            if ((currWindow[3][3] != 1) && ($('furnitura').value == 2)) sum3 = sum3 + furnitura;
        }

        total = sum1 + sum2 + sum3;
        if (total > 0) {
            if ((profil == 1) || (profil == 2)) {
                total = total - 8.84 * currWindow[1][2] / 1000;
            } else if ((profil == 5) || (profil == 6)) {
                total = total - 5.46 * currWindow[1][2] / 1000;
            } else {
                total = total - (currWindow[1][2] / 1000) * komplekt2_splicing[profilSel]; // Стык окон
            }
            total = total + (currWindow[1][2] / 1000) * 5; // Стык дверь, +5 на каждый метр
            if ($('otkos').value != 0) total = total + otkos[$('otkos').value][4];
        }
    }

    var totalWidth = 0.0;
    for (i = 1; i < currWindow.length; i++) {
        totalWidth = totalWidth + (currWindow[i][1] / 1000.0);
    }
    
    if (total > 0) {
        if (podokonnikSel > 0) total = total + totalWidth * podokonnik[podokonnikSel];
        if (otlivSel > 0) total = total + totalWidth * otliv[otlivSel];
        if (kozirekSel > 0) total = total + totalWidth * kozirek[kozirekSel];
    
        if (discount != 0) total = total - total * discount / 100;
    }
    
    total = Math.round(total);
    total2 = Math.round(total * exchange);
    $('price').innerHTML = total.toString();
    $('price2').innerHTML = total2.toString();
    $('iprice').value = total.toString();
    $('iprice2').value = total2.toString();
    if ((total == 0) && !inCart)
        $('submit').disabled = 'disabled';
    else
        $('submit').disabled = '';
}

function makeInt(val) {
    if (val == '') return 0;
    else return parseInt(val);
}

// Проверка размеров на допустимые значения
// id - номер створки
function checkWidth(width, type, id) {
    currWindow[id][6] = false;
    $('width' + id + '_error').innerHTML = '';
    width = makeInt(width);
    if (width == 0) {
        if (type <= 5)
            $('width' + id + '_error').innerHTML = 'Не задано значение ширины створки';
        else
            $('width' + id + '_error').innerHTML = 'Не задано значение ширины двери';
        return 0;
    }
    if (type == 1) {
        if ((width < minWidthG) || (width > maxWidthG)) {
            $('width' + id + '_error').innerHTML = 'Возможный диапазон от '+ minWidthG +' до '+ maxWidthG +' мм';
            currWindow[id][6] = true;
            return width;
        }
    }
    else if ((type >= 2) && (type <= 5)) {
        if ((width < minWidthPO) || (width > maxWidthPO)) {
            $('width' + id + '_error').innerHTML = 'Возможный диапазон от '+ minWidthPO +' до '+ maxWidthPO +' мм';
            currWindow[id][6] = true;
            return width;
        }
    }
    else {
        if ((width < minWidthD) || (width > maxWidthD)) {
            $('width' + id + '_error').innerHTML = 'Возможный диапазон от '+ minWidthD +' до '+ maxWidthD +' мм';
            currWindow[id][6] = true;
            return width;
        }
    }
    return width;
}
function checkHeight(height, type, id) {
    currWindow[id][7] = false;
    $('height' + id + '_error').innerHTML = '';
    height = makeInt(height);
    if (height == 0) {
        if (type <= 5)
            $('height' + id + '_error').innerHTML = 'Не задано значение высоты конструкции';
        else
            $('height' + id + '_error').innerHTML = 'Не задано значение высоты двери';
        return 0;
    }
    if (type == 1) {
        if ((height < minHeightG) || (height > maxHeightG)) {
            $('height' + id + '_error').innerHTML = 'Возможный диапазон от '+ minHeightG +' до '+ maxHeightG +' мм';
            currWindow[id][7] = true;
            return height;
        }
    }
    else if ((type >= 2) && (type <= 5)) {
        if ((height < minHeightPO) || (height > maxHeightPO)) {
            $('height' + id + '_error').innerHTML = 'Возможный диапазон от '+ minHeightPO +' до '+ maxHeightPO +' мм';
            currWindow[id][7] = true;
            return height;
        }
    }
    else {
        if ((height < minHeightD) || (height > maxHeightD)) {
            $('height' + id + '_error').innerHTML = 'Возможный диапазон от '+ minHeightD +' до '+ maxHeightD +' мм';
            currWindow[id][7] = true;
            return height;
        }
    }
    return height;
}

// Подсчет стоимости одной створки (окно и дверь)
// type - тип створки (1..5 для окон и 6..7 для дверей)
// profil = 1...6
function calcStvorka(width, height, type, profil) {
    if ((profil == 1) || (profil == 2)) { width = width + 29; }
    if ((profil == 5) || (profil == 6)) { width = width + 27; }
    var S = width * height / 1000000; // Площать в м2
    if (S == 0) return 0;
    var sum = 0;
    var prices = new Array();
    var Ss = new Array();
    if (type == 1) {
        for (i = 1; i <= 5; i++) {
            prices[i] = pricesLeafG[i][profil];
            Ss[i] = pricesLeafG[0][i];
        }
    }
    else if ((type >= 2) && (type <= 5)) {
        for (i = 1; i <= 5; i++) {
            prices[i] = pricesLeafPO[i][profil];
            Ss[i] = pricesLeafPO[0][i];
        }
    }
    else {
        for (i = 1; i <= 5; i++) {
            prices[i] = pricesLeafD[i][profil];
            Ss[i] = pricesLeafD[0][i];
        }
    }
    // Интерполяция и вычисление цены
    var price_prev = prices[1];
    var price_curr = 0;
    for (i = 2; i <= 5; i++) {
        price_curr = prices[i];
        if (S <= Ss[i]) {
            Ai = (price_curr - price_prev) / (Ss[i] - Ss[i-1]);
            Bi = price_curr - Ai * Ss[i];
            sum = Ai * S + Bi;
            break;
        }
        else {
            price_prev = price_curr;
        }
    }
    if (sum && ((type == 2) || (type == 3) || (type == 6) || (type == 7))) sum = sum - 12; // Поправка для чисто поворотной створки
    return sum;
}

function intval(mixed_var) {
    base = 10;
    // Get the integer value of a variable using the optional base for the conversion  
    // 
    // version: 910.813
    // discuss at: http://phpjs.org/functions/intval
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: stensi
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Matteo
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0
    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
    // *     example 4: intval('09');
    // *     returns 4: 9
    // *     example 5: intval('1e', 16);
    // *     returns 5: 30
    var tmp;

    var type = typeof( mixed_var );

    if (type === 'boolean') {
        return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {
        return Math.floor(mixed_var);
    } else {
        return 0;
    }
}
