// JavaScript Document
$(function(){
	$('.qtdeProdutos,').change(function(){
		calculoGeral();
	});	
	$('input:radio[name=frete]').click(function(){
		calculoGeral();
	});
	$('input:text[name=cep]').blur(function(){
		if($(this).attr('value') != ''){
			calculoGeral();	
		}		
	});
	

});
function calculoGeral(){
	var subTotalGeral = 0;
	var descricaoProdutos = "";
	$('.qtdeProdutos').each(function(){
		var id = $(this).attr('id').substr(5);
		var qtde = parseFloat(eval($(this).attr('value')));
//		if(qtde > 0){
			var valor_ingresso = parseFloat(eval($('#valor_ingresso_'+id).attr('value')));
			var taxa_ingresso = parseFloat(eval($('#taxa_ingresso_'+id).attr('value')));		
			var vr_taxa = qtde*(valor_ingresso*(taxa_ingresso/100));
			var sub_total = qtde*valor_ingresso + vr_taxa;
			subTotalGeral += sub_total;
			if(qtde > 0){
				var desc_ingresso = $('#desc_ingresso_'+id).attr('value');
				descricaoProdutos += qtde+"x "+desc_ingresso+'; ';
			}
			$('#subTotal_'+id).html('R$ '+number_format(sub_total,2,',','.'));
//		}
	});
	if(subTotalGeral != 0){
		var frete = $('input:radio[name=frete]:checked').attr('value');
		if(frete != undefined){
			var valorFrete = 0;
			if(frete == 1){
				valorFrete = '5';
				descricaoProdutos += "Motoboy BH";
			} else if(frete == 2){
				valorFrete = '10';
				descricaoProdutos += "Motoboy Metro";
			} else {
				var cep = $('input:text[name=cep]').attr('value');
				$.ajax({
				  url: 'http://www.blocoreduto.com.br/carnaval-2012/ajaxCalcularSedex.php',
				  data: "cep="+ cep ,
				  async: false,
				  success: function(dados) {
						valorFrete = dados;
						return parseFloat(eval(dados));
				  },error: function() {
						alert("Ocorreu um erro!");
						return false;
				  }			  
				});
				descricaoProdutos += "Sedex "+cep;
			}
			if(valorFrete != 0){
				var valorTotal = parseFloat(eval(subTotalGeral)) + parseFloat(eval(valorFrete));
				$('#subtotal').html('Sub Total:	R$ '+number_format(subTotalGeral,2,',','.'));
				$('#frete').html('Frete :	R$ '+number_format(valorFrete,2,',','.'));
				$('#total').html('Total:	R$ '+number_format(valorTotal,2,',','.'));
				$('#item_descr_1').attr('value',descricaoProdutos);
				$('#item_valor_1').attr('value',number_format(valorTotal,2,'',''));
				$('#botaoComprar').css('display','block');			
			} else {
				zerarTotais();	
			}
		}
	} else {
		zerarTotais();
	}
//	alert(subTotalGeral);
}
function zerarTotais(){
	$('#subtotal').html('');
	$('#frete').html('');
	$('#total').html('');
	$('#botaoComprar').css('display','none');	
}
function ajaxSedex(){
	
}

function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}
