path ='/korzina/';
function purchaseItem(id, item_size) {
	$.get(path, {
		itemId :id,
		action :'new',
		size: item_size
	}, function(xml) {
		$('.basket').removeClass('emptyBasket').addClass('fullBasket');
		var orderCount = 0;
		$('count', xml).each( function() {
			orderCount += parseInt($(this).text());
		});
		
		var result;
		var number = orderCount;
		if(number.length == 1){
			switch(number){
				case 1: result = number+" товар";
					break;
				case 2:
				case 3:
				case 4: result = number+" товара";
					break;
				case 5:
				case 6:
				case 7:
				case 8:
				case 9: result = number+" товаров";
					break;
			}
		}
		else{
			var last = number % 10;
			var rest = Math.floor(number / 10);
			var plast = rest % 10;
			if (plast == '1'){
				result = number+" товаров";
			}
			else{
				switch(last){
					case 1: result = number+" товар";
						break;
					case 2:
					case 3:
					case 4: result = number+" товара";
						break;
					case 5:
					case 6:
					case 7:
					case 8:
					case 9:
					case 0: result = number+" товаров";
						break;
				}
			}
		}
		$('#orderCount').html('<a href="'+path+'">'+result+'</a>');
		$('#fullBasketPrice').html('<b>'+$('fullprice', xml).text()+'</b> р.');
	}, 'xml');
	return false;
}

function purchasePlus(id, size) {
	$.get(path, {
		itemId :id,
		action :'plus',
		size: size
	}, function(xml) {
		$('#cnt' + id).text($('item[id=' + id + ']>count', xml).text());
		setFullprice(xml);
	}, 'xml');
	return false;
}
function purchaseMinus(id, size) {
	$.get(path, {
		itemId :id,
		action :'minus',
		size: size
	}, function(xml) {
		$('#cnt' + id).text($('item[id=' + id + ']>count', xml).text());
		setFullprice(xml);
	}, 'xml');
	return false;
}
function setFullprice(xml) {
	var fullprice = 0;
	$('item', xml).each(
			function() {
				fullprice += parseInt($(this).find('count').text())	* $(this).find('price').text();
			});
	//alert(fullprice);
	fullprice = moneyFormat(fullprice);
	$('#fullprice').text(fullprice);
}
function moneyFormat(money) {
	var moneyStr = money.toString();
	if (moneyStr.indexOf(".") != -1) 
	var intMoneyStr = moneyStr.substr(0, moneyStr.indexOf("."));
	else intMoneyStr = moneyStr;
	
	if ((intMoneyStr >= 1000) && (intMoneyStr < 1000000))
		var intVal = intMoneyStr.substr(0, intMoneyStr.length - 3) + ' ' + intMoneyStr.substr(-3);
	if ((intMoneyStr >= 1000000) && (intMoneyStr < 1000000000))
		intVal = intMoneyStr.substr(0, intMoneyStr.length - 6) + ' ' + ' ' + intMoneyStr.substr(-6, 3) + ' ' + intMoneyStr.substr(-3);
	if (intMoneyStr < 1000) intVal = intMoneyStr;
	if (moneyStr.indexOf(".") != -1) {
		var floatVal = moneyStr.substr(moneyStr.indexOf(".") + 1, moneyStr.length);
		if (floatVal.length == 1)
			floatVal += '0';
	} else {
		floatVal = '00';
	}

	money = intVal + ',' + floatVal;
	return money;
}