/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function trim(str){
    return str.replace(/^\s+|\s+$/g,"");
}

function isEmail(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{
            return false;
        }
    }
}

// cria data a partir de uma string (dd/mm/aaaa)
function toDate(data, formato){
    if(formato == "dd/MM/yyyy"){
        dia = parseInt(data.substr(0, 2), 10);
        mes = parseInt(data.substr(3, 2), 10);
        ano = parseInt(data.substr(6, 4), 10);
    }else{
        dia = parseInt(data.substr(3, 2), 10);
        mes = parseInt(data.substr(0, 2), 10);
        ano = parseInt(data.substr(6, 4), 10);
    }
    return new Date(ano, mes - 1, dia);
}

// verifica se uma string é uma data (dd/mm/aaaa)
function isDate(data, formato){
  if(data.length == 10)
  {
    if(formato == "dd/MM/yyyy"){
        dia = parseInt(data.substr(0, 2), 10);
        mes = parseInt(data.substr(3, 2), 10);
        ano = parseInt(data.substr(6, 4), 10);
    }else{
        dia = parseInt(data.substr(3, 2), 10);
        mes = parseInt(data.substr(0, 2), 10);
        ano = parseInt(data.substr(6, 4), 10);
    }
    if(dia > 0 && dia < 32 && mes > 0 && mes < 13 && ano > 999 && ano < 10000)
    {
      if(mes == 2 && dia > 28)
      {
        if(ano % 4)
          return false;
        else if(dia > 29)
          return false;
      }
      switch(mes)
      {
        case 4:
        case 6:
        case 9:
        case 11:
          if(dia > 30)
            return false;
          break;
      }
    }
    else
      return false;
  }
  else
    return false;
  return true;
}

// Diferença entre dias de duas datas
function dias(data1, data2, formato){
    var dataDiff = toDate(data2, formato) - toDate(data1, formato);
    dataDiff = (((dataDiff / 1000) / 60)/60)/24;
    return dataDiff;
}

function replaceAll(string, caracterOriginal, caracterSubstituto){
    return string.replace(new RegExp(caracterOriginal, 'g'),caracterSubstituto);
}

function isCPF(cpf) {
    erro = new String;
    cpf = cpf.replace(".", "");
    cpf = cpf.replace(".", "");
    cpf = cpf.replace("-", "");
    if (cpf.length < 11) erro += "Sâo necessários 11 digitos para verificação do CPF! \n\n";
    var nonNumbers = /\D/;
    if (nonNumbers.test(cpf)) erro += "A verificação de CPF suporta apenas números! \n\n";
    if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
        erro += "Número de CPF inválido!"
    }
    var a = [];
    var b = new Number;
    var c = 11;
    for (i=0; i<11; i++){
        a[i] = cpf.charAt(i);
        if (i < 9) b += (a[i] * --c);
    }
    if ((x = b % 11) < 2) {a[9] = 0} else {a[9] = 11-x}
    b = 0;
    c = 11;
    for (y=0; y<10; y++) b += (a[y] * c--);
        if ((x = b % 11) < 2) {a[10] = 0;} else {a[10] = 11-x;}
        if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
        erro +="Digito verificador do CPF com problema!";
    }
    if (erro.length > 0){
        jAlert(erro,"CPEorto");
        return false;
    }
    return true;
}

function semCaractereEspecial(e){
    var estado=false;
    var codigoTecla, cadenaTecla;
    var correctos = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","x","z","y","w","_","-","1","2","3","4","5","6","7","8","9","0");

    //estado=false;

    if(document.all){
        codigoTecla = event.keyCode
        cadenaTecla = (String.fromCharCode(event.keyCode));
    }else if(document.layers){
        codigoTecla = e.which
        cadenaTecla = String.fromCharCode(e.which);
    }else if(document.getElementById){
        codigoTecla = (window.Event) ? e.which : e.keyCode;
        cadenaTecla=(String.fromCharCode(codigoTecla));
    }
    
    // Procura pelas backspace (8) e teclas delete, left e right (0)
    if(codigoTecla == 8 || codigoTecla == 0){
        estado=true;
    }else{
        for(i=0;i<correctos.length;i++){
            // Verifica se a tecla digitada faz parte dos caracteres válidos
            if(cadenaTecla==correctos[i])estado=true;
        }
    }   

    if(estado==false){
        if(document.all){
            event.returnValue = false;
        }else{
            return false;
        }
    }
}

