path ='/cart/';
function purchaseItem(id) {
	$.get(path, {
		itemId :id,
		action :'new'
	}, function(xml) {
		// $('.basket').removeClass('emptyBasket').addClass('fullBasket');
		var orderCount = 0;
		$('count', xml).each( function() {
			orderCount += parseInt($(this).text());
		});
		$('#orderCount').text(orderCount);
		// alert("Товар добавлен в корзину покупок");
		setFullprice(xml);
	}, 'xml');
	return false;
}
function purchasePlus(id) {
	$.get(path, {
		itemId :id,
		action :'plus'
	}, function(xml) {
		var cnt = $('item[id=' + id + ']>count', xml).text();
		var price = moneyFormat(parseInt($('item[id=' + id + ']>price', xml).text()));
		var sum = moneyFormat($('item[id=' + id + ']>price', xml).text() * cnt);
		$('span.cnt[rel=' + id + ']').text(cnt);
		$('span.price[rel=' + id + ']').text(price);
		$('span.sum[rel=' + id + ']').text(sum);
		setFullprice(xml);
	}, 'xml');
	return false;
}
function purchaseMinus(id) {
	$.get(path, {
		itemId :id,
		action :'minus'
	}, function(xml) {
		var cnt = $('item[id=' + id + ']>count', xml).text();
		var price = moneyFormat(parseInt($('item[id=' + id + ']>price', xml).text()));
		var sum = moneyFormat($('item[id=' + id + ']>price', xml).text() * cnt);
		$('span.cnt[rel=' + id + ']').text(cnt);
		$('span.price[rel=' + id + ']').text(price);
		$('span.sum[rel=' + id + ']').text(sum);
		setFullprice(xml);
	}, 'xml');
	return false;
}
function purchaseDel(id) {
	$.get(path, {
		itemId :id,
		action :'delete'
	}, function(xml) {
		$('div.item[rel=' + id + ']').remove();
		setFullprice(xml);
	}, 'xml');
	return false;
}
function setFullprice(xml) {
	//var fullprice = parseFloat($(xml).find('fullprice').text());
	var fullprice = moneyFormat($(xml).find('fullprice').text());
	var count = 0;
	$('item', xml).each(function() {
		count += parseInt($(this).find('count').text());
		// fullprice += parseInt($(this).find('count').text())	* $(this).find('price').text();
	});
	// if (count > 0) {
		$('span.ordersCnt').text(count);
		$('span.ordersPrice').text(fullprice);
	// }
	// var cartText = (count > 0) ? 'Покупки: ' + count + ', на сумму: <nobr>' + fullprice + ' р.</nobr>' : 'Ваша корзина пуста';
	// $('#cart a').html(cartText);
}

function moneyFormat(money) {
	var moneyStr = money.toString();
	if (moneyStr.indexOf(".") != -1) 
	intMoneyStr = moneyStr.substr(0, moneyStr.indexOf("."));
	else intMoneyStr = moneyStr;
	
	if ((intMoneyStr >= 1000) && (intMoneyStr < 1000000))
		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) {
		floatVal = moneyStr.substr(moneyStr.indexOf(".") + 1, moneyStr.length);
		if (floatVal.length == 1)
			floatVal += '0';
	} else {
		floatVal = '00';
	}

	money = intVal + '.' + floatVal;
	// alert(money);
//	money = money.replace(" ", "");
//	money = money.replace(",", ".");
	//money = parseFloat(money);
	//alert(money);
	return money;
}
