function setCookie(name, value, expires, path, domain, secure) {
  path = "/";
  value = escape(value);
  domain = document.domain;
  domain = '.'+domain.replace(/www./, "");
  
  var curCookie = name + "=" + value +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "");

  document.cookie = curCookie;
}


function getCookie(name) {
 var prefix = name + "=";
 var cookieStartIndex = document.cookie.indexOf(prefix);
 if (cookieStartIndex == -1) return null;
 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 DeleteCookie(code) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1000000000);  // This cookie is history (changed -1 to make it previous time)
	var cval = GetCookie (code);
	document.cookie =code + "=" + cval + "; expires=" + exp.toGMTString();	
}	

function addCart(n,elem,price,ch) { 
  var cart = getCookie("cart");
  var cart_total = 0;
  var v = new String(elem.value);
  var cart_count = 0;
  
  if (getCookie("cart_total") > 0) cart_total = getCookie("cart_total");
  
  v = v.replace(/[^0-9]+/,'');
  v = v.replace(/^0+/,'0');
  if (v=='') v = '0';
  elem.value = v=='0'?'':v;
  var scart = "|";
  cart = cart==null?'':cart;
  // корзина пустая
  if (cart.length<3&&v.length>0) {
    scart += n+"="+v+"|";
    cart_count += v*1;
	 cart_total = cart_total*1 + v*price;
  } else {
    var ar = cart.split("|");
    var f = 0;
    for(var i=0; i<ar.length; i++) {
      var s = ar[i];
      if (s.length>2) {
        var p = s.indexOf(n+"=");
		  // товар ежу есть в корзине
        if (p==0) {
          if (v.length>0) {
            scart += n+"="+v+"|";
            cart_count += v*1;
				var reg = /[^=]+\=(.+)/;
            var v_old = reg.exec(s);
				
				v_old[1] = v_old[1]>0?v_old[1]:0;
				cart_total = cart_total*1 + (v-v_old[1])*price;
          }
          f = 1;
        } else {
          cart_count += s.replace(/.+=([0-9]+)$/,'$1')*1;
          scart = scart+s;
          scart = scart+"|";
        }
      }
    }
    if (f==0&&v.length>0) {
		// добавляем как новый в корзину
      scart += n+"="+v+"|";
      cart_count += v*1;
		cart_total = cart_total*1 + v*price;
    }
  }
  
  basket_count = GEByID('orderCount');
  
  if(basket_count) {
	  basket_count.innerHTML = cart_count;
  }
  
  if(cart_total<0.01 || !(cart_total>0)) cart_total = 0;
  basket_total = GEByID('orderTotal');
  if(basket_total) {
	  basket_total.innerHTML = cart_total.toFixed(2);
  }
  
  if (ch==0) {
    if (v=='0') {
      elem.style.backgroundColor = "#FFFFFF";
      var img = GEByID('img'+n);
      img.style.visibility = 'hidden';
      
    } else {
      elem.style.backgroundColor = "#EDFABB";
      var img = GEByID('img'+n);
      img.style.visibility = 'visible';
    }
  }  
  
  var exp = new Date();
  exp.setTime (exp.getTime() + 3*31*24*60*60);  // This cookie is history (changed -1 to make it previous time)
  setCookie("cart",scart,exp);
  setCookie("cart_total",cart_total,exp);
  
  MoveWindow();
}

function GEByID(id) {
   var nn4 = document.layers ? 1 : 0;
   var d = document;
   var menuVersion = 2;
   if ( navigator.appName == "Microsoft Internet Explorer" ) menuVersion = 1; else if ( d.layers ) menuVersion = 0;
   if ( menuVersion == 0 ) return null;
   if ( menuVersion == 1 ) return d.all[id];
   if ( menuVersion == 2 ) return d.getElementById(id);
}

function CheckImgCar(n,c) {
  var img = GEByID('img'+n);
  if (c>0) {
    img.style.visibility = 'visible';
  } else {
    img.style.visibility = 'hidden';
  }  
}

// Проверка корзины, если она пуста, то показать предупреждение и не дать перейти по ссылке
function isCart() {
  var cart = getCookie("cart");
  var empty = false;
  if(!cart) empty = true;
  if(cart) {
	  if(!cart.match(/=[1-9]/)) {
		  empty = true;
	  }
  }
  if(empty) {
	  alert('Ваша корзина пуста. Для того, чтобы сделать заказ нужного товара, введите необходимое количество в соответствующее поле или воспользуйтесь специальными кнопками-стрелками. Товар автоматически попадет в корзину.');
	  return false;
  }
}

