
// VALIDA TAMANHO DA STRING
function WSsize(obj,size){
    var stri = new String(obj.value);
         if ( stri.length < size ){
                 return false;
         }
         return true;
}

// VALIDA NÚMEROS NA STRING
function WSnum(obj){
     var str = new String(obj.value);
     var bOk = 1;
     for (i = 0; i < str.length; i++){
             if ( str.substring(i,i+1) < "0" ||  "9" < str.substring(i,i+1) ){
                     i = str.length;
                     bOk = 0;
             }
     }
     if (bOk == 0)        {
             return false;
     }
     return true;
}

// VALIDA E-MAIL
function WSemail(obj){
     var str = new String(obj.value);
     var bOk = 0;
     for (i = 0; i < str.length; i++){
             if ( str.charAt(i) == "@" ){
                     i = str.length;
                     bOk = 1;
             }
     }
     if (bOk == 0)        {
             return false;
     }
     return true;
}

// VALIDA CPF / cnpj
function WScpf(obj) {
     var soma1, soma2, s1, s2;
     var cpf = obj.value;
     var str_cpf = "";

     for (i = 0; i <= cpf.length - 1; i++)
          if ((cpf.charAt(i)).match(/\d/))
            str_cpf += cpf.charAt(i);
          else if (!(cpf.charAt(i)).match(/[\.\-]/)) {
            alert ("O campo 'CPF/CNPJ' apresenta caracteres inválidos");
            return false;
          }

       if (str_cpf.length != 11) 
	   {
			 if (str_cpf.length != 14)
			 {
               alert ("O campo 'CPF' deve conter 11 dígitos. Se for 'CNPJ' deve conter 14 dígitos");
               return false;
			 }
        }
		
		if (str_cpf.length == 14)
		{
			return (valida_cnpj(cpf));
		}
		
		if (str_cpf.length == 11)
		{
		
        soma1 = soma2 = 0;
        for (i = 0; i <= 8; i++) {
          soma1 += str_cpf.charAt(i) * (10-i);
          soma2 += str_cpf.charAt(i) * (11-i);
        }
        s1 = ((soma1 * 10) % 11) % 10;
        s2 = (((soma2 + (s1 * 2)) * 10) % 11) % 10;

        if ((s1 != str_cpf.charAt(9)) || (s2 != str_cpf.charAt(10))) {
               alert ("O CPF '"+cpf+"' não é válido");
               return false;
        }
        return true;
		}
}

function valida_cnpj(cnpj){
      var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
      digitos_iguais = 1;
      if (cnpj.length < 14 && cnpj.length < 15)
            return false;
      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))
                 {
				  alert("CNPJ '"+cnpj+"' não é válido");
                  return false;
				}
            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))
			{
				  alert("CNPJ '"+cnpj+"' não é válido");
                  return false;
			}
            return true;
            }
      else
	  {
		    alert("CNPJ '"+cnpj+"' não é válido");
            return false;
	  }
} 

// VALIDA DADOS
function WSvalidadados(){
    d = document.WSdados;
    // informações pessoais
    // NOME
    if ( !WSsize(d.nome,4) ){
        alert("Você deve informar o seu nome!");
        d.nome.focus();
        return false;
    }
    // NASCIMENTO
    if ( !WSsize(d.nascimento,10) ){
        alert("Você deve informar a sua data de nascimento!");
        d.nascimento.focus();
        return false;
    }
    // ENDEREÇO
    if ( !WSsize(d.endereco,4) ){
        alert("Você deve informar o seu endereço!");
        d.endereco.focus();
        return false;
    }
    // BAIRRO
    if ( !WSsize(d.bairro,4) ){
        alert("Você deve informar o seu bairro!");
        d.bairro.focus();
        return false;
    }
    // CEP
    if ( !WSsize(d.cep,8) ){
        alert("Você deve informar o seu CEP!\nEx.: 99999000\nNão utilize '-'");
        d.cep.focus();
        return false;
    }
    if ( !WSnum(d.cep) ){
        alert("Você deve informar o seu CEP!\nEx.: 99999000\nNão utilize '-'");
        d.cep.focus();
        return false;
    }
    // CIDADE
    if ( !WSsize(d.cidade,2) ){
        alert("Você deve informar a sua cidade!");
        d.cidade.focus();
        return false;
    }
    // ESTADO
    if( d.estado.value == "UF" ){
        alert("Você deve informar o seu Estado!");
        d.estado.focus();
        return false;
    }
    // TELEFONE
    if ( !WSsize(d.telefone,9) ){
        alert("Você deve informar o seu DDD e o telefone!");
        d.telefone.focus();
        return false;
    }
    // CELULAR
    // E-MAIL
    if ( !WSemail(d.email) ){
        alert("Você deve informar o seu e-mail!");
        d.email.focus();
        return false;
    }
    // CPF
    if( !WScpf(d.cpf) ){
        d.cpf.focus();
        return false;
    }
    // RG
    if ( !WSsize(d.rg,1) ){
        alert("Você deve informar o seu RG!\nNão utilize '.' ou '-'");
        d.rg.focus();
        return false;
    }
    // SENHA
    if ( !WSsize(d.senha,3) ){
        alert("Você deve informar a sua senha!");
        d.senha.focus();
        return false;
    }

    return true;

} // wsvalidadados

// VALIDAR DADOS DO FORMULÁRIO
function WSdadosForm(){
       if ( WSvalidadados() ){
               document.WSdados.submit();
       }
}
