function vercpf (cpf) 
{if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
return false;
add = 0;
for (i=0; i < 9; i ++)
add += parseInt(cpf.charAt(i)) * (10 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(9)))
return false;
add = 0;
for (i = 0; i < 10; i ++)
add += parseInt(cpf.charAt(i)) * (11 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(10)))
return false;
return true;}



function validaCNPJ(CNPJ) {
  erro = new String;
  if (CNPJ.length < 18) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
  if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
  if (erro.length == 0) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
  }
  //substituir os caracteres que nao sao numeros
  if(document.layers && parseInt(navigator.appVersion) == 4){
  x = CNPJ.substring(0,2);
  x += CNPJ.substring(3,6);
  x += CNPJ.substring(7,10);
  x += CNPJ.substring(11,15);
  x += CNPJ.substring(16,18);
  CNPJ = x; 
  } else {
  CNPJ = CNPJ.replace(".","");
  CNPJ = CNPJ.replace(".","");
  CNPJ = CNPJ.replace("-","");
  CNPJ = CNPJ.replace("/","");
  }
  var nonNumbers = /\D/;
  if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n"; 
  var a = [];
  var b = new Number;
  var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
  for (i=0; i<12; i++){
  a[i] = CNPJ.charAt(i);
  b += a[i] * c[i+1];
  }
  if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
  b = 0;
  for (y=0; y<13; y++) {
  b += (a[y] * c[y]); 
  }
  if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
  if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
  erro +="Digito verificador com problema!";
  }
  if (erro.length > 0){
  alert(erro);
  return false;
  } else {
 // alert("CNPJ valido!");
  }
  return true;
}

function validaFormGeral(){
	//alert("sdfds")
	obj = document.getElementsByTagName("input");
	
	for (i=0;i<obj.length;i++){
		
		if (obj[i] != null){
			if (obj[i].className == "Obrigatório" || obj[i].className == "obrigatorio"){
				
				switch(obj[i].type){
					
					case "text":
					
						if(obj[i].value == ""){
							
							obj[i].focus();
							alert("Preencha o campo "+obj[i].title)
							return false;
						}
						if (obj[i].name == "end_eletronico")
						{
							if (validaEmail(obj[i].value)== false)
							{
								alert("E-mail inválido");
								obj[i].focus();
								return false;
							}
						}
					
					
					break;
					
						
				}
		
			}
		}
		
	}
	
	
	return true;
}

function confSenha(){
	
	wsenha = document.getElementById("senha");
	wsenhac = document.getElementById("senhac");
	
	if(wsenha.value != ""){
		document.getElementById("alerta").style.display="none";
		if(wsenhac.value != ""){
			if(wsenha.value == wsenhac.value){
				document.getElementById("alerta").style.display="none";
			}else{
				document.getElementById("alerta").style.display='';
				document.getElementById("alerta").innerHTML="Os campos \"Senha\" não coicidem.";
			}
		}else{
			document.getElementById("alerta").style.display='';
			document.getElementById("alerta").innerHTML="O campo \"Senhac\" não pode ser vazio.";
		}
	}else{
		document.getElementById("alerta").style.display='';
		document.getElementById("alerta").innerHTML="O campo \"Senha\" não pode ser vazio.";
	}
}

function confEmail(){
	
	wemail = document.getElementById("alerta");
	
	if(wemail.value == "S"){
		if(senha.value == senhac.value && senha.value != ''){
			return validaFrmGeral()
		}else{
			alert('Os campos Senha não podem ser vazios');
			return false
		}
	}else{
		alert('Digite outro e-mail.');
		return false
	}
}

function fmtDate(campo, e)

{

    myVal = campo.value;

    

    if (myVal.length > 2 && !myVal.match(/\//))

    {

       myVal = '';

    }

    else

    {

        if (window.event)

        {

           keycode = window.event.keyCode;

        }

        else if (e)

        {

            keycode = e.which;

        }

        

        if (keycode < 48 || keycode > 57)

        {
		 	if(keycode >= 96 && keycode <= 105 ){
    		}else{
			

            myVal = myVal.substr(0, (myVal.length - 1));
			}

        }

        

        if (myVal.length == 2 || myVal.length == 5)

        {

            myVal += '/';

        }

    }



    campo.value = myVal;

 }

 function fmtTel(campo, e)
 {
     myVal = campo.value;

     if (myVal.length > 4 && !myVal.match(/\-/))
     {
        myVal = '';
     }
     else
     {
         if (window.event)
         {
            keycode = window.event.keyCode;
         }
         else if (e)
         {
             keycode = e.which;
         }

         if (keycode < 48 || keycode > 57  )
         {
		 	if(keycode >= 96 && keycode <= 105 ){
    		}else{
				myVal = myVal.substr(0, (myVal.length - 1));
			}
         }

         if (myVal.length == 4)
         {
             myVal += '-';
         }
    }

     campo.value = myVal;
}

function validaFrmGeral(){


	if (document.getElementById("titulo").value == ""){
		document.getElementById("titulo").focus();
		alert("Titulo - preenchimento obrigatório")
		return false;
	}

	if (document.getElementById("tipo").value == ""){
		document.getElementById("tipo").focus();
		alert("Tipo - preenchimento obrigatório")
		return false;
	}
	
	if (document.getElementById("data").value == ""){
		document.getElementById("data").focus();
		alert("Data - preenchimento obrigatório")
		return false;
	}

	if (document.getElementById("data_validade").value == ""){
		document.getElementById("data_validade").focus();
		alert("Data de Validade - preenchimento obrigatório")
		return false;
	}
	
	if (document.getElementById("texto").value == ""){
		document.getElementById("texto").focus();
		alert("Texto - preenchimento obrigatório")
		return false;
	}

	if (document.getElementById("email").value == ""){
		document.getElementById("email").focus();
		alert("E-mail - preenchimento obrigatório")
		return false;
	}
	if (document.getElementById("razao_social").value == ""){
		document.getElementById("razao_social").focus();
		alert("Razão Social - preenchimento obrigatório")
		return false;
	}
	
	return true;
}

function formatar(mascara, documento, e){
	var unicode = e.charCode? e.charCode : e.keyCode
	var i = documento.value.length;
	var saida = mascara.substring(0,1);
	var texto = mascara.substring(i)
	
	if (unicode==8) { 
		return true;
	}
	
	if (texto.substring(0,1) != saida){ 
		documento.value += texto.substring(0,1);
	}
	
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
		if(er.test(mail)){
			return true;
		}
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){ 
			return true;
		}
    }else{
        return false;
    }
}

function valida(){
	var email = document.frm_cadastro.email.value;
	if(email != ""){
		if (!checkMail(email)){
			alert("E-mail inválido. Por favor, digite um e-mail válido.");
			return false;
		}else{
			return true;
		}
	}else{
		alert("Digite seu e-mail");
		return false;
	}
}
function formataDataMysql(data){
	if (data != "") {
	
		try{
		
			vetData = data.split("/");
			
			dia = vetData[0];
			
			if (dia.length < 2){	dia = "0" + dia;}
			
			mes = vetData[1];
			
			if (mes.length < 2 ){	mes = "0" + mes; }
			
			ano = vetData[2];
			
			if (isNaN(ano) == false && isNaN(mes) == false && isNaN(dia) == false){
				return ano +"-"+ mes +"-"+ dia;
			}else{
				//alert("Formato da data inválido");
				return false;
			}
			
		}catch(e){
			//alert("Formato da data inválido")
			return false;
		}
	
	}else{
		return false
	}
	
}



function validaCadastroUsuario()
{
	//if(document.getElementById("senha").value == document.getElementById("senhac").value && document.getElementById("senha").value!='')
	//{
		document.getElementById("data_nascimento").value = formataDataMysql(document.getElementById("data_nascimento_tmp").value);
		if (validaFormGeral() == false)
		{
			return false; 
		}
		if (validaData(document.getElementById("data_nascimento_tmp").value) == false)
		{
			alert("Data inválida");
			return false;
		}
		if (document.getElementById("foto").value != "" )
		{
			foto = document.getElementById("foto").value;
			indice = foto.indexOf(".");
			indice = indice + 1;
			
			foto = foto.substring(foto.length-3)
			//foto = foto.substr(indice);
			foto = foto.toLowerCase();
			if (foto != "jpg")
			{
				alert("Atenção, foto - extensão não permitida");
				document.getElementById("foto").focus();
				return false;
			}
		}
		else
		{
			if(!document.getElementById("imgAvatar") )	
			{
				alert("Insira seu avatar")	
				document.getElementById("foto").focus()
				return false	
			}
		}
			
		if( document.getElementById("senhaCad").value == "")
		{
			alert("Preencha o campo Senha")	
			document.getElementById("senhaCad").focus()
			return false
			
		}
		else
		{
			if( document.getElementById("senhac").value == "")
			{
				alert("Preencha o campo Confirmar senha")	
				document.getElementById("senhac").focus()
				return false	
			}
			else
			{
				if( document.getElementById("senhac").value != document.getElementById("senhaCad").value)
				{
					alert("Senha não conferem, digite novamente")	
					document.getElementById("senhac").focus()
					return false	
				}
				else
				{
					if( document.getElementById("codVerif2").value =='')
					{
						alert('Código de verificação não pode estar em branco')
						return false
					}
					else
					{
						return true	
					}
				}
			}
		}
			
			
		
		
		
	
	/*}
	else
	{
		alert('Senha n?o confere, ou est? em branco');
		return false;
	}*/
	return true;
}
	function validaData(data)
	{
		var expre =	/^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/;
		if (expre.test(data) == false)
		{
			return false;
		}
		return true
	}
function validaEmail(email)
{
	
	var reTipo = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	return reTipo.test(email);

}

function validaCadTopico()
{
	if(validaFormGeral())
	{
		if(document.getElementById("descricao").value != "")
		{
			return true	
		}
		else
		{
			alert("Preencha o campo Texto")	
			document.getElementById("descricao").focus();
			return false;
		}
	}
	else
	{
		return false	
	}
	
}