function calcularFrete() {
	var cep = $("input[name='data[cep]']").val();
	var tipo_envio = $("select[name='data[tipo_envio]']").val();

	// se quiser as seguintes validacoes, basta descomentar
	/*if (isNaN(parseInt(cep))) {
		alert('O CEP deve conter somente números.');
		return;
	}
	if (cep.length < 8) {
		alert('O CEP deve conter 8 dígitos');
	}*/

	$("#carregando").fadeIn();

	$.get(webroot+"/shipping/calcular/"+cep+"/"+tipo_envio, null, function(retorno) {
			enviarCart('index_geral');
		});
}

function calcularDesconto() {
	var coupon = $("input[name='data[coupon]']").val();

	$("#carregando").fadeIn();

	$.get(webroot+"/coupons/calcular/"+coupon, null, function(retorno) {
			enviarCart('index_geral');
	});
}

function enviarCart(view) {
	var post = $('#cartForm').serialize();

	$("#carregando").fadeIn();

	$.post(webroot+'/cart/index/'+view, post, function(retorno) {
		$('#cartDiv').html(retorno);
		$("#carregando").fadeOut();
	});
}

function adicionarAoCarrinho(iCode){
	//if (validaOpcoes()) {
		$.ajax({
			type: "GET",
			url: webroot+"/cart/add/"+iCode+'/true?'+Math.random(),
			data: 'opcoes='+$("#opcoesEscolhidas").val(),
			loading:$("#carregando").fadeIn(),
			success:function(html) { $('#carrinhoLateral').fadeOut('normal'); eval(html); }
		});
	//}
}

function excluirDoCarrinho(iCode){
	$.ajax({
		type: "GET",
		url: webroot+"/cart/remove/"+iCode+'/true?'+Math.random(),
		loading:$("#carregando").fadeIn(),
		success:function(html) { $('#carrinhoLateral').fadeOut('normal'); eval(html); }
	});
}

function recarregaCarrinhoLateral(){
	$.ajax({
		type: "GET",
		url: webroot+"/cart/cart/index_coluna?"+Math.random(),
		success:function(html){
					$("#carrinhoLateral").html(html);
					$('#carrinhoLateral').fadeIn('normal');
					$("#carregando").fadeOut();
				}
	});
}

function alertQtde() {
	alert('A quantidade pedida ultrapassou a quantidade em estoque desse produto.');
}
function alertCepNaoEncontrado() {
	alert('O CEP não foi encontrado');
}
