function openForm(){
	document.getElementById("form").style.display = 'block';
}
function openPop(){
	document.getElementById("pop").style.display = 'block';
}
function erro(){
	document.getElementById('pop').style.display = 'none';
}
function elemento(v) {
	return document.getElementById(v);
}

function validarCPF(campo){
	var cpf = elemento(campo).value;
	if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  document.getElementById('popCPF').style.display = 'block';
	  return false;
	}
	soma = 0;
	for(i = 0; i < 9; i++)
	 soma += parseInt(cpf.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
	 resto = 0;
	if(resto != parseInt(cpf.charAt(9))){
	 document.getElementById('popCPF').style.display = 'block';
	 return false;
	}
	soma = 0;
	for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
	 resto = 0;
	if(resto != parseInt(cpf.charAt(10))){
	 document.getElementById('popCPF').style.display = 'block';
	 return false;
	}
	document.getElementById('popCPF').style.display = 'none';
	return true;
}

function validarCNPJ(campo) {
     var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais, cnpj = elemento(campo).value.replace(/\D+/g, '');
     digitos_iguais = 1;
     if (cnpj.length != 14)
            {
                 //alert('CNPJ inválido');
                 //c.focus();
                 return false;
                 }

     for (i = 0; i < cnpj.length - 1; i++)
           if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                 {
                 digitos_iguais = 0;
                 break;
                 }
     if (!digitos_iguais)
           {
           tamanho = cnpj.length - 2
           numeros = cnpj.substring(0,tamanho);
           digitos = cnpj.substring(tamanho);
           soma = 0;
           pos = tamanho - 7;
           for (i = tamanho; i >= 1; i--)
                 {
                 soma += numeros.charAt(tamanho - i) * pos--;
                 if (pos < 2)
                       pos = 9;
                 }
           resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
           if (resultado != digitos.charAt(0))
			{
                 //alert('CNPJ inválido');
				 document.getElementById('popCNPJ').style.display = 'block';
                 //c.focus();
                 return false;
                 }

           tamanho = tamanho + 1;
           numeros = cnpj.substring(0,tamanho);
           soma = 0;
           pos = tamanho - 7;
           for (i = tamanho; i >= 1; i--)
                 {
                 soma += numeros.charAt(tamanho - i) * pos--;
                 if (pos < 2)
                       pos = 9;
                 }
           resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
           if (resultado != digitos.charAt(1)){
                 //alert('CNPJ inválido');
				 document.getElementById('popCNPJ').style.display = 'block';
                 //c.focus();
                 return false;
                 }
           else {
     		//alert('CNPJ  OK !');
			document.getElementById('popCNPJ').style.display = 'none';
            return true;
            }
           }
     else{
           //alert('CNPJ inválido');
		   document.getElementById('popCNPJ').style.display = 'block';
           //c.focus();
           return false;
           }
} 
	 
	 
function validaForm(){
	
		var mensagem = "";
		var email = "";
		if(elemento('txt_nome').value == "")
		{
			mensagem += "Preencha o campo Nome Completo;\n";
			document.getElementById('cadastroNome').style.display = 'block';
		}
		else
		{
			document.getElementById('cadastroNome').style.display = 'none';

		}
		
		if(checkMail(elemento('txt_email').value)==true){
			document.getElementById('popEmail').style.display = 'none';			
		}else{
			email += "Email inválido;\n";
			document.getElementById('popEmail').style.display = 'block';
		}

		if(document.FormIphone.CelularVivo[0].checked)
		{
			celularDigito = document.getElementById("txt_cel").value.substring(0,1);
			
			//if(elemento('txt_celddd').value == '' || elemento('txt_celddd').value.length != 2 || elemento('txt_cel').value == '' || elemento('txt_cel').value.length != 8 || elemento('txt_celddd').value == 'DDD' || elemento('txt_cel').value == 'Telefone' || celularDigito != 9 && celularDigito != 8 && celularDigito != 7)
			if(elemento('txt_celddd').value.length != 2 || document.getElementById("txt_cel").value.length !=8)			
			{
				mensagem += "Preencha o campo Celular com DDD e Número;\n";
				document.getElementById('cadastroCelular').style.display = 'block';
			}
			else
			{
				
				document.getElementById('cadastroCelular').style.display = 'none';
			}
		}
		else
		{
			celular2Digito = document.getElementById("txt_cel2").value.substring(0,1);
			// || elemento('txt_cel2').value == '' || elemento('txt_cel2').value.length != 8 || elemento('txt_celddd2').value == 'DDD' || elemento('txt_cel2').value == 'Telefone' || celular2Digito != 9 && celular2Digito != 8 && celular2Digito != 7			
			if(elemento('txt_operadora').value == '' || elemento('txt_celddd2').value == ''  || elemento('txt_celddd2').value.length != 2 || document.getElementById("txt_cel2").value.length !=8)
			{
				mensagem += "Preencha o campo Operadora/Celular com DDD e Número;\n";
				
				document.getElementById('cadastroOperadora').style.display = 'block';
			}
			else
			{
				document.getElementById('cadastroOperadora').style.display = 'none';
			}
		}

		if(elemento('txt_email').value == "") {
			mensagem += "Preencha o campo Email;\n";
		}
		
		//alert(document.getElementById('txt_cnpj'));
		
		if(elemento('txt_cnpj')==null){
			if(elemento('txt_cpf').value == "") {
				mensagem += "Preencha o campo CPF;\n";
			}
			if(validarCPF('txt_cpf') != true){
				mensagem += "CPF inválido."
			}			
		}else{
			if(elemento('txt_cnpj').value == "") {
				document.getElementById('popCNPJ').style.display = 'block';
				mensagem += "Preencha o campo CNPJ;\n";
			}
			if(validarCNPJ('txt_cnpj') != true){
				mensagem += "CNPJ inválido."
			}			
		}
		


		
		if(mensagem == "" && email == "") {
			return true;
		} else {
			document.getElementById('pop').style.display = 'block';
			return false;
		}
		return true;
    }
	
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 contaChar(v, e) {
	var total = elemento(v).value.length;
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	key = String.fromCharCode(whichCode);
    if(isNaN(parseInt(key)) && whichCode == 8 || whichCode == 0){
	alert('oi');
			elemento(v+"C").innerHTML = total;
	} else {
		if(total >= 501) {
			return false;
		}
	}	alert(total);
		elemento(v+"C").innerHTML = total;
	}
	
function letra(let)
  {
   var lets = "0123456789"; // "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   var valor;
   
   for (var i=0;i<let.value.length;i++)
   {
    valor = let.value.substring(i,i+1) 
    if (lets.indexOf(valor) != -1)
    {
     let.value = let.value.substring(0,i);
     break;
    }
   }
  }
	
function soNumeros(fld, e, tipo) 
{
	//-- Como chamar a função : onkeypress="return(TwSoNumeros(this,event))" onkeyup="return(TwSoNumeros(this,event))"
	//  Usar no onKeyPress="return(soNumeros(this,event))" do input(text)
	var whichCode = (window.Event) ? e.which : e.keyCode;
	key = String.fromCharCode(whichCode);  // Get key value from key code
	//alert(whichCode);

    if(isNaN(parseInt(key)) && whichCode == 8 || whichCode == 0){
	valor = fld.value;
	tamanho = fld.value.length
	    switch (tipo) {
		    case "data" : 		//-- DATA
		        if(tamanho == 2){fld.value = valor + '/';}
			    if(tamanho == 5){fld.value = valor + '/';}
			    break;
		    case "nascimento" : 
			    if(tamanho == 2){fld.value = valor + '/';}
			    break;			    
		    case "cnpj" :  		//-- CNPJ
			    if(tamanho == 2 || tamanho == 6){fld.value = valor + '.';}
			    if(tamanho == 10){fld.value = valor + '/';}
			    if(tamanho == 15){fld.value = valor + '-';}
			    break;		
		    case "exp_telefone" ://-- TELEFONE
		    case "telefone" :	 
			    if(tamanho == 4){fld.value = valor + '-';}
			    break;
		    case "cep" :		 //-- CEP
			    if(tamanho == 5){fld.value = valor + '-';}
			    break;			
    			
	    } 
	    }
	else {
		var pattern = new RegExp('[0-9]+', 'g');
    	var val = key.replace(pattern, 'proibido');
        if(val !='proibido'){return false;}
	}


	
}	
//--- Limitador de caracteres do campo
function TwLimCaracter(campo, lim){
//-- onKeyDown="TwLimCaracter(this,255);" onKeyUp="TwLimCaracter(this,255);"
	eval("tam=campo.value.length");
	if( tam > lim ){
		alert("Você atingiu o limite máximo de caracteres permitidos.");
		eval("campo.value = campo.value.slice(0,lim)");
		return true;
	}
}

//Coloque o maximo de caracteres permitido
limite = 500; 
function contaChars(objText, objCount){
var limit = 500; 
	TwLimCaracter(objText, limit)
	if(objText.value.length < 0){
		alert("Você atingiu o limite máximo de caracteres permitidos.");
		objText.value = objText.value.substr(0, limit);
	}
	objCount.value = (limit - objText.value.length);	
}

function ocultaOperadoraNao()
{
	$("#operadoraNaoCelular").hide('slow');
	$("#operadoraNaoOperadora").hide('slow');
		
	$("#operadoraSimCelular").show('slow');
}

function ocultaOperadoraSim()
{
	$("#operadoraNaoCelular").show('slow');
	$("#operadoraNaoOperadora").show('slow');
		
	$("#operadoraSimCelular").hide('slow');
}





function TwVoid(){
	//void();
}
function chamaPag(url){
// alert(window.location);
 //alert(url);
 if (window.document.getElementById)
 { //Verifica se o Browser suporta DHTML.

	 if (window.XMLHttpRequest){
		 HttpReq = new XMLHttpRequest();
		 HttpReq.onreadystatechange = TwVoid;
		 HttpReq.open('GET', url, true);
		 HttpReq.send(null);
	 }else if (window.ActiveXObject){
		 try{
			 HttpReq = new ActiveXObject("Msxml2.XMLHTTP");
		 }catch (e){
			 try{
				 HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			 }catch (e)
			 {
			 }
		 }
		 if (HttpReq){
			 HttpReq.onreadystatechange = TwVoid;
			 HttpReq.open("GET", url, true);
			 HttpReq.send();
			 }
		 }
	 }
}

function fnCadastro(n){
	if(n==1 || n=='1'){
		document.getElementById('parte1').style.display="none";
		document.getElementById('parte2').style.display="block";
		document.getElementById('parte3').style.display="none";
	}
	if(n==2 || n=='2'){
		document.getElementById('parte1').style.display="block";
		document.getElementById('parte2').style.display="none";
		document.getElementById('parte3').style.display="none";
	}
	if(n==3 || n=='3'){
		document.getElementById('parte1').style.display="none";
		document.getElementById('parte2').style.display="none";
		document.getElementById('parte3').style.display="block";
		//$("#divInfo").attr("class","divMensagem");	
	}	
	
}