function valida_email(email){
  var arroba = email.indexOf("@");
  var ponto = email.lastIndexOf(".");
  var espaco = email.indexOf(" ");
  return ((arroba != -1) && (arroba != 0) && (ponto != -1) && (ponto > arroba + 1) && (ponto < email.length - 1) && (espaco == -1))  
}


function conserta_texto(texto){
  novo_texto = texto;
  
  contagem_maiusculas = 0;
  somente_letras = novo_texto.replace(/[^a-zA-Z]/g, "");
  for(i=0; i<somente_letras.length; i++)
    if(somente_letras.charCodeAt(i) < 97)
      contagem_maiusculas++;
  
  if((contagem_maiusculas/somente_letras.length) >= 0.66)
    novo_texto = novo_texto.toLowerCase();
  
  novo_texto = novo_texto.replace(/[.]/g, ". ");
  novo_texto = novo_texto.replace(/[ ]*[.]/g, ".");
  novo_texto = novo_texto.replace(/[,]/g, ", ");
  novo_texto = novo_texto.replace(/[ ]*[,]/g, ",");
  novo_texto = novo_texto.replace(/[;]/g, "; ");
  novo_texto = novo_texto.replace(/[ ]*[;]/g, ";");
  novo_texto = novo_texto.replace(/[:]/g, ": ");
  novo_texto = novo_texto.replace(/[ ]*[:]/g, ":");
  novo_texto = novo_texto.replace(/[!]/g, "! ");
  novo_texto = novo_texto.replace(/[ ]*[!]/g, "!");
  novo_texto = novo_texto.replace(/[?]/g, "? ");
  novo_texto = novo_texto.replace(/[ ]*[?]/g, "?");
  
  novo_texto = novo_texto.replace(/([ ]+)/g, " ");
  novo_texto = novo_texto.replace(/^([ ]+)/, "");
  novo_texto = novo_texto.replace(/([ ]+)$/, "");
  
  while(novo_texto.indexOf(String.fromCharCode(13, 10, 13, 10, 13, 10)) != -1)
    novo_texto = novo_texto.replace(String.fromCharCode(13, 10, 13, 10, 13, 10), String.fromCharCode(13, 10, 13, 10));
    
  while(novo_texto.indexOf(String.fromCharCode(10, 10, 10)) != -1)
    novo_texto = novo_texto.replace(String.fromCharCode(10, 10, 10), String.fromCharCode(10, 10));
    
  while(novo_texto.indexOf(String.fromCharCode(32, 13, 10)) != -1)
    novo_texto = novo_texto.replace(String.fromCharCode(32, 13, 10), String.fromCharCode(13, 10));
    
  while(novo_texto.indexOf(String.fromCharCode(32, 10)) != -1)
    novo_texto = novo_texto.replace(String.fromCharCode(32, 10), String.fromCharCode(10));
    
  while(novo_texto.indexOf(String.fromCharCode(10, 32)) != -1)
    novo_texto = novo_texto.replace(String.fromCharCode(10, 32), String.fromCharCode(10));
  
  while((novo_texto.charCodeAt(novo_texto.length-1) == 10)||(novo_texto.charCodeAt(novo_texto.length-1) == 13)||(novo_texto.charCodeAt(novo_texto.length-1) == 32))
    novo_texto = novo_texto.substr(0, novo_texto.length-1);

  for(i=0; i<novo_texto.length; i++){
    if(i == 0)
      novo_texto = novo_texto.substr(0, 1).toUpperCase()+novo_texto.substr(1, novo_texto.length);
    if(novo_texto.charAt(i) == " "){
      if((novo_texto.charAt(i-1) == ".")||(novo_texto.charAt(i-1) == ";"))
        novo_texto = novo_texto.substr(0, i)+" "+novo_texto.substr(i+1, 1).toUpperCase()+novo_texto.substr(i+2, novo_texto.length);
      if((novo_texto.charAt(i-1) == ":")||(novo_texto.charAt(i-1) == "!")||(novo_texto.charAt(i-1) == "?"))
        novo_texto = novo_texto.substr(0, i)+" "+novo_texto.substr(i+1, 1).toUpperCase()+novo_texto.substr(i+2, novo_texto.length);
    }
    if((novo_texto.charCodeAt(i) == 13)||(novo_texto.charCodeAt(i) == 10))
      novo_texto = novo_texto.substr(0, i+1)+novo_texto.substr(i+1, 1).toUpperCase()+novo_texto.substr(i+2, novo_texto.length);
  }
  
  return novo_texto;
}


function conserta_nome(nome){
  novo_nome = nome.toLowerCase();
  
  novo_nome = conserta_texto(novo_nome);
  for(i=0; i<novo_nome.length; i++)
    if(novo_nome.charAt(i) == " ")
      novo_nome = novo_nome.substr(0, i)+" "+novo_nome.substr(i+1, 1).toUpperCase()+novo_nome.substr(i+2, novo_nome.length);

  novo_nome = novo_nome.replace(/ Da /g, " da ");
  novo_nome = novo_nome.replace(/ De /g, " de ");
  novo_nome = novo_nome.replace(/ Do /g, " do ");
  novo_nome = novo_nome.replace(/ Das /g, " das ");
  novo_nome = novo_nome.replace(/ Dos /g, " dos ");

  return novo_nome;
}