﻿function addToBasket(type, id, price)
{
	if (id != "")
	{	
		var today = new Date()
		var expires = new Date();
		expires.setTime(today.getTime() + 1000*60*60*24*30);
		var favorits_count = document.getElementById("favorits_count").innerHTML;
		var poscount = 1;//document.getElementById("p_c_"+type+"_"+id).value;
		poscount = isNumber(poscount);
		favorits_count = favorits_count*1 + poscount*1;

		if(favorits_count >99)
		{
			alert('можно добавить не более 100 позиций');
		}
		else
		{
			var p_f_c = getCookie("cook_fav[" + type + "_" + id + "_"+ price +"]");
			p_f_c = p_f_c*1 + poscount*1;
			document.cookie = "cook_fav[" + type + "_" + id + "_"+ price +"]="+p_f_c+"; expires =" + expires.toGMTString() + "; path =/";

			document.getElementById("favorits_count").innerHTML = favorits_count;
			var favorits_sum = document.getElementById("favorits_sum").innerHTML;
			favorits_sum = str_replace(' ','',favorits_sum);
			favorits_sum = favorits_sum*1 + price*poscount;

			document.getElementById("favorits_sum").innerHTML = humanInt(favorits_sum);

			alert("Товар добавлен в корзину. \nДля того чтобы отправить заказ нужно перейти в корзину товаров (сверху справа).");
		}
	}
}
function updateBasket(type, id, price){

	if (id != "")
	{	
		var today = new Date()
		var expires = new Date();
		expires.setTime(today.getTime() + 1000*60*60*24*30);
		var favorits_count = document.getElementById("favorits_count").innerHTML;
		var poscount = document.getElementById("p_c_"+type+"_"+id).value;
		poscount = isNumber(poscount);

		var p_f_c = getCookie("cook_fav[" + type + "_" + id + "_"+ price +"]");
		p_f_c = p_f_c*1;
		var f_c = favorits_count*1 - p_f_c + poscount*1;

		if(poscount==0)
		{

			removeFromBasket(type, id, price);
		}
		else
		{	
			if(f_c >99)
			{
				alert('можно добавить не более 100 позиций');
			}
			else
			{
				document.cookie = "cook_fav[" + type + "_" + id + "_"+ price +"]="+poscount+"; expires =" + expires.toGMTString() + "; path =/";

				document.getElementById("favorits_count").innerHTML = f_c;
				var favorits_sum = document.getElementById("favorits_sum").innerHTML;
				favorits_sum = str_replace(' ','',favorits_sum);
				favorits_sum = favorits_sum*1 + price*(poscount - p_f_c);
				document.getElementById("favorits_sum").innerHTML = humanInt(favorits_sum);
				document.getElementById("price_"+type+"_"+id).innerHTML = price*poscount;

				alert("Количество выбранного товара было изменено.");		
			}
		}
	}	
}

function removeFromBasket(type, id, price)
{
	if (id != "")
	{ 
		var today = new Date()
		var expires = new Date();
		expires.setTime(today.getTime() - 1000*60*60*24*30);
		
		document.getElementById(type + "_" + id).style.display = "none";
		document.getElementById("pos_"+ type + "_" + id).value=-1;
		var favorits_count = document.getElementById("favorits_count").innerHTML;
		var poscount = getCookie("cook_fav[" + type + "_" + id + "_"+ price +"]");
		poscount = isNumber(poscount);

		document.cookie = "cook_fav[" + type + "_" + id + "_" + price + "]=0; expires =" + expires.toGMTString() + "; path =/";

		poscount = poscount*1;
		
		favorits_count = favorits_count - poscount;
		
		
		document.getElementById("favorits_count").innerHTML = favorits_count;
		var favorits_sum = document.getElementById("favorits_sum").innerHTML;
		favorits_sum = str_replace(' ','',favorits_sum);
		favorits_sum = favorits_sum*1 - price*poscount;
		document.getElementById("favorits_sum").innerHTML = humanInt(favorits_sum);

		alert("Товар удален из корзины");
	}
}

function humanInt(intOrNum){

	var price = '';
	intOrNum = intOrNum + '';
	for(var s=intOrNum.length;s>0;s--)
	{
		price = price + intOrNum.substring((intOrNum.length - s),(intOrNum.length - s + 1));
		if( (s-1)%3 == 0 ) price = price+' ';
	}
	return price;
}

function isNumber(src){

	var res = 1;
	var re = /[0-9]+/;
	var arr = re.exec(src);
	if (arr != null) 
	{
			res = src;
	}

	return res;
} 

/*  function getCookie(name) {
   var cookie = " " + document.cookie;
   var search = " " + name + "=";
   var setStr = 0;
   var offset = 0;
   var end = 0;
   if (cookie.length > 0) {
      offset = cookie.indexOf(search);
      if (offset != -1) {
         offset += search.length;
         end = cookie.indexOf(";", offset)
         if (end == -1) {
            end = cookie.length;
         }
         setStr = unescape(cookie.substring(offset, end));
      }
   }
   return setStr;
}  */

function str_replace(search, replace, subject) 
{
	return subject.split(search).join(replace);
}

function clEnter(event, type, id, price)
    {
	    if( ((event.keyCode == 0xA)||(event.keyCode == 0xD)))
	        {
	        	updateBasket(type, id, price);
	        }
    }
function clAddEnter(event, type, id, price)
    {
	    if( ((event.keyCode == 0xA)||(event.keyCode == 0xD)))
	        {
	        	addToBasket(type, id, price);
	        }
    }
