function AddInCart(id)
{
	var count = $('#cnt_' + id).val();
	$('#cart' + id).html('<img src="/_images/ajax-loader_small.gif">');
	if (isNaN(count))
		count = 1;
	
	$.ajax({
		dataType: 'json',
		url: '/index.php?a=shop',
		type: 'POST',
		data: {
			action: 'add_in_cart',
			id: id,
			count: count
		},
		success: function(data)	{
			if (data.status == 'ok')
			{
				$('#cart' + id).html('<div class="item_in_cart">Товар добавлен в корзину.</div>');
				$('#cart').html(data.cart);
			}
			else if (data.status == 'error')
			{
				alert('Ошибка при добавлении товара в корзину.');
			}				
		}
	});
}
function DeleteItem(id)
{
	$('#RecheckButton').attr('disabled', true);
	$('#CartLoader').show();
	
	$.ajax({
		dataType: 'json',
		url: '/index.php?a=shop',
		type: 'POST',
		data: {
			action: 'delete_item_from_cart',
			id: id
		},
		success: function(data)	{
			if (data.status == 'ok')
			{
				$('#RecheckButton').attr('disabled', false);
				$('#CartLoader').hide();
				if (data.cart_data != '')
				{
					$('#cart_data').html(data.cart_data);
				}
				else
				{
					$('#cart_data').html('<p>Ваша корзина пуста.</p>');
					$('#cart_button').html('');
				}
				$('#cart').html(data.cart);
			}
			else if (data.status == 'error')
			{
				alert('Ошибка удаления товара из корзины.');
			}				
		}
	});
}
function ClearCart()
{
	$('#RecheckButton').attr('disabled', true);
	$('#CartLoader').show();
	
	var options = {
		dataType: 'json',
		url: '/index.php?a=shop',
		type: 'POST',
		data: {
			action: 'clear_cart'
		},
		success: function(data)	{
			if (data.status == 'ok')
			{
				location.href = location.href;
			}
		}
	};
	
	$('#cart_form').ajaxSubmit(options);
}
function RecheckCart()
{
	$('#RecheckButton').attr('disabled', true);
	$('#CartLoader').show();
	
	var options = {
		dataType: 'json',
		url: '/index.php?a=shop',
		type: 'POST',
		data: {
			action: 'recheck_cart'
		},
		success: function(data)	{
			if (data.status == 'ok')
			{
				$('#RecheckButton').attr('disabled', false);
				$('#CartLoader').hide();
				if (data.cart_data != '')
				{
					$('#cart_data').html(data.cart_data);
				}
				else
				{
					$('#cart_data').html('<p>Ваша корзина пуста.</p>');
					$('#cart_button').html('');
				}
				$('#cart').html(data.cart);
			}
		}
	};
	
	$('#cart_form').ajaxSubmit(options);
}
function SendReserv()
{
	if ($('#fio').val() == '')
	{
		$('#fio').addClass('error');
		alert('Вы не ввели своё имя!');
		return false;
	}
	else
		$('#fio').removeClass('error');
	
	if ($('#city').val() == '')
	{
		$('#city').addClass('error');
		alert('Вы не ввели свой город!');
		return false;
	}
	else
		$('#city').removeClass('error');
		
	if ($('#tel').val() == '')
	{
		$('#tel').addClass('error');
		alert('Вы не ввели свой телефон!');
		return false;
	}
	else
		$('#tel').removeClass('error');
	
	if ($('#email').val() == '')
	{
		$('#email').addClass('error');
		alert('Вы не ввели свой E-mail!');
		return false;
	}
	else
		$('#email').removeClass('error');
	
	$('#ReservButton').attr('disabled', true);
	$('#CartLoader').show();
	
	$.ajax({
		dataType: 'json',
		url: '/index.php?a=shop',
		type: 'POST',
		data: {
			action: 'send_reserv',
			fio: $('#fio').val(),
			city: $('#city').val(),
			tel: $('#tel').val(),
			email: $('#email').val(),
			txt: $('#txt').val()
		},
		success: function(data)	{
			if (data.status == 'ok')
			{
				$('#reserv_cart').html('<br /><br /><p>Ваш заказ успешно отправлен нашему менеджеру. Мы в ближайшее время с Вами свяжемся для уточнения деталей заказа. Спасибо что воспользовались нашими услугами!</p>');
				$('#CartLoader').hide();
			}
			else if (data.status == 'error')
			{
				alert('Ошибка при отправке заказа!');
			}				
		}
	});
}
function RatingVote(id, vote)
{
	$('#unit_long' + id).hide();
	$('#rating_loading_' + id).show();

	$.ajax({
		url: '/index.php?a=shop',
		dataType: 'json',
		type: 'POST',
		data: {
			action: 'add_vote',
			id: id,
			vote: vote
		},
		success: function(data){
			if (data.status == 'ok')
			{
				$('#rating_' + id).html(data.data);
				alert('Ваш голос учтён. Спасибо за голосование.');
			}
			else if (data.status == 'error')
			{
				$('#unit_long' + id).show();
				$('#rating_loading_' + id).hide();
				alert(data.message);
			}
		}
	});
	
	return false;	
}
