sdfs=false;
to=0;
lef=0;
widt=200;
heigh=40;
vvv=0;

function SetBasketAutoOpen(ch) {
	setCookie('HideAutoBasket',(ch.checked)?1:0);
}

function setCookie(name, value,expires) {
    document.cookie = name + "=" + escape(value)+"; path=/; "+((expires)?"":"expires=Thursday, 31-Dec-2020 23:59:59 GMT;");
}


function getCookie(name) {
        var prefix = name + "=";
        var cookieStartIndex = document.cookie.indexOf(prefix);
        if (cookieStartIndex == -1)
                return '';
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length;
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function CheckLoad(form) {
	goods = GetGoodsFromBasket();
	if (form) {
		var el = form.elements;
	        ss = el.length;
		for (var i=0;i<ss;i++) {
			if (el[i].name && (el[i].name.indexOf('amount_') == 0)) {
				id = el[i].name.substr(7,el[i].name.length - 7)
				if (goods[id] && goods[id] > 0) {
					el[i].value = goods[id];
					el[i].style.fontWeight = 'bold'
					el[i].style.color = 'black'
					OnEditChange(id,el[i]);
				} else {
					el[i].style.color = 'gray'
					el[i].style.fontWeight = 'normal'
					el[i].style.align = 'center'
					el[i].value='1';
				}
			}
		}
	}
	UpdateWholePrice();
  //UpdateCompareButtons();
}

function AmountChange(form,id,delta) {
	basket = false;
	if (!form) {
		form = document.basket_form
		basket = true;
	}

	el = form.elements['amount_'+id];
	if (el) {
		el.value = parseInt(el.value) + delta;
		if (el.value == 'NaN' || el.value < 0) el.value = '0'
		el.style.color = 'gray'
		el.style.fontWeight = 'normal'
	}
	if (basket) {
		UpdateWholePrice();
		BasketChanged = 1;
	}
}

function GetGoodsFromBasket() {
  var basket = getCookie('eshop_basket');
  var goods_arr = basket.split("\t");
	var goods = new Array;
	for (i in goods_arr) {
		if (goods_arr[i] == "") continue;
		var arr = goods_arr[i].split(' ');
		var am = parseInt(arr[1]);
		if (am>0)	goods[arr[0]] = am;
	}
	return goods;
}

function UpdateBasketCookie(basket) {
	var goods_arr = new Array;
	for (i in goods) {
		if (parseInt(goods[i]) == 0) continue;
		goods_arr[goods_arr.length] = i+" "+goods[i];
	}
  setCookie('eshop_basket',goods_arr.join("\t"),'true');
}

function PutInBasket(id, amount) {
	goods = GetGoodsFromBasket();
	goods[id]=amount;
	UpdateBasketCookie(goods);
	UpdateWholePrice();
}

function BuyPress(form, id) {
  cnt2 = 0;
  for (i in goods) {
    amount = parseInt(goods[i]);
    if (isNaN(amount))amount=0;
    if (amount==0) continue;
    cnt2++;
  }
  amount = form['amount_'+id];
	if (amount) {
		am = parseInt(amount.value);
		if (isNaN(am)) am = 0;
		PutInBasket(id,am);
		amount = form['amount_'+id];
		if (am == 0) {
			amount.style.color = 'gray'
			amount.style.fontWeight = 'normal'
		} else {
			amount.style.color = 'black'
			amount.style.fontWeight = 'bold'
		}
		OnEditChange(id,amount);
    if(cnt2==0&&am!=0)
    {
      //alert('Выбранный товар был добавлен в корзину. При дальнейших покупках это окно отображаться не будет.')
      if(!document.getElementById("ale"+id))
		form['amount_'+id].parentNode.innerHTML+="<div style='position:absolute' id=ale"+id+"></div>";
      to=-35;
      lef=-175;
      widt=180;
      heigh=30;
      hhh=document.getElementById("ale"+id);
      sdfs=false;
      vvv=window.setInterval("eff(hhh)",1);
      /*
      if (!(getCookie('HideAutoBasket') > 0)) {
        window.open("/basket/","basket","width=500,height=350,left=50,top=50,copyhistory=no,directories=no,menubar=yes,resizable=yes,status=yes,tollbar=yes,scrollbars=yes").focus();
      }
      */
    }
  } else {
    //PutInBasket(id,1);
    //UpdateBasketSummary();
	}

  //form['amount_'+id].parentNode.innerHTML+=tmps;
              

	return false;
}

function eff(ff)
{
  tmps='<div style="position:relative"><div style="position:absolute;top:'+to+'px;left:'+lef+'px;width:'+widt+'px;background:#fff;border:1px solid #ddd;height:'+heigh+'px">&nbsp;</div></div>';
  lef-=10;
  to-=5;
  widt+=22;
  heigh+=17;
  ff.innerHTML=tmps;
  if(widt>280){
    clearInterval(vvv);
    tmps2='<div style="position:relative"><div id="alert'+id+'" style="font:11px tahoma;position:absolute;top:-60px;left:-220px;width:300px;background:#fff;border:1px solid #ddd;padding:15px">Выбранный товар был добавлен в корзину. При дальнейших покупках это окно отображаться не будет.<br><br><div style="text-align:center"><div style="width:80px;padding:2px;border:1px solid #eee" onmouseover="this.style.cursor=\'hand\'" onmouseout="this.style.cursor=\'auto\'" onclick="var opa=this.parentNode.parentNode.parentNode.parentNode;eff2(opa);"><div style="padding:5px 10px;background:#E6EFFA">OK</div></div></div></div></div>';
    rtg=ff;
    setTimeout("eff2(rtg)",4000);
    ff.innerHTML=tmps2;
    
  }
}

function eff2(gg)
{
  if(!sdfs)
  {
  jjj=gg;
  to=-50;lef=-210;widt=280;heigh=80;
  rtrt=setInterval("eff3(jjj)",1);
  }
}

function eff3(pp)
{
  tmps='<div style="position:relative"><div style="position:absolute;top:'+to+'px;left:'+lef+'px;width:'+widt+'px;background:#fff;border:1px solid #ddd;height:'+heigh+'px">&nbsp;</div></div>';
  lef+=10;to+=5;widt-=22;heigh-=30;pp.innerHTML=tmps;
  if(widt<200){
    clearInterval(rtrt);
    pp.innerHTML='';
    sdfs=true;
  }
}

function OnEditChange(id,edit) {
	SomeChanges = true;
	edit.value = parseInt(edit.value);
	if (!(edit.value > 0)) {edit.value = '0'};
	if (!document.all('whole_price_all')) {
		//alert(edit.id+' | '+edit.name)
		if (edit.name.indexOf('amount_')==0)
			PutInBasket(id,edit.value);
		else
			PutInBasket(id+'|'+CurrentSetId, edit.value);
		var el = document.getElementById('amount_div_'+id)
		//alert(el);
		if (edit.value == 0) {
			if (el) {
				el.style.visibility = 'hidden';
				el.style.display = 'none';
			}
		} else {
			if (el) {
				el.style.visibility = 'visible';
				el.style.display = 'block';
			}

			var el1 = document.getElementById('amount_amount_'+id)
			if (el1) {
				el1.innerHTML = ':&nbsp;'+edit.value;
			}
		}
	}
	//OnEditBasketChange(id,edit)
	UpdateWholePrice();
}




function UpdateWholePrice() {
  b_div = document.getElementById('basket_div');
	c_div = document.getElementById('price_caption');
  v_div = document.getElementById('price_value');
  d_div = document.getElementById('deliv_value');
  if (b_div == null && c_div == null) return;
  goods = GetGoodsFromBasket();
	summ = 0;
	summ_original = 0;
	cnt = 0;
  for (i in goods) {
		amount = parseInt(goods[i]);
    if (isNaN(amount)){ amount=0;}
		if (amount==0) continue;
		cnt++;
		pr = parseFloat(Goods[i]);
    if (isNaN(pr)) pr=0;
    summ+=Goods[i][1]*amount;
    summ_original+=Goods[i][0]*amount;
	}
  // Обновить картинку корзины: (ноль/не ноль товаров)
	var s='';
	var sc='';
	var sv='';

		if (cnt==0) {
			s+='<table border=0 cellpadding=0 cellspacing=0 width=185><tr><td  style="text-align:center; background:#134084; padding:2px 0 2px 0;"><a href="/goods/div1/div7/?search_avto=0" style="color:#ffff00; font-size:95%; font-family:arial; font-weight:bold;">ИНТЕРНЕТ - МАГАЗИН</a></td></tr><tr><td><object type="application/x-shockwave-flash" data="/imgs-new/basket2.swf" width="185" height="69"><param name="movie" value="/imgs-new/basket2.swf"><param name="quality" value="best"></object></td></tr></table>';
		} else {
			//s+='<a href=/basket/ onclick=window.open("/basket/","basket","") title="Отправить заказ"><img src=/imgs/el27.gif width=38 height=41 border=0></a>'
			//s+='</td><td><img src=/imgs/x.gif width=8 height=1></td><td _width=100% class=lnk1>'
      s+='<table width=186 border="0" cellspacing="0" cellpadding="0" style="height:95%;"><tr><td colspan="2" height="10" style="text-align:center; background:#134084; padding:2px 0 2px 0;"><a href="/goods/div1/div7/?search_avto=0" style="color:#ffff00; font-size:95%; font-family:arial; font-weight:bold;">ИНТЕРНЕТ - МАГАЗИН</a></td></tr><tr><td style="padding:0 3px 0 3px; height:100%;"><img src="/imgs/basket_small.gif" width="52" height="50" alt=корзина></td><td valign=top width=100% style="padding:3px 0 3px 0; vertical-align:middle;"><p><nobr><a class=link2 href=/basket/>Позиций в корзине: ' + cnt + '<br><nobr>на сумму: <span id=price_value style="font-size:10px;">' + rus_price(summ)+' руб.</span></a></p></td></tr></table>';
      //window.open("/basket/","basket","width=500,height=350,left=50,top=50,copyhistory=no,directories=no,menubar=yes,resizable=yes,status=yes,tollbar=yes,scrollbars=yes").focus();return false;\
      //s+='</td></tr><tr><td colspan=3><img src=/imgs/x.gif width=1 height=6><br>';
			//s+='<div class=d13><a class=lnk4 href=/basket/ onclick=window.open("/basket/","basket","")>Отправить заказ</a>';
			//s+='</td></tr></table>';
		}

  if (b_div != null) b_div.innerHTML=s;
	if (c_div != null) c_div.innerHTML=sc;
  if (v_div != null) v_div.innerHTML=sv;

  if (v_div != null) {
    v_div.innerHTML=rus_price(summ)+' руб.';
	}
  if (d_div != null)
  {
    if(summ<4000)d_div.innerHTML='100 руб.';else d_div.innerHTML='бесплатно';
  }
}

function gotoOrder(){
	goods = GetGoodsFromBasket();



	summ = 0;
	summ_original = 0;
	cnt = 0;
	for (i in goods) {

		amount = parseInt(goods[i]);
		if (isNaN(amount)) amount=0;
		if (amount==0) continue;
		cnt++;
		pr = parseFloat(Goods[i]);
		if (isNaN(pr)) pr=0;
    summ+=Goods[i][1]*amount;
    summ_original+=Goods[i][0]*amount;
	}


  /*if (summ < 2000){
		alert('Оформление заказа возможно только при наличие в корзине\nтовара на сумму более 2000 рублей');
		return false;
  }else{*/
		opener.location.href='/order/new/';
		window.close();
		return true;
  //}
}

function bigimage(s)
{
  w=window.open(null,null,"top=50,left=50,height=600,width=400,status=no,toolbar=no,menubar=no,location=no,resizable=yes");
  w.document.write("<title>Просмотр товара</title><body style='margin:0'><img src='/pictdb/pict/"+s+"'></body>");
}


function open_picture(u,w,h,max_w,max_h) {
  if (window.screen) {
    max_w = window.screen.width - 20;
    max_h = window.screen.height - 53;
  }
  win_w = w + 0;
  win_h = h + 0;
  if (win_w > max_w) win_w = max_w;
        if (win_h > max_h) win_h = max_h;
  win = window.open('','',"width=" + win_w + ", height=" + win_h + ",scrollbars=auto,resizable=1,resizable=yes");
  win.document.open();
  win.document.write('<body bgcolor=#F1F2F2 leftMargin=0 rightMargin=0 topMargin=0 marginwidth=0 marginheight=0>');
  win.document.write('<div style="position:absolute;right:0;bottom:20px"><img width="171" height="21" src="/imgs/stamp2.gif"></div>');
  win.document.write('<img width=' + w + ' height=' + h + ' src="' + u + '">');
  win.document.close();
  win.focus();
        return(win);
}


function rus_price (value) {
	value = ""+parseFloat(Math.round(value*100)/100);
	var parts = value.split('.');
	if (!parts[1]) parts[1]='00';
	while ((parts[1]).length<2) parts[1]+='0';

	ctr = 0;
	for (var i = parts[0].length - 1; i>=1; i--) {
		ctr++;
		if (ctr != 3) continue;
		parts[0] = parts[0].substr(0,i) + ' ' + parts[0].substr(i,parts[0].length - i);
		ctr = 0;
	}

	value = parts.join(',');
	return value;
}
function menuclick(flag) {
	menuclick_cleardt();
	var flag = flag || (document.getElementById('submenu').style.display=='none');
	if (flag==true) {
		document.getElementById('submenu').style.display='';
		document.getElementById('imgar').src='/imgs/h-161.gif';
	} else {
		document.getElementById('submenu').style.display='none';
		document.getElementById('imgar').src='/imgs/h-16.gif';
	}
	return(false);
}

function menuclick_cleardt() {
	clearTimeout(window.ttmenu);
}
function menuclick_dt() {
	window.ttmenu=setTimeout('menuclick(false)',200);
}

function UpdateBasketCookie_parametrize(basket) {
	var goods_arr = new Array;
	for (i in basket) {
		if (parseInt(basket[i]) == 0) continue;
		goods_arr[goods_arr.length] = i+" "+basket[i];
	}
  setCookie('eshop_basket',goods_arr.join("\t"),'true');
}

