function excluir(url) { // Confirma exclusï¿½o de um link
	var click = confirm("Confirma exclusï¿½o de Registro?");
	if (click == true) {
		window.open(url,"_self");
	}
}

//####################################################################
//########### VALIDA A DATA
//####################################################################
function validarData(data){
	var dia = parseInt(data.substring(0,2),10);
	var mes = parseInt(data.substring(3,5),10);
	var ano = parseInt(data.substring(6,10),10);
	
	if (dia <= 31 && mes <=12 && ano >= 1000){
        if (data.substring(0,1)=='0' && data.substring(1,2) != '0' || data.substring(0,1)!='0'){
        	if (data.substring(2,3)=="/"){
            	if (data.substring(3,4)=='0' && data.substring(4,5)!='0' ||	data.substring(3,4)!='0'){
                	if (data.substring(5,6)=="/"){
                    	if (data.substring(6,7)== '0' || data.substring(6,7)=='' && data.substring(7,8)!='0'){
                        	window.alert('O ano que você digitou não existe!');
                        	return false;

                        	if (mes == 2){
                            	if ((dia > 0 ) && (dia <= 29)){
                                	if (dia == 29){
                                    	if ((ano % 4) == 0){
                                        	return true;
                                    	}else{
                                        	window.alert('Este dia não existe, certifique - se de que digitou corretamente!');
                                        	return false;
                                    	}
                                	}
                            	}else{
                                	window.alert('Este dia não existe, certifique - se de que digitou corretamente!');
                                	return false;
                            	}
                        	}
                        	if ((mes == 4)||(mes == 6)||(mes == 9)||(mes == 11)){
                            	if ((dia > 0 ) && (dia <= 30)){
                                	return true;
                               	}else{
                                	window.alert('Este dia não existe, certifique - se de que digitou corretamente!');
                                	return false;
                            	}
                        	}
                        	if ((mes == 1)||(mes == 3)||(mes == 5)||(mes ==7)||(mes == 8)||(mes == 10)||(mes == 12)){
                            	if ((dia > 0) && (dia <= 31)) {
                                	return true;
                            	}else{
                                	window.alert('Este dia não existe, certifique -se de que digitou corretamente!');
                                	return false;
                            	}
                        	}
                    	}
                	}else{
                    	window.alert('A data foi digitada fora do padrão (dd/mm/aaaa) !');
                    	return false;
                	}
            	}else{
                	window.alert('Você digitou um mês que não existe!');
                	return false;
            	}
        	}else{
            	window.alert('A data foi digitada fora do padrão(dd/mm/aaaa) !');
            	return false;
        	}
    	}else{
        	window.alert('Você digitou um dia que não existe!');
        	return false;
    	}
	}else{
    	window.alert('O dia e/ou o mês que você digitou não existe, ou Você digitou fora do padrão (dd/mm/aaaa) !');
    	return false;
	}
	return true;
}


//####################################################################
//########### VALIDA A DATA
//####################################################################
function validarPeriodoData(campo1, campo2) {
	try {
		if (campo1.value != "" && 
				campo2.value != "") {
			var d = campo1.value;
			var dataInicial = new Date(d.substring(6,10), d.substring(3,5), d.substring(0,2));
			var d = campo2.value;
			var dataFinal = new Date(d.substring(6,10), d.substring(3,5), d.substring(0,2));
			
			if (dataInicial > dataFinal) {
				alert('A data inicial não pode ser maior que a data final no período.');
				return false;
			}
		} else {
			if ((campo1.value != "" && 
				campo2.value == "") || 
				(campo1.value == "" && 
				campo2.value != "")) {
					alert('Ambas as datas deste período devem ser preenchidas.');
					return false;
				}
		}
	} catch(e) {}
	return true;
}

//####################################################################
//########### VALIDA A DATA
//####################################################################
function isDataMenor(campo1, campo2) {
	try {
		if (campo1.value != "" && 
				campo2.value != "") {
			var d = campo1.value;
			var dataInicial = new Date(d.substring(6,10), d.substring(3,5), d.substring(0,2));
			var d = campo2.value;
			var dataFinal = new Date(d.substring(6,10), d.substring(3,5), d.substring(0,2));
			
			if (dataInicial > dataFinal) {
				return false;
			}
		} else {
			if ((campo1.value != "" && 
				campo2.value == "") || 
				(campo1.value == "" && 
				campo2.value != "")) {
					return false;
				}
		}
	} catch(e) {}
	return true;
}

function validaCPF(cpf) { // Verifica a validade do CPF informado
	var msg = "CPF válido";
	var sch;
	var nRecebeCPF, nSoma, nresultado1, nresultado2, ns, nx;
	var arrNumero = Array(11);

	nRecebeCPF = cpf;
	ns = "";
	for (nx = 0; nx < nRecebeCPF.length; nx++) {
		sch = nRecebeCPF.substring(nx,nx+1);
		sch = parseInt(sch);
		sch = new String(sch);
		if (sch != "NaN") {
			ns += sch;
		}
	}

	nRecebeCPF = ns

	tudoIgual = 1; // Verifica se todos os números são iguais
	for (i = 1; i < nRecebeCPF.length; i++) {
		if (nRecebeCPF.substring(i-1, i) != nRecebeCPF.substring(i, i+1)) {
			tudoIgual = 0;
		}
	}

    if (tudoIgual == 1){
        resultado = false;
    }
    
	if (nRecebeCPF.length != 11) {
		msg = "Quantidade de dígitos inferior a quantidade de dígitos de CPF";
		resultado = false;
	}
	else if (tudoIgual == 1) {
		msg = "Todos us números são iguais";
		resultado = false;
	}
	else if (nRecebeCPF == "12345678909") {
		msg = "Número de CPF inexistente";
		resultado = false;
	}
	else{
		arrNumero[0] = nRecebeCPF.substring(0,1);
		arrNumero[1] = nRecebeCPF.substring(1,2);
		arrNumero[2] = nRecebeCPF.substring(2,3);
		arrNumero[3] = nRecebeCPF.substring(3,4);
		arrNumero[4] = nRecebeCPF.substring(4,5);
		arrNumero[5] = nRecebeCPF.substring(5,6);
		arrNumero[6] = nRecebeCPF.substring(6,7);
		arrNumero[7] = nRecebeCPF.substring(7,8);
		arrNumero[8] = nRecebeCPF.substring(8,9);
		arrNumero[9] = nRecebeCPF.substring(9,10);
		arrNumero[10] = nRecebeCPF.substring(10,11);

		nSoma = 10 * arrNumero[0] + 9 * arrNumero[1] + 8 * arrNumero[2] + 7 * arrNumero[3] + 6 * arrNumero[4] + 5 * arrNumero[5] + 4 * arrNumero[6] + 3 * arrNumero[7] + 2 * arrNumero[8]
		nSoma = nSoma-(11*parseInt(nSoma/11));

		if (nSoma == 0 || nSoma == 1) {
			nresultado1 = 0;
		}
		else{
			nresultado1 = 11-nSoma;
		}

		if (nresultado1 == arrNumero[9]) {
			nSoma = arrNumero[0] * 11 + arrNumero[1] * 10 + arrNumero[2] * 9 + arrNumero[3] * 8 + arrNumero[4] * 7 + arrNumero[5] * 6 + arrNumero[6] * 5 + arrNumero[7] * 4 + arrNumero[8] * 3 + arrNumero[9] * 2
			nSoma = nSoma-(11*parseInt(nSoma/11));
			if (nSoma == 0 || nSoma == 1) {
				nresultado2 = 0;
			}
			else{
				nresultado2 = 11 - nSoma;
			}

			if (nresultado2 == arrNumero[10]) {
				resultado = true;
			}
			else{
				msg = "CPF inválido";
				resultado = false;
			}
		}
		else{
			msg = "CPF inválido";
			resultado = false;
		}
	}

	//resultado = msg;
	return resultado;
}

	function validarCPF2(cpfValue){
	   var cpf = cpfValue;
	   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
	   if(!filtro.test(cpf)){
	     alert(unescape("CPF inválido. Tente novamente."));
		 return false;
	   }
	   
	   cpf = remove(cpf, ".");
	   cpf = remove(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"){
		  window.alert("CPF inválido. Tente novamente.");
		  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))){
		 window.alert("CPF inválido. Tente novamente.");
		 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))){
	     window.alert("CPF inválido. Tente novamente.");
		 return false;
	   }
	   return true;
	 }
	 
	 function remove(str, sub) {
	   i = str.indexOf(sub);
	   r = "";
	   if (i == -1) return str;
	   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
	   return r;
	 }

 function trim(s) {
	    try {
	      while (s.substring(0,1) == ' ') {
	        s = s.substring(1,s.length);
	      }
	      while (s.substring(s.length-1,s.length) == ' ') {
	        s = s.substring(0,s.length-1);
	      }
	      return s;
	    } catch (e) {}
	}
	 
/*
*  Valida Email
*  Utilizar da seguinte forma: validaEmail(document.forms[#].elements[#])
*/
function validaEmail2(email){
    if ((email.value.indexOf('@') == -1) || (email.value.indexOf('.') == -1) ){
        email.focus();
        return false;
    }

    return true;
}

function validaEmail(field) {
    var frm = trim(field.value);

    if (!isEmail(frm)) {
        return false;
    } else {
        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 isEmail(field) {
    if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field))) {
        return false;
    } else {
        return true;
    }
}

/*function validaEmail(pStr) {
    var reEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
    if (reEmail.test(pStr)) {
        return true;
    } else if (pStr != null && pStr != "") {
        return false;
    }
} // validaEmail
*/
/*
* Valida CNPJ
*/
function isNUMB(c) { 
    if((cx=c.indexOf(","))!=-1) { 
        c = c.substring(0,cx)+"."+c.substring(cx+1); 
    } 
    if((parseFloat(c) / c != 1)) { 
        if(parseFloat(c) * c == 0) { 
            return true; 
        } 
        else { 
            return false; 
        } 
    } else { 
        return true; 
    } 
} 

function LIMP(c) { 
    while((cx=c.indexOf("-"))!=-1) { 
        c = c.substring(0,cx)+c.substring(cx+1); 
    } 
    while((cx=c.indexOf("/"))!=-1) { 
        c = c.substring(0,cx)+c.substring(cx+1); 
    } 
    while((cx=c.indexOf(","))!=-1) { 
        c = c.substring(0,cx)+c.substring(cx+1); 
    } 
    while((cx=c.indexOf("."))!=-1) { 
        c = c.substring(0,cx)+c.substring(cx+1); 
    } 
    while((cx=c.indexOf("("))!=-1) { 
        c = c.substring(0,cx)+c.substring(cx+1); 
    } 
    while((cx=c.indexOf(")"))!=-1) { 
        c = c.substring(0,cx)+c.substring(cx+1); 
    } 
    while((cx=c.indexOf(" "))!=-1) { 
        c = c.substring(0,cx)+c.substring(cx+1); 
    } 
return c; 
}

function validaCNPJ(CNPJ) {
    CNPJ = LIMP(CNPJ); 
    if(isNUMB(CNPJ) != 1) { 
        return false; 
    } else { 
        if(CNPJ == 0) { 
            return false; 
        } 
        else { 
            g = CNPJ.length-2; 
            if(RealTestaCNPJ(CNPJ,g) == 1) { 
                g=CNPJ.length-1; 
                if(RealTestaCNPJ(CNPJ,g) == 1) { 
                    return true; 
                } else { 
                    return false; 
                } 
            } else { 
                return false; 
            } 
        } 
    } 
}

function RealTestaCNPJ(CNPJ,g) { 
    var VerCNPJ=0; 
    var ind=2; 
    var tam; 
    
    for(f=g;f>0;f--) { 
        VerCNPJ += parseInt(CNPJ.charAt(f-1)) * ind; 
        if(ind>8) { 
            ind=2; 
        } else { 
            ind++; 
        } 
    } 

    VerCNPJ%=11; 
    if(VerCNPJ==0 || VerCNPJ==1) { 
        VerCNPJ = 0; 
    } else { 
        VerCNPJ=11-VerCNPJ; 
    } 

    if(VerCNPJ!=parseInt(CNPJ.charAt(g))) { 
        return false; 
    } else { 
        return true; 
    } 
}

