// --------------------------------------------------------------------------
// Formata os valores monetários do carrinho
// --------------------------------------------------------------------------
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];
}	

// --------------------------------------------------------------
// Atualiza a quantidade de itens e valores do carrinho
// --------------------------------------------------------------
function recalcular()
{
	Ext.get(document.body).mask('Recalculando preço ...','x-mask-loading');
	var formComprar = document.getElementById('frmCarrinho');
	formComprar.target = "branca";
	formComprar.action = "iframe.php?acao=recalcular";
	formComprar.submit();
}

// --------------------------------------------------------------
// Calcula o valor do frete
// --------------------------------------------------------------
function frete(aValor, aPeso, parcelas)
{
	var oCEP = document.getElementById('cep1').value + document.getElementById('cep2').value;
	
	if(aPeso != '0,00' && aValor != '0,00'){
		
		if (oCEP.length != 8) {
			window.alert('Por favor preencha corretamente o CEP!');
		} else {
			Ext.get(document.body).mask('Calculando Frete ...','x-mask-loading');
			
			var ajax = new Ext.data.Connection().request({
				url: 'pages/lojavirtual/carrinho/frete.php',
				method: 'GET',
				params: {cep: oCEP, peso: aPeso, valor: aValor},
				callback: function(options, success, response) {
					if (success == true) {
						
						var retorno = Ext.util.JSON.decode(response.responseText);
						
						document.getElementById('valorFrete').value = parseFloat(retorno.Endereco_Frete.replace('.','').replace(',','.'));
						document.getElementById('infoValorFrete').innerHTML = String.format('R$ {0}', retorno.Endereco_Frete);
	
						if (parcelas == true) preencheParcelas();
	
						var valorFinal = parseFloat(document.getElementById('valorTotalDoPedido').value) + parseFloat(retorno.Endereco_Frete.replace('.','').replace(',','.'));
						document.getElementById('valorTotalPedido').innerHTML = 'R$ ' + number_format(valorFinal, 2, ',', '.');
					}
					Ext.get(document.body).unmask();
				}
			});
		}
	}
}

function freteConfirmacao(aValor, aPeso, parcelas)
{
	var oCEP = document.getElementById('cep1').value + document.getElementById('cep2').value;
	
	if(aPeso != '0,00' && aValor != '0,00'){
		
		if (oCEP.length != 8) {
			window.alert('Por favor preencha corretamente o CEP!');
		} else {
			Ext.get(document.body).mask('Calculando Frete ...','x-mask-loading');
			
			var ajax = new Ext.data.Connection().request({
				url: 'pages/lojavirtual/carrinho/frete.php',
				method: 'GET',
				params: {cep: oCEP, peso: aPeso, valor: aValor},
				callback: function(options, success, response) {
					Ext.get(document.body).unmask();
					return true;		
				}
			});
		}
	}
	
	return false;
}

// --------------------------------------------------------------
// Preenche a quantidade de parcelas do produto
// --------------------------------------------------------------
function preencheParcelas()
{
	var maxParcelas = document.getElementById('maxParcelas').value;
	var valorFinal = parseFloat(document.getElementById('valorTotalDoPedido').value) + (parseFloat(document.getElementById('valorFrete').value || 0));
	document.getElementById('parcelas').options.length = 1;
	for (var i=1; i<=maxParcelas; i++) {
		var valorParcela = valorFinal / i;
		var strOption = i+'x sem juros de R$ '+number_format(valorParcela, 2, ',', '.');
		document.getElementById('parcelas').options[i] = new Option(strOption, i);
	}
}

// --------------------------------------------------------------
// Alteração da forma de pagamento
// --------------------------------------------------------------
function alteraFormaPgto(el)
{
	var valorFinal = parseFloat(document.getElementById('valorTotalDoPedido').value) + (parseFloat(document.getElementById('valorFrete').value || 0));
	document.getElementById('valorTotalPedido').innerHTML = 'Valor Total: R$ '+number_format(valorFinal, 2, ',', '.');
	switch(el.value) {
		case '1':
		case '2':
		case '3':
			document.getElementById('selParcelas').style.display = 'block';
			preencheParcelas();
		break;
		default:
			document.getElementById('selParcelas').style.display = 'none';
		break;
	}
}

// --------------------------------------------------------------------------
// Remove a mascara na consulta do CEP 
// --------------------------------------------------------------------------
function removeMascara()
{
	Ext.get(document.body).unmask();
}