
function _win2unicode(str) {
   var charmap   = unescape(
      "%u0402%u0403%u201A%u0453%u201E%u2026%u2020%u2021%u20AC%u2030%u0409%u2039%u040A%u040C%u040B%u040F"+
      "%u0452%u2018%u2019%u201C%u201D%u2022%u2013%u2014%u0000%u2122%u0459%u203A%u045A%u045C%u045B%u045F"+
      "%u00A0%u040E%u045E%u0408%u00A4%u0490%u00A6%u00A7%u0401%u00A9%u0404%u00AB%u00AC%u00AD%u00AE%u0407"+
      "%u00B0%u00B1%u0406%u0456%u0491%u00B5%u00B6%u00B7%u0451%u2116%u0454%u00BB%u0458%u0405%u0455%u0457")
   var code2char = function(code) {
               if(code >= 0xC0 && code <= 0xFF) return String.fromCharCode(code - 0xC0 + 0x0410)
               if(code >= 0x80 && code <= 0xBF) return charmap.charAt(code - 0x80)
               return String.fromCharCode(code)
            }
   var res = ""
   str = escape(str);
   for(var i = 0; i < str.length; i++) res = res + code2char(str.charCodeAt(i))
	//alert(str+' '+res);
   return res
}

function setCookie(name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "")
	//alert(value);
        if ((name + "=" + escape(value)).length <= 4000)
                document.cookie = curCookie
        else
                if (confirm("Cookie превышает 4KB и будет вырезан !"))
                        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(name, path, domain) {
        if (getCookie(name)) {
                document.cookie = name + "=" + 
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                "; expires=Thu, 01-Jan-70 00:00:01 GMT"
}
}

	var basket_count=0;
	var basket;
	var countdiv;
	var path;
	
	function loadCart(ID, url, scookie) {
		path = url;
		//alert(ID);
		//countdiv = document.getElementById(ID);
		countdiv = document.getElementById('basket_count');
		
		if(scookie.length>2) {
			deleteCookie('TheBasket','/');
			setCookie('TheBasket',scookie,'','/');
		}
		else if(scookie=='<?clear?>') deleteCookie('TheBasket','/');
		
		if(getCookie('TheBasket') && scookie!='<?clear?>') {
		
			basket=getCookie('TheBasket').split("|||");
			for(i=0;i<basket.length;i++) {
				if(basket[i]!='null' && basket[i]!='undefined') {
					var item=basket[i].split("@");
					basket_count+=parseInt(item[2]);
				}
			}
			//alert('В корзине: <b>'+basket_count+' товаров</b>');
			if(path.length>2) {
				
				countdiv.innerHTML='<a href="/'+path+'cart">В корзине</a>: <b>'+basket_count+' товаров</b>';
			}
			else {
				countdiv.innerHTML='<font color="red">(<img src="template/chart.gif" alt="добавить в корзину заказа" title="добавить в корзину заказа" /> <b>'+basket_count+'</b>)</font>';
			}
		}
	}
	
	
	function addItem(newItem, newItemId, newQuantity) {
		if (newQuantity >= 1) {
			window.alert(newItem+' добавлено в корзину');
            
			if(getCookie('TheBasket')=='') {
				setCookie('TheBasket',(_win2unicode(newItem)+"@"+newItemId+"@"+newQuantity,'','/'));
			}
			else {
				//alert(getCookie('TheBasket'));
				cook=getCookie('TheBasket')+"|||"+_win2unicode(newItem)+"@"+newItemId+"@"+newQuantity;
				//deleteCookie('TheBasket');
				//alert(getCookie('TheBasket'));
				setCookie('TheBasket',cook,'','/');
				//alert(getCookie('TheBasket'));
			}
		basket_count++;
		//alert(basket_count);
		//alert('<a href="/'+path+'cart">В корзине</a>: <b>'+basket_count+' товаров</b>');
		countdiv.innerHTML='<font color="red">(<img src="template/chart.gif" alt="добавить в корзину заказа" title="добавить в корзину заказа" /> <b>'+basket_count+'</b>)</font>';
		//alert('asd'+countdiv.innerHTML);
		}
		
	}
	
	function delItem(itemId) {
		var cart_item=document.getElementById('cart_item');
		cart_item.value=itemId;
		document.getElementById('frm_cart').submit();
	}
	
	function resetCart() {
		deleteCookie('TheBasket','/');
	}