function trim(str){
  s = str.replace(/^(\s)*/, '');
  s = s.replace(/(\s)*$/, '');
  return s;
}
function replace(string,text,by){
  var str = new String;
  str = string;
  str.replace(text,by);
  return str;
}
function isEmpty(_input,_label){
  var errMsg = "";
  if (trim(_input.value)==""){
    errMsg = "- O campo "+_label+" está vazio.\n";
  }
  return errMsg;
}
function isChecked(_input,_label){
  var errMsg = "";
  var checked=0;
  for(i=0; i<_input.length; i++){
    if (_input[i].checked==true){
      checked=1;
    }
  }
  if (checked==0){
    errMsg = "- Selecione uma opção em "+_label+".\n";
  }
  return errMsg;
}
function isDate(_input,_label){
  var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
  var returnval=false
  if (!validformat.test(_input.value)) {
    returnval=0;
  } else {
    var dayfield=_input.value.split("/")[0]
    var monthfield=_input.value.split("/")[1]
    var yearfield=_input.value.split("/")[2]
    var dayobj = new Date(yearfield, monthfield-1, dayfield)
    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield)){
      returnval=0;
    } else {
      returnval=1;
    }
  }
  if (returnval==0){
    return "- "+_label+" inválida.\n";
  } else {
    return "";
  }
}
function isCPF(_input){
  var errMsg="";
  var errNro=0;
  
  dig_1 = 0;
  dig_2 = 0;
  controle_1 = 10;
  controle_2 = 11;
  lsucesso = 1;
  numero = replace(_input.value,'.','');
  if (numero == "000000000-00" || numero == "111111111-11" || numero == "222222222-22" || numero == "333333333-33" || numero == "444444444-44" || numero == "555555555-55" || numero == "666666666-66" || numero == "777777777-77" || numero == "888888888-88" || numero == "99999999-99"){
    errNro=1;
  }    
  if ((numero.length != 12) || (numero.substring(9, 10) != "-")){
    errNro=1;
  } else {
    for (i=0 ; i < 9 ; i++){
      dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);
      controle_1 = controle_1 - 1;
    }
    resto = dig_1 % 11;
    dig_1 = 11 - resto;
    if ((resto == 0) || (resto == 1))
      dig_1 = 0;
    for ( i=0 ; i < 9 ; i++){
      dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);
      controle_2 = controle_2 - 1;
    }
    dig_2 = dig_2 + 2 * dig_1;
    resto = dig_2 % 11;
    dig_2 = 11 - resto;
    if ((resto == 0) || (resto == 1))
      dig_2 = 0;
      dig_ver = (dig_1 * 10) + dig_2;
      if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length)))
      {
        errNro=1;
      }
  }
  if (errNro==1){
    errMsg = "- Você não digitou um CPF válido.\n";
  }
  return errMsg;
}
function isCNPJ(_input){
  var errNro=0;
  var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais, cnpj = _input.value.replace(/\D+/g, '');
  digitos_iguais = 1;
  if (cnpj.length != 14) errNro=1;
  for (i = 0; i < cnpj.length - 1; i++)
           if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                 {
                 digitos_iguais = 0;
                 break;
                 }
     if (!digitos_iguais)
           {
           tamanho = cnpj.length - 2
           numeros = cnpj.substring(0,tamanho);
           digitos = cnpj.substring(tamanho);
           soma = 0;
           pos = tamanho - 7;
           for (i = tamanho; i >= 1; i--)
                 {
                 soma += numeros.charAt(tamanho - i) * pos--;
                 if (pos < 2)
                       pos = 9;
                 }
           resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
           if (resultado != digitos.charAt(0)){
                 errNro=1;
                 }

           tamanho = tamanho + 1;
           numeros = cnpj.substring(0,tamanho);
           soma = 0;
           pos = tamanho - 7;
           for (i = tamanho; i >= 1; i--)
                 {
                 soma += numeros.charAt(tamanho - i) * pos--;
                 if (pos < 2)
                       pos = 9;
                 }
           resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
           if (resultado != digitos.charAt(1)){
                 errNro=1;
                 }
            }
     else{
           errNro=1;
           }
  if (errNro==1){
    return "- Você não informou um CNPJ válido.\n";
  } else {
    return "";
  }
}
function isEmail(_input,_label){
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (!filter.test(_input.value)) {
    return "- "+_label+" não é um e-mail válido.\n";
  } else {
    return "";
  }
}