// JavaScript Document

//-------------------------------------------------------
// Confirmar se uma data é valida.
//-------------------------------------------------------
function DataNaoValida (DataBR) {
  var mes31 = ['01','03','05','07','08','10','12'];
  var dia = DataBR.substr(0,2); var mes = DataBR.substr(3,2); var ano = DataBR.substr(6,4);
  var bisesto = false; if ( (ano % 4 == 0) && ( (ano % 100 != 0) || (ano % 400 == 0))) { bisesto = true;}
  var tam = DataBR.length;
  var erro = false;

        function oc(a){
          var o = {};
          for(var i=0;i<a.length;i++) {    o[a[i]]=''; }
          return o;
        }

  //Tamanho da data
  if (tam != 10) {
    erro = true;
  }
  if (!erro) {
      //validacao do dia
      if ((dia < 1) || ( dia > 31)){
        erro = true;
      }else {    
        if ((mes < 1) || (mes > 12)) {
          erro = true;
        }else {
            if(mes != '02') {
                if (!(mes in oc(mes31)) ){
                    //meses com 30 dias
                    if (dia > 30) { erro = true;}
                }
            }else {
              //Mes de fevereiro
                  if (bisesto) {
                       if (dia > 29) {erro = true;}
                  }else { if (dia >28) {erro = true;} }
            }        
        }
      }
  }
return erro;  
}

//-------------------------------------------------------
// Formatação de campo de CEP:ddddd-ddd 
//-------------------------------------------------------
function ValidaCEP(Formulario, Campo, TeclaPres){
	 
//    var tecla = TeclaPres.keyCode;
    var strCampo;
    var vr;
    var tam;
    var TamanhoMaximo = 10;
	var treat = true; var erro = false;
	var dia; var mes; var ano;var msg="";

if (!(isnumber(TeclaPres))){
	//Não é numero
    eval("strCampo = document." + Formulario + "." + Campo);
    vr = strCampo.value;
    tam = vr.length;	
	vr = vr.substring (vr,tam-1);	
	strCampo.value = vr;
	treat = false; 
}

if (treat){
	//Numero 
    if(window.event) { // IE
       var tecla = TeclaPres.keyCode;
    }else if(TeclaPres.which) {// Netscape/Firefox/Opera
        var tecla = TeclaPres.which;
    }

    eval("strCampo = document." + Formulario + "." + Campo);
    vr = strCampo.value;
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    tam = vr.length;

    if (tam < TamanhoMaximo && tecla != 8)
    {
      tam = vr.length + 1;
    }

    if (tecla == 8)
    {
//      tam = tam - 1;
tam = tam +1;
    }
    //Formatar Campo
    if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105){
//		alert (tam);
       if (tam <= 5){
        strCampo.value = vr;
       }
       if (tam > 5) {
         strCampo.value = vr.substr(0, 5) + '-' + vr.substr(5, tam);
       }
    }
}
} 

//-------------------------------------------------------
// Formatação de campo de Data dd/mm/aaaa
//-------------------------------------------------------
function ValidaData(Formulario, Campo, TeclaPres){
	 
//    var tecla = TeclaPres.keyCode;
    var strCampo;
    var vr;
    var tam;
    var TamanhoMaximo = 10;
	var treat = true; var erro = false;
	var dia; var mes; var ano;var msg="";

if (!(isnumber(TeclaPres))){
	//Não é numero
    eval("strCampo = document." + Formulario + "." + Campo);
    vr = strCampo.value;
    tam = vr.length;	
	vr = vr.substring (vr,tam-1);	
	strCampo.value = vr;
	treat = false; 
}

if (treat){
	//Numero 
    if(window.event) { // IE
       var tecla = TeclaPres.keyCode;
    }else if(TeclaPres.which) {// Netscape/Firefox/Opera
        var tecla = TeclaPres.which;
    }

    eval("strCampo = document." + Formulario + "." + Campo);
    vr = strCampo.value;
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    tam = vr.length;

    if (tam < TamanhoMaximo && tecla != 8)
    {
      tam = vr.length + 1;
    }

    if (tecla == 8)
    {
//      tam = tam - 1;
tam = tam +1;
    }
    //Formatar Campo
    if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105){
//		alert (tam);
       if (tam <= 3){
        strCampo.value = vr;
       }
       if ((tam > 3) && (tam <= 5)){
		 dia = vr.substr(0,2);
		 if ((dia < 0) || (dia > 31)) {
			alert ("Dia está fora da faixa");
		 }
         strCampo.value = vr.substr(0, 2) + '-' + vr.substr(2, tam);
       }
       if ((tam > 5) && (tam <= 10)){
		 dia = vr.substr(0,2); mes = vr.substr(2,2);
		 if ((dia < 0) || (dia > 31)) {erro = true;	msg = "Dia está fora da faixa \n"; }
		 if ((mes < 0) || (mes > 12)) {erro = true;	msg = msg +  "Mes está fora da faixa \n"; }
         strCampo.value = dia + '-' + mes + '-' + vr.substr(4, tam);
		 if (erro) {
			 alert (msg);
		 }
       }
    }
}
} 

//-------------------------------------------------------
// Formatação de valores monetários R$
//-------------------------------------------------------
/* Formatar valores na entrada de dados */
function FormataValor(Formulario, Campo, TeclaPres){
	 
//    var tecla = TeclaPres.keyCode;
    var strCampo;
    var vr;
    var tam;
    var TamanhoMaximo = 18;
	var treat = true;

if (!(isnumber(TeclaPres))){
	//Não é numero
    eval("strCampo = document." + Formulario + "." + Campo);
    vr = strCampo.value;
    tam = vr.length;	
	vr = vr.substring (vr,tam-1);	
	strCampo.value = vr;
	treat = false;
}

if (treat){
	//Numero 
    if(window.event) { // IE
       var tecla = TeclaPres.keyCode;
    }else if(TeclaPres.which) {// Netscape/Firefox/Opera
        var tecla = TeclaPres.which;
    }

    eval("strCampo = document." + Formulario + "." + Campo);
    vr = strCampo.value;
    vr = vr.replace(",", "");
    vr = vr.replace(",", "");
    vr = vr.replace(",", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    tam = vr.length;

    if (tam < TamanhoMaximo && tecla != 8)
    {
      tam = vr.length + 1;
    }

    if (tecla == 8)
    {
//      tam = tam - 1;
tam = tam +1;
    }
    //Formatar Campo
    if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105){
//		alert (tam);
       if (tam <= 3){
        strCampo.value = vr;
       }
       if ((tam > 3) && (tam <= 6)){
         strCampo.value = vr.substr(0, tam - 3) + ',' + vr.substr(tam - 3, tam);
       }
       if ((tam >= 7) && (tam <= 9)){
         strCampo.value = vr.substr(0, tam - 6) + '.' + vr.substr(tam - 6, 3) + ',' + vr.substr(tam - 3, tam);
       }
       if ((tam >= 10) && (tam <= 12)) {
         strCampo.value = vr.substr(0, tam - 9) + '.' + vr.substr(tam - 9, 3) + '.' + vr.substr(tam - 6, 3) + ',' + vr.substr(tam - 3, tam);
       }
       if ((tam >= 13) && (tam <= 15)){
         strCampo.value = vr.substr(0, tam - 12) + '.' + vr.substr(tam - 12, 3) + '.' + vr.substr(tam - 9, 3) + '.' + vr.substr(tam - 6, 3) + ',' + vr.substr(tam - 3, tam);
       }
       if ((tam >= 16) && (tam <= 18)){
         strCampo.value = vr.substr(0, tam - 15) + '.' + vr.substr(tam - 15, 3) + '.' + vr.substr(tam - 12, 3) + '.' + vr.substr(tam - 9, 3) + '.' + vr.substr(tam - 6, 3) + ',' + vr.substr(tam - 3, tam);
       }
    }
}
} 

//-------------------------------------------------------
// Aceita Somente Numeros
//-------------------------------------------------------
/* Formatar valores na entrada de dados */
function validaNumero(Formulario, Campo, TeclaPres){
	 
//    var tecla = TeclaPres.keyCode;
    var strCampo;
    var vr;
    var tam;
    var TamanhoMaximo = 18;
	var treat = true;

if (!(isnumber(TeclaPres))){
	//Não é numero
    eval("strCampo = document." + Formulario + "." + Campo);
    vr = strCampo.value;
    tam = vr.length;	
	vr = vr.substring (vr,tam-1);	
	strCampo.value = vr;
	treat = false;
}
}
function validaCNPJ(val) {  
	var cnpj = document.getElementById(val).value;  
	//Para analise retirar os caracteres . / -
	cnpj = cnpj.replace("/", "");
	cnpj = cnpj.replace("-", "");
	while(cnpj.indexOf(".") >= 0) 
			cnpj = cnpj.replace(".", "");
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;  
	digitos_iguais = 1;  

	for (var 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 (var 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)) {  
			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)) {  
			return false;  
		}  
		return true;  
	}else  
		return false;  
}  

function checkDOC (val) {
 var doc = document.getElementById(val).value;  
 var tam = doc.length;	
 if (tam > 11) {
	 checkCNPJ(val);
 }else {
	 checkCPF(val);
 }
}

function checkCNPJ(val) {
  if(validaCNPJ(val) == false ) {
    window.alert("CNPJ incorreto! ");
    return false;
  }
  return true;
}

function checkCPF(val) {
  if(validacpf(val) == false ) {
    window.alert("CPF incorreto! ");
    return false;
  }
  return true;
}

function validacpf(val){ 
	var cpf = document.getElementById(val).value;  
	cpf = cpf.replace("/", "");
	cpf = cpf.replace("-", "");
	while(cpf.indexOf(".") >= 0) 
		cpf = cpf.replace(".", "");
	var c = cpf.substr(0,9); 
	var dv = cpf.substr(9,2); 
	var i; 
	var d1 = 0; 
	for (i = 0; i < 9; i++) { 
	  d1 += c.charAt(i)*(10-i); 
	} 
  
	if (d1 == 0){ 
//	 alert("CPF não válido") 
	 return false; 
	} 
  
	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(0) != d1) { 
//	  alert("CPF não válido") 
	  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 não válido") 
	 return false; 
	} 
	return true; 
} 
//-------------------------------------------------------
// Formatar CPF na entrada de dados 
// Aceita Somente Numeros  xxx.xxx.xxx-xx
//-------------------------------------------------------
function FormataCPF(Formulario, Campo, TeclaPres){
//id = cnpjid	 
//    var tecla = TeclaPres.keyCode;
    var strCampo;
    var vr;
    var tam;
    var TamanhoMaximo = 14;
	var treat = true;

if (!(isnumber(TeclaPres))){
	//Não é numero
  eval("strCampo = document." + Formulario + "." + Campo);
//	eval (strCampo = document.getElementById('cnpjid'));
    vr = strCampo.value;
    tam = vr.length;	
	vr = vr.substring (vr,tam-1);	
	strCampo.value = vr;
	treat = false;
}

if (treat){
	//Numero 
    if(window.event) { // IE
       var tecla = TeclaPres.keyCode;
    }else if(TeclaPres.which) {// Netscape/Firefox/Opera
        var tecla = TeclaPres.which;
    }

    eval("strCampo = document." + Formulario + "." + Campo);
//	eval (strCampo = document.getElementById('cnpjid'));
    vr = strCampo.value;
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace("-", "");
    tam = vr.length;

    if (tam < TamanhoMaximo && tecla != 8)
    {
      tam = vr.length + 1;
    }

    if (tecla == 8)
    {
      tam = tam - 1;
    }
    //Formatar Campo
    if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105){
       if (tam <= 4){
        strCampo.value = vr;
       }
       if ((tam > 4) && (tam <= 7)){
         strCampo.value = vr.substr(0,3) + '.' + vr.substr(3, tam);
       }
       if ((tam >= 8) && (tam <= 10)){
         strCampo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, tam);
       }
       if ((tam >= 11) ){
         strCampo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6,3)+ '-' + vr.substr(9,tam);
       }
    }
}
} 

//-------------------------------------------------------
// Aceita Somente Numeros  xx.xxx.xxx/xxx1-xx
//-------------------------------------------------------
/* Formatar CNPJ na entrada de dados */
function FormataCNPJ(Formulario, Campo, TeclaPres){
//id = cnpjid	 
//    var tecla = TeclaPres.keyCode;
    var strCampo;
    var vr;
    var tam;
    var TamanhoMaximo = 14;
	var treat = true;

if (!(isnumber(TeclaPres))){
	//Não é numero
  eval("strCampo = document." + Formulario + "." + Campo);
//	eval (strCampo = document.getElementById('cnpjid'));
    vr = strCampo.value;
    tam = vr.length;	
	vr = vr.substring (vr,tam-1);	
	strCampo.value = vr;
	treat = false;
}

if (treat){
	//Numero 
    if(window.event) { // IE
       var tecla = TeclaPres.keyCode;
    }else if(TeclaPres.which) {// Netscape/Firefox/Opera
        var tecla = TeclaPres.which;
    }

    eval("strCampo = document." + Formulario + "." + Campo);
//	eval (strCampo = document.getElementById('cnpjid'));
    vr = strCampo.value;
    vr = vr.replace("/", "");
    vr = vr.replace("/", "");
    vr = vr.replace("/", "");
    vr = vr.replace(",", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    tam = vr.length;

    if (tam < TamanhoMaximo && tecla != 8)
    {
      tam = vr.length + 1;
    }

    if (tecla == 8)
    {
      tam = tam - 1;
    }
    //Formatar Campo
    if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105){
       if (tam <= 2){
        strCampo.value = vr;
       }
       if ((tam > 2) && (tam <= 6)){
         strCampo.value = vr.substr(0, tam - 2) + '-' + vr.substr(tam - 2, tam);
       }
       if ((tam >= 7) && (tam <= 9)){
         strCampo.value = vr.substr(0, tam - 6) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam);
       }
       if ((tam >= 10) && (tam <= 12)) {
         strCampo.value = vr.substr(0, tam - 9) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam);
       }
       if ((tam >= 13) && (tam <= 14)){
         strCampo.value = vr.substr(0, tam - 12) + '.' + vr.substr(tam - 12, 3) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam);
       }
       if ((tam >= 15) && (tam <= 17)){
         strCampo.value = vr.substr(0, tam - 14) + '.' + vr.substr(tam - 14, 3) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + '-' + vr.substr(tam - 2, tam);
       }
    }
}
} 


//-------------------------------------------------------
// Função para aceitar apenas número e return
//-------------------------------------------------------
//Função para aceitar apenas número e return
function isnumber(e) {
 var keynum;
 var keychar;
 var numcheck;
 if(window.event) { // IE
    keynum = e.keyCode;
 }else if(e.which) {// Netscape/Firefox/Opera
    keynum = e.which;
 }
 if ((keynum > 47 && keynum < 58) || // numeros de 0 a 9
     (keynum > 95 && keynum < 106) ){ // numeros de 0 a 9 no bloco numerico
	return true;
 }else {
	if (keynum == 8){return true; }else {return false;}
 }
}


//-------------------------------------------------------
// Tratamento e Email
//-------------------------------------------------------
function review_Email(email_in){
var email_out = email_in;
  if (email_out.indexOf("@hotmail.com.br") >= 0) {
     // O E-mail HOTMAIL não termina com .BR
	email_out = email_out.replace("hotmail.com.br", "hotmail.com");
  }

  if (email_out.indexOf("@aol.com.br") >= 0) {
		// O E-mail AOL não termina com .BR
		email_out = email_out.replace("aol.com.br", "aol.com");
  }
  
  if (email_out.indexOf(" ") >= 0){
		// E-mail não deve conter espaços em branco
 		while(email_out.indexOf(" ") >= 0) 
			email_out = email_out.replace(" ", "");
  }
  email_out = email_out.replace("/", "");
  email_out = email_out.replace("@.", "@");
  email_out = email_out.replace(".@", "@");
  email_out = email_out.replace(",", ".");
  email_out = email_out.replace(";", ".");
  
  return email_out;
}

function is_email(email_in){
var email = email_in;
 if (   (email.indexOf("@") <= 0) || 
		(email.indexOf(".") <=0)  || 
	  ( (email.length) == (email.indexOf(".")+1) ) ){
	 return false;
 }else {
  return true;
 }
}
function check_email (val) {
  var email = document.getElementById(val).value;  
  email = review_Email(email);
  if (!(email_valido = is_email(email))) {
	//Email inválido
    alert (">> E-mail Incorreto!");
	return false;
  }
  return true;
}


//-------------------------------------------------------


//-------------------------------------------------------
// Formatação de máscara padrão
// onkeypress="formatar_mascara(this,'###.###.###-##') => CPF
// onkeypress="formatar_mascara(this, '##.###.###/####-##') => CNPJ
// onkeypress="formatar_mascara(this, '#####-###') => CEP
//-------------------------------------------------------
function formatar_mascara(src, mascara) {
	var campo = src.value.length;
	var saida = mascara.substring(0,1);
	var texto = mascara.substring(campo);
	if(texto.substring(0,1) != saida) {
		alert (texto.substring(0,1));
		src.value += texto.substring(0,1);
	}
}                      

