function obterItens(){
	xajax.$('loading').className = "aparece";
	xajax_obterItens();	
}

function selectVariacao(id){
	document.getElementById('idVariacao').value = id;
}

function selectVariacaoValor(id,valor,valorUnit){
	document.getElementById('idVariacao').value = id;
	
	document.getElementById('preco').innerHTML = valor;	
	document.getElementById('preco-un').innerHTML = valorUnit;		
}

function comprar(){
	
	if (document.getElementById('variacoes').value=='S'){
		if (document.getElementById('idVariacao').value==''){
			alert("Selecione uma das opções do produto!");				
		}else{
			xajax_comprar(document.getElementById('codigo').value,document.getElementById('quantidade').value,document.getElementById('idVariacao').value);
		}
		
		if (document.getElementById('codigoFreteGratis').value!=''){
			xajax_setFreteGratis(document.getElementById('codigo').value,document.getElementById('idVariacao').value,document.getElementById('codigoFreteGratis').value);
		}
		
	}else{
		xajax_comprar(document.getElementById('codigo').value,document.getElementById('quantidade').value,document.getElementById('idVariacao').value);
		
		if (document.getElementById('codigoFreteGratis').value!=''){
			xajax_setFreteGratis(document.getElementById('codigo').value,'',document.getElementById('codigoFreteGratis').value);
		}
	}
	
		
}

function alterarItem(codigo){
	xajax.$('loading').className = "aparece";
	var nome,nome1;
	nome = 'quantidade'+codigo;
	nome1 = 'presente'+codigo;
	
	if (document.getElementById(nome1)==null){
		xajax_alterarItem(codigo,document.getElementById(nome).value,'N');
	}else{	
		if (document.getElementById(nome1).checked){
			xajax_alterarItem(codigo,document.getElementById(nome).value,'S');
		}else{
			xajax_alterarItem(codigo,document.getElementById(nome).value,'N');
		}
	}
	
}

function removerItem(codigo){
	xajax.$('loading').className = "aparece";
	xajax_removerItem(codigo);
}

function calcularfrete(){
	xajax.$('loading').className = "aparece";	
	document.getElementById('idTransp').value = '';
	xajax_calcularfrete(document.getElementById('cepbuscar').value);
	return false;
}

function obterDadosFrete(cep,valor){
	xajax.$('loading').className = "aparece";
	xajax_calcularfrete(cep,valor);	
}

function setFreteGratis(campo,valor){
	document.getElementById('tipoFrete').value='gratis'
	if (document.getElementById(campo)){
		document.getElementById(campo).value = valor;
	}	
}

function selecionarFrete(idTransp){
	xajax.$('loading').className = "aparece";
	
	var nome,tipo,prazo;
	valor = 'valorfrete'+idTransp;
	tipo = 'tipofrete'+idTransp;
	prazo = 'prazoentrega'+idTransp;
	
	xajax_setFrete(idTransp,document.getElementById(valor).value,document.getElementById(tipo).value,document.getElementById(prazo).value);	
}

function recalcular(){
	xajax.$('loading').className = "aparece";	
	xajax_recalcular(xajax.getFormValues("FormCarrinho"));
}

function esvaziarCarrinho(){
	xajax_limparCarrinho();
}

function ocultarLoad(){
	xajax.$('loading').className = "desaparece";
}

function continuarComprando(){
	xajax_continuarComprando();
}

function finalizarcompra() {
	
	form = document.forms.FormCarrinho;
	
	if ((document.getElementById('nroItens').value=='') || (document.getElementById('nroItens').value=='0')){
		alert("Seu carrinho de compras está vazio! Escolha ao menos um produto para finalizar a compra!");
		return false;
	}
	
	if (document.getElementById('cep').value==''){
		alert("Informe o cep e clique na opção calcular.");
	}else if (document.getElementById('erroCep').value!='') {
		alert(document.getElementById('erroCep').value + ', selecione um cep válido!');
	}else{ 
		marcado = -1
		
		elmts = document.getElementsByName('transportadora[]');
		nro = elmts.length;
		
		var nome;
		
		for (i=0; i< nro; i++) {
			
			nome = elmts[i].id;
			
			if (document.getElementById(nome).checked) {
				marcado = i
				resposta = document.getElementById(nome).value;
			}
			
			/*if (document.getElementById(nome).value!='') {
				marcado = i
				resposta = document.getElementById(nome).value;
			}*/
		}
		
		if (document.getElementById('idTransp').value!=''){
			marcado = document.getElementById('idTransp').value;
		}
		
		if (document.getElementById('tipoFrete').value=='gratis'){
			marcado = 1;
		}
		
		if (document.getElementById('tipoFrete').value=='Gratis'){
			marcado = 1;
		}
		
		if (marcado == -1) {
			alert("Selecione uma opção de frete.");
			//form.transportadora[0].focus();
			return false;
		} else { //esse else so foi colocado para evitar que o form desse o submit
			xajax_finalizarCompra(xajax.getFormValues("FormCarrinho")); 
		} 
	}
}

function mascara(e,src,mask) {
	if(window.event){
		 _TXT = e.keyCode; 
	}else if(e.which){
		 _TXT = e.which; 
	}
	if(_TXT > 47 && _TXT < 58){
	 	var i = src.value.length; 
	 	var saida = mask.substring(0,1); 
	 	var texto = mask.substring(i)
	 	if (texto.substring(0,1) != saida){ 
	 		src.value += texto.substring(0,1); 
		}
	    return true; 
	 }else{ 
	 	if (_TXT != 8){ 
	 		return false; 
	 	}else{ 
	 		return true; 
	 	}
	 }
}

function abrirJanela(pagina, l, a){
	var winl = (screen.width - l)/2;
	var wint = (screen.height - a)/2
	winprops = 'height='+a+',width='+l+',top='+wint+',left='+winl+',scrollbars=no,resizable';
	window.open(pagina, 'popup', winprops)
}	
	
function ajuda(){
	var d = document.getElementById('ajuda');
	var c = document.getElementById('correios');
	c.onclick = function(){
		if(d.style.display == 'none'){
			d.style.display = 'block';
		}else{
			d.style.display = 'none';
		}
	}
}