/**********************************************************/	
/* Função Validar Data ************************************/
/* Passar como parâmetro a Data ***************************/
/**********************************************************/	
function validarData(campo){//valida datas
var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
var msgErro = 'Data incorreta.';
if (campo.value=="") return true;


if ((campo.value.match(expReg)) && (campo.value!='')){
		var dia = campo.value.substring(0,2);
		var mes = campo.value.substring(3,5);
		var ano = campo.value.substring(6,10);
	if(mes==4 || mes==6 || mes==9 || mes==11 && dia > 30) {
		alert("Data incorreta!");
		campo.value="";
		return false;
	} 
	else {
		if(ano%4!=0 && mes==2 && dia>28) {
			alert("Data incorreta!");
			campo.value="";
		return false;
		} 
		else {
			if(ano%4==0 && mes==2 && dia>29) {
				alert("Data incorreta!");
				campo.value="";
				return false;
			} 
			else { 
				return true;
			}
		}
	}
} 
else {
	alert(msgErro);
	campo.value="";	
	campo.focus();
	return false;
	}
}

/**********************************************************/	
/* Função Somenete Números*********************************/
/* Passar como parâmetro o número**************************/
/**********************************************************/	
function OnlyNumbers(e) { 
	tecla = "sem";
		if (window.event) { 
		tecla = e.keyCode; 
		} 
	   else if (e.which) { 
		tecla = e.which; 
	   } 
	   if (tecla=="sem") return true
	   else if ( (tecla >= 48 && tecla <= 57)||(tecla == 8 ) )  return true;
	   else return false;
}


/**********************************************************/	
/* Função que coloca máscara no campo *********************/
/* Passar como parâmetro o tipo de máscara, campo e a tecla*/
/**********************************************************/	
function MascaraCampo(tipo, campo, teclaPress) {
    if (window.event)  {
        var tecla = teclaPress.keyCode;
    } else {
        tecla = teclaPress.which;
    }

    var s = new String(campo.value);
    // Remove todos os caracteres à seguir: ( ) / - . e espaço, para tratar a string denovo.
    s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');

    tam = s.length + 1;

    if ( tecla != 9 && tecla != 8 ) {
        switch (tipo)
        {
        case 'CPF' :
            if (tam > 3 && tam < 7)
                campo.value = s.substr(0,3) + '.' + s.substr(3, tam);
            if (tam >= 7 && tam < 10)
                campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);
            if (tam >= 10 && tam < 12)
                campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);
        break;

        case 'CNPJ' :

            if (tam > 2 && tam < 6)
                campo.value = s.substr(0,2) + '.' + s.substr(2, tam);
            if (tam >= 6 && tam < 9)
                campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam-5);
            if (tam >= 9 && tam < 13)
                campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam-8);
            if (tam >= 13 && tam < 15)
                campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4)+ '-' + s.substr(12,tam-12);
        break;

        case 'TEL' :
            if (tam > 2 && tam < 4)
                campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,tam);
            if (tam >= 7 && tam < 11)
                campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,4) + '-' + s.substr(6,tam-6);
        break;

        case 'DATA' :
            if (tam > 2 && tam < 4)
                campo.value = s.substr(0,2) + '/' + s.substr(2, tam);
            if (tam > 4 && tam < 11)
                campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam-4);
        break;
        
        case 'CEP' :
            if (tam > 5 && tam < 7)
                campo.value = s.substr(0,5) + '-' + s.substr(5, tam);
        break;
        }
    }
}



/****************************************************************/	
/* Função que determina a quantidade de caracteres do textarea **/
/* Passar como parâmetro o objeto,tamanho e a div ***************/
/***************************************************************/	
function Contar(Campo,NumCaracteresMax,DivResposta){
	a = window.document.getElementById(Campo).value.length;
	elemento = document.getElementById(DivResposta);
	elemento.innerHTML = NumCaracteresMax - a;
	if(a>NumCaracteresMax){
		resposta = document.getElementById(DivResposta);
		resposta.innerHTML = 0;
		c = document.getElementById(Campo).value.substring(0,NumCaracteresMax);
		document.getElementById(Campo).value = c;
	}
}

/****************************************************************/	
/* Função que Valida o e-mail ***********************************/
/* Passar como parâmetro o email que retorna true ou false ******/
/***************************************************************/	
function ValidarEmail(email) {
    er = /^[a-z0-9\._-]+@([a-z0-9-]{2,26}(\.[a-z0-9-]{2,26})+)|([0-9-]{1,3}\.[0-9-]{1,3}\.[0-9-]{1,3}\.[0-9-]{1,3})1$/;
    if (!er.test(email.toLowerCase())) { 
		return false;
	}
    else { 
		return true; 
	}
}

/****************************************************************/	
/* Função que Formata o Valor ***********************************/
/* Passar como parâmetro o id, tamanho máximo e a tecla   ******/
/***************************************************************/	
function FormataValor(id,tammax,teclapres) {
    
        if(window.event) { // Internet Explorer
         var tecla = teclapres.keyCode; }
        else if(teclapres.which) { // Nestcape / firefox
         var tecla = teclapres.which;
        }
    
	vr = document.getElementById(id).value;
	vr = vr.toString().replace( "/", "" );
	vr = vr.toString().replace( "/", "" );
	vr = vr.toString().replace( ",", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	tam = vr.length;

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

	if (tecla == 8 ){ tam = tam - 1; }

	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
	if ( tam <= 2 ){
		document.getElementById(id).value = vr; }
	if ( (tam > 2) && (tam <= 5) ){
		document.getElementById(id).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }
	if ( (tam >= 6) && (tam <= 8) ){
		document.getElementById(id).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
	if ( (tam >= 9) && (tam <= 11) ){
		document.getElementById(id).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
	if ( (tam >= 12) && (tam <= 14) ){
		document.getElementById(id).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
	if ( (tam >= 15) && (tam <= 17) ){
		document.getElementById(id).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 Validar CNPJ ************************************/
/* Passar como parâmetro o número do CNPJ com a máscara****/
/**********************************************************/	
function ValidaCNPJ(campo) {
			 CNPJ = campo;
			 erro = new String;
			 if (CNPJ.length < 18) return false; 
			 if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
			 if (erro.length == 0) return false;
			 }
			 //substituir os caracteres que nao sao números
		   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)) return false; 
		   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])){
				   return false;
		   }
		   if (erro.length > 0){
				   return false;
		   } else {
				 return true;
		   }
		   return true;
   }	

/**********************************************************/	
/* Função Validar CPF ************************************/
/* Passar como parâmetro o número do CPF com a máscara****/
/**********************************************************/	
function ValidaCPF (numero) {
	var numcpf, cpf;
	numcpf = numero.substr(0,3);
	numcpf += numero.substr(4,3);
	numcpf += numero.substr(8,3);
	numcpf += numero.substr(12,2);
	cpf = numcpf;

	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;
	}	



/**********************************************************/	
/* Função que retira acentos ******************************/
/* Passar como parâmetro o objeto *************************/
/**********************************************************/	
 function retiraAcento(obj)
 {
   palavra = String.fromCharCode(event.keyCode);
  
  var caracteresInvalidos = 'aeiouâeîôuäëiöüáéíóúaoAEIOUÂEÎÔUÄËIÖÜÁÉÍÓÚAO';
  var caracteresValidos =   'aeiouaeiouaeiouaeiouaoAEIOUAEIOUAEIOUAEIOUAO';
  var acento = "´`^¨~";
  if(acento.indexOf(palavra)!= -1)
  {
    window.event.keyCode = 0;
  }
 
 if (caracteresInvalidos.indexOf(palavra) == -1) 
  {
       if (caracteresValidos.indexOf(palavra) != -1) {
         window.event.keyCode = 0;
         obj.value = obj.value + palavra;
       }
  } 
  else 
  {
           window.event.keyCode = 0;
           nova = caracteresValidos.charAt(caracteresInvalidos.indexOf(palavra));
           obj.value =  obj.value + nova;
  }
  
 }


/**********************************************************/	
/* Funçao que remove espaçoes em branco *******************/
/**********************************************************/	
function trim(str){
	return str.replace(/^\s+|\s+$/g,"");
}


