
/**************************************************************
 Funcao que mostra ou esconde uma layer
 Parametros : player --> Nome da DIV
              pAcao  --> block ou none
*************************************************************/                

function ShowHideLayer(layer,acao){
  obj=document.getElementById(layer);
  obj.style.visibility = acao;
}

/**************************************************************
 Funcao que marca ou desmarca todos os Checkbox do Formulario
 Parametros : pformulario --> Nome do Formulario 
              pmarca      --> Se true ele marca todos 
                        Se false ele desmarca todos
*************************************************************/                
function marcaCheckbox( pformulario, pmarca )
{
   for (var i = 0; i < pformulario.elements.length ; i++) 
   {
     if ( pformulario.elements[i].type == "checkbox" ) 
        pformulario.elements[i].checked = pmarca;
  }   
}

/**************************************************************
 Funcao que Verifica se um campo eh vazio
 Parametros : field --> Nome do campo
 msg   --> mensagems que serexibida se houver erro

*************************************************************/                

function isVazio(field,msg){
   var thisChar;
   var counter = 0;
   var str;
   
    str = field.value
    if ( str == "" ) {
            if (typeof(msg) != "undefined"){ 
       alert(msg)
       field.focus()
    }   
    return true;
    }
    
    for (var i=0; i<str.length; i++){
       thisChar = str.substring(i, i+1);
       if (thisChar == " ")
           counter++;
   }
   
   if (counter == str.length){
       if (typeof(msg) != "undefined"){
      alert(msg);
          field.focus();    
   }   
      return true;
   }
   
    return false;  
}

/**************************************************************
 Funcao que Verifica se um campo nao eh vazio
 Parametros : field --> Nome do campo
 
*************************************************************/                
function notVazio(field){
   var thisChar;
   var counter = 0;
   var str;
    str = field.value;
  
    if ( str == '') {
    return false;
    }
  
  //conta espaco vazio (nao considera como conteudo)
    for (var i=0; i<str.length; i++){
       thisChar = str.substring(i, i+1);
       if (thisChar == " ")
           counter++;
   }
   
   if (counter == str.length){
      return(false);
   }
   
    return true;  
}

/**************************************************************
 Funcao que Verifica  se ou naum um email valido
 Parametros : str --> Email

*************************************************************/                
function isEmail(str) {
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

/**************************************************************
 Funcao que testa uma mascara passada.
 Parametros : codigo --> o objeto que se quer testar.
         mascara --> mascara usando a barra como separador.
                                       utiliza formato 99/99/999 para valores numericos
                  e aaa/aa/aaaa/aa para valores alpha

*************************************************************/                

function mascara(codigo,mascara){
  str = mascara;
  fim = str.length;
  valor = codigo.value;
  posicao = valor.length -1;
  tipo = str.charAt(posicao);
 
  
  if ((tipo == "a") || (tipo == "A")){
     if ((valor.charCodeAt(posicao) >= 48) && (valor.charCodeAt(posicao) <= 57) || // 0 9
    (valor.charCodeAt(posicao) >= 65) && (valor.charCodeAt(posicao) <= 90) || // A Z
    (valor.charCodeAt(posicao) >= 97) && (valor.charCodeAt(posicao) <= 122)) { // a z
    tipo = str.charAt(posicao+1);
             cont = posicao + 1;
    while ((tipo!="a") && (tipo!="A") && (tipo!="9") && (tipo!="") ) {
      codigo.value=codigo.value+tipo;
                    cont = cont + 1;
            tipo = str.charAt(cont);
    }
   } else { 
    codigo.value = codigo.value.substr(0,posicao);
   }
  } else {
  if (tipo == "9") {
    if ((valor.charCodeAt(posicao) >= 48) && (valor.charCodeAt(posicao) <= 57)) {
      tipo = str.charAt(posicao+1);
            cont = posicao + 1;
      while ((tipo!="a") && (tipo!="A") && (tipo!="9") && (tipo!="")) {
        codigo.value=codigo.value+tipo;
                           cont = cont + 1;
                      tipo = str.charAt(cont);
      }
    } else { 
      codigo.value = codigo.value.substr(0,posicao);
    }
  } else {
          tipo = str.charAt(posicao+1);
            cont = posicao + 1;
      while ((tipo!="a") && (tipo!="A") && (tipo!="9") && (tipo!="")) {
        codigo.value=codigo.value+tipo;
                           cont = cont + 1;
                      tipo = str.charAt(cont);
      }
  }
  }
  codigo.value = codigo.value.substr(0,fim - 1) ;
  return true;
}

/**************************************************************
 Funcao que verifica se uma data eh vida no formato dd/mm/aaa
 Parametros : objdata --> objeto do form de data (campo de data)

*************************************************************/                

function isDate(objdata){
  if ((objdata.value.length < 10) || (objdata.value.length == "")) {
    alert("Formato inválido. Formato indicado: dd/mm/aaaa. Ex: 01/01/2002");
    objdata.focus();
    return false;
  }
  dia = objdata.value.substr(0,2);
  mes = objdata.value.substr(3,2);
  ano = objdata.value.substr(6,4);
    if   ((isNaN(parseInt(dia)) || isNaN(parseInt(mes)) ||
    isNaN(parseInt(ano))) ||
    (ano < 1900 || ano > 9999) ||
        (mes < 1 || mes > 12) ||
        (dia < 1 || dia > 31) ||
        (mes == 2 && dia > 28 && (ano % 4 != 0)) ||
        (mes == 2 && dia > 29 && (ano % 4 == 0)) ||
        (dia > 30 && (mes == 4 || mes == 6 || mes == 9 || mes == 11))) {
      alert("Data inválida! Informe uma data váida.");
      objdata.focus();
      return false;
    }else {
            return true;
    }
}
/**************************************************************
 Funcao que valida a exclusao de um registro
 Parametros : frm - nome do formulario

*************************************************************/                
function valida_excluir(frm)
{
   if(confirm("Tem certeza que deseja excluir?"))
    return true;
   else
    return false;
}

/**************************************************************
 Funcao que transforma um numero em decimal para javascrip.
 Parametros : numero - objeto com numero

*************************************************************/    

function  converte_decimal(numero){

  decimal = numero.value;
  
  decimal = decimal.replace(/\./g,"");
  decimal = decimal.replace(/,/g,".");
    
  return decimal;

}
/**************************************************************
 Funcao que valida CPF e CGC com mcara 
 
 Parametros : frm - nome do formulario
        acao - acao (INCLUIR/EXCLUIR/CONSULTAR/etc..)

*************************************************************/                

function valida_CpfCnpj_mascara(tx_cpfCnpj,msg){

  if(notVazio(tx_cpfCnpj)){
    var cpfCnpj 
    
    cpfCnpj = tx_cpfCnpj.value
    
    cpfCnpj = cpfCnpj.replace(/\./g,"");
    cpfCnpj = cpfCnpj.replace("/","");
    cpfCnpj = cpfCnpj.replace(/-/g,"");
    
    alert(cpfCnpj);
        
    if (!valida_cpf(cpfCnpj, msg)){
      return false;
    }
  }
  return true;
}





/**************************************************************
 Funcao que valida CPF e CGC
 
 Parametros : frm - nome do formulario
        acao - acao (INCLUIR/EXCLUIR/CONSULTAR/etc..)

*************************************************************/                
// funcao de validacao de cgc & cpf
var cpf_;
cpf_ = new Array(10);
cpf_[0]="11111111111";
cpf_[1]="22222222222";
cpf_[2]="33333333333";
cpf_[3]="44444444444";
cpf_[4]="55555555555";
cpf_[5]="66666666666";
cpf_[6]="77777777777";
cpf_[7]="88888888888";
cpf_[8]="99999999999";
cpf_[9]="00000000000";

//********* valida cpf
function valida_cpf(msCPF_CGC, msMSG){
  if ((msCPF_CGC.length != 14) && (msCPF_CGC.length !=11)){
    alert(msMSG);
    return false;
  }

  if ((!(modulo(msCPF_CGC.substring(0,msCPF_CGC.length - 2)).toString()+modulo(msCPF_CGC.substring(0,msCPF_CGC.length - 1)).toString() == msCPF_CGC.substring(msCPF_CGC.length - 2,msCPF_CGC.length))) && (modulo_cic(msCPF_CGC.substring(0,msCPF_CGC.length - 2)) + "" + modulo_cic(msCPF_CGC.substring(0,msCPF_CGC.length - 1)) != msCPF_CGC.substring(msCPF_CGC.length - 2,msCPF_CGC.length))){
    alert(msMSG);
    return false;
  }
  return true;
}

function modulo(msCPF_CGC){
  soma=0;
  ind=2;

  for(pos=msCPF_CGC.length-1;pos>-1;pos=pos-1){
    soma = soma + (parseInt(msCPF_CGC.charAt(pos)) * ind);
    ind++;

    if(msCPF_CGC.length>11){
      if(ind>9) ind=2;
        }
  }

  resto = soma - (Math.floor(soma / 11) * 11);

  if(resto < 2){
    return 0;
  }
  else{
    return (11 - resto);
  }
}
function modulo_cic(msCPF_CGC){
     soma=0;
  ind=2;

  for(pos=msCPF_CGC.length-1;pos>-1;pos=pos-1){
     soma = soma + (parseInt(msCPF_CGC.charAt(pos)) * ind);
     ind++;

     if(msCPF_CGC.length>11){
        if(ind>9) ind=2;
     }
  }

  resto = soma - (Math.floor(soma / 11) * 11);

  if(resto < 2){
    return 0;
  }
  else{
    return 11 - resto;
  }
}


function validacaoParam(tx_cpf)
{
  if (tx_cpf.value==""){
    alert("Preencha o campo 'CPF'.");
    return false;
  }
  else if (tx_cpf.value.length<11)
  {
    if(isNaN(tx_cpf.value))
    {
      alert("CPF deverá conter apenas nmeros.");
      return false;
    }
    var tot = 11-tx_cpf.value.length;
    var comp1="";
    for (i=1;i<=tot;i++)
    {
      comp1+="0";
    }
    comp1+=comp;
    if(!checa(comp1, "CPF Inválido."))
    {
      //tx_cpf.value="";
      return false;
    }
    //document.form.submit();
  }else if (tx_cpf.value.length>=11)
  {
    if(isNaN(tx_cpf.value))
    {
      alert("CPF deverá conter apenas nmeros.");
      return false;
    }
    var comp = tx_cpf.value;
  var sString = "-/.";
    for(i=0;i<sString.length;i++)
    {
      if (comp.indexOf(sString.substring(i))!=-1)
      {
        alert("fez replace");
        comp = comp.replace(indexOf(i),1);
      }
    }
    alert("comp  "+comp);
    for (var i=0;i<=cpf_.length;i++)
    {
      if (comp.indexOf(cpf_[i])!=-1){
        alert("CPF Inválido.");
        return false;
        }
    }

    if(!checa(tx_cpf.value, "CPF Inválido."))
    {
      //tx_cpf.value="";
      return false;
    }
  }
}

/**************************************************************
 Funcao que Insere um item num combo aberto
 Parametros : 
*************************************************************/                

function insereItemCombo( origem,  destino,msg )
  {
      if (origem.selectedIndex < 0 ){
      return false;
    }
    
    existe = false;
      for ( lnx = 0 ;  lnx < destino.length && !existe ; lnx++ )
    {
        if ( destino.options[ lnx ].value == origem.value ) 
      {
         existe = true;
              alert( msg );
         return false;
       
      }
    }
    
  
    destino.length =  destino.length + 1;
    destino.options[ destino.length -1 ].value = origem.value;
    destino.options[ destino.length -1 ].text = origem.options[ origem.selectedIndex ].text
  }
/**************************************************************
 Funcao que remove item do Combo aberto
 Parametros : 
*************************************************************/                
  
  
  function removeItemCombo( campo ) 
  {
    
      if ( campo.selectedIndex >= 0 )
         campo.options[ campo.selectedIndex ] = null;
          
  }
  
 
 function gera_lista( campolista, idcampolista)
  {
    // limpa valor
    idcampolista.value = '';
  // percorre combo de associados

  for ( x = 0; x<=campolista.length-1; x++ )
     {   
    // concatena itens do combo de associados, separando-os por virgula
    if ( x == 0 )  
      idcampolista.value = campolista.options[x].value;
    else
      idcampolista.value = idcampolista.value + '|' + campolista.options[x].value;
  }

  return true;
  }
  
   /**************************************************************
 Funcao que Verifica se um valor eh válido no formato 1.200,00
 Parametros : field --> Nome do campo

*************************************************************/                
function FormataValor(Campo) {
  var tecla = Campo.value.keyCode;
  vr = Campo.value;
  vr = vr.replace( ",", "" );
  tam = vr.length + 1;
  if ( tecla != 2 ){
    if ( tam > 2 )
      Campo.value = vr.substr( 0, tam - 2  ) + ',' + vr.substr( tam - 2, tam );
  }
}


/**************************************************************
 Funcao que Verifica se um campo eh inteiro
 Parametros : field --> Nome do campo
                     msg   --> mensagems que será exibida se houver erro

*************************************************************/                
function isInteiro(objNum,msg) {
   data = objNum.value;   
   var numStr = "0123456789";
   var thisChar;
   var counter = 0;
   for (var i=0; i<data.length; i++){
       thisChar = data.substring(i, i+1);
       if (numStr.indexOf(thisChar) != -1)
           counter++;
   }
   if (counter == data.length){
      return(true);
   }else{
    alert(msg);
    objNum.focus();    
      return false;
   }
  return true;
}

/**************************************************************
 Funcao que transforma um numero em decimal para javascrip.
 Parametros : numero - objeto com numero

*************************************************************/    

function  converte_decimal(numero){

  decimal = numero.value;
  
  decimal = decimal.replace(/\./g,"");
  decimal = decimal.replace(/,/g,".");
    
  return decimal;

}
  
  
  


