
var cart = new Cart;
cart.conf_refreshAfterAction = 0
cart.conf_confirmAction = 1
cart.conf_reportActionComplete = 0


var anchorUsed = 0

function isInt(x) {
	var y=parseInt(x);
	if (isNaN(y)) return false;
	return x==y && x.toString()==y.toString();
}

function add2cart_local ( uid, price, table, title ) {
	var quantity = document.getElementById("quantity"+uid).value
	if(!isInt(quantity)){
		alert('Ââåäèòå êîððåêòíîå êîëè÷åñòâî')
		return		
	}
	cart.addItem( uid, '_', price, quantity, table, title )
	showCartInfo()
	
	window.parent.location.href = anchorUsed ? window.parent.location : window.parent.location + '#top';
	anchorUsed = 1
}

function removeFromBasket( uid, totalCost ){
	var totalBasketCost = cart.countTCost()
	
	if( ! cart.deleteItem(uid) ) return
	table = document.getElementById('basket_table')
	row = document.getElementById('basket_row_'+uid)
	row.parentNode.removeChild(row);
	
	var totalBasket = document.getElementById('totalBasket')
	totalBasket.innerHTML = cart.countTCost()
	showCartInfo()	
}

function changeQuantity( uid, quantity ){
	var itemTotal = document.getElementById('item_total_'+uid)
	if(!isInt(quantity) || quantity > 9999 ) {
		itemTotal.innerHTML = "0"
		return
	}
	cart.setQuantity(uid, quantity)
	itemTotal.innerHTML = cart.Items[ cart.isItemExists(uid) ].price * quantity
	
	var totalBasket = document.getElementById('totalBasket')
	totalBasket.innerHTML = cart.countTCost()
	showCartInfo()
}


function showCartInfo(){
	var content = '';
	
	var TItems = cart.countTItems();
	cs = String(TItems);
	cs = cs.substring(-1,1);
	var word = ' òîâàðîâ';
	if (cs == 1) word = ' òîâàð';
	else if (cs >= 2 && cs <= 4) word = ' òîâàðà';
	
	content += '(<strong>' + ( TItems ? TItems + word : 'âûáåðèòå òîâàðû'  ) + '</strong>)';
	if (cart.countTCost()>0) content += ', ñóììà <strong>' + cart.countTCost() + ' ð.</strong>';
	
	var target = document.getElementById("cartInfo")
	target.innerHTML = content
}
