function GetObj (id) {
  return obj=(document.getElementById)?document.getElementById(id):
    (document.all)?document.all[id]:
    (document.layers)?document.layers[id]:null;
}


function dropComp(btn){
  if(document.getElementById) {
    tr = btn;
    while (tr.tagName != 'TR') tr = tr.parentNode;
    tr.parentNode.removeChild(tr);
    ProdSumm();
  }
}


function addComp(btn,ids){
  if(document.getElementById) 
  {
    tr = btn;
    while (tr.tagName != 'TR') tr = tr.parentNode;
    var idSuffix = Math.round(Math.random()*1000);
    var newTr = tr.parentNode.insertBefore(tr.cloneNode(true),tr.nextSibling);
    thisChilds = newTr.getElementsByTagName('td');
    for (var i = 0; i < thisChilds.length; i++)
    {
      if (thisChilds[i].id == 'button_plus'+ids) thisChilds[i].innerHTML = '<input style="width:28px" type="button" value=" - " onClick="dropComp(this);" />';
    }
		thisChilds =tr.getElementsByTagName('select');
		for (var j = 0; j < thisChilds.length; j++)
		{
			a=/^(comp_name|comp_count)_(\d+)\.(\d+)\.(\d+)$/.exec(thisChilds[j].id);
			if (a && a.length>0) thisChilds[j].id=a[1]+"_"+a[2]+"."+parseInt(a[3])+"."+( parseInt(a[4])+1 );
		}
  }
}



function show_div(id) {
        obody=
                (document.getElementById)?document.getElementById('add_'+id):
                (document.all)?document.all['add_'+id]:
                (document.layers)?document.layers['add_'+id]:null;

        if (obody) {
          if(obody.style.display!="block") {
    obody.style.display="block";
    ProdSumm();
    } else {
    obody.style.display="none";
//  obody.style.display=(obody.style.display!="block")?"block":"none"
    DropIngr(id);
    ProdSumm();
    }
        }
 }


function GetPrice (id) {
  var pobj=GetObj("price_"+id);
        if (pobj) {
                return parseInt(pobj.innerHTML);
        }
        return 0;
}


function draw_comp(j) {
  document.write('<option selected value="0"><'+'/'+'option>');
  for(i=1;i<Comp.length;i++) {
	    document.write('<option value="'+i+'">'+Comp[i][1]+' ('+( (Comp[i][j]>0)?(Comp[i][j]+'руб.'):'беспл.' ) + ')<'+'/'+'option>');
  }
}


function scheet(id,c,d){
var kolvo = document.getElementById("kvo_"+id);
var summO = document.getElementById("products");
var ingrO = document.getElementById("ingridients");
var totals = document.getElementById("total");
var kol=parseFloat(kolvo.value);
var sumO=parseFloat(summO.value);
if(d==0) {
  if(kol>0) {
    kol--;
    kol=""+Math.round(1000.*parseFloat(kol))/1000.;
    kolvo.value=""+kol;
    summO.value = sumO-c;
    totals.value = parseInt(ingrO.value)+parseInt(summO.value);
    TotalSumm();
    }
}
  if(d==1) {
    kol++;
    kol=""+Math.round(1000.*parseFloat(kol))/1000.;
    kolvo.value=""+kol;
    summO.value = sumO+c;
    totals.value = parseInt(ingrO.value)+parseInt(summO.value);
    TotalSumm();
    }
}


function open_win() {
  var w=550,h=400,t=0,l=0;
  var mw=screen.availWidth;
  var mh=screen.availHeight;
        var name="w1";
        var url="";
  if (mw>0 && mh>0) {
    l=(mw-w)*.5;
    t=(mh-h)*.5;
    }
  var win=window.open(url,name,"width="+w+",height="+h+"," +"left="+l+",top="+t+"," +
    "directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,titlebar=no");
  if (win) {
    win.focus();
        if (win.opener==null) win.opener=window;
    }
}



function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return "";
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function saveall(){
var order_form=document.buy;
        for(i=0;i<order_form.elements.length;i++) {
        el=order_form.elements[i];
        a = /^f\[(\d+)\]$/.exec(el.name);
          if(a && (Field[a[1]]["req"]==1 || Field[a[1]]["req"]==3) && (el.value=='' || el.value==0)) {
                alert('Не заполнено обязательное поле!\n"'+Field[a[1]]["title"]+'"');
                order_form.elements[i].focus();
                return false;
                }
        }
}


function ShowOrder() {
        var so = document.getElementById("showorder");
        var soas = document.getElementById("hrefshoworder");
        var soah = document.getElementById("hrefhideorder");
        if(so.style.display == "none") {
          so.style.display = "block";
          soas.style.display = "none";
          soah.style.display = "block";
        }
        else {
          so.style.display = "none";
          soas.style.display = "block";
          soah.style.display = "none";
        }
}


function DropIngr(id) {
var order_form=document.buy;
var sales = (100-salesValue)/100;
var prod=parseInt(order_form.products.value);
var ingr=parseInt(order_form.ingridients.value);
var total=parseInt(order_form.total.value);
var regex = /(comp_count_)(\d+).(\d+).(\d+)$/;
var str=0;
obody=
(document.getElementById)?document.getElementById('add_'+id):
(document.all)?document.all['add_'+id]:
(document.layers)?document.layers['add_'+id]:null;
        if(obody.style.display!="block") {
                for(i=0;i<order_form.elements.length;i++) {
                el=order_form.elements[i];
                        if(regex.test(el.id)) {
                        c = /(comp_count_)(\d+)\.(\d+)\.(\d+)$/.exec(el.id);
                                if(c[2]+"."+c[3]==id) {
                                        if(c.length>0 && c[1]=='comp_count_') {
                                        nel=GetObj("comp_count_"+c[2]+"."+c[3]+"."+c[4]);
                                        names=GetObj("comp_name_"+c[2]+"."+c[3]+"."+c[4]);
                                        nel_kvo=GetObj("kvo_"+c[2]+"."+c[3]);
                                                if(el.value!=0 && nel_kvo.value!=0) {
                                                j=Comp[parseInt(names.value*3-1)];
                                                str+=parseInt(j)*parseInt(nel.value);
                                                }
                                        }
                                }
                        }
                }
        }
        order_form.ingridients.value=ingr-parseInt(str);
        prod=parseInt(order_form.products.value);
        ingr=parseInt(order_form.ingridients.value);
        total=parseInt(order_form.total.value);
        ingr=parseInt(order_form.ingridients.value);
        total=prod+ingr;
        order_form.total.value=parseInt(total*sales);
}

function IngrSumm(id) {
	var order_form=document.buy;
	var sales = (100-salesValue)/100;
	var prod=parseInt(order_form.products.value);
	var total=parseInt(order_form.total.value);
	var regex = /(comp_count_|comp_name_)(\d+).(\d+).(\d+)$/;
	var str=0;
	var pc=0;
	var el;
	var print = GetObj('overview');
	
	for(var i=0;i<order_form.elements.length;i++) 
	{
		el=order_form.elements[i];
		if(regex.test(el.id)) 
		{
			a = /(comp_count_|comp_name_)(\d+)\.(\d+)\.(\d+)$/.exec(el.id);

			if (a[2]+'.'+a[3] == id){
			obody=
			(document.getElementById)?document.getElementById('add_'+a[2]+"."+a[3]):
			(document.all)?document.all['add_'+a[2]+"."+a[3]]:
			(document.layers)?document.layers['add_'+a[2]+"."+a[3]]:null;

			if(a.length>0 && a[1]=='comp_name_' && obody.style.display!="none")
			{
				nel=GetObj("comp_count_"+a[2]+"."+a[3]+"."+a[4]);
				nel_kvo=GetObj("kvo_"+a[2]+"."+a[3]);
				pc=0;
				if(el.value!=0 && nel_kvo.value!=0) 
				{
					j=Comp[parseInt(el.value)][( (a[3]==30)?('2'):'3' )];
					str+=parseInt(j)*parseInt(nel.value);
					pc=parseInt(nel.value);
				}
				order_form.ingridients.value=parseInt(str);
				total+=parseInt(str);
				if ( pc > 0 ) 
				{
					print.innerHTML += "<div style='border-bottom: 1px solid #cdcdcd;'><div style='float:right;'>"+ pc + "&nbsp;шт.&nbsp;</div> &nbsp;&nbsp;&rarr; " + Comp[parseInt(el.value)][1] +"</div>";								
				}
			}
			order_form.total.value=parseInt((prod+str)*sales);
			}
		}
	}
}

function ProdSumm() {
  var order_form=document.buy;
	var sales = (100-salesValue)/100;
  var ingr=parseInt(order_form.ingridients.value);
  var total=parseInt(order_form.total.value);
  var regex = /(kvo_)(\d+).(\d+)$/;
  var str=0;
  var pc=0;
  var print = GetObj('overview');
  
  print.innerHTML = "<br/><div align='center'><b><a href='#order'>Перейти к оформлению</a></b></div><br/>";
	print.style.display = "block";
  for(var i=0;i<order_form.elements.length;i++) {
	        el=order_form.elements[i];
          if(regex.test(el.id)) {
						a = /(kvo_)(\d+)\.(\d+)$/.exec(el.id);
						pc=0;
						if(a.length>0 && a[1]=='kvo_') 
						{
							nel_kvo=GetObj("kvo_"+a[2]+"."+a[3]);
							if(el.value!=0 && nel_kvo.value!=0) 
							{
								j=Prod[parseInt(a[2]+""+a[3])];
								str+=parseInt(j)*parseInt(nel_kvo.value);
								pc=parseInt(nel_kvo.value);
							}
							order_form.products.value=parseInt(str);
							total+=parseInt(str);
							if ( pc > 0 ) 
							{
								print.innerHTML += "<div style='border-bottom: 1px solid #cdcdcd;'><div style='float:right;'>"+ pc + "&nbsp;шт.&nbsp;</div> &nbsp; " + ProdName[parseInt(a[2]+""+a[3])] +"</div>";
							}
							IngrSumm(a[2]+'.'+a[3]);
						}
          order_form.total.value=parseInt((ingr+str)*sales);
          }
  }        

	if(order_form.total.value == "0" ) print.style.display = "none";
}


function SetMenuPosition(){
	NS = (document.layers) ? 1 : 0;
	IE = (document.all) ? 1: 0;
	o = document.getElementById('overview');
	if(o)
	if(IE) {
		o.style.top = document.body.scrollTop + 150 + "px";
	} else {
		o.style.top = self.pageYOffset + 150 + "px";
	}
}
onscroll = SetMenuPosition;


