/////////////////////////////////////////////////////////////////
function e_numerico(campo) {
   if(isNumber(campo.value)){
      //alert("Por favor preencha o campo número com um número válido");
	  document.all('operacao').innerHTML = "Preencha o campo com um número válido"; setTimeout("document.all('operacao').innerHTML = ''", 3000)
      campo.value="";
	  campo.focus();
      return false;
   }
}

function isNumber(numero){
   var CaractereInvalido = false;
   for (i=0; i < numero.length; i++){
      var Caractere = numero.charAt(i);
      //if(Caractere != "." && Caractere != "," && Caractere != "-"){
         if (isNaN(parseInt(Caractere))) CaractereInvalido = true;
      //}
   }
   return CaractereInvalido;
} 

/////////////////////////////////////////////////////////////////
function valida_selecao(formobj, msg_error) {
  for (var i = 0; i < formobj.length; i++){
    if(formobj[i].checked){return true};
  }
  alert(msg_error)
  return false;
}

/////////////////////////////////////////////////////////////////
function posmed(largura, altura){
  esquerda= (screen.width/2)-(largura/2);
  topo    = (screen.height/2)-(altura/2)-20;
  posmed = ',width='+largura+',height='+altura+', top='+topo+', left='+esquerda;
  return(posmed);
}

/////////////////////////////////////////////////////////////////
function validnofuture(dateField, hoje) {
  this.inDate = dateField.value;
  if (inDate.indexOf('/')) {
    var day = inDate.substring(0,inDate.indexOf("/"));
    var month   = inDate.substring(inDate.indexOf("/") + 1, inDate.lastIndexOf("/"));
    var year  = parseInt(inDate.substring(inDate.lastIndexOf("/") + 1, inDate.length));
  }
  if(!day || !month || !year) {return}
  var digitado = year + "" + month + "" + day;
  if (digitado > hoje){
    alert("Data futura não é permitido");
    dateField.value=hoje.substring(6,8)+"/"+hoje.substring(4,6)+"/"+hoje.substring(0,4) ;
    return(false)
  }
  return(true)
}

/////////////////////////////////////////////////////////////////
function comparadata(contents, data1, data2){
  dt1=(eval(contents+'.'+data1+'.value'));
  dt2=(eval(contents+'.'+data2+'.value'));

  dt1=dt1.substring(6,10)+""+dt1.substring(3,5)+""+dt1.substring(0,2)
  dt2=dt2.substring(6,10)+""+dt2.substring(3,5)+""+dt2.substring(0,2)

  resultado = dt1 <= dt2;
  return resultado;

}

function formCheck(formobj){
  //alert("form.name = "+formobj.name);
  //return false;
  //return true;
  var pegoufocu=0;
  e=0;
  fieldRequired    = Array();
  fieldDescription = Array();

  for (var i = 0; i < formobj.length; i++){
    //alert(formobj[i].name+" "+formobj[i].type+" "+formobj[i].value);
    if (formobj[i].NONULL || formobj[i].nonull) {
      if (!formobj[i].DESCRICAO) {formobj[i].DESCRICAO = formobj[i].descricao}
	  if (!formobj[i].NONULL) {formobj[i].NONULL = formobj[i].nonull}
      fieldRequired[e] = formobj[i].name;
	  fieldDescription[e] = formobj[i].DESCRICAO;
      //parent.frames["dwn_oculto"].document.write(e+" - "+fieldRequired[e]+" = "+formobj[i].value+" Descrição "+formobj[i].DESCRICAO+"<br>");
      e++;
    }
  }
  //return true; //Comente essa linha para validar
  alertMsg = 'Por favor preencha:\n';
  var l_Msg = alertMsg.length;
  var foco;
  for (var i = 0; i < fieldRequired.length; i++){
    var obj = formobj.elements[fieldRequired[i]];
    //parent.frames["dwn_oculto"].document.write(i+" - "+obj.name+"  "+obj.type+"<br>");
	if (obj && obj.disabled==false){ 
      //parent.frames["dwn_oculto"].document.write(i+" - "+obj.name+"  "+obj.type+"<br>");
      switch(obj.type){
        case "select-one":
          if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
            alertMsg += fieldDescription[i] + "\n";
            if (!foco) {foco = (obj.id) ? obj.id : obj.name;}
          }
        break;
        case "select-multiple":
          if (obj.selectedIndex == -1){
            alertMsg += fieldDescription[i] + "\n";
            if (!foco) {foco = (obj.id) ? obj.id : obj.name;}
           }
        break;
        case "text":
        case "textarea":
        case "file":
		case "hidden":		
        if (obj.value == "" || obj.value == null){
          alertMsg += fieldDescription[i] + "\n";
          if (!foco) {foco = (obj.id) ? obj.id : obj.name;}
        }
        break;
        case "password":
        if (obj.value == "" || obj.value == null){
          alertMsg += fieldDescription[i] + "\n";
          if (!foco) {foco = (obj.id) ? obj.id : obj.name;}
        }
        break;
        default:
      }
    } 
    if (obj.type == null){
      var blnchecked = false;
      for (var j = 0; j < obj.length; j++){
        if (obj[j].checked){
          blnchecked = true;
        }
      }
      if (!blnchecked){
        alertMsg +=fieldDescription[i] + "\n";
        if (!foco) {foco = "nenhum";}
      }
    }
  }
  if (alertMsg.length == l_Msg){
    return true;
  }else{
  	type=eval("document."+formobj.name+"."+foco+".type");
	//alert(type);
    pre_msg = "Preencha o campo ";
    if (type == "hidden") {
	  pre_msg="Selecione o ";
	}
    if(document.all('operacao')) {
      document.all('operacao').innerHTML = pre_msg+eval(formobj.name+"."+foco+".descricao"); setTimeout("document.all('operacao').innerHTML = ''", 3000)
	}else{
	  alert(pre_msg+eval(formobj.name+"."+foco+".descricao"));
	}
    if(eval(formobj.name+"."+foco+".type") != "select-one") {
	var cor = formobj.name+"."+foco+".style.background='#cccccc'";
	eval(cor);
	var parametro = formobj.name+"."+foco+".onkeypress = function anounymous(){this.style.background=''};";
	eval(parametro);
	var parametro = formobj.name+"."+foco+".onmouseover = function anounymous(){this.style.background=''};";
	eval(parametro);
	}
	//alert(alertMsg);
	if(top.ancora_topo) {
	  top.ancora_topo.focus();
	}  
    if (foco != "nenhum" && type != "hidden") {
      eval("document."+formobj.name+"."+foco+".focus()");
    }
    return false;
  }
  return false
}
/////////////////////////////////////////////////////////////////
function FormataCEP(Campo, teclapres){
  var tecla = teclapres.keyCode;
  var vr = new String(Campo.value);
  count = 0;
  while (vr.search("-") !=-1 && count < 25) {
    vr = vr.replace("/", "");
    vr = vr.replace(".", "");
    vr = vr.replace("-", "");
    count ++;
  }
  tam = vr.length + 1;
  if (tecla != 9 && tecla != 8){
    if (tam > 5 && tam < 7)
    Campo.value = vr.substr(0, 5) + '-';
  }
}

/////////////////////////////////////////////////////////////////
function FormataHORA(Campo, teclapres){
  var tecla = teclapres.keyCode;
  var vr = new String(Campo.value);
  count = 0;
  while (vr.search(":") !=-1 && count < 25) {
    vr = vr.replace(":", "");
    count ++;
  }
  tam = vr.length + 1;
  if (tecla != 9 && tecla != 8){
    if (tam > 2 && tam < 4)
    Campo.value = vr.substr(0, 5) + ':';
  }
}

/////////////////////////////////////////////////////////////////
function TestaHora(cHora){
	var hora; 
	hora = LimpaCampo(cHora.value,10);
	var tam = hora.length;
	if(tam == 4) {hora += "00"; tam=6}
	if	(tam != 6){
		alert('A hora está incorreta');
		cHora.value = "";
		cHora.focus();
		return(false);
		}
	
	var hr = hora.substr(0,2)
	var min = hora.substr (2,2)
	var seg = hora.substr (4,2)	
	if ((hr > 23) || (min > 59) || (seg > 59)){
		alert('A hora está incorreta');
		cHora.value = "";
		cHora.focus();
		return(false);
		}
	return(true);
}

/////////////////////////////////////////////////////////////////
function busca_cartorio(cartorio, form){
  var_temp = "/sis/funcoes/pegacartorio.php?id="+cartorio.value+"&form="+form+"";
  window.open(var_temp, 'dwn_oculto', 'location=yes,toolbar=yes,status=yes,directories=yes,scrollbars=yes,top=2000,left=2000,width=1,height=1');
  return(true); 
}

/////////////////////////////////////////////////////////////////
function TestaCEP(cCEP, form, newcache){
  var sulfixo =  cCEP.name.replace("cep", "");
  if(top.memoriza==cCEP.value){return(true)}
  if (!cCEP.value){return(true)}
  var mensagem = "O CEP digitado está incorreto";
  var msgerro;
  var data; 
  cep = LimpaCampo(cCEP.value,10);
  var tam    = cep.length;
  var cidade = cep.substr(0,5)
  var rua    = cep.substr (5,3)

  if(tam != 8) {
    msgerro = mensagem;
  }

  if((cep/1) != cep){
    msgerro = mensagem;
  }

  if(cidade<1){
    msgerro = mensagem;
  }
  if (msgerro) {
    alert(msgerro); cCEP.value=""; cCEP.focus(); return(false);
  }

  var_temp = "/sis/funcoes/pegacep.php?cep="+cep+"&form="+form+"&sulfixo="+sulfixo;
  if(newcache) {var_temp = newcache+"/"+var_temp;}
  document.all(sulfixo+"proc_cep").innerHTML = '<font color=red> Aguarde...</font>'
  window.open(var_temp, 'sis_oculto', 'location=yes,toolbar=yes,status=yes,directories=yes,scrollbars=yes,top=2000,left=2000,width=1,height=1');
  return(true); 
}

/////////////////////////////////////////////////////////////////
function TestaData(cData){
  if (!cData.value){return(true)}
  var mensagem = "Data incorreta";
  var msgerro;
  var data; 
  data = LimpaCampo(cData.value,10);
  var tam = data.length;
  var dia = data.substr(0,2)
  var mes = data.substr (2,2)
  var ano = data.substr (4,4)	

  if(dia<1 || mes<1){
    msgerro = mensagem;
  }

  if(tam != 8 || ano < 1900 || mes >12 || dia >31){
    msgerro = mensagem;
  }

  if (mes==04 || mes ==06 ||mes==09 || mes==11) {
    if (dia > 30){
      msgerro = mensagem;
    }
  }

  if (mes==02) {
    if (dia > 29){
      msgerro = mensagem;
    }
  }

  if (mes==02 && (ano%4))  {
    if (dia > 28){
      msgerro = mensagem;
    }
  }

  if (msgerro) {
    //alert(msgerro); cData.value=""; cData.focus(); return(false);
	top.document.all('proc_dt_nascimento').innerHTML='<font color="red">'+msgerro+'</font>'
	cData.focus(); return(false);
   }
   top.document.all('proc_dt_nascimento').innerHTML=''
   return(true); 
}

/////////////////////////////////////////////////////////////////
function FormataData(Campo, teclapres){
  var tecla = teclapres.keyCode;
  var vr = new String(Campo.value);
  count = 0;
  while (vr.search("/") !=-1 && count < 25) {
    vr = vr.replace("/", "");
    vr = vr.replace(".", "");
    vr = vr.replace("-", "");
    count ++;
  }
  tam = vr.length + 1;
  if (tecla != 9 && tecla != 8){
    if (tam > 2 && tam < 5)
    Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, tam);
    if (tam >= 5 && tam <=10)
    Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);
	if(Campo.value.length == 10) {
      Campo.onchange();
    }
	if(Campo.onBlur) {alert();}
  }
}

/////////////////////////////////////////////////////////////////
function FormataCNPJ(Campo, teclapres){

  var tecla = teclapres.keyCode;
  var vr = new String(Campo.value);
  count = 0;
  while (vr.search('.') !=-1 && count < 25) {
    vr = vr.replace(".", "");
    vr = vr.replace("/", "");
    vr = vr.replace("-", "");
    count ++;
  }
  tam = vr.length + 1 ;
  if (tecla != 9 && tecla != 8){
    if (tam > 2 && tam < 6)
      Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
    if (tam >= 6 && tam < 9)
      Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
    if (tam >= 9 && tam < 13)
      Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
    if (tam >= 13 && tam < 16)
    Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
  }
}

/////////////////////////////////////////////////////////////////
function	CertidaoNegativa(){
	var	s;
	s = 'Resultado.asp?NI=' + document.CertidaoNegativa.NI.value;
	window.open(s, null, "height=460,width=680,resizable=yes,status=no,scrollbars=yes,toolbar=yes,menubar=no,location=no");
	}
/////////////////////////////////////////////////////////////////
var da = (document.all) ? 1 : 0;
var pr = (window.print) ? 1 : 0;
var mac = (navigator.userAgent.indexOf("Mac") != -1);
/////////////////////////////////////////////////////////////////
function ImprimeCertidao(){
	if (pr){
		var obj = parent.document.frames("main");
		obj.focus();
		obj.print();		 
		}
	else
		if (da && !mac)
			vbPrintPage();
		else{
			alert ("Seu browser não suporta esta função. Favor utilizar a barra de trabalho para imprimir a página.");
      return false;
      }	 	
	}

/////////////////////////////////////////////////////////////////
function FormataCPF(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1;
	
	if (tecla != 9 && tecla != 8){
		if (tam > 3 && tam < 7)
			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		if (tam >= 7 && tam <10)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
		if (tam >= 10 && tam < 12)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);
	}
}

/////////////////////////////////////////////////////////////////
function SaltaCampo(campo,prox,tammax,teclapres){

	var tecla = teclapres.keyCode;
	vr = document.forms[0].elements[campo].value;
		
	if( tecla == 109 || tecla == 188 || tecla == 110 || tecla == 111 || tecla == 223 || tecla == 108){
	   document.form[campo].value = vr.substr(0, vr.length - 1); }
	else {
		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 (tecla != 0 && tecla != 9 && tecla != 16 )
			if ( tam == tammax )
				document.forms[0].elements[prox].focus();
		}
}

//////////////////////////////////////////////////////////////////
function CalcularDV(sCampo, iPeso){
	
	var iTamCampo;
	var iPosicao, iDigito;
	var iSoma1 = 0;
	var iSoma2=0;
	var iDV1, iDV2;
		
	iTamCampo = sCampo.length;

	for (iPosicao=1; iPosicao<=iTamCampo; iPosicao++){
		iDigito = sCampo.substr(iPosicao-1, 1);
		iSoma1 = parseInt(iSoma1,10) + parseInt((iDigito * Calcular_Peso(iTamCampo - iPosicao, iPeso)),10);
		iSoma2 = parseInt(iSoma2,10) + parseInt((iDigito * Calcular_Peso(iTamCampo - iPosicao + 1, iPeso)),10);
		}

	iDV1 = 11 - (iSoma1 % 11);
	if (iDV1 > 9)
		iDV1 = 0;

	iSoma2 = iSoma2 + (iDV1 * 2);
	iDV2 = 11 - (iSoma2 % 11);
	if (iDV2 > 9)
		iDV2 = 0;

	Ret = (parseInt(iDV1 * 10,10) + parseInt(iDV2));

	Ret = "0" + Ret;
	Ret = Ret.substr(Ret.length - 2,Ret.length);
		
	return(Ret);
}

//////////////////////////////////////////////////////////////////		
function Calcular_Peso(iPosicao, iPeso){

	//Pesos
	//CPF 11
	//CNPJ 9
	return (iPosicao % (iPeso - 1)) + 2;
	}
	
/////////////////////////////////////////////////////////////////
function LimpaCampo(sValor,iBase){
	var tam = sValor.length
	var saida = new String
	for (i=0;i<tam;i++)
		if (!isNaN(parseInt(sValor.substr(i,1),iBase)))
			saida = saida + String(sValor.substr(i,1));
	return (saida);		
	}
/////////////////////////////////////////////////////////////////
function TestaForm(theForm,iTipo){
	if (!(TestaNI (theForm.NI,iTipo)))
		return (false);
	
	if (!(TestaControle (theForm.Controle)))  
		return (false);

	var controle = LimpaCampo(theForm.Controle.value,16);
	var tam = controle.length;

	if	(tam == 16){ 
		if (!(TestaData(theForm.Data)))
			return (false);
	
		if (!(TestaHora(theForm.Hora)))
			return (false);		
		}		
	else{
		if (theForm.Data.value != "")	{
			alert('Só preencher data da emissão para certidão emitida pela Internet');
			theForm.Data.value = "";
			theForm.Data.focus();
			return(false);
		}
		if (theForm.Hora.value != "")	{	
			alert('Só preencher hora da emissão para certidão emitida pela Internet');
			theForm.Hora.value = "";
			theForm.Hora.focus();
			return(false);
		}
	}		
	return (true);
}
/////////////////////////////////////////////////////////////////
function TestaFormItr(theForm,iTipo){
	if (!(TestaNI (theForm.NI,iTipo)))
		return (false);
	
	if (!(TestaControle (theForm.Controle)))  
		return (false);
	
	return (true);
	}

/////////////////////////////////////////////////////////////////
function TestaNI(cNI,iTipo){
  if (!cNI.value){return(true)}
  var msgerro;
  var NI
  NI = LimpaCampo(cNI.value,10);
  switch (iTipo) {
    case 1:
      var mensagem = "O número do CNPJ está incorreto";
      if (NI.length != 14){
        msgerro = mensagem;
      }
      if (NI.substr(12,2) != CalcularDV(NI.substr(0,12), 9)){
	    //alert(CalcularDV(NI.substr(0,12), 9));
        msgerro = mensagem;
      }
    break;
    case 2:
      var mensagem = "O número do CPF está incorreto";
      if (NI.length != 11){
        msgerro = mensagem;
      }
      if (NI.substr(9,2) != CalcularDV(NI.substr(0,9), 11)){
        msgerro = mensagem;
      }
    break;
    case 3:
      if (NI.length != 8){
        msgerro = mensagem;
      }
      var dv = new String(); 
      dv = CalcularDV(NI.substr(0,7), 9);
      dv = dv.substr(0,1);
      if (NI.substr(7,1) != dv){
        msgerro = mensagem;
      }
    break;
    default:
  }
  //if (msgerro) {alert(msgerro); cNI.value=""; cNI.focus();return(false);}
  if (msgerro) {
  //alert(msgerro);
  top.document.all('proc_'+cNI.name).innerHTML=msgerro; setTimeout("document.all('proc_'+cNI.name).innerHTML = ''", 3000)
  cNI.focus(); return(false);}
  top.document.all('proc_'+cNI.name).innerHTML="";
  return (true);	
}  

/////////////////////////////////////////////////////////////////
function TestaControle(cControle){
	var controle; 
	controle = LimpaCampo(cControle.value,16);
	var tam = controle.length;
	if	(tam != 7 && tam != 16)	{
		alert('O número da Certidão/Controle informado está incorreto');
		cControle.value = "";
		cControle.focus();
		return(false);
		}
	return(true); 
	}

///////////////////////////////////////////////////////////////////
function VerAlfaNumerico(pInd){
	var pValor = document.forms[0].elements[pInd].value
	var AuxTam = pValor.length  
	for(var j=0;j<AuxTam;j++)
		if ((!IndAlfaNumerico(pValor.charAt(j))) || (pValor.charAt(j) == " ")){
			document.forms[0].elements[pInd].focus();  
			document.forms[0].elements[pInd].value = pValor = pValor.substring(0,j)           
			} 
	}
////////////////////////////////////////////////////////////////////
function IndAlfaNumerico(N){
	for(var i=0;i<10;i++)
	if(N == i)
		return true;
	return false;    
	}
////////////////////////////////////////////////////////////////////

function e_numero(evt){
    nTecla = evt.keyCode;
    numerico = ( (nTecla >= 46) && (nTecla <= 57)  );
	numerico += ( (nTecla >= 96) && (nTecla <= 105)  );
    numerico +=( (nTecla==8) );
	numerico +=( (nTecla==9) );   // Tab
	numerico +=( (nTecla==13) );  // Enter
	numerico +=( (nTecla==16) );  // Shift
	numerico +=( (nTecla==17) );  // Control	
    numerico +=( (nTecla==27) );  // Esc
	numerico +=( (nTecla==37) );  // <-
	numerico +=( (nTecla==39) );  // ->
	numerico +=( (nTecla==82) );  // Ctrl + R	
	numerico +=( (nTecla==144) ); // Num Lock
	numerico +=( (nTecla==188) ); // ,
	numerico +=( (nTecla==189) ); // -
	numerico +=( (nTecla==190) ); // .
    if(numerico) {return true;}
	//alert(nTecla);
	return false;    
	}
////////////////////////////////////////////////////////////////////