window.onload = ld;
var is_active = null;
function add_to_basket(id)
	{  kol = document.getElementById("t_" + id).value;
	if (!parseInt(kol)){ alert("введите цифры!"); return;}
  kol = parseInt(eval(kol));
  if (kol<=0) { alert("должно быть больше 0 !"); return;}
  price = document.getElementById("c_"+id).value;
  tst=parseFloat(price);
  if (isNaN(tst)) price=0;
  if (in_basket(id, kol) == "") return;
  if ((getCookie('bsk') == in_basket(id, kol)) || (getCookie('bsk') == null) || (in_basket(id, kol)=='newtov'))  
{    setCookie('bsk', getCookie('bsk') + id + ',' +  kol + ',' + price + ';', 0, '/');
}
  else
{
    setCookie('bsk', in_basket(id, kol), 0, '/');
}
  alert("Товар добавлен в корзину.");
//  if (window.opener) window.opener.document.location = window.opener.document.location;
  
}

// чтобы куки не повторялись;
function in_basket(id, kol){
  cook = getCookie('bsk');
  cook = new String(cook);
  cook = cook.split(';');
  newstr = "";
  i = 0;
  while(c = cook[i]){
    t = new String(c);
    tmp = t.split(',');
    if ((tmp[0] == id) && (tmp[1] == kol) ) return "";
    if (tmp[0] == id){  // то есть такой товар имеется в куках  
      newstr = newstr + tmp[0] + ',' + kol + ',' + tmp[2] + ';';
    }else{
       newstr = newstr + t + ";";
    }
    i++;
  }
  if (i==0) newstr='newtov';
  return newstr;
}


function setCookie(name, value, expires, path, domain, secure){
     doc = (window.opener != null)?window.opener.document:document;
     doc.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
     UpDataMBasket();
}

function getCookie(name){
        doc = (window.opener != null)?window.opener.document:document;
        var prefix = name + "=";

        var cookieStartIndex = doc.cookie.indexOf(prefix)
        if (cookieStartIndex == -1)
                return null
        var cookieEndIndex = doc.cookie.indexOf(";", cookieStartIndex + prefix.length)
        if (cookieEndIndex == -1)
                cookieEndIndex = doc.cookie.length
        return unescape(doc.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

function deleteCookie(name, path, domain) {
 //       doc = (window.opener.document != null)?window.opener.document:document;
        if (getCookie(name)) {
                doc.cookie = name + "=" +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                "; expires=Thu, 01-Jan-70 00:00:01 GMT"
        }
}

function deleteFromCookie(id, redirect){
   c = getCookie('bsk');
   cook = c.split(';');
   newstr = "";
   i = 0;
   while (c = cook[i]){
   	
      t = new String(c);
      tmp = t.split(',');
      
      if (tmp[0] != id){
         newstr = newstr + t + ';';
      }
    i++;
   }
   setCookie('bsk', newstr, 0, '/');
//   if (tmp[0] == id){clear_cookie('bsk', redirect);end;}
   if (i==0){clear_cookie('bsk', redirect);end;}
   document.location.href = redirect;
}

function clear_cookie(cookieName, redirect){
 var expDate = new Date();
 expDate.setTime (expDate.getTime() - (3 * 24 * 60 * 60 *1000));
 document.cookie = cookieName+"=ImOutOfHere; expires="+
 expDate.toGMTString();
 document.location.href = redirect
}

function ShowDetails(i) {
        window.open('./details.php?id=' + i,'m_details','width=650,height=550,resizable=no, scrollbars=yes, menubar=yes, status=no, location=yes');
}

function PrintBasket(i) {
        window.open('./print.php?id=' + i,'m_details','resizable=no, scrollbars=yes, menubar=yes, status=yes, location=yes');
}

// изменor в корзине количество товара
function ncounttov(id){
   doc = (window.opener != null)?window.opener.document:document;
   old_id = eval(document.getElementById("ob_" + id).innerHTML);
   old_sum = eval(document.getElementById("ob_summ").innerHTML);
   old_kol = eval(document.getElementById("ob_kol").innerHTML);
   old_kol_t = eval(document.getElementById("t_" + id).value);
   if (parseInt(doc.getElementById('t_' + id).value)){
        doc.getElementById("ob_" + id).innerHTML = Math.round((doc.getElementById("t_" + id).value * doc.getElementById("c_" + id).value)*100)/100;
   }else{
        doc.getElementById("ob_" + id).innerHTML = 0;
   }

  /* f = ((document.all["t_" + id].value == NaN) || (document.all["t_" + id].value == ""))?-1:eval(document.all["t_" + id].value);
//   if (f == -1) f = (-1)*old_kol_t;
   document.all["ob_kol"].innerHTML = old_kol + f;*/
   doc.getElementById("ob_summ").innerHTML = Math.round((old_sum - old_id + eval(doc.getElementById("ob_" + id).innerHTML))*100)/100;
   if (doc.getElementById("t_" + id).value != "")
     add_to_basket(id);
}

function ld(){
 UpDataMBasket();
/* d = window.document.getElementsByTagName("input");
 for (i = 0; i < d.length; i++){
 }*/
}

function UpDataMBasket(){
   doc = (window.opener != null)?window.opener.document:document;
   cc = (getCookie('bsk') != null)?getCookie('bsk'):"";
   cook = cc.split(';');
   summa = 0;
   zv=0;
   i = 0;
   kol = 0;
   while(c = cook[i]){
      t = new String(c);
      tmp = t.split(',');
//      summa += Math.round((tmp[1]*tmp[2])*100)/100;
      summa1 =parseFloat(tmp[1])*parseFloat(tmp[2]);
	if (summa1==0) zv=1;
      summa = summa +summa1;
      kol = kol + parseInt(tmp[1]);
//      alert(kol + "<>" + tmp[1] + "<>" + tmp[2] + "<>" + Math.round((tmp[1]*tmp[2])*100)/100);
      i++;
   }
	summa = Math.round(summa*100)/100;
	summa18=Math.round(summa*18)/100;
	summa18all=summa+summa18;
	summa18all=Math.round(summa18all*100)/100;
   d = new String(kol);
   d = d.split("");
   if(kol > 0){
     doc.getElementById('minibasket').innerHTML = "<a class=\"mini_basket\" href='/order/basket.html'><font size=2>В&nbsp;корзине&nbsp;товаров&nbsp;" + kol + " шт.</font></a>";
     if(doc.getElementById('ob_kol')) doc.getElementById('ob_kol').innerHTML = kol;
	if (zv==1) zv="<br>Товары, помеченные * в стоимость заказа не включены. ";else zv='';
     if(doc.getElementById('itogo')) doc.getElementById('itogo').innerHTML ="<b><font size=2>Всего товаров на&nbsp;суmmу&nbsp;" + summa + "р.<br> НДС - "+summa18+"р.<br>ИТОГО С НДС - "+summa18all+"р.</font></b><br>"+zv;

   }else{
     doc.getElementById('minibasket').innerHTML = "<a class=\"mini_basket\" href='/order/basket.html'><font size=2>Корзина пуста.</font></a>";
   }
}

function aaa(ss){
        var str="";
//      var ss=window;
        for(s in ss) {
                str=str+s+"="+ss[s]+"\n";
        }
//alert(str);
document.getElementById("zzz").value = str;
}
function showp() {
document.images[0].src = location.search.substring(1);}

/*function resizep() {
var w, h;
w = document.images[0].width;
h = document.images[0].height;
window.resizeTo(w+50, h+110);}*/
