var countPar = new Array();
summ=0;
g=0;
quantity=0;

function item(parent,caption,price,description,count){
   this.parent=parent;
   this.caption=caption;
   this.price=price;
   this.description=description;
   this.count=count;
}

function discount(quantity,sum,value){
   this.quantity=quantity;
   this.sum=sum;
   this.value=value;
}

function preset(caption,description){
   this.caption=caption;
   this.description=description;
}

function options(id_option,count){
   this.id_option=id_option;
   this.count=count;
}


function SwitchVis(Id,Hide) {
        //if (isNaN(countPar[Id])) countPar[Id]=0;
        if (document.getElementById("ParImg_"+Id))
        {
           // ParentCheckbox=document.getElementById("ParChb_"+Id);
          //checked=document.getElementById("chb"+id).checked;
          //alert(document.getElementById("span_"+Id).style.display);
            display=document.getElementById("span_"+Id).style.display;

            //if (NeedCheck) document.getElementById("ParChb_"+Id).checked=true; else document.getElementById("ParChb_"+Id).checked=false;
            if (display!="none"||Hide)
            {
            document.getElementById("span_"+Id).style.display = "none";
            eval('document.all.ParImg_'+Id+'.src="/img/plus.gif"');
            }
	        else
            {
             document.getElementById("span_"+Id).style.display = "block";
             eval('document.all.ParImg_'+Id+'.src="/img/minus.gif"');
            }
      }
}

function UnCheck() {
var spans = document.getElementsByTagName('span');
for (var k = 0; k < spans.length; ++k)
{
    document.getElementsByTagName('span')[k].style.display="none";
}
    for (Id in itm) {
    SwitchVis(Id,1);
    if (document.getElementById('Chb_'+Id))
        {
        //if (!isNan(document.getElementById("span_"+Id).style.display)) alert();
        //document.getElementById("span_"+Id).style.display = "none";
        //eval('document.all.ParImg_'+Id+'.src="/img/plus.gif"');
        Check(false,Id,0,1);
        }
    }
}

function Check(Checked,ChbId,count,rec) {
    par=itm[ChbId].parent;
    if (document.getElementById("ParImg_"+par)) ParentCheckbox=document.getElementById("ParImg_"+par); else ParentCheckbox=false;
    if (Checked&&count>0)
    {
        if (document.getElementById('chb_'+ChbId)) eval('document.all.chb_'+ChbId+'.checked=true');
        if (rec)
        {
            document.getElementById('quantity_'+ChbId).value=count;
            itm[ChbId].count=count;
        }
        for (Id in itm)
        {
            if (ParentCheckbox)
            {
                document.getElementById("tr_"+par).style.backgroundColor = "#CCCCFF";
            }
        }
            document.getElementById("tr_"+ChbId).style.backgroundColor = "#DDDDFF";
            itm[ChbId].count=count;
    }
    else
    {
        if (rec) document.getElementById('quantity_'+ChbId).value=0;
        itm[ChbId].count=0;
        document.getElementById("tr_"+ChbId).style.backgroundColor = "";
        if (document.getElementById('chb_'+ChbId)) eval('document.all.chb_'+ChbId+'.checked=false');
        for (Id in itm)
        {
            parCheck=itm[ChbId].parent;
            if (itm[Id].parent==parCheck&&itm[Id].count>0) return;
        }
        if (ParentCheckbox)
        {
            document.getElementById("tr_"+par).style.backgroundColor = "";
        }
    }
}

function changePreset(preset,innerCall) {
 //if (preset=='custom')

 if (!innerCall) document.all.id_preset.value=preset;
    if (preset==0)
    {
        document.all.id_preset.value=0;
        UnCheck();
    }
    else
    {
        UnCheck();
        //len=opt[preset].length;
        for(id in opt[preset])
        {
            optID=opt[preset][id].id_option;
            count=opt[preset][id].count;
            Check(true,optID,count,1);
            SwitchVis(itm[optID].parent);
        }
    }
CountChecked();
}



function CountChecked(){
summ=0;
amount=0;
document.all.discount.value=0;
//l=document.all.order.elements.length;
maxDsc=dsc.length;
  for (Id in itm)
    {
        summ=summ+itm[Id].price*parseInt(itm[Id].count);
        amount=amount+parseInt(itm[Id].count);
        //if (!document.getElementById("ParImg_"+Id))
        Check(true,Id,itm[Id].count);
        //SwitchVis(itm[Id].parent);
    }
    //alert(amount);
            for (i=1;i<maxDsc;i++)
            {
            //alert(dsc[i].quantity);
                if (dsc[i].sum>0)
                {
                    if(summ>=dsc[i].sum) document.all.discount.value=dsc[i].value;
                }
                else
                {
                    if (amount>=dsc[i].quantity) document.all.discount.value=dsc[i].value;
                }
            }
            discountValue=parseInt(Math.round(summ/100*document.all.discount.value));
            document.all.discount.value=discountValue;
            document.all.total.value=summ-discountValue;
            document.all.price_all.value=summ;
            document.all.summ.innerText="Общая стоимость: "+document.all.price_all.value+" руб.";
            document.all.skidka.innerText="Cкидка: "+document.all.discount.value+" руб.";
            document.all.itogo.innerText="Стоимость с учётом скидок: "+document.all.total.value+" руб.";
}

