path ='/cart/';

function purchaseItem(id, gtab, count) { 
	var dummy = new Date().getTime();
	var orderCount = 0; 
	var orderSum = 0;
	$.get(path+'?dummy='+dummy, {
		itemId :id,
		goodtable :gtab,
		action :'new',
		count:count
	}, function(xml) {
		$('#cart').removeClass('emptycart').addClass('fullcart');
		$(xml).find('item').each( function() {
			orderCount += parseInt($(this).find('count').text());
			orderSum += parseInt($(this).find('count').text())	* $(this).find('price').text();
		});
		$('#orderCount').text(orderCount);
		if (orderSum>=1000) {
			orderSum = orderSum-bonus;
			if (orderSum > 0) orderSum;
			else orderSum = 0
		}
		orderSum = moneyFormat(orderSum);
		$('#orderSum').text(orderSum);
	}, 'xml');
	return false;
}

function checkimage(oc){
	if (oc && $('#basketlink').hasClass('emptyBasket')) {
		$('#basketlink').addClass('fullBasket');
		$('#basketlink').removeClass('emptyBasket');
	}
	if (!(oc) && $('#basketlink').hasClass('fullBasket')){
		$('#basketlink').addClass('emptyBasket');
		$('#basketlink').removeClass('fullBasket');		
	}
}

function purchasePlus(id, gtab) {
	var dummy = new Date().getTime();
	var orderCount = 0;
	var orderSum = 0;
	$.get(path+'?dummy='+dummy, {
		itemId :id,
		goodtable :gtab,
		action :'plus'
	}, function(xml) {
		var itemClass;
		$('#cnt'+id).text($('item[id="' + id +'"]>count', xml).text());
		$('#amount'+id).text(moneyFormat(parseInt($('item[id="' + id +'"]>count', xml).text())*parseInt($('item[id="' + id +'"]>price', xml).text())));
		setFullprice(xml);
		$('item', xml).each( function() {
			orderCount += parseInt($(this).find('count').text());
			orderSum += parseInt($(this).find('count').text())	* $(this).find('price').text();
		});
		$('#orderCount').text(orderCount);
		if (orderSum>=1000) {
			orderSum = orderSum-bonus;
			if (orderSum > 0) orderSum;
			else orderSum = 0
		}
		orderSum = moneyFormat(orderSum);
		//$('#orderSum').text(orderSum);
		checkimage(orderCount);
	}, 'xml');
	return false;
}
function purchaseMinus(id, gtab) {
	var dummy = new Date().getTime();
	var orderCount = 0;
	var orderSum = 0;
	$.get(path+'?dummy='+dummy, {
		itemId :id,
		goodtable :gtab,
		action :'minus'
	}, function(xml) {
		$('#cnt'+id).text($('item[id="' + id +'"]>count', xml).text());
		$('#amount'+id).text(moneyFormat(parseInt($('item[id="' + id +'"]>count', xml).text())*parseInt($('item[id="' + id +'"]>price', xml).text())));
		setFullprice(xml);
		$('item', xml).each( function() {
			orderCount += parseInt($(this).find('count').text());
			orderSum += parseInt($(this).find('count').text())	* $(this).find('price').text();
		});
		$('#orderCount').text(orderCount);
		if (orderSum>=1000) {
			orderSum = orderSum-bonus;
			if (orderSum > 0) orderSum;
			else orderSum = 0
		}
		orderSum = moneyFormat(orderSum);
		$('#orderSum').text(orderSum);
		checkimage(orderCount);
	}, 'xml');
	return false;
}

function getBasketXML(id, gtab){
	$('#dost').attr('value', $('#delivery_select').attr('value'));
	if ($('#dost').attr('value') == '1') $('#dtype').attr('value', 'в банке на расчетный счет');
	else $('#dtype').attr('value', 'на почте при получении');
	$.get(path, {
		itemId :id,
		goodtable :gtab,
		action :'getxml'
	}, function(xml) {
		setFullprice(xml);
	}, 'xml');
	return false;
}

function setFullprice(xml) {
	var fullprice = 0;
	var fullcount = 0;
	var perc;
	$('item', xml).each(
			function() {
				fullprice += parseInt($(this).find('count').text())	* $(this).find('price').text();
				fullcount += parseInt($(this).find('count').text());
			});
	num2 = moneyFormat(fullprice);
	if (fullprice >= 1000){
		fullprice = fullprice-bonus;
		if (fullprice > 0) fullprice;
		else fullprice = 0;
	}
	ldelivery_cost=delivery_cost;
	if(typeof delivery_summa != 'undefined'){
		if(fullprice>=delivery_summa){ldelivery_cost-=delivery_cost*(delivery_discount/100);$('#delivery_action').show();}
		else $('#delivery_action').hide();
	}
	fullprice+=ldelivery_cost;
	num = moneyFormat(fullprice);
	$('#delivery_cost').text(moneyFormat(ldelivery_cost));
	$('#fullprice').text(num);
	$('#orderSum').text(num);
	$('#fullcount').text(fullcount);
	$('#fullbonus').text(moneyFormat($('fullbonus', xml).text()));
    if(fullprice>=min_order){
        $('#form').show();
        $('#related').hide();
    }
    else {$('#form').hide();$('#related').show();}
}

function number_format( number, decimals, dec_point, thousands_sep ) {	// Format a number with grouped thousands
	var i, j, kw, kd, km;

	if( isNaN(decimals = Math.abs(decimals)) ){
		decimals = 2;
	}
	if( dec_point == undefined ){
		dec_point = ",";
	}
	if( thousands_sep == undefined ){
		thousands_sep = ".";
	}
	i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
	if( (j = i.length) > 3 ){
		j = j % 3;
	} else{
		j = 0;
	}
	km = (j ? i.substr(0, j) + thousands_sep : "");
	kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
	kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
	return km + kw + kd;
}


function moneyFormat(money) {
		money = number_format(money,2,',',' ');
	return money;
}
