function validaCampos(qual){
	if(qual == "2"){
		with (document.frm_cadastro){
			if(txt_nome.value == ""){
				alert('Por favor informe seu nome');
				txt_nome.focus();
				return false;
			}
			if(txt_email.value == ""){
				alert('Por favor informe seu e-mail');
				txt_email.focus();
				return false;
			}else{
				submit();
				return true;
			}
		}
	}else if(qual == "1"){
		with (document.frm_contato){
			if(txt_nome.value == ""){
				alert('Por favor informe seu nome');
				txt_nome.focus();
				return false;
			}
			if(txt_email.value == ""){
				alert('Por favor informe seu e-mail');
				txt_email.focus();
				return false;
			}else if(!validaEmail(txt_email.value)){
				alert('E-mail inválido, por favor digite novamente');
				txt_email.focus();
				txt_email.select();
				return false;
			}
			if(ls_assunto.value == "NM"){
				alert('Por favor selecione o assunto');
				ls_assunto.focus();
				return false;
			}
			if(txt_mensagem.value == ""){
				alert('Por favor digite uma mensagem');
				txt_mensagem.focus();
				return false;
			}
			
			if(ls_conheceu.value == "NM"){
				alert('Por favor informe como nos conheceu');
				ls_conheceu.focus();
				return false;
			}else{
				var indic = document.getElementById("cont_indique");
				if(indic.style.display == ""){
					if (txt_indicacao.value == ""){
						alert('O campo ESPECIFIQUE é de preenchimento obrigatório.');
						txt_indicacao.focus();
						return false;
					}
				}
			}
			submit();
			return true;
		}
	}else if(qual == "3"){
		with (document.frm_login){
			if(txt_email.value == ""){
				alert('Por favor, informe o seu email.')
				txt_email.focus();
				return false;
			}else if(!validaEmail(txt_email.value)){
				alert('E-mail inválido, por favor digite novamente');
				txt_email.focus();
				txt_email.select();
				return false;
			}
			if(txt_senha.value == ""){
				alert('Por favor, informe sua senha.')
				txt_senha.focus();
				return false;
			}
			submit();
			return true;
		}
	}
}

// Valida endereço de e-mail
function validaEmail(email){
	var resultado = false
	var emailStr = new String(email)
	var index = emailStr.indexOf("@");
	if (index > 0){
		var indice = emailStr.indexOf(".",index);
		if ((indice > index+1) && (emailStr.length > indice+1)){
			resultado = true;
		}
	}
	return resultado;
}

function verificaEmail(email, formu, tipo){
	var sMail = new String(email.value)
	var index = sMail.indexOf("@");
	if (index > 0){
		var pindex = sMail.indexOf(".",index);
		if ((pindex > index+1) && (sMail.length > pindex+1)){
			result = true;
		}else{
			result = false;
		}
	}else{
		result = false;
	}
	if(!result){
		alert('EMAIL inexistente, por favor insira um e-mail válido.');
		if(tipo == 1){
			formu.elements[0].focus();
			formu.elements[0].select();
		}
	}else{
		if(tipo == 1){
			formu.submit();
		}else{
			return result;
		}
	}
}

//valida CPF
function validaCpf(cpf){ 
	var i;
	cpf = cpf.substr(0,3) + cpf.substr(4,3) + cpf.substr(8,3) + cpf.substr(12,2);
	var c = cpf.substr(0,9); 
	var dv = cpf.substr(9,2); 
	var d1 = 0; 
	for (i = 0; i < 9; i++) { 
		d1 += c.charAt(i)*(10-i); 
	} 
	if (d1 == 0){
// 		alert("CPF Invalido") 
		return false; 
	} 
	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(0) != d1){ 
// 		alert("CPF Invalido") 
		return false;  
	}
	d1 *= 2; 
	for (i = 0; i < 9; i++){ 
		d1 += c.charAt(i)*(11-i); 
	} 
	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(1) != d1){ 
// 		alert("CPF Invalido")
		return false; 
	} 
	return true; 
} 

function limpaBtsAvanca(){
	for(i = 1; i < 8; i++){
		var temBotao = document.getElementsByName('btn_avanca_' + i);
		if(temBotao[0] != undefined){
			botao = eval('btn_avanca_' + i);
			botao.style.display = 'none';
		}
	}
}

function limpaBtsVoltar(){
	for(i = 0; i < 7; i++){
		var temBotao = document.getElementsByName('btn_volta_' + i);
		if(temBotao[0] != undefined){
			botao = eval('btn_volta_' + i);
			botao.style.display = 'none';
		}
	}
}

function avancaFoto(botao, p, btP, alt, larg){
	mostraFoto = "fig_produto/imagens.php?";
	mostraOk = false;
	limpaBtsAvanca();
	limpaBtsVoltar();
	
	var temBotao = document.getElementsByName(btP);
	if(temBotao[0] != undefined){
		btProximo = eval(btP);
		mostraOk = true;
	}
	proxima = mostraFoto + p;
	document.frm_guarda_foto.foto_real.value = p;
	
	document.frm_guarda_foto.alt_real.value = alt;
	document.frm_guarda_foto.larg_real.value = larg;
	
	document.img_produto.src = proxima;
	if(mostraOk){btProximo.style.display = '';}
}

function voltaFoto(botao, a, btA, alt, larg){
	mostraFoto = "fig_produto/imagens.php?";
	limpaBtsVoltar();
	limpaBtsAvanca();
	if(btA != "btn_volta_-1"){btAnterior = eval(btA);}
	anterior = mostraFoto + a;
	document.frm_guarda_foto.foto_real.value = a;
	
	document.frm_guarda_foto.alt_real.value = alt;
	document.frm_guarda_foto.larg_real.value = larg;
	
	document.img_produto.src = anterior;
	if(btA != "btn_volta_-1"){btAnterior.style.display = '';}
}

function mostraImagem(a,l,f){
	pg = "fig_produto/" + f;
	l = eval(l) + 17;
	a = eval(a) + 4;
	if(a > 520) a = 520
	if(l > 650) l = 650
	nova_janela = window.open('','mostra_imagem','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,menubar=no,width=' +l+ ',height=' + a);
	nova_janela.document.writeln('<HTML><HEAD><TITLE></TITLE></HEAD><BODY style="margin=0px;text-align=center;"><IMG style="cursor=hand;" src="'+pg+'" onClick="window.close()" alt="Clique na imagem para fechar"></BODY></HTML>');
	nova_janela.document.close();
}

function mostraImagemFotos(a,l,pg){
	l = eval(l) + 17;
	a = eval(a) + 4;
	if(a > 427) a = 435
	if(l > 640) l = 660
	nova_janela = window.open('','mostra_imagem','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,menubar=no,width=' +l+ ',height=' + a);
	nova_janela.document.writeln('<HTML><HEAD><TITLE></TITLE></HEAD><BODY style="margin=0px;text-align=center;"><IMG style="cursor=hand;" src="'+pg+'" onClick="window.close()" alt="Clique na imagem para fechar"></BODY></HTML>');
	nova_janela.document.close();
}

function criaLista(l, p, id_prod){
	window.open('cria_lista.php?listar=' + l + '&id_prod=' + id_prod + '&p=' + p, 'ifr_executa');
}

function mudaEspec(idEspec){
	document.frm_envia_carrinho.prod_espec.value = idEspec;
}


function abreDigitacao(fim, tpCampos, sel){
	var campo1 = document.getElementById("tt_indique");
	var campo2 = document.getElementById("cont_indique");
	if(tpCampos == 2){
		if((sel != "1") && (sel != "2")){
			fim = 0;
		}
	}
	if(fim == 1){
		campo1.style.display="";
		campo2.style.display="";
	}else if(fim == 0){
		campo1.style.display="none";
		campo2.style.display="none";
	}
}

function ajustarFone(input, evento){
	var tecla = evento.keyCode;
	valor = input.value;
	valor = valor.replace( "(", "" );
	valor = valor.replace( ")", "" );
	valor = valor.replace( " ", "" );
	valor = valor.replace( "-", "" );
	tamanhoMaximo = 10;
	tamanho = valor.length;
    if(!soNumero(evento)){
	    return false;
	} else { 
		if (tamanho < tamanhoMaximo && tecla != 8) {
		tamanho = valor.length + 1 ;
		}
		if (tecla == 8 ) {
			tamanho = tamanho - 1 ;
		}
		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
			if ( tamanho <= 4 ) { 
		 		input.value = valor ;
			}
		 	if ( (tamanho > 4) && (tamanho <= 8) ) {
		 		input.value = valor.substr(0,tamanho-4) + '-' + valor.substr( tamanho - 4, tamanho ) ;
			}
		 	if ( (tamanho >= 9) && (tamanho <= 10) ) {
				input.value = '(' + valor.substr(0,2) + ') ' + valor.substr(2,tamanho-6) + '-' + valor.substr(tamanho-4,tamanho) ;			
			}
		}
	}
	return true;
}

function soNumero(evento){
	var BACKSPACE=  8; 
	var TAB= 9;
    var DEL=  46; 
    var FRENTE=  39; 
    var TRAS=  37; 
    var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    
    if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)||(tecla == TAB)){
         return true; 
	}
    if ( tecla == 13 )     return false; 

	if ((tecla >= 48 && tecla <= 57) || (tecla == 44)) {
		return true;
	}else{
		evento.returnValue = false; 
		return false;
	}
}

//Permite digitar apenas letras
function soTexto(evento){
//65 - 90 A-Z
//97 - 122 a-z
    var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    
	if ((tecla>=65 && tecla<=90) || (tecla>=97 && tecla<=122) || (tecla == 32 || tecla == 38)) {
		return true;
	}else{
		evento.returnValue = false; 
		return false;
	}
}

//Completa a data com as barras "/"
function ajustarData(input, evento){
    if(!soNumero(evento)){
	    return false;
	} else { 
		if ((input.value.length == 2)||(input.value.length == 5)) {
		   input.value = input.value + "/" ;
		   }
	}
	return true;
}

//Formata CPF / CNPJ
function ajustarCpf(input, evento){
	tecla = evento.keyCode;
    if(!soNumero(evento)){
	    return false;
	}else{
		if((tecla<48)||(tecla>57)){
			evento.returnValue =false;
			return false;
		}else{
			if ((input.value.length == 3)||(input.value.length == 7)) {
				input.value = input.value + "." ;
			}
			if (input.value.length == 11) {
				input.value = input.value + "-" ;
			}
			if(input.value.length == 15){
				input.value = input.value.replace('.','');
				input.value = input.value.replace('.','');
				input.value = input.value.replace('-','');
				input.value = input.value.substr(0, 2) + '.' + input.value.substr(2, 3) + '.' + input.value.substr(5, 3) + '/' + input.value.substr(8, 4);
				input.value = input.value + "-" ;
			}
		}
	}
	return true;
}

//Formata CEP
function ajustarCep(input, evento){
    if(!soNumero(evento)){
	    return false;
	} else { 
		if (input.value.length == 5) {
		   input.value = input.value + "-";
		}
	}
	return true;
}

function iniciaValorEspec(){
	if(document.getElementById('ls_cor')){
		document.frm_envia_carrinho.prod_espec.value = document.frm_envia_carrinho.ls_cor.value;
	}else if(document.getElementById('ls_tamanho')){
		document.frm_envia_carrinho.prod_espec.value = document.frm_envia_carrinho.ls_tamanho.value;
	}
}

function abreCadastro(tipo){
	if(tipo == "1"){
		document.getElementById("novo_usuario").style.display = "none";
	}else{
		document.getElementById("novo_usuario").style.display = "";
	}
}

function calcFrete(subTotal){
	teste = document.getElementById("valor_frete");
	teste.innerHTML = "Calculando...&nbsp;&nbsp;";
	cepDestino = document.frm_carrinho.cep_destino.value;
	peso	  = document.frm_carrinho.fl_total_peso.value;
	window.open("extras/calcula-frete.php?cep_destino="+cepDestino+"&peso="+peso+"&sub_total="+subTotal, "ifr_executa");
}

function fecharPedido(){
	if(document.frm_carrinho.fl_valorfrete.value == ""){
		alert('Por favor informe seu CEP para calcular o valor do frete.');
		document.frm_carrinho.cep_destino.focus();
	}else{
		document.frm_carrinho.action='login.php'; 
		document.frm_carrinho.submit();
	}
}

function acrescentaRegime(){
	
}