	
	function isDataValida(campo) {
		var reDate = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
	
		eval(reDate);
		if (reDate.test(campo.value)) {
			return true;
		} else if (campo.value != null && campo.value != "") {
			alert(campo.value + " não é uma data válida.");
			campo.value = '';
			campo.focus() ;
			return false;	
		}
	} 
	
	function verificaDatas(dtInicial, dtFinal){

				var dtini = dtInicial.value;
				var dtfim = dtFinal.value;
				
							
				datInicio = new Date(dtini.substring(6,10), 
									 dtini.substring(3,5), 
									 dtini.substring(0,2));
				datInicio.setMonth(datInicio.getMonth() - 1); 
				
				
				datFim = new Date(dtfim.substring(6,10), 
								  dtfim.substring(3,5), 
								  dtfim.substring(0,2));
								 
				datFim.setMonth(datFim.getMonth() - 1); 
			
				if(datInicio <=  datFim){				
					var one_day=1000*60*60*24;			
							
					document.forms[0].qtdeDias.value = Math.ceil((datFim.getTime()-datInicio.getTime())/(one_day));
					return true;
				} else {
					alert('ATENÇÃO: Data de Início maior que Data de Término!');
					dtFinal.value = '';
					dtFinal.focus();
					dtFinal.select();
					return false;
				}	
		}
			
			function soNumero(campo) {
				pVal = campo.value;
				var reTipo = /^\d+$/;
				if(!reTipo.test(pVal)){
					campo.value = '';
					campo.focus() 
				}

			}
			
			function eNumerico(campo){
				
				if (campo.value != ''){
					if (IsNumeric(campo.value ) == false){
						alert('Por favor digite apenas números!');
						campo.value = '';
						campo.focus() ;
					}				
				}
				
			}

/*
*	Rotinas de validações.
*
*
*/
function validaCNPJ(Campo) {
	CNPJ = Campo.value;
		
	
		if (CNPJ.length == 0) {
			return true;
		}
		if (CNPJ.length < 18) {
			return false;
		}
		if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
			return false;
		}
		//substituir os caracteres que não são 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;
		}
		return true;

}
  function validaCPF(campo) {
               var cpf; 		
			   cpf = campo.value;
			   cpf = cpf.replace(".", "");
			   cpf = cpf.replace(".", "");
			   cpf = cpf.replace("-", "");
			
			   erro = new String;
               
               if (cpf.length < 11) {
               		if (cpf.length == 0) {
						erro = 'O preenchimento do campo "CPF" é obrigatório.';
               		}else{
               			campo.value = '';
						erro = 'O "CPF" informado é inválido.';
					}
               }
               var nonNumbers = /\D/;
               if (nonNumbers.test(cpf)) {
               		campo.value = '';
					erro = 'O "CPF" informado é inválido.';
			   }
               if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
               		campo.value = '';
					erro = 'O "CPF" informado é inválido.';
               }
               var a = [];
               var b = new Number;
               var c = 11;
               for (i=0; i<11; i++){
                       a[i] = cpf.charAt(i);
                       if (i < 9) b += (a[i] * --c);
               }
               if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
               b = 0;
               c = 11;
               for (y=0; y<10; y++) b += (a[y] * c--);
               if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
               if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
                       erro = 'O "CPF" informado é inválido.';
                       campo.focus();
               }
               if (erro.length > 0){
               		   campo.value = '';
                       alert(erro);
					   return false;
               }
			   return true;
       }
 

function maxTextArea(obj) {
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength) {
		obj.value=obj.value.substring(0,mlength)
	}
}

function validaCabecalho(form){
	if (form.codOgm.value.length == '0') {
		alert('O preenchimento do campo "OGM" é obrigatório.');
	 	form.codOgm.focus();
		return false;
    }
 	else if (form.dscObjetivoImediato.value.length == '0' ){ 
 		alert('O preenchimento do campo "Objetivo Imediato/Produto N" é obrigatório.');
	 	form.dscObjetivoImediato.focus();
	 	return false;
	 }else{
	 	return true;
	 }
}

function validaPessoaFisicaJuridica(form){
	if (form.nome.value.length == '0' ){ 
 		alert('Pesquise pelo CPF/CNPJ para o preenchimento correto dos campos.');
 		return false;
 		}else{
 			return true;
 		}
}

/**
 *Validação genérica de formulários
 */
function validaForm(){
		
		var campos = document.getElementsByClassName("obrigatorio");
		for(i = 0 ; i < campos.length ; i++){
			var string = campos[i].value.toString().trim();
			if(string == ''){
				alert('O preenchimento do campo "'+campos[i].id+'" é obrigatório.');
				campos[i].focus();
				return false;
			}
		}
		return true;
	}
/**
 *função genérica para submeter formulários
 */
function submitForm(form){
	if(validaForm()){
		form.submit();
	}
}	
