var GdsNumRound = true;

function Gds2Cart(id){
	setCartCookie("");
	ToCart(id, 1, false, true)	
	return false
}

function DeleteFromCart(gds_id){
	if (confirm("Удалить товар из корзины?")){
		var cookie_gds = getCartCookie();
		var cookie_num = parseInt(getValue(cookie_gds, gds_id), 10);
		cookie_num--	
		
		ToCart(gds_id, cookie_num, false)
		window.history.go(0)
	}
}

function ClearCart()
{
	setCookie('gds_cart', '');
}

function CartRecalc(){
	var pForm = document_all("cart_form")
	if (pForm){
		for (var i=0; i< pForm.elements.length; i++)
			ToCart (pForm.elements[i].name.substr(7), pForm.elements[i].value, false);
	}
	window.history.go(0)
}

function GdsNumChange(p){
	var num = p.value
	num = GdsNumRound? parseInt(num): parseFloat(num.replace(/,/g, "."))
	if (!isNaN(num) && num > 0) p.value=num
	else p.value='1'
}

function getValue(st,name){
	var prefix = name + "=";
	var StartIndex = st.indexOf(prefix);
	if (StartIndex == -1)
			return '';
	var EndIndex = st.indexOf(";", StartIndex + prefix.length);
	if (EndIndex == -1)
			EndIndex = st.length;
	return st.substring(StartIndex + prefix.length, EndIndex);
}


function ToCart(gds_id, new_num, show_alert, go_to_cart){
	if (GdsNumRound)
		new_num = parseInt(new_num)	
	else
		new_num = parseFloat(new_num)
		
	var gds_name = gds_id;
	var cookie_gds = getCartCookie();
	var cookie_num = getValue(cookie_gds, gds_name); 
	if (new_num > 0) new_cookie = gds_name+'='+new_num+';'
	else {
		new_cookie = '';
		if (show_alert)
			alert ("Товар удален из корзины")
	}

	cookie_gds = cookie_gds.replace(gds_name+'='+cookie_num+';', new_cookie);
	if (!cookie_num) {
		cookie_gds += new_cookie;
		if (show_alert)
			alert ("Товар добавлен в корзину")
	}
	else{
		if (new_num > 0)
			if (show_alert)
				alert ("Количество изменено")
	}
	
	setCartCookie(cookie_gds);
	
	if (go_to_cart)
		window.location='/shop/cart.php'
}

function GetPrice(price){
	var price = "" + parseFloat(price)
	var arrPrice = price.split(".")
	var integer = arrPrice[0]
	var i = integer.length - 3;
	
	while (i > 0){
		integer = integer.substr(0, i) + " " + integer.substr(i)
		i -= 3;
	}
	return integer + "," + arrPrice[1].substr(0, 2);
}
// C O O K I E 
function setCookie(name, value){
	var d=new Date, nd=new Date;
	nd.setHours(d.getHours()+5);
	var curCookie = name + "=" + escape(value)+"; expires="+nd.toGMTString()+"; path=/";
	document.cookie = curCookie;
}

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 getCartCookie()
{
	return getCookie("gds_cart");
}

function setCartCookie(value)
{
	setCookie("gds_cart", value);
}

