function validaNumeroCPF(objcpf){
var cpf = document.getElementById(objcpf).value;
exp = /\.|\-/g
cpf = cpf.toString().replace( exp, "" );

	if (!cpf=="") {
		var digitoDigitado = eval(cpf.charAt(9)+cpf.charAt(10));
		var soma1=0, soma2=0;
		var vlr =11;
		
		for(i=0;i<9;i++){
		soma1+=eval(cpf.charAt(i)*(vlr-1));
		soma2+=eval(cpf.charAt(i)*vlr);
		vlr--;
		}
		soma1 = (((soma1*10)%11)==10 ? 0:((soma1*10)%11));
		soma2=(((soma2+(2*soma1))*10)%11);
		
		var digitoGerado=(soma1*10)+soma2;
		
		if(digitoGerado!=digitoDigitado && digitoDigitado!=0) {
			if (digitoDigitado!=10) {
				return false;
			} else {
				return true
			}
		} else {
			return true;
		}
	}
}

function validaCEP(objcep,objuf) {
	var uf = document.getElementById(objuf).value;
	var cep = document.getElementById(objcep).value
	
	cep = cep.replace(".","");
	
	if (uf=="SP") regex = /^([1][0-9]{3}|[01][0-9]{4})-[0-9]{3}$/;
	else if(uf=="RJ") regex = /^[2][0-8][0-9]{3}-[0-9]{3}$/;
	else if(uf=="MS") regex = /^[7][9][0-9]{3}-[0-9]{3}$/;
	else if(uf=="MG") regex = /^[3][0-9]{4}-[0-9]{3}$/;
	else if(uf=="MT") regex = /^[7][8][8][0-9]{2}-[0-9]{3}$/;
	else if(uf=="AC") regex = /^[6][9]{2}[0-9]{2}-[0-9]{3}$/;
	else if(uf=="AL") regex = /^[5][7][0-9]{3}-[0-9]{3}$/;
	else if(uf=="AM") regex = /^[6][9][0-8][0-9]{2}-[0-9]{3}$/;
	else if(uf=="AP") regex = /^[6][89][9][0-9]{2}-[0-9]{3}$/;
	else if(uf=="BA") regex = /^[4][0-8][0-9]{3}-[0-9]{3}$/;
	else if(uf=="CE") regex = /^[6][0-3][0-9]{3}-[0-9]{3}$/;
	else if(uf=="DF") regex = /^[7][0-3][0-6][0-9]{2}-[0-9]{3}$/;
	else if(uf=="ES") regex = /^[2][9][0-9]{3}-[0-9]{3}$/;
	else if(uf=="GO") regex = /^[7][3-6][7-9][0-9]{2}-[0-9]{3}$/;
	else if(uf=="MA") regex = /^[6][5][0-9]{3}-[0-9]{3}$/;
	else if(uf=="PA") regex = /^[6][6-8][0-8][0-9]{2}-[0-9]{3}$/;
	else if(uf=="PB") regex = /^[5][8][0-9]{3}-[0-9]{3}$/;
	else if(uf=="PE") regex = /^[5][0-6][0-9]{3}-[0-9]{3}$/;
	else if(uf=="PI") regex = /^[6][4][0-9]{3}-[0-9]{3}$/;
	else if(uf=="PR") regex = /^[8][0-7][0-9]{3}-[0-9]{3}$/;
	else if(uf=="RN") regex = /^[5][9][0-9]{3}-[0-9]{3}$/;
	else if(uf=="RO") regex = /^[7][8][9][0-9]{2}-[0-9]{3}$/;
	else if(uf=="RR") regex = /^[6][9][3][0-9]{2}-[0-9]{3}$/;
	else if(uf=="RS") regex = /^[9][0-9]{4}-[0-9]{3}$/;
	else if(uf=="SC") regex = /^[8][89][0-9]{3}-[0-9]{3}$/;
	else if(uf=="SE") regex = /^[4][9][0-9]{3}-[0-9]{3}$/;
	else if(uf=="TO") regex = /^[7][7][0-9]{3}-[0-9]{3}$/;
	
	if (!uf=="" && !cep=="" && !(regex.test(cep)))
		return false;
    else
		return true;
}

function copy(text2copy) {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
}


$(document).ready(function(){
			
	$(document).mouseup(function() {
		copy("Equilibradiet.com.br");
	});
	
	$(document).keyup(function() {
		copy("Equilibradiet.com.br");
	});	

	
   $("#txtpeso").click(function() {
		$("#titulo-peso").html("Você está no seu<br />peso ideal?");
	});
   
   $("#txtaltura").click(function() {
		$("#titulo-peso").html("Você está no seu<br />peso ideal?");
	});
   
   $("#frmprograma").submit(function(){  
		if ($("#txtpeso").val()=="" || !(/^[\d.,]*$/.test($("#txtpeso").val()))) {
			window.alert("Informe um peso válido. Utilize apenas números.");
			$("#txtpeso").focus();
			return false;
		} else if ($("#txtaltura").val()=="" || !(/^[\d.,]*$/.test($("#txtaltura").val()))) {
			window.alert("Informe uma altura válida.");
			$("#txtaltura").focus();
			return false;
		} else {
			IMC($("#txtpeso").val(),$("#txtaltura").val(),"#hdimc","#hdpesomin","#hdpesomax");
			return true;
		}
	});
   
   
    $("#frmcontato").submit(function(){  
		if ($("#txtcontatonome").val()=="") {
			window.alert("Por favor, informe seu nome.");
			$("#txtcontatonome").focus();
			return false;
		} if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#txtcontatoemail").val()))) {
			window.alert("Por favor, informe um e-mail válido.");
			$("#txtcontatoemail").focus();
			return false;
		} else if ($("#txtcontatotelefone").val()=="" || !(/^[\d ()-]*$/.test($("#txtcontatotelefone").val()))) {
			window.alert("Por favor, informe um telefone de contato válido.");
			$("#txtcontatotelefone").focus();
			return false;		
		} else if ($("#slcontatoassunto").val()=="") {
			window.alert("Por favor, informe o assunto do contato.");
			$("#slcontatoassunto").focus();
			return false;
		} else if ($("#tacontatomensagem").val()=="") {
			window.alert("Por favor, informe sua mensagem.");
			$("#tacontatomensagem").focus();
			return false;
		} else {
			return true;
		}
	});
	
	$("#frmrecomende").submit(function(){  
		if ($("#txtindicanome").val()=="") {
			window.alert("Por favor, informe seu nome.");
			$("#txtindicanome").focus();
			return false;
		} if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#txtindicaemail").val()))) {
			window.alert("Por favor, informe um e-mail válido.");
			$("#txtindicaemail").focus();
			return false;
		} else if ($("#txtindicadonome").val()=="") {
			window.alert("Por favor, informe seu nome.");
			$("#txtindicadonome").focus();
			return false;
		} if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#txtindicadoemail").val()))) {
			window.alert("Por favor, informe um e-mail válido.");
			$("#txtindicadoemail").focus();
			return false;
		} else {
			return true;
		}
	});

  function IMC(peso,altura, resultado, peso_min, peso_max) {
 
 	var _altura = altura;
	var _peso = peso;
	
	if (_altura.charAt(1)==",") {
		_altura = _altura.replace(/,/,".");
	} else if (_altura.charAt(1)!=".") {
		_altura = _altura.charAt(0)+"."+_altura.charAt(1)+_altura.charAt(2);
	}	
	
	if (_peso.charAt(2)==",") {
		_peso = _peso.replace(/,/,".");
	} else if (_peso.charAt(2)!="." && _peso < 100) {
		_peso = _peso.charAt(0)+_peso.charAt(1)+"."+_peso.charAt(2)+"0";
	}	
	
	var quadrado = (_altura * _altura);
	var calculo = (_peso/quadrado);	
	calculo = parseFloat(Math.round(calculo*100)/100);
	
	var minpeso = Math.round(Math.round((((_altura*100)*(_altura*100))*18.5)/100)/100);
	var maxpeso = Math.round(Math.round((((_altura*100)*(_altura*100))*24.9)/100)/100);
	
	$("#txtaltura").val(_altura);
	$("#txtpeso").val(_peso);
	$(resultado).val(calculo);
	$(peso_min).val(minpeso);
	$(peso_max).val(maxpeso);
  }
  
  var getIMC = $("#imc").html();
  
  if (getIMC < 15.99) {
		$("#magreza-iii").html("<img src='pics/arrow-voce.gif' />");
  	} else if ((getIMC >= 16) && (getIMC < 16.99)) {
		$("#magreza-ii").html("<img src='pics/arrow-voce.gif' />");
  	} else if ((getIMC >= 17) && (getIMC < 18.49)) {
		$("#magreza-i").html("<img src='pics/arrow-voce.gif' />");
  	} else if ((getIMC >= 18.50) && (getIMC < 24.99)) {
		$("#normalidade").html("<img src='pics/arrow-voce.gif' />");
	} else if ((getIMC >= 25) && (getIMC < 29.99)) {
		$("#sobrepeso").html("<img src='pics/arrow-voce.gif' />");
  	} else if ((getIMC >= 30) && (getIMC < 34.99)) {
		$("#obesidade-i").html("<img src='pics/arrow-voce.gif' />");
  	} else if ((getIMC >= 35) && (getIMC < 39.99)) {
		$("#obesidade-ii").html("<img src='pics/arrow-voce.gif' />");
  	} else if (getIMC >= 40) {
		$("#obesidade-iii").html("<img src='pics/arrow-voce.gif' />");
 	}

	$("#frmcadastro").submit(function(){  
		var curdate = new Date();
		var year = curdate.getFullYear();

		if (!document.getElementById("rdcadastrosexof").checked && !document.getElementById("rdcadastrosexom").checked) {
			window.alert("Por favor, informe seu sexo");
			$("#rdcadastrosexof").focus();
			return false;
		} else if ($("#slcadastrodianascimento").val()=="") {
			window.alert("Por favor, informe o dia do seu nascimento");
			$("#slcadastrodianascimento").focus();
			return false;
		} else if ($("#slcadastromesnascimento").val()=="") {
			window.alert("Por favor, informe o mês do seu nascimento");
			$("#slcadastromesnascimento").focus();
			return false;
		} else if ($("#slcadastroanonascimento").val()=="") {
			window.alert("Por favor, informe o ano do seu nascimento");
			$("#slcadastroanonascimento").focus();
			return false;
		} else if (!(/^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/.test($("#slcadastrodianascimento").val()+"/"+$("#slcadastromesnascimento").val()+"/"+$("#slcadastroanonascimento").val()))) {
			window.alert("Por favor, informe uma data válida de nascimento");
			$("#slcadastroanonascimento").focus();
			return false;
		} else if ((year-$("#slcadastroanonascimento").val() < 18) || (year-$("#slcadastroanonascimento").val() > 59)) {
			$("#box-info-idade").show("small");
			$("#box-info-idade").html("<p class='box-info-small'>De acordo com a idade informada, você não está apto a receber uma orientação nutricional do Equilibra Diet.\n\nProcure um profissional adequado para fazer o seu acompanhamento. Caso seja de seu interesse, estamos disponíveis no Equilibra Centro de Nutrição (<a href='http://www.equilibraweb.com' target='_blank'>www.equilibraweb.com</a>).</p>");
			$("#slcadastroanonascimento").focus();
			return false;
		} else if ($("#txtcadastronome").val()=="") {
			window.alert("Por favor, informe seu nome.");
			$("#txtcadastronome").focus();
			return false;
		} else if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#txtcadastroemail").val()))) {
			window.alert("Por favor, informe um e-mail válido.");
			$("#txtcadastroemail").focus();
			return false;
		} else if ($("#txtcadastroemail").val()!=$("#txtcadastroconfirmaemail").val()) {
			window.alert("E-mail e confirmação de e-mail não coincidem.");
			$("#txtcadastroconfirmaemail").focus();
			return false;
		} else if ($("#txtcadastrocidade").val()=="") {
			window.alert("Por favor, informe sua cidade.");
			$("#txtcadastrocidade").focus();
			return false;
		} else if ($("#txtcadastrocidade").val().length <= 5) {
			window.alert("Por favor, informe o nome da sua cidade por extenso.");
			$("#txtcadastrocidade").focus();
			return false;
		} else if ($("#slcadastroestado").val()=="") {
			window.alert("Por favor, informe seu estado.");
			$("#slcadastroestado").focus();
			return false;
		} else if ($("#slcadastrocomoconheceu").val()=="") {
			window.alert("Por favor, informe como nos conheceu.");
			$("#slcadastrocomoconheceu").focus();
			return false;
		} else if ($("#slcadastrocomoconheceu").val()=="Outros" && $("#txtcadastrocomoconheceu").val()=="") {
			window.alert("Por favor, informe como nos conheceu.");
			$("#txtcadastrocomoconheceu").focus();
			return false;
		} else {
			return true;
		}
	});
	
	$("#slcadastrocomoconheceu").change(function(){
		if ($("#slcadastrocomoconheceu").val()=="Outros") {
			$("#lblcadastrocomoconheceu").show("small");
		} else {
			$("#lblcadastrocomoconheceu").hide("small");
		}
	});
	
	$("#slcadastroanonascimento").click(function() {
		$("#box-info-idade").hide("small");		 
	});
	
	$("#chkbalteracao10").click(function() {
		if (document.getElementById("chkbalteracao10").checked) {
			document.getElementById("chkbalteracao1").checked = false;
			document.getElementById("chkbalteracao2").checked = false;
			document.getElementById("chkbalteracao3").checked = false;
			document.getElementById("chkbalteracao4").checked = false;
			document.getElementById("chkbalteracao5").checked = false;
			document.getElementById("chkbalteracao6").checked = false;
			document.getElementById("chkbalteracao7").checked = false;
			document.getElementById("chkbalteracao8").checked = false;
			document.getElementById("chkbalteracao9").checked = false;
			document.getElementById("chkbalteracao10").checked = true;
		}
	});
	
	for (i=1;i<10;i++) {
		$("#chkbalteracao"+i).click(function() {
			if (document.getElementById("chkbalteracao"+i).checked) {
				document.getElementById("chkbalteracao10").checked = false;
			}
		});
	}
	
	$("#frmcompletacadastro").submit(function(){  
		if ($("#slcadastrodiafim").val()=="") {
			window.alert("Por favor, informe o dia para você chegar ao peso alvo");
			$("#slcadastrodiafim").focus();
			return false;
		} else if ($("#slcadastromesfim").val()=="") {
			window.alert("Por favor, informe o mês para você chegar ao peso alvo");
			$("#slcadastromesfim").focus();
			return false;
		} else if ($("#slcadastroanofim").val()=="") {
			window.alert("Por favor, informe o ano para você chegar ao peso alvo");
			$("#slcadastroanofim").focus();
			return false;
		} else if (!(/^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/.test($("#slcadastrodiafim").val()+"/"+$("#slcadastromesfim").val()+"/"+$("#slcadastroanofim").val()))) {
			window.alert("Por favor, informe uma data válida para você chegar ao peso alvo");
			$("#slcadastrodiafim").focus();
			return false;
		} else if (!document.getElementById("chkbalteracao1").checked && !document.getElementById("chkbalteracao2").checked && !document.getElementById("chkbalteracao3").checked && !document.getElementById("chkbalteracao4").checked && !document.getElementById("chkbalteracao5").checked && !document.getElementById("chkbalteracao6").checked && !document.getElementById("chkbalteracao7").checked && !document.getElementById("chkbalteracao8").checked && !document.getElementById("chkbalteracao9").checked && !document.getElementById("chkbalteracao10").checked) {
			window.alert("Por favor, informe se você tem alguma das alterações citadas");
			return false;
		} else {
			return true;
		}
	});
	
	
	$("#rdplano0").click(function() {
		$("#lblplanoempresa").show("slow");
	});
	
	$("#txtplanochave").blur(function() {
		if (document.getElementById("rdplano0").checked) {
			$("#lblplanoempresa").hide("slow");
		}
	});
	
	$("#txtusuariocpf").blur(function(){
		var _cpf = $("#txtusuariocpf").val();
		var newCpf = "";
		
		for (i=0;i<_cpf.length;i++) {
			if (_cpf.charAt(i)!="." && _cpf.charAt(i)!="-") {
				if (i==2 || i==5) {
					newCpf += _cpf.charAt(i)+".";	
				} else if (i==8) {
					newCpf += _cpf.charAt(i)+"-";	
				} else {
					newCpf += _cpf.charAt(i);
				}
			}
		}
		
		$("#txtusuariocpf").val(newCpf);
	});
	
	$("#txtusuariocpf").click(function(){
		$("#txtusuariocpf").val("");						   
	});
	
	
	$("#txtusuariocep").blur(function(){
		var _cep = $("#txtusuariocep").val();
		var newCep = "";
		
		_cep = _cep.replace(".","");
		
		for (i=0;i<_cep.length;i++) {
			if (_cep.charAt(i)!="." && _cep.charAt(i)!="-") {
				if (i==1) {
					newCep += _cep.charAt(i)+".";	
				} else if (i==4) {
					newCep += _cep.charAt(i)+"-";	
				} else {
					newCep += _cep.charAt(i);
				}
			}
		}
		
		$("#txtusuariocep").val(newCep);
	});
	
	$("#txtusuariocep").click(function(){
		$("#txtusuariocep").val("");						   
	});
	
	$("#frmfinalizacadastro").submit(function(){  
		$("#chkbleutermo").focus();
		if (!(/^\d{3}.\d{3}.\d{3}-\d{2}$/.test($("#txtusuariocpf").val()))) {
			window.alert("Por favor, informe um cpf válido");
			$("#txtusuariocpf").focus();
			$("#txtusuariocpf").val("");
			return false;
		} else if (!validaNumeroCPF("txtusuariocpf")) {
			window.alert("Por favor, informe um cpf válido");
			$("#txtusuariocpf").val("");
			$("#txtusuariocpf").focus();
			return false;
		} else if ($("#txtusuarioendereco").val()=="") {
			window.alert("Por favor, informe o seu endereço");
			$("#txtusuarioendereco").focus();
			return false;
		} else if ($("#txtcadastrobairro").val()=="") {
			window.alert("Por favor, informe o seu bairro");
			$("#txtcadastrobairro").focus();
			return false;
		} else if ($("#txtusuariocidade").val()=="") {
			window.alert("Por favor, informe a sua cidade");
			$("#txtusuariocidade").focus();
			return false;
		} else if ($("#slusuarioestado").val()=="") {
			window.alert("Por favor, informe o estado");
			$("#slusuarioestado").focus();
			return false;
		} else if ($("#txtusuariocep").val()=="" || !(/^[\d.-]*$/.test($("#txtusuariocep").val()))) {
			window.alert("Por favor, informe um cep válido");
			$("#txtusuariocep").focus();
			return false;
		} else if (!validaCEP("txtusuariocep","slusuarioestado")) {
			window.alert("O CEP informado não corresponde ao estado de origem.");
			$("#txtusuariocep").val("");
			return false;
		} else if ($("#txtusuariotelefoneresidencial").val()=="" && $("#txtusuariotelefonecomercial").val()=="" && $("#txtusuariotelefonecelular").val()=="") {
			window.alert("Por favor, informe pelo menos um telefone de contato.");
			return false;
		} else if (!(/^[\d ()-]*$/.test($("#txtusuariotelefoneresidencial").val()))) {
			window.alert("Por favor, informe um telefone residencial válido.");
			$("#txtusuariotelefoneresidencial").focus();
			return false;		
		} else if (!(/^[\d ()-]*$/.test($("#txtusuariotelefonecomercial").val()))) {
			window.alert("Por favor, informe um telefone comercial válido.");
			$("#txtusuariotelefonecomercial").focus();
			return false;		
		} else if (!(/^[\d ()-]*$/.test($("#txtusuariotelefonecelular").val()))) {
			window.alert("Por favor, informe um telefone celular válido.");
			$("#txtusuariotelefonecelular").focus();
			return false;		
		} else if ($("#txtusuariologin").val()=="") {
			window.alert("Por favor, informe seu login de acesso");
			$("#txtusuariologin").focus();
			return false;
		} else if ($("#txtusuariosenha").val()=="") {
			window.alert("Por favor, informe sua senha de acesso");
			$("#txtusuariosenha").focus();
			return false;
		} else if ($("#txtusuariosenha").val()!=$("#txtusuarioconfirmasenha").val()) {
			window.alert("Senha e confirmação de senha não conferem");
			$("#txtusuarioconfirmasenha").focus();
			return false;
		} else if (!document.getElementById("chkbleutermo").checked) {
			window.alert("Por favor, informe-nos se você leu e concorda com os termos e condições apresentados.");
			return false;
		} else if (confirm("Tem certeza de que deseja prosseguir com o plano selecionado?")) {
			return true;
		} else {
			return false;
		}
	});
	
	
	$("#frmnews").submit(function(){  
		if ($("#txtnewsnome").val()=="") {
			window.alert("Por favor, informe seu nome.");
			$("#txtnewsnome").focus();
			return false;
		} else if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#txtnewsemail").val()))) {
			window.alert("Por favor, informe um e-mail válido.");
			$("#txtnewsemail").focus();
			return false;
		} else if (!document.getElementById("rdprofissional").checked && !document.getElementById("rdpublico").checked) {
			window.alert("Por favor, informe se você é profissional de saúde o faz parte do público geral.");
			$("#rdcadastrosexof").focus();
			return false;
		} else {
			return true;
		}
	});
	
	$("#frmassine").submit(function(){  
		if ($("#txtnewsnome").val()=="") {
			window.alert("Por favor, informe seu nome.");
			$("#txtnewsnome").focus();
			return false;
		} else if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#txtnewsemail").val()))) {
			window.alert("Por favor, informe um e-mail válido.");
			$("#txtnewsemail").focus();
			return false;
		} else if ($("#txtnewsemail").val()!=$("#txtnewsemailconfirma").val()) {
			window.alert("E-mail e confirmação de e-mail não coincidem.");
			$("#txtnewsemailconfirma").focus();
			return false;
		} else if (!document.getElementById("rdprofissional").checked && !document.getElementById("rdpublico").checked) {
			window.alert("Por favor, informe se você é profissional de saúde o faz parte do público geral.");
			$("#rdcadastrosexof").focus();
			return false;
		} else {
			return true;
		}
	});
	
	$("#enviaSug").submit(function(){ 	
		
		if ($("#nome").val()==""){
			window.alert("Por favor, informe seu nome.");
			$("#nome").focus();
			return false;
		} else if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#email").val()))) {
			window.alert("Por favor, informe um e-mail válido");
			$("#email").focus();
			return false;
		} else if ($("#email").val() != $("#redigiteemail").val()) {
			window.alert("E-mail e confirmação de e-mail não coincidem.");
			$("#redigiteemail").focus();
			return false;
		} else {
			return true;
		}
		
	});									
	
	$("#frmtesteferias").submit(function(){  
		if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#txttesteemail").val()))) {
			window.alert("Por favor, informe um e-mail válido.");
			$("#txttesteemail").focus();
			return false;
		} else {
			return true;
		}
	});
});