/*-------------------------------------------------------------------------------------------------
	SOMENTE NUMEROS
-------------------------------------------------------------------------------------------------*/

function soNumeros(e)
{
	var tecla = (window.event) ? event.keyCode : e.which;
	
	if ((tecla > 47 && tecla < 58)) 
	{
		return true;
	}
	else 
	{
		if (tecla != 8) 
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}


/*-------------------------------------------------------------------------------------------------
	MANIPULAÇÃO FORM
-------------------------------------------------------------------------------------------------*/

$(document).ready(function(){
	
	$("#tamanho").change(function(){
		var val1 = $("#tamanho option:selected").attr("valor");
		var val2 = $("#tamanho option:selected").attr("valor_desconto");
		var text = "";
		
		if (val2 != "0")
		{
			text += "<span class='texto_cinza_14' style='text-decoration:line-through'>de R$ "+val1+"</span><br>";
			text += "<span class='texto_cinza_18'>por R$ "+val2+"</span>";
		}
		else
		{
			text += "<span class='texto_cinza_18'>R$ "+val1+"</span>";	
		}
		
		
		$("#preco").html(text);										
	});	
	
	var posL = ($(window).width()-400)/2;
	var posT = 170;
	
	$("#pop_espelho, #pop_cartela").css("left", posL+"px");
	$("#pop_espelho, #pop_cartela").css("top", posT+"px");
	
	$(window).resize(function(){
		var posL = ($(window).width()-400)/2;
		var posT = 170;
		$("#pop_espelho, #pop_cartela").css("left", posL+"px");
		$("#pop_espelho, #pop_cartela").css("top", posT+"px");						  
	});
	
});


/*-------------------------------------------------------------------------------------------------
	ALERT
-------------------------------------------------------------------------------------------------*/

function alertSobre(msg)
{
	alert(msg);
}


/*-------------------------------------------------------------------------------------------------
	BUSCA PELA TAG
---------------------------------------------------------------------------------------------------*/

function buscaPorTag(tag)
{
	location.href = "?idTela=2&busca=["+escape(tag)+",0]";
}


/*-------------------------------------------------------------------------------------------------
	ENVIA COMPRA
---------------------------------------------------------------------------------------------------*/

function validaCompra()
{
	with (document.compra)
	{
		if (cores.value > 0)
		{
			var sel = 0;
			
			for (i=0; i<cores.value; i++)
			{
				if (eval("cor"+i).value != "")
				{
					sel += 1;
				}
			}
			
			if (sel == 0 || sel < cores.value)
			{
				alert("Você deve selecionar as cores desejadas.");
				return;
			}
		}
		if (isEmpty(tamanho))
		{
			alert("Selecione o tamanho desejado.");
			tamanho.focus();
			return;
		}
		if (isEmpty(espelho))
		{
			alert("Selecione se você deseja ou não o espelhamento.");
			espelho.focus();
			return;
		}
		/*if (isEmpty(quantidade))
		{
			alert("Preencha a quantidade desejada.");
			quantidade.focus();
			return;
		}*/
		
		submit();
	}
}



/*-------------------------------------------------------------------------------------------------
	ENVIA CADASTRO DE NEWS
---------------------------------------------------------------------------------------------------*/

function validaNews()
{
	with (document.cadastro)
	{
		if (isEmpty(nome))
		{
			alert("Preencha seu nome.");
			nome.focus();
			return;
		}
		if (isEmpty(email))
		{
			alert("Preencha seu e-mail.");
			email.focus();
			return;
		}
		if (!isEmail(email))
		{
			alert("O e-mail digitado é inválido.");
			email.focus();
			return;
		}
		
		submit();
	}
}



/*-------------------------------------------------------------------------------------------------
	ENVIA A PALAVRA A SER PESQUISADA
---------------------------------------------------------------------------------------------------*/

function validaPesquisa()
{
	with (document.pesquisa)
	{
		if (isEmpty(busca) || isDefValue(busca, "Palavra-chave"))
		{
			alert("Preencha a palavra chave que deseja buscar.");
			busca.focus();
			return;
		}
		
		location.href = "?idTela=2&busca=["+escape(busca.value)+","+categoria.value+"]";
	}
}


/*-------------------------------------------------------------------------------------------------
	ENVIA LOGIN
---------------------------------------------------------------------------------------------------*/

function validaLogin()
{
	with (document.login)
	{
		if (isEmpty(email))
		{
			alert("Preencha seu e-mail.");
			email.focus();
			return;
		}
		if (!isEmail(email))
		{
			alert("O e-mail digitado é inválido.");
			email.focus();
			return;
		}
		if (isEmpty(senha))
		{
			alert("Preencha sua senha.");
			senha.focus();
			return;
		}
		
		action = "login.php";
		submit();
	}
}


/*-------------------------------------------------------------------------------------------------
	ENVIA ESQUECI SENHA
---------------------------------------------------------------------------------------------------*/

function validaEsqueci()
{
	with (document.login)
	{
		if (isEmpty(email))
		{
			alert("Preencha seu e-mail.");
			email.focus();
			return;
		}
		if (!isEmail(email))
		{
			alert("O e-mail digitado é inválido.");
			email.focus();
			return;
		}
		
		action = "esqueci.php";
		submit();
	}
}


/*-------------------------------------------------------------------------------------------------
	ENVIA CADASTRO
---------------------------------------------------------------------------------------------------*/

function validaCadastro()
{
	with (document.cadastro)
	{
		if (isEmpty(nome))
		{
			alert("Preencha seu nome ou razão social da empresa.");
			nome.focus();
			return;
		}
		if (isEmpty(cgc))
		{
			alert("Preencha seu cpf ou cnpj da empresa.");
			cgc.focus();
			return;
		}
		if (!isCpfCnpj(cgc.value))
		{
			alert("O cpf ou cnpj digitado o inválido.");
			cgc.focus();
			return;
		}
		if (isEmpty(email))
		{
			alert("Preencha seu e-mail.");
			email.focus();
			return;
		}
		if (!isEmail(email))
		{
			alert("O e-mail digitado é inválido.");
			email.focus();
			return;
		}
		if (emailok.value != email.value)
		{
			alert("A confirmação de e-mail não confere com o e-mail digitado.");
			emailok.focus();
			return;
		}
		if (isEmpty(ddd1) || ddd1.value.length < 2)
		{
			alert("Preencha o código de área (DDD).");
			ddd1.focus();
			return;
		}
		if (isEmpty(fone1) || fone1.value.length < 8)
		{
			alert("Preencha um telefone de contato.");
			fone1.focus();
			return;
		}
		if (isEmpty(endereco))
		{
			alert("Preencha o endereço.");
			endereco.focus();
			return;
		}
		if (isEmpty(numero))
		{
			alert("Preencha o número.");
			numero.focus();
			return;
		}
		if (isEmpty(bairro))
		{
			alert("Preencha o bairro.");
			bairro.focus();
			return;
		}
		if (isEmpty(cep1) || isEmpty(cep2))
		{
			alert("Preencha o cep.");
			cep1.focus();
			return;
		}
		if (cep1.value.length < 5 || cep2.value.length < 3)
		{
			alert("O cep deve conter 8 digitos.");
			cep1.focus();
			return;
		}
		if (isEmpty(cidade))
		{
			alert("Preencha a cidade.");
			cidade.focus();
			return;
		}
		if (isEmpty(estado))
		{
			alert("Selecione/Preencha o estado.");
			estado.focus();
			return;
		}
		if (isEmpty(pais))
		{
			alert("Selecione o país.");
			pais.focus();
			return;
		}
		if (isEmpty(senha))
		{
			alert("Preencha uma senha.");
			senha.focus();
			return;
		}
		if (senha.value.length < 4)
		{
			alert("A senha deve conter no mínimo 4 caracteres.");
			senha.focus();
			return;
		}
		if (senhaok.value != senha.value)
		{
			alert("A confirmação de senha não confere com a senha digitada.");
			senhaok.focus();
			return;
		}
		
		submit();
	}
}

/*-------------------------------------------------------------------------------------------------
	ENVIA ENTREGA
---------------------------------------------------------------------------------------------------*/

function validaEntrega()
{
	with (document.entrega)
	{
		if (isEmpty(endereco))
		{
			alert("Preencha o endereço.");
			endereco.focus();
			return;
		}
		if (isEmpty(numero))
		{
			alert("Preencha o número.");
			numero.focus();
			return;
		}
		if (isEmpty(bairro))
		{
			alert("Preencha o bairro.");
			bairro.focus();
			return;
		}
		if (isEmpty(cep1) || isEmpty(cep2))
		{
			alert("Preencha o cep.");
			cep1.focus();
			return;
		}
		if (cep1.value.length < 5 || cep2.value.length < 3)
		{
			alert("O cep deve conter 8 digitos.");
			cep1.focus();
			return;
		}
		if (isEmpty(cidade))
		{
			alert("Preencha a cidade.");
			cidade.focus();
			return;
		}
		if (isEmpty(estado))
		{
			alert("Selecione/Preencha o estado.");
			estado.focus();
			return;
		}
		if (isEmpty(pais))
		{
			alert("Selecione o país.");
			pais.focus();
			return;
		}
		
		submit();
	}
}


/*-------------------------------------------------------------------------------------------------
	ENVIA CALCULO DE FRETE
---------------------------------------------------------------------------------------------------*/

function validaFrete()
{
	with (document.correios)
	{
		if (isEmpty(cep) || cep.value.length < 8)
		{
			alert("Preencha seu cep.");
			cep.focus();
			return;
		}
		if (frete[0].checked == false && frete[1].checked==false && frete[2].checked==false)
		{
			alert("Selecione a modalidade de envio.");
			return;
		}
		
		action = "frete.php";
		submit();
	}
}


/*-------------------------------------------------------------------------------------------------
	VERIFICA O CUPOM DE DESCONTO É VÁLIDO
---------------------------------------------------------------------------------------------------*/

function validaCupom()
{
	with (document.correios)
	{
		if (isEmpty(cupom) || cupom.value.length < 5)
		{
			alert("Preencha o código do cupom.");
			cupom.focus();
			return;
		}
		
		action = "cupom.php";
		submit();
	}
}


/*-------------------------------------------------------------------------------------------------
	ENVIA COMPRA
---------------------------------------------------------------------------------------------------*/

function validaCompraCarrinho(compra, cupom)
{
	with (document.carrinho)
	{
		if (isEmpty(frete_destino) || frete_destino.value.length < 8)
		{
			alert("Preencha seu cep antes de prosseguir com sua compra.");
			cep.focus();
			return;
		}
		if (isEmpty(frete_codigo))
		{
			alert("Selecione a modalidade de envio para prosseguir com sua compra.");
			return;
		}
		if (isEmpty(frete_valor))
		{
			alert("Você deve calcular o valor do frete antes de prosseguir com sua compra.");
			return;
		}
		if (frete_valor.value > 0 || frete_valor.value != "")
		{
			if (frete_ok.value == 0)
			{
				alert("Você deve recalcular o valor do frete, pois adicinou ou removeu produtos de seu carrinho de compras.");
				return;
			}
		}
		if (cupom > compra)
		{
			if (confirm("Identificamos que restou R$ "+(cupom-compra)+" de seu cupom para gastar, deseja continuar comprando ou deseja finalizar a compra?\nLembramos que ao finalizar a compra o crédito restante não poderá ser utilizado posteriormente.\n\nOK=Finalizar a Compra | Cancelar=Continuar Comprando"))
			{
				submit();
			}
			else
			{
				location.href = "index.php?idTela=2";
			}
		}
		else
		{
			submit();
		}
	}
}


/*-------------------------------------------------------------------------------------------------
	ENVIA COMPRA
---------------------------------------------------------------------------------------------------*/

function atualizaCarrinho()
{
	with (document.carrinho)
	{
		action = "atualiza.php";		
		submit();
	}
}


/*-------------------------------------------------------------------------------------------------
	RESET FORM
---------------------------------------------------------------------------------------------------*/

function resetForm(form)
{
	document[form].reset();	
}


/*-------------------------------------------------------------------------------------------------
	VALIDA��ES PADR�O
---------------------------------------------------------------------------------------------------*/

function isEmpty(str)
{
	if (str.value == "" || str.value.length == 0)
	{
		return true;	
	}
	else
	{
		return false;
	}
}

function isDefValue(str, def)
{
	if (str.value == def)
	{
		return true;	
	}
	else
	{
		return false;
	}
}

function isInvalidFormat(str)
{
	var er = new RegExp(/[@!#$%&*+=?|-]/);
	
	if (typeof(str.value) == "string")
	{
        if (er.test(str.value))
		{ 
			return true; 
		}
    }
	else if (typeof(str) == "object")
	{
        if (er.test(str))
		{
           return true;
        }
    }
	else
	{
        return false;
    }
}

function isEqual(str1, str2)
{
	if (str2.value == str1.value)
	{
		return true;	
	}
	else
	{
		return false;
	}
}

function isNotEqual(str, val)
{
	if (str.value != val)
	{
		return true;	
	}
	else
	{
		return false;
	}
}

function isDate(str) 
{
	var dia = str.value.substr(0,2);
	var mes = str.value.substr(3,2);
	var ano = str.value.substr(6,4);
	
	var data = new Date();
	var intDia;
	var intMes;
	var intAno;

	data.setFullYear(ano, mes-1, dia);

	intDia = (data.getDate() == dia);
	intMes = (data.getMonth() == mes-1);
	intAno = (data.getFullYear() == ano);

	if (intDia && intMes && intAno)
	{
		return true;
	}
	else
	{
		return false;	
	}
}

function isEmail(str)
{
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
   
    if (typeof(str.value) == "string")
	{
        if (er.test(str.value))
		{ 
			return true; 
		}
    }
	else if (typeof(str) == "object")
	{
        if (er.test(str))
		{
           return true;
        }
    }
	else
	{
        return false;
    }
}

function isImage(str)
{
    var er = new RegExp(/[\w]+\.(gif|jpg|bmp|png|jpeg)$/gi);
   
    if (typeof(str.value) == "string")
	{
        if (er.test(str.value))
		{ 
			return true; 
		}
    }
	else if (typeof(str) == "object")
	{
        if (er.test(str))
		{
           return true;
        }
    }
	else
	{
        return false;
    }
}

function isDDD(str)
{
	var ddd = str.value.substr(1,2);
	
	if (ddd == "00" || str.length == 0 || str == "")
	{
		return false;
	}
	else
	{
		return true;	
	}
}

function isPhoneNumber(str)
{
	var fone   = str.value.substr(5,9);
	var digito = str.value.substr(5,1);
	var numero = fone.replace(".", "");	
	
	if (numero == "00000000" || numero == "11111111" || numero == "22222222" || numero == "33333333" || numero == "44444444" || numero == "55555555" || numero == "66666666" || numero == "77777777" || numero == "88888888" || numero == "99999999" || str.value.length < 14 || str.value == "" || digito == 0 || digito == 8 || digito == 9)
	{
		return false;
	}
	else
	{
		return true;	
	}
}

function isCelularNumber(str)
{
	var fone   = str.value.substr(5,9);
	var digito = str.value.substr(5,1);
	var numero = fone.replace(".", "");
	
	if (numero == "00000000" || numero == "11111111" || numero == "22222222" || numero == "33333333" || numero == "44444444" || numero == "55555555" || numero == "66666666" || numero == "77777777" || numero == "88888888" || numero == "99999999" || str.value.length < 14 || str.value == "" || digito < 8)
	{
		return false;

	}
	else
	{
		return true;	
	}
}

function isInteger(str)
{
	var er = new RegExp(/^[0-9]+$/);
	
	if (er.test(str.value)) 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}

function testFileType(fileName, fileTypes)
{
	if (!fileName) return;
	
	dots = fileName.split(".");
	
	//get the part AFTER the LAST period.
	fileType = "." + dots[dots.length-1];
	
	return (fileTypes.join(".").indexOf(fileType) != -1) ? true : false;
}

function calculaIdade(d, m, y)
{
	var dia = parseInt(d);
	var mes = parseInt(m)-1;
	var ano = parseInt(y);
	
	var age;
	var now = new Date();
	
	diaAtual = now.getDate();
	mesAtual = now.getMonth();
	anoAtual = now.getFullYear();
	
	if ((mesAtual > mes)||(mesAtual == mes & diaAtual >= dia))
	{
		age = ano;
	}
	else
	{
		age = ano+1;
	}
	
	return anoAtual-age;
}
