function formatPreco(tmp_number) {
	var r = "";
	var n = String((Math.round(tmp_number * 100) / 100));
	var c, i2;
	var offset = 0;
	
	if (n.indexOf(".") > -1) {
		var arr = n.split(".");
		
		offset = 3;
		r = "," + arr[1];
		if (arr[1].length == 1) {
			offset = 2;
			r = r + "0";
		}
	} else {
		r = ",00";
	}
	
	i2 = 0;
	for (var i = (n.length - offset - 1); i >= 0; i--) {
		c = n.charAt(i);

		if (c == ".") {
			break;
		}
		
		i2++;
		if (i2 > 3) {
			r =  c + "." + r;	
			i2 = 1;
		} else {
			r = c + r;
		}
		
	}
	
	return r;
}


var valor_frete = 0;
var valor_subtotal = 0;
var valor_total = 0;
var valor_inicial = 0;

function calcFrete() {
	showProximoPasso(false);
	
	if ((document.getElementById('CEP_CEP1').value == "") || (document.getElementById('CEP_CEP1').value.length < 9)) {
		document.getElementById('CEP_CEP1').focus();
		alert("Digite o cep corretamente");
		document.getElementById('CEP_CEP1').focus();
		
		return;
	}

	document.getElementById('id_frete').innerHTML = "Carregando...";
	var a = new Ajax();
	a.unique = true;
	a.onLoad = function () {
		this.runJS(this.html);

		document.getElementById('resposta_cep').innerHTML += this.html + "<br>";
		
		calcTotal();
	}
	cep = document.getElementById('CEP_CEP1').value;
	a.get("ajax_frete.php?cep=" + cep.replace('-',''));
}

function calcTotal() {
	document.getElementById('id_subtotal').innerHTML = "R$ " + formatPreco(valor_subtotal);
	
	if (valor_frete > 0) {
		document.getElementById('id_frete').innerHTML = "R$ " + formatPreco(valor_frete);
		
		showProximoPasso(true);
	} else {
		if (document.getElementById('id_frete')) {
			document.getElementById('id_frete').innerHTML = '<a href="#frete" onClick="javascript: calcFrete();">Calcular</a>';
			
			showProximoPasso(false);
		} else {
			showProximoPasso(true);
		}
	}
	valor_subtotal++; valor_subtotal--;
	valor_frete++; valor_frete--;
	
	document.getElementById('id_total').innerHTML = "R$ " + formatPreco(valor_subtotal + valor_frete);
}


function showProximoPasso(tmp_mostrar) {
	if (tmp_mostrar) {
		document.getElementById('proximo_passo').style.display = '';
		document.getElementById('proximo_passo_explicacao').style.display = 'none';
	} else {
		document.getElementById('proximo_passo').style.display = 'none';
		document.getElementById('proximo_passo_explicacao').style.display = '';
	}
}

function nextStep() {
	document.location = "identificacao.php";
}

function updateList() {
	document.location = "index.php";
}

function setValorinicial(valor) {
	valor_inicial = valor;
}

function atualizaCarrinho(id,valor,indice) {
	if (valor_inicial != valor) {
		var a = new Ajax();
		a.onLoad = function () {
			alert(this.html);
			
			if (!document.getElementById('id_frete')) {
				showProximoPasso(true);
			}
		}
		a.get("rotinas_carrinho.php?rotina=alterar&indice=" + indice + "&valor="+valor+"&dado=quantidade");
		showProximoPasso(false);
	}
	
	valor_inicial = 0;
}

function trim(tmp_value) {
	var r = /\s*((\S+\s*)*)/g;
	var r2 = /((\s*\S+)*)\s*/g;
	
	return tmp_value.replace(r, "$1").replace(r2, "$1");
}
