﻿// JScript File
var req = null;

var count_dors;
var width;
var sist_price = 0;
var prev_price = 0;

var count_mirrors = 1;
var mirrors_price = 0;
var chb_mirrors = null;

var count_side = 1;
var side_price = 0;
var chb_side = null;

var count_falsSide = 1;
var falsSide_price = 0;
var chb_falsSide = null;

var count_rolling = 1;
var rolling_price = 0;
var chb_rolling = null;

var chb_falsTop = null;
var chb_buttom = null;
var chb_light = null;
var chb_korpus = null;

var korpusPriceMin = 0;
var korpusPriceMax = 0;
var countKorpusWidthParts = 9; //количество шкафов с шириной для корпусного
var currentWidthNumber = 0;     //порядковый номер ширины

var sp_price;

var arr_width = new Array(31);
var arr_dors = new Array(31);
var arr_price = new Array(31);
var arr_params = new Array(); // ID, price
var arr_sist = new Array(); //ID, price
var arr_konstr = new Array(); //ID, price

var prev_rb;

window.onload = function()
{
    sp_price = document.getElementById("sp_price");
    chb_mirrors = document.getElementById("chb_mirrors");
    chb_side = document.getElementById("chb_side");
    chb_falsSide = document.getElementById("chb_falsSide");
    chb_falsTop = document.getElementById("chb_falsTop");
    chb_buttom = document.getElementById("chb_buttom");
    chb_rolling = document.getElementById("chb_rolling");
    chb_light = document.getElementById("chb_light");
    chb_korpus = document.getElementById("chb_korpus");
    
    parsePrice(document.getElementById("sp_PricesStr").innerHTML);
    DiactivateMirrors();
    //ActivateCountMirrors();
    //ActivateCountSides();
    DiactivateSides();
    DiactivateFalsSides();
}

function parsePrice(str)
{
    var arr_wdp = str.split('@')[0].split(';');
    var s="";
    for(var i = 0; i < arr_wdp.length - 1; i++)
    {
        s = arr_wdp[i].split(',');
        arr_width[i] = s[0]; arr_dors[i] = s[1]; arr_price[i] = s[2];
    }
    width = arr_width[0];
    count_dors = arr_dors[0];
    
    arr_params = str.split('@')[1].split(';');
    arr_sist = str.split("@")[2].split(';');
    
    var rb;
    for(var i=0;i<arr_sist.length-1;i++)
    {
        rb = document.getElementById("rb" + arr_sist[i].split(',')[0]);
        if(i==0) clickrb(rb);
        else rb.checked = false;
    }
    
    mirrors_price = parseInt(str.split("mirror=")[1]);
    side_price = parseInt(str.split("side=")[1]);
    falsSide_price = parseInt(str.split("falsSide=")[1]);
    
    arr_konstr = str.split('@')[6].split(';');
    korpusPriceMin = parseInt(arr_konstr[9].split(",")[1]);
    korpusPriceMax = parseInt(arr_konstr[10].split(",")[1]);
    
    rolling_price = parseInt(str.split("rolling=")[1]);
    
    price();
}

//===============================================================================
function show(a)
    {
        a.className = "on";
    
        var n = a.id.split('a')[1];
        var div = document.getElementById("div" + n);
        
        if(div) div.style.visibility = "visible";
    }
function hiden(a, e)
    {
        var a_save = a;
        var n = a.id.split('a')[1];
        var div = document.getElementById("div" + n);
        
        var aWidth = a.offsetWidth;
        var aHeight = a.offsetHeight;
    
        var aX = 0;
        var aY = 0;

        while (a)
        {
            aX += a.offsetLeft;
            aY += a.offsetTop;
            a = a.offsetParent;
        }
        
        if (e.pageX || e.pageY)
        {
            mouseX = e.pageX;
            mouseY = e.pageY;
        }
        else if (e.clientX || e.clientY)
        {
            mouseX = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
            mouseY = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
        }
        
        if ( (aX + 1) > mouseX || mouseY > (aY + aHeight - 1) || (aY + 1) > mouseY)
        {
            a_save.className = "";
            if(div) div.style.visibility = "hidden";
        }
    }
    function hiden_div(div, e)
    {
        var div_save = div;
        
        var divWidth = div.offsetWidth;
        var divHeight = div.offsetHeight;
        
        var divX = 0;
        var divY = 0;

        while (div)
        {
            divX += div.offsetLeft;
            divY += div.offsetTop;
            div = div.offsetParent;
        }

        if (e.pageX || e.pageY)
        {
            mouseX = e.pageX;
            mouseY = e.pageY;
        }
        else if (e.clientX || e.clientY)
        {
            mouseX = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
            mouseY = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
        }
        
        if ( (divX + 2) > mouseX || mouseX > (divX + divWidth - 1) || (divY + 1) > mouseY || mouseY > (divY + divHeight - 1))
        {
            var n = div_save.id.split('div')[1];
            var a = document.getElementById("a" + n);
            a.className = "";
            if(div_save) div_save.style.visibility = "hidden";
        }
    }
    function ona(span)
    {
        if(span.className != "anactive" && span.className != "active")
        {
            span.className = "on";
        }
    }
    function outa(span)
    {
        if(span.className != "anactive" && span.className != "active")
        {
            span.className = "";
        }
    }
    function clicka(span)
    {
        var div = span.parentNode.parentNode.parentNode.parentNode.parentNode;
        var n = div.id.split('div')[1];
        if(n == 1)
        {
            if(span.className != "anactive" && span.className != "active")
            {
                span.className = "active";
                document.getElementById("span" + count_dors + "_dor").className = "";
                
                price("-");
                
                if(chb_falsTop.checked) { FalsTop("-"); }
                if(chb_buttom.checked) { Buttom("-"); }
                if(chb_light.checked) { Light("-"); }
                
                count_dors = span.firstChild.nodeValue;
                
                if(chb_falsTop.checked) { FalsTop(); }
                if(chb_buttom.checked) { Buttom(); }
                if(chb_light.checked) { Light(); }
            }
            else return;
        }
        else
        if(n == 3)
        {
            if(span.className != "anactive" && span.className != "active")
            {
                price("-");
                count_mirrors = span.firstChild.nodeValue;
            }
            else return;
        }
        else
        if(n == 4)
        {
            if(span.className != "anactive" && span.className != "active")
            {
                price("-");
                count_side = span.firstChild.nodeValue;
            }
            else return;
        }
        else
        if(n == 5)
        {
            if(span.className != "anactive" && span.className != "active")
            {
                price("-");
                count_falsSide = span.firstChild.nodeValue;
            }
            else return;
        }
        else 
        {
            price("-");
                       
            var a2 = document.getElementById("a" + n);
            width = a2.firstChild.nodeValue = span.firstChild.nodeValue;
                    
            var span2 = document.getElementById("span2_dor");
            var span3 = document.getElementById("span3_dor");
            var span4 = document.getElementById("span4_dor");
            
            var s2="anactive", s3="anactive", s4="anactive";
            for(var i=0;i<arr_width.length;i++)
            {
                if(arr_width[i] == width)
                {
                    if(arr_dors[i] == "2") s2 = "";
                    if(arr_dors[i] == "3") s3 = "";
                    if(arr_dors[i] == "4") s4 = "";
                }
            }
            span2.className = s2; span3.className = s3; span4.className = s4;
            if(eval("span" + count_dors).className == "anactive")
            {
                if(chb_falsTop.checked) { FalsTop("-"); }
                if(chb_buttom.checked) { Buttom("-"); }
                if(chb_light.checked) { Light("-"); }
            
                if(s2 == "") { span2.className = "active"; count_dors = "2"; }
                else if(s3 == "") { span3.className = "active"; count_dors = "3"; }
                else if(s4 == "") { span4.className = "active"; count_dors = "4"; }
                
                if(chb_falsTop.checked) { FalsTop(); }
                if(chb_buttom.checked) { Buttom(); }
                if(chb_light.checked) { Light(); }
            }
            else if(eval("span" + count_dors).className == "") eval("span" + count_dors).className = "active";
            div.style.visibility = "hidden";
            a2.className = "";
        }
        
        if(count_dors < count_mirrors) count_mirrors = count_dors;
        if(chb_mirrors.checked) ActivateCountMirrors();
        if(chb_side.checked) ActivateCountSides();
        if(chb_falsSide.checked) ActivateCountFalsSides();
        dors_width();
        if(chb_korpus.checked) { Korpus("-"); };
        price();
        if(chb_korpus.checked) { Korpus(); };
    }
    function clickchb(chb)
    {
        var n = chb.id.split("chb")[1];
        for(var i=0;i<arr_params.length;i++)
        {
            if(arr_params[i].split(',')[0] == n)
            {
                if(chb.checked) sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) + parseInt(arr_params[i].split(',')[1]);
                else sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - parseInt(arr_params[i].split(',')[1]);
                break;
            }
        }
    }
    function clickrb(rb)
    {
        if(prev_rb) prev_rb.checked = false;
        rb.checked = true;
        prev_rb = rb;
        
        var n = rb.id.split("rb")[1];
        for(var i=0;i<arr_sist.length-1;i++)
        {
            if(arr_sist[i].split(',')[0] == n)
                {
                    sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - parseInt(sist_price)*parseInt(count_dors);
                    sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) + parseInt(arr_sist[i].split(',')[1])*parseInt(count_dors);
                    sist_price = parseInt(arr_sist[i].split(',')[1]);
                    break;
                }    
        }
    }
    function price(mode)
    {
        var countMirrors = 0;
        if(chb_mirrors.checked) countMirrors = count_mirrors;
        var countSide = 0;
        if(chb_side.checked) countSide = count_side;
        var countFalsSide = 0;
        if(chb_falsSide.checked) countFalsSide = count_falsSide;
        
        for(var i=0;i<arr_width.length;i++)
        {
            if(width == arr_width[i])
                if(count_dors == arr_dors[i])
                    {
                      if((width == "1.8" || width == "1.9" || width == "2.0") && count_dors == "3") currentWidthNumber = i - 3;
                      else currentWidthNumber = i;
                      
                      if(mode == "-") sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - parseInt(arr_price[i]) - parseInt(sist_price)*count_dors - countMirrors * mirrors_price - countSide * side_price - countFalsSide * falsSide_price;
                      else sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) + parseInt(arr_price[i]) + parseInt(sist_price)*count_dors + countMirrors * mirrors_price + countSide * side_price + countFalsSide * falsSide_price;
                      break;
                    }
        }
    }
    function dors_width()
    {
        var sp = document.getElementById("dors_width");
        var i = (parseFloat(width)/parseInt(count_dors)).toString();
        if(i.split('.').length > 1)
        {
            var n = i.split('.')[0];
            var m = i.split('.')[1].slice(0,2);
            i = n + "." + m;
        }
        sp.firstChild.nodeValue = i;
    }

    function ActivateCountMirrors()
    {
        var span1 = document.getElementById("span1_mirror");
        var span2 = document.getElementById("span2_mirror");
        var span3 = document.getElementById("span3_mirror");
        var span4 = document.getElementById("span4_mirror");
        
        for(var i = 1; i < 5; i++)
        {
            if(i <= count_dors) eval("span" + i).className = "";
            else eval("span" + i).className = "anactive";
        }
        eval("span" + count_mirrors).className = "active";
    }
    
    function DiactivateMirrors()
    {
        var span1 = document.getElementById("span1_mirror");
        var span2 = document.getElementById("span2_mirror");
        var span3 = document.getElementById("span3_mirror");
        var span4 = document.getElementById("span4_mirror");
        
        for(var i = 1; i < 5; i++)
        {
            eval("span" + i).className = "anactive";
        }
    }
    
    function ActivateCountSides()
    {
        var span1 = document.getElementById("span1_side");
        var span2 = document.getElementById("span2_side");
        
        for(var i = 1; i < 3; i++)
        {
            eval("span" + i).className = "";
        }
        eval("span" + count_side).className = "active";
    }
    
    function DiactivateSides()
    {
        var span1 = document.getElementById("span1_side");
        var span2 = document.getElementById("span2_side");
        
        for(var i = 1; i < 3; i++)
        {
            eval("span" + i).className = "anactive";
        }
    }
    
    function ActivateCountFalsSides()
    {
        var span1 = document.getElementById("span1_falsSide");
        var span2 = document.getElementById("span2_falsSide");
        
        for(var i = 1; i < 3; i++)
        {
            eval("span" + i).className = "";
        }
        eval("span" + count_falsSide).className = "active";
    }
    
    function DiactivateFalsSides()
    {
        var span1 = document.getElementById("span1_falsSide");
        var span2 = document.getElementById("span2_falsSide");
        
        for(var i = 1; i < 3; i++)
        {
            eval("span" + i).className = "anactive";
        }
    }
    
    function MirrorsMode()
    {
        if(chb_mirrors.checked)
        {
            sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) + mirrors_price * count_mirrors;
            ActivateCountMirrors();
        }
        else
        {
            sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - mirrors_price * count_mirrors;
            DiactivateMirrors();
        }
    }
    
    function SideMode()
    {
        if(chb_side.checked)
        {
            sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) + side_price * count_side;
            ActivateCountSides();
        }
        else
        {
            sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - side_price * count_side;
            DiactivateSides();
        }
    }
    
    function FalsSideMode()
    {
        if(chb_falsSide.checked)
        {
            sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) + falsSide_price * count_falsSide;
            ActivateCountFalsSides();
        }
        else
        {
            sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - falsSide_price * count_falsSide;
            DiactivateFalsSides();
        }
    }
    
    function FalsTop(mode)
    {
        count_dors = parseInt(count_dors);
        if(mode == "-")
        {
            sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - parseInt(arr_konstr[(count_dors - 2)*2].split(',')[1]);
        }
        else
        {
            if(chb_falsTop.checked) sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) + parseInt(arr_konstr[(count_dors - 2)*2].split(',')[1]);
            else sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - parseInt(arr_konstr[(count_dors - 2)*2].split(',')[1]);
        }
    }
    
    function Buttom(mode)
    {
        count_dors = parseInt(count_dors);
        if(mode == "-")
        {
            sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - parseInt(arr_konstr[(count_dors - 2)*2 + 1].split(',')[1]);
        }
        else
        {
            if(chb_buttom.checked) sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) + parseInt(arr_konstr[(count_dors - 2)*2 + 1].split(',')[1]);
            else sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - parseInt(arr_konstr[(count_dors - 2)*2 + 1].split(',')[1]);
        }
    }
    
    function CountRolling(span)
    {
        if(chb_rolling.checked) Rolling("-");
        count_rolling = parseInt(span.innerHTML);
        document.getElementById("a_rolling").innerHTML = count_rolling;
        if(chb_rolling.checked) Rolling();
    }
    
    function Rolling(mode)
    {
        if(mode == "-")
        {
            sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - count_rolling * rolling_price;
        }
        else
        {
            if(chb_rolling.checked)
            {
                sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) + count_rolling * rolling_price;
                document.getElementById("a_rolling").className = "";
                document.getElementById("rollingLock").style.display = "none";
            }
            else 
            {
                sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - count_rolling * rolling_price;
                document.getElementById("a_rolling").className = "anactive";
                document.getElementById("rollingLock").style.display = "block";
            }
        }
    }
    
    function Light(mode)
    {
        count_dors = parseInt(count_dors);
        if(mode == "-")
        {
            sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - parseInt(arr_konstr[count_dors + 4].split(',')[1]);
        }
        else
        {
            if(chb_light.checked) sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) + parseInt(arr_konstr[count_dors + 4].split(',')[1]);
            else sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - parseInt(arr_konstr[count_dors + 4].split(',')[1]);
        }
    }
    
    function Korpus(mode)
    {
        if(currentWidthNumber < 10)
        {
            var price = parseInt(((korpusPriceMax - korpusPriceMin) / countKorpusWidthParts) * currentWidthNumber) + korpusPriceMin;
            if(mode == "-")
            {
                sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - price;
            }
            else
            {
                if(chb_korpus.checked) sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) + price;
                else sp_price.firstChild.nodeValue = parseInt(sp_price.firstChild.nodeValue) - price;
            }
        }
    }
