// JavaScript Document
var whitespace = " \t\n\r";
function isEmpty(s)
{
return ((s == null) || (s.length == 0));
}
function isWhitespace(s)
{ var i;
if (isEmpty(s)) return true;
for (i = 0; i < s.length; i++){
var c = s.charAt(i);
if (whitespace.indexOf(c) == -1) return false;
}
return true;
}
function isEmail(s)
{ if (isEmpty(s))
return false;
if (isWhitespace(s)) return false;
var i = 1;
var sLength = s.length;
while ((i < sLength) && (s.charAt(i) != "@")){
i++;
}
if ((i >= sLength) || (s.charAt(i) != "@")) return false;
else i += 2;
while ((i < sLength) && (s.charAt(i) != ".")){
i++;
}
if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
else return true;
}
function ValidaCep(cep){
    if (isEmpty(cep)) return true;
    else {
    if (cep.length == 8) {
	exp = /[1-9]\d{7}/
	if(!exp.test(cep)) 
	return false;
	else return true;
    } else return false;
    } // 1º else
} // function


function validaCpf(cpf)
if (isEmpty(tel)) return true;
    else {
      var numeros, digitos, soma, i, resultado, digitos_iguais;
      digitos_iguais = 1;
      if (cpf.length < 11)
            return false;
      for (i = 0; i < cpf.length - 1; i++)
            if (cpf.charAt(i) != cpf.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            numeros = cpf.substring(0,9);
            digitos = cpf.substring(9);
            soma = 0;
            for (i = 10; i > 1; i--)
                  soma += numeros.charAt(10 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            numeros = cpf.substring(0,10);
            soma = 0;
            for (i = 11; i > 1; i--)
                  soma += numeros.charAt(11 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
}


function ValidaTelefone(tel){
    if (isEmpty(tel)) return true;
    else {
    if ((tel.length == 7) || (tel.length == 8)) {
	exp = /[2-9]\d{6,7}/
	if(!exp.test(tel)) 
	return false;
	else return true;
    } else return false;
    } // 1º else
} // function


function ValidaDDD(ddd){
    if (isEmpty(ddd)) return true;
    else {
    exp = /[1-9]{2}/
    if(!exp.test(ddd))
	return false;
else return true;
    }
}


function Confirma(){
var str = "";
//nome
if (isWhitespace(document.formmail.nome.value))
str = str + " - preencha o campo 'Nome'\n";
//email
if (!isEmail(document.formmail.email.value))
str = str + " - preencha o campo 'E-mail' corretamente\n";
//cidade
if (isWhitespace(document.formmail.cidade.value))
str = str + " - preencha o campo 'Cidade'\n";
//Assunto
if (isWhitespace(document.formmail.assunto.value))
str = str + " - preencha o campo 'Assunto'\n";
//cep
if (!ValidaCep(document.formmail.cep.value))
str = str + " - preencha o campo 'cep' corretamente\n";
//CPF
if (!validaCpf(document.formmail.cpf.value))
str = str + " - preencha o campo 'cep' corretamente\n";
//telefone
if (!ValidaDDD(document.formmail.ddd.value) || !ValidaTelefone(document.formmail.tel.value))
str = str + " - preencha o campo 'Telefone' corretamente\n";
//celular
if (!ValidaDDD(document.formmail.ddd2.value) || !ValidaTelefone(document.formmail.tel2.value))
str = str + " - preencha o campo 'Celular' corretamente\n";
if (str == "") {
return true;
} else {
alert("Por favor, procure corrigir o(s) seguinte(s) erro(s):\n" + str + "e tente enviar este formulario novamente.");
return false;
}
}
function enviar()
{
if (Confirma()){
document.formmail.action = "send_form.php";
document.formmail.submit();
}
}

