var caminhoCGI='/cgi-bin/sai';

function mudapagina(a) {
        if (a) {
                document.forms[0].limite.value=document.forms[0].limitex.options[document.forms[0].limitex.selectedIndex].value;
        } else {
                document.forms[0].limite.value-=15;
        }
        document.forms[0].submit();
}
 
function mudapagina2(a) {
        document.forms[0].limite.value=a;
        document.forms[0].submit();
}
//function TravaBotaoEsquerdo() {
//	if (document.layers) {
//		if (document.event.which==3) {
//			window.alert('ERRO: Botão esquerdo não possui função no sistema.');
//		}
//	} else {
//		if (document.event.button==3) {
//			window.alert('ERRO: Botão esquerdo não possui função no sistema.');
//		}
//	}
//}

function VerificaData (dt){
        var dia;
        var mes;
        var ano;
        var tipo;
        var diafev;
        if (dt != 0){
            if (dt.substr(2,1) == '/'){
                dia = dt.substr(0,2);
                mes = dt.substr(3,2);
                ano = dt.substr(6,4);
            } else {
                if (dt.substr(4,1) == '-'){
                     ano = dt.substr(0,4);
                     mes = dt.substr(5,2);
                     dia = dt.substr(8,2);
                }
            }
            var pattern = new RegExp("[^0-9\s]");
            if (pattern.test(dia)) {
                return (1);
            }
            if (pattern.test(mes)) {
                return (1);
            }
            if (pattern.test(ano)) {
                return (1);
            }
            if ((dia > 31) || (mes >12)){
                return (1);
            }
            if (ano.length < 3){
                return (1);
            }
          }
        if (dia == 0){
            dia = '';
        }
        if (dia == ''){
            if (((mes%2) == 0) && (mes != 2) && (mes < 7)){
                  return(0);
            } if ((mes%2 != 0) && (mes == 7)){
                      return(1);
              } if (((mes%2) == 0) && (mes > 7)){
                         return(1);
                } if (((mes%2) != 0) && (mes != 2) && (mes < 7)){
                           return(1);
                  } if (((mes%2) != 0) && (mes > 7)){
                             return(0);
                    } if (mes == 2){
                             if ((ano%4) != 0){
                                  return(1);
                             } else {
                                if ((ano%4) == 0){
                                       return(0);
                                }
                             }
                      }
        }
        if ((mes == 2) && (dia > 28)){
            if ((ano%4) != 0){
                 diafev = 28;
            }else{
                if ((ano%4) == 0){
                     diafev = 29;
                }
             }
            if (diafev >= dia){
                return (0);
            } else {
                    return (1);
              }
        } else {
           if ((mes == 2) && (dia <= 28)){
                  return (0);
          } else{
               if ((dia%2) != 0){
                    tipo = 'impar';
               }
               if ((tipo != '') && (dia < 32) && (dia > 30)){
                   if ((tipo == 'impar') && ((mes <= 7) && ((mes%2) != 0))){
                        return (0);
                   } else {
                        if((tipo == 'impar') && ((mes%2) == 0) && (mes >7)) {
                            return (0);
                        } else {
                             return (1);
                        }
                  }
               }
          }
       }
}


function PontoDecimal(numero) {
	numero=numero.toString();
        numero=numero.replace("[\,]",".");
        var ponto=numero.indexOf(".");
        var resultado='';
        if (ponto == -1) {
        	resultado=numero+".00";
        } else {
        	numero=numero+"0";
                if (ponto == 0) {
                	resultado="0"+numero.substr(0,3);
                } else {
                	resultado=numero.substr(0,ponto+3)
                }
        }
        resultado=resultado.replace("[\.]",",");
        return resultado;
}

function abrejanela(caminho,x,y,nome) {
	if ((nome == null) || (nome == "")) {
		nome = "";
	}
	var windowProps='directories=no,location=no,toolbar=no,status=no,resizable=no,scrollbars=yes,height='+y+'width='+x;
	var w = window.open(caminho,nome,windowProps);
}

function abrecombo(chave,login,caminho,nome) {
	if ((caminho == null) || (caminho == "")) {
		caminho = "";
	} else {
	var x = 400;
	var y = 500;
	caminho = caminho+"?login="+login+"&chave="+chave;
	var windowProps='directories=no,location=no,toolbar=no,status=no,resizable=no,scrollbars=yes,height='+y+'width='+x;
	var w = window.open(caminho,nome,windowProps);
 }
}

function CriticaNumero(elemento,valor)  {
	var pattern = new RegExp("[^0-9]");
	if (pattern.test(valor)) {
		elemento.focus();
		elemento.select();
		alert("Este campo deve apenas conter números.");
	}
}

function CriticaValor(elemento,valor)  {
	var pattern = new RegExp("[^0-9\.\,\s]");
	if (pattern.test(valor)) {
		elemento.focus();
		elemento.select();
		alert("Este campo deve apenas conter números, ponto ou vírgula.");
	} 
}

function CriticaValorJS(elemento,valor)  {
        var pattern = new RegExp("[^0-9\.\s]");
        if (pattern.test(valor)) {
                elemento.focus();
                elemento.select();
                alert("Este campo deve apenas conter números ou ponto.");
        }
}

function ValidaValor(elemento)  {
	var valor=elemento.value;
	var pattern = new RegExp("[^0-9\.\,]");
	if (pattern.test(valor)) {
		return false;
	} else {
		return true;
	}
}

function CriticaLetras(elemento,texto)  {
	var pattern = new RegExp("[^A-Za-z\s]");
	if (pattern.test(texto)) {
		elemento.focus();
		elemento.select();
		alert("Este campo deve apenas conter letras.");
	}
}

function CriticaNome(elemento,texto)  {
	var pattern = new RegExp("[^ A-Za-zçÇáéíóúÁÉÍÓÚàèìòùÀÈÌÒÙãõÃÕâêîôûÂÊÎÔÛäëïöüÄËÏÖÜ\-]");
	if (pattern.test(texto)) {
		elemento.focus();
		elemento.select();
		alert("Este campo deve apenas conter letras e espaços.");
	}
}

function CriticaPalavra(elemento,texto)  {
	var pattern = new RegExp("[^ 0-9A-Za-zçÇáéíóúÁÉÍÓÚàèìòùÀÈÌÒÙãõÃÕâêîôûÂÊÎÔÛäëïöüÄËÏÖÜ\-\s\(\)\-\.\,\/\\\!\@\#\$\%\&\*\+\=\_\[\]\{\}\?\;\:\<\>\|]");
	if (pattern.test(texto)) {
		elemento.focus();
		elemento.select();
		alert("Este campo deve apenas conter números e/ou letras.");
	}
}

function CriticaData(elemento,data)  {
	var pattern = new RegExp("(0?[1-9]|[12][0-9]|3[01])\/(0?[1-9]|1[0-2])\/([1-3][0-9]{3}))");
	var pattern2 = new RegExp("(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[0-2])([1-3][0-9]{3}))");
	if (! pattern.test(data)) {
		if (! pattern2.test(data)) {
			elemento.focus();
			elemento.select();
			alert("Este campo deve conter uma data no formato \"DD/MM/AAAA\" .");
		} else {
			var texto=data.substr(0,2)+"/"+data.substr(2,2)+"/"+data.substr(4,4);
			elemento.value=texto
		}
	}
		
}
							
function CriticaSerial(elemento,numero)  {
	var pattern = new RegExp("[^0-9A-Za-z\.\-\s]");
	if (pattern.test(numero)) {
		elemento.focus();
		elemento.select();
		alert("Este campo deve apenas conter números, letras, hífen ou ponto.");
	}
}


function CriticaNF(elemento,NF)  {
	var pattern = new RegExp("[^0-9\-\.\s]");
	if (pattern.test(NF)) {
		elemento.focus();
		elemento.select();
		alert("Este campo deve apenas conter números, ponto ou hífen.");
	}
}

function CriticaUF(elemento) {
	elemento.value=elemento.value.toUpperCase();
	var UF = elemento.value;
	var pattern = new RegExp ("AC|AL|AM|AP|BA|CE|DF|ES|GO|MA|MG|MS|MT|PA|PB|PE|PI|PR|RJ|RN|RO|RR|RS|SC|SE|SP|TO");
	if (!pattern.test(UF)) {
		elemento.focus();
		elemento.select();
		alert('A UF digitada não existe');
		
	}
}


function CriticaCEP(elemento,CEP)  {
	var pattern = new RegExp("([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])|(\s)");
	if (! pattern.test(CEP)) {
		elemento.focus();
		elemento.select();
		alert("O CEP digitado deve conter 8 algarismos, sem hífen.");
	}
}

function CriticaCPF(CNPF) {
   var cpf = CNPF.value;
   var dig1;
   var dig2;
   var n1 = cpf.substr(0,1);
   var n2 = cpf.substr(1,1);
   var n3 = cpf.substr(2,1);
   var n4 = cpf.substr(3,1);
   var n5 = cpf.substr(4,1);
   var n6 = cpf.substr(5,1);
   var n7 = cpf.substr(6,1);
   var n8 = cpf.substr(7,1);
   var n9 = cpf.substr(8,1);
   var n10 = cpf.substr(9,1);
   var n11 = cpf.substr(10,1);
//   var n12 = cpf.substr(11,1);
 
//Primeiro digito do CNPF
   var soma1 = ((n1*10)+(n2*9)+(n3*8)+(n4*7)+(n5*6)+(n6*5)+(n7*4)+(n8*3)+(n9*2));
   var resto = (soma1 % 11);
 
   if ((resto == 0) || (resto == 1)) { dig1 = 0; }
   else if (resto > 1) { dig1 = (11 - resto); }
 
//Segundo digito do CNPF
 
   var soma2 = ((n1*11)+(n2*10)+(n3*9)+(n4*8)+(n5*7)+(n6*6)+(n7*5)+(n8*4)+(n9*3)+(dig1*2));
   var resto2 = (soma2 % 11);
 
   if ((resto2 == 0) || (resto2 == 1)) { dig2 = 0; }
   else if (resto2 > 1) { dig2 = (11 - resto2); }
 
   if ((dig1 != n10) || (dig2 != n11)) { alert('CPF invalido'); CNPF.focus(); CNPF.select(); }
}

function CriticaCNPJ(campo) {
	var cnpj = campo.value;
	var pattern = new RegExp("[0-9]{14}");
	if (!(pattern.test(cnpj))) {
		campo.select();
		campo.focus();
		window.alert('CNPJ inválido. Campo deve conter 14 algarismos.');
	} else {
   		var dig1;
   		var dig2;
		var n1 = cnpj.substr(0,1);
		var n2 = cnpj.substr(1,1);
		var n3 = cnpj.substr(2,1);
		var n4 = cnpj.substr(3,1);
		var n5 = cnpj.substr(4,1);
		var n6 = cnpj.substr(5,1);
		var n7 = cnpj.substr(6,1);
		var n8 = cnpj.substr(7,1);
		var n9 = cnpj.substr(8,1);
		var n10 = cnpj.substr(9,1);
		var n11 = cnpj.substr(10,1);
		var n12 = cnpj.substr(11,1);
		var n13 = cnpj.substr(12,1);
		var n14 = cnpj.substr(13,1);
		//   var n15 = cnpj.substr(14,1);
		//   var n16 = cnpj.substr(15,1);
 
	//Primeiro digito do CNPJ
		var soma1 = ((n1*5)+(n2*4)+(n3*3)+(n4*2)+(n5*9)+(n6*8)+(n7*7)+(n8*6)+(n9*5)+(n10*4)+(n11*3)+(n12*2));
		var resto = (soma1 % 11);
 
		if ((resto == 0) || (resto == 1)) { dig1 = 0; }
		else if (resto > 1) { dig1 = (11 - resto); }
 
	//Segundo digito do CNPJ
 
		var soma2 = ((n1*6)+(n2*5)+(n3*4)+(n4*3)+(n5*2)+(n6*9)+(n7*8)+(n8*7)+(n9*6)+(n10*5)+(n11*4)+(n12*3)+(dig1*2));
		var resto2 = (soma2 % 11);
 
		if ((resto2 == 0) || (resto2 == 1)) { dig2 = 0; }
		else if (resto2 > 1) { dig2 = (11 - resto2); }
 
		if ((dig1 != n13) || (dig2 != n14)) { 
			campo.select();
			campo.focus();
			window.alert('CNPJ invalido. Número fornecido é inválido.');
		}
}



//function CriticaCNPJ(elemento,campo)  {
//	var pattern = new RegExp("[0-9]{14}");
//	var pattern2 = new RegExp("[0-9]{2}\.[0-9]{3}\.[0-9]{3}\/[0-9]{4}\-[0-9]{2}");
//	if (!(pattern.test(campo) || pattern2.test(campo))) {
//		elemento.focus();
//		elemento.select();
//		alert("Este campo deve conter 14 algarismos, sem pontos, barras ou hífens.");
//	} else {
//		if (!(pattern2.test(campo))) {
//			var tmp = campo.substr(0,2)+'.'+campo.substr(2,3)+'.'+campo.substr(5,3)+'/'+campo.substr(8,4)+'-'+campo.substr(12,2);
//			elemento.value = tmp;
//		}
//		
//	}
			
}

function CriticaHora(elemento,campo) {
	var pattern = new RegExp("([01][0-9]|2[0-3])\:([0-5][0-9])");
	if (! pattern.test(campo)) {
		elemento.focus();
		elemento.select();
		alert("Este campo deve conter um valor de hora no formato HH:MM.");
	}
}


function CriticaTelefone(elemento,campo) {
        var pattern = new RegExp("([0-9][0-9][0-9][0-9])\-([0-9][0-9][0-9][0-9])");
        if (! pattern.test(campo)) {
                elemento.focus();
                elemento.select();
                alert("Este campo deve conter o formato de telefone 9999-9999.");
        }
}

function CriticaUpper(elemento) {
	var texto=elemento.value;
        var pattern=new RegExp('[^A-Za-z0-9\_çÇáàãâäÁÀÃÂÄéèêëÉÈÊËíìîïÍÌÎÏóòõôöÓÒÕÔÖúùûüÚÙÛÜýÿÝ ]');
        if (pattern.test(texto)) {
        	elemento.focus();
                elemento.select();
                window.alert('Este campo deve conter apenas letras, números, underscore (_) ou espaços.');
        } else {
        	elemento.value=texto.toUpperCase();
        }
}

function CriticaLogin(elemento) {
	var login=elemento.value;
	var pattern=new RegExp('[a-z\_]');
	if (!pattern.test(texto)) {
		elemento.select();
		elemento.focus();
		window.alert("Este campo permite apenas letras minúsculas ou underscore (_).");
	}
}

function NomeRandomico() {
	var nome
	for (var i=0;i<=9;i++) {
		var rand=Math.random()*26;
		var ascii=Math.round(rand)+97;
		var letra=String.fromCharCode(ascii);
		nome+=letra;
	}
	return nome;
}

function DiaLimiteMes (campo1,campo2,campo3) {
	var dia = parseInt(campo1.value);
        var mes = parseInt(campo2.value);
        var ano = parseInt(campo3.value);
        if (mes == 2){
        	if (ano % 4 == 0){
			if (dia > 29){
				alert('Dia inválido - alem do limite do mes');
                                return true;
                        } else {
                        	return false;
                        }
                } else {
                        if (dia > 28) {
                        	alert('Dia inválido - alem do limite do mes');
                                return true;
                        } else {
                                return false;
                        }
                }
        } else if ((mes <= 7) && ((mes % 2) == 1)) {
                if (dia > 31) {
        		alert('Dia inválido - alem do limite do mes');
                        return true;
                } else {
                        return false;
                }
        } else if ((mes > 7) && ((mes % 2) == 0)) {
                if (dia > 31) {
                        alert('Dia inválido - alem do limite do mes');
                        return true;
                } else {
                        return false;
                }
        } else {
                if (dia > 30) {
        		alert('Dia inválido - alem do limite do mes');
                        return true;
                } else {
                        return false;
                }
        }
}
function FormataData(teclapres,elemento){
        var tecla = teclapres.keyCode;
        vr  = elemento.value;
        vr  = vr.replace( "/", "" );
        vr  = vr.replace( "/", "" );
        tam = vr.length + 1;

        if ( tecla != 9 && tecla != 8 ){
                if ( tam > 2 && tam < 5 )
                        elemento.value = vr.substr(0,tam - 2) + '/' + vr.substr(tam - 2,tam);
                if ( tam >= 5 && tam <= 10 )
                        elemento.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);
        }
}
