
var CartURL = 'index.php?controller=ajax';

		function addCartSuccess(res) {
			$('#cart_p').html('В корзине <span>'+res[1]+'</span> товара(ов)<br/>на сумму <span>'+res[0]+'</span> руб.');
			$('#cart_div').html("<a href='order.html' class='abut'>оформить заказ</a>");
		}
		
		function deleteCartSuccess(res) {
			if (res[1] > 0) {
				addCartSuccess(res);
				$('#cart_'+res[2]).remove();
				$('#cart_sum').text(res[0]+' руб.');
			} else
				if (page == 'order') window.location = "index.php";
		}
		
		function recountCartSuccess(res) {
			addCartSuccess(res);		
			$('#cart_sum').text(res[0]+' руб.');
			$('#cart_sum_'+res[2]).text(res[3]+' руб');
		}

function getCartRespounce(text) {
	values = new Array();
	if (text.length > 8) text = text.split('[{}]');
	if (text.length > 0) values = text[1].split('[{;}]');
	return values;
}

function addCart(id) {
	var data = $('#formOrder_'+id).serialize();
	$('#preloader').show();
	$('#formOrder_'+id+' .but').hide();
	$.ajax({
		url : CartURL+'&task=add',
		data : data,
		success : function (text) {
			respounce = getCartRespounce(text);
			if (respounce.length > 1) addCartSuccess(respounce);
			$('#formOrder_'+id+' .but').show();
			$('#preloader').hide();
			// $('#added_mes').css("top","300px").css("left","500px").show();
			alert('Товар добавлен');
		}
	});
}

function deleteCart(id) {
	$('#cart_'+id+' input.but').hide().parent().append('<img src="admin/mod/afm/theme/default/images/loading.gif"/>');
	$.ajax({
		url : CartURL+'&task=delete',
		data : 'id='+id,
		success : function (text) {
			respounce = getCartRespounce(text);
			deleteCartSuccess(respounce);
		}
	});
}

function recountCart(e, id, n) {
	if ((e.keyCode > '95' && e.keyCode < '106') || (e.keyCode > '47' && e.keyCode < '58')) {
		$.ajax({
			url : CartURL+'&task=recount',
			data : 'id='+id+'&count='+n.value,
			success : function (text) {
				respounce = getCartRespounce(text);
				if (respounce.length > 1) recountCartSuccess(respounce);
			}
		});
	 } else {
		 return false;
	 }
}


