// Desabilita botão
function clickDisable(obj) {
  obj.disabled = true;
}

function linkDisable(obj) {
  $(obj).removeAttr('onClick')
}

// Abre um alerta e retorna falso
function abreAlerta(msg) {
  alert(msg);
  return false;
}

function colocaFoco(obj) {
  if (obj) {
    //obj.style.borderColor     = '#cc0000';
    obj.style.backgroundColor = '#FFF0f0';
    obj.focus();
  }
}

function retiraFoco(obj) {
  if (obj) {
    //obj.style.borderColor     = '#666666';
    obj.style.backgroundColor = '#ffffff';
  }
}

// Verifica a existência de caracteres especiais
function validaEspeciais(obj) {
  var caracter = obj.value
  var resp = false;
  for(c = 0; c < caracter.length; c++) {
    n = (caracter.substring(c,c+1)).charCodeAt();
    if(!(n > 47 && n < 58 || n > 96 && n <123) && !(n==46 || n==95 || n==45)) resp = true;
  }
  return resp;
}

// Verifica Caracteres
function validaTexto(obj){
  var caracter = obj.value;
  if (caracter.length == 0){
    return true;
  }
  return false;
}

// Verifica Radio buttom e Check box
function validaOpcao(obj) {
  var resp = true
  for(var ii=0; ii < obj.length; ii++){
    if(obj[ii].checked) resp = false;
  }
  if(resp){
    return true;
  }else{
    return false;
  }
}

// Verifica Objetos SELECT do formulário
function validaSelecao(obj){
  if(obj.options[obj.selectedIndex].value == ""){
    return true;
  }
  return false;
}

// Verifica data no formato brasil (dd/mm/aaaa)
function validaData(obj){
  var numero  = obj.value;
  var partes  = numero.split("/");
  
  if (partes.length==3){
    numeros = partes[0] + partes[1] + partes[2]
    if (numeros.length ==0 ){
      return true;
    }
    if (partes[0] < 1 || partes[0] > 31){
      return true;
    }
    if (partes[1] < 1 || partes[1] > 12){
      return true;
    }
    if (partes[2].length < 4){
      return true;
    }
    if (partes[2] < 1900){
      return true;
    }
  }else{
    return true;
  }
  return false;
}

// Verifica email's quanto ao forma e carácteres válidos
function validaEmail(obj) {
  
  var invalid = Array("~","!","@","#","$","%","^","&","*","(",")","+","=","[","]",":",";",",","\"","'","|","{","}","\\","/","<",">","?"," ","ç","á","à","ã","â","é","è","ê","í","ì","î","ó","ò","ô","õ","ú","ù","û");
  
  var xemail = obj.value;

    
  if (xemail.indexOf("@")==-1){
    return true;
  } else {
    var partes=xemail.split("@");
    if(partes[0] == '' || partes[0].length < 3){
      return true;
    }else{
      for(var ii=0; ii < invalid.length; ii++){
        if(partes[0].indexOf(invalid[ii])!=-1){
          return true;
        }
      }
    }
    
    if(partes[1]==""){
      return true;
    }else{
      if (partes[1].indexOf(".")==-1){
        return true;
      }else{
        ponto=partes[1].split(".")
        if(ponto[0]=="" || ponto[0].length < 2){
          return true;
        }else{
          for(ii=0;ii < invalid.length; ii++){
            if(ponto[0].indexOf(invalid[ii])!=-1){
              return true;
            }
          }
        }
        if(ponto[1]==""){
          return true;
        }
      }
    }
  }
  return false;
}

function validaValor(obj){
  var valorx  = obj.value;
  var virgula = valorx.split(",");
  
  if (virgula.length >=2){
    if (virgula[1].length ==2 && virgula[0].length !=0){
      var ponto = virgula[0].split(".");
      if (ponto.length == 2){
        for(var i=1; i< ponto.length; i++){
          if(ponto[i].length != 3){
            return true;
          }
        }
      }
    }else{
      return true;
    }
  }else{
    return true;
  }
  return false;
}

// Valida CEP no formato 00000-000
function validaCep(obj){
  var validos = Array("0","1","2","3","4","5","6","7","8","9");
  var valor   = obj.value;
  var partes  = valor.split("-");
  var resp    = 0;
  
  if (partes.length ==2){
    var numeros = partes[0] + partes[1];
    if (numeros.length ==0 ){
      return true;
    }
    for(var ii=0; ii < validos.length; ii++){
      for(jj=0; jj < numeros.length; jj++){
        if(numeros.substring(jj,jj+1) == validos[ii]) { resp++ }
      }
    }
    if (numeros.length != resp){
      return true;
    }
    if (partes[0].length != 5){
      return true;
    }
    if (partes[1].length != 3){
      return true;
    }
  }else{
    return true;
  }
  return false;
}


// Permite somente números inteiros
function validaNumero(obj){
  var numero = obj.value;
  var resp   = 0;
  
  if(numero.length==0){
    return true;
  }
  for(var ii=0; ii < numero.length; ii++){
    for(jj=0; jj < 11; jj++){
      if (numero.substring(ii,ii+1) == jj) resp++;
    }
  }
  if (numero.length != resp){
    return true;
  }
  return false;
}


// Valida campo CPF
function validaCpf(obj) {
  
  if (isCpf(obj.value))
    return false;
  else
    return true;
  
}

// Valida campo CNPJ
function validaCnpj(obj) {
  
  //unformatNumber();
  if (isCnpj(obj.value))
    return false;
  else
    return true;

}

function validaIMG(obj) {

  var nome = (obj.value).split("\\");
  
  if (nome != '') {
    
    var valido = 0;
    for (var i= 0; i < nome[nome.length-1].length; i++) {
      if (nome[nome.length-1].substring(i,i+1)==" ")
        valido = 1;
    }
    
    if (valido==1) {
      alert("Não são permitidos espaços no nome da imagem.");
      return true;
    }
    
    //var tam = nome.length;
    var ext = (nome).split(".");
    
    alert(ext)
    var ext = (nome.substr(tam-4,4)).toUpperCase();
    if(ext != ".GIF" && ext != ".JPG"){
      alert("Para a imagem é somente aceitos os formatos .JPG e .GIF");
      return true;
    }
  }
  
  return false;
  
}

function verificaTamanho(target, contador, total) {
  var StrLen;
  var adicional;   
  StrLen = 0;
  
  if (document.getElementById(target).value.length != "" ){
    StrLen = StrLen + document.getElementById(target).value.length;
  }
  if (StrLen == 1 && document.getElementById(target).value.substring(0,1) == " "){ 
    document.getElementById(target).value = "";
    StrLen = StrLen - 1;
  }
  if (StrLen > total){
    document.getElementById(target).value = document.getElementById(target).value.substring(0,total);
    StrLen = StrLen - 1;
  }
 document.getElementById(contador).value = total - StrLen;
}