
// 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(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 if(typeof(mail) == "object"){
        if(er.test(mail.value)){ 
                    return true; 
                }
    }else{
        return false;
        }
}

// VALIDA CPF
function WScpf(obj, tipo) {
     var soma1, soma2, s1, s2;
	 if (tipo == '1'){
     var cpf = obj.value;
	 }
	 else{
		var cpf = obj; 
	 }
     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' apresenta caracteres inválidos");
            return false;
          }

       if (str_cpf.length != 11) 
	   {
			
               alert ("O campo 'CPF' deve conter 11 dígitos. Utilize somente números!");
               return false;
			 
        }
		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 ("CPF invá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 && cnpj.length != 11){
	  		alert('CNPJ inválido');
			return false;
	  }
	 
	  if (cnpj.length == 11){
			if (WScpf(cnpj, '2')){
				return true;
			}
			else{
				return false;	
			}
	  }
	  
	  else{
	  
      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 invá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 inválido");
                  return false;
			}
            return true;
            }
      else
	  {
		    alert("CNPJ inválido");
            return false;
	  }
	  
	  }
} 


function RealTestaCNPJ(CNPJ,g)
 {
 var VerCNPJ=0;
 var ind=2;
 var tam;
 for(f=g;f>0;f--)
  {
  VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
  if(ind>8)
   {
   ind=2;
   }
  else
   {
   ind++;
   }
  }
  VerCNPJ%=11;
  if(VerCNPJ==0 || VerCNPJ==1)
   {
   VerCNPJ=0;
   }
  else
   {
   VerCNPJ=11-VerCNPJ;
   }
 if(VerCNPJ!=parseInt(CNPJ.charAt(g)))
  {
  return(0);
  }
 else
  {
  return(1);
  }
 } 


// VALIDA DADOS
function WSvalidadados(){
    d = document.WSdados;
    // informações pessoais
    // NOME
    if ( !WSsize(d.nome,4) ){
        alert("Você deve informar a Razão Social da empresa!");
        d.nome.focus();
        return false;
    }
    if( !valida_cnpj(d.cnpj.value) ){
		//alert('CNPJ inválido!');
        d.cnpj.focus();
        return false;
    }
    // ie
    if ( !WSsize(d.ie,4) ){
        alert("Você deve informar a inscrição estadual da empresa! Se for ISENTO, digite a palavra 'ISENTO' no campo Inscrição Estadual!");
        d.ie.focus();
        return false;
    }
	// ENDEREÇO
    if ( !WSsize(d.endereco,4) ){
        alert("Você deve informar o endereço da empresa!");
        d.endereco.focus();
        return false;
    }
    // BAIRRO
    if ( !WSsize(d.bairro,4) ){
        alert("Você deve informar o bairro da empresa!");
        d.bairro.focus();
        return false;
    }
    // CEP
    if ( !WSsize(d.cep,8) ){
        alert("Você deve informar o CEP da empresa!\nEx.: 99999000\nNão utilize '-'");
        d.cep.focus();
        return false;
    }
    if ( !WSnum(d.cep) ){
        alert("Você deve informar o CEP da empresa!\nEx.: 99999000\nNão utilize '-'");
        d.cep.focus();
        return false;
    }
    // CIDADE
    if ( !WSsize(d.cidade,4) ){
        alert("Você deve informar a cidade da empresa!");
        d.cidade.focus();
        return false;
    }
    // ESTADO
    if( d.estado.value == "UF" ){
        alert("Você deve informar o Estado da empresa!");
        d.estado.focus();
        return false;
    }
	// DDD Telefone
    if ( !WSsize(d.ddd_tel,2) ){
        alert("Você deve informar o DDD do telefone da empresa!");
        d.ddd_tel.focus();
        return false;
    }
    // TELEFONE
    if ( !WSsize(d.tel,7) ){
        alert("Você deve informar o telefone da empresa!");
        d.tel.focus();
        return false;
    }
    // E-MAIL
    if ( !WSemail(d.email.value) ){
        alert("Email da empresa inválido!");
        d.email.focus();
        return false;
    }
	 // SENHA
    if ( !WSsize(d.senha,3) ){
        alert("Você deve informar a senha do cadastro!");
        d.senha.focus();
       return false;
    }
    // CPF
   
	
	if ( !WSsize(d.nome_resp,4) ){
        alert("Você deve informar o nome do responsável pelo cadastro!");
        d.nome_resp.focus();
        return false;
    }
   
    // SENHA
    if ( !WSsize(d.cargo,3) ){
        alert("Você deve informar o cargo do responsável pelo cadastro!");
        d.cargo.focus();
       return false;
    }
	
	 // NASCIMENTO
    if ( !WSsize(d.nascimento,10) ){
        alert("Você deve informar a data de nascimento do responsável pelo cadastro!");
        d.nascimento.focus();
        return false;
    }
	 // cpf
    if ( !WScpf(d.cpf,'1') ){
        d.cpf.focus();
        return false;
    }
	
	 // rg
    if ( !WSsize(d.rg,5) ){
		alert('Você deve informar o RG do responsável pelo cadastro!');
        d.rg.focus();
        return false;
    }
	
	// DDD Telefone
    if ( !WSsize(d.ddd_tel_resp,2) ){
        alert("Você deve informar o DDD do telefone do responsável pelo cadastro!");
        d.ddd_tel_resp.focus();
        return false;
    }
    // TELEFONE
    if ( !WSsize(d.tel,7) ){
        alert("Você deve informar o telefone do responsável pelo cadastro!");
        d.tel_resp.focus();
        return false;
    }
	
	//profissao
	
	if ( !WSsize(d.profissao,3) ){
        alert("Você deve informar a profissão do responsável pelo cadastro!");
        d.profissao.focus();
        return false;
    }
	
	//profissao
	
	if ( !WSsize(d.email_resp,3) ){
        alert("Você deve informar o email do responsável pelo cadastro!");
        d.email_resp.focus();
        return false;
    }
    return true;

} // wsvalidadados

function isNUMB(c) 
 { 
 if((cx=c.indexOf(","))!=-1) 
  { 
  c = c.substring(0,cx)+"."+c.substring(cx+1); 
  } 
 if((parseFloat(c) / c != 1)) 
  { 
  if(parseFloat(c) * c == 0) 
   { 
   return(1); 
   } 
  else 
   { 
   return(0); 
   } 
  } 
 else 
  { 
  return(1); 
  } 
 } 

function LIMP(c) 
 { 
 while((cx=c.indexOf("-"))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf("/"))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf(","))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf("."))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf("("))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf(")"))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 while((cx=c.indexOf(" "))!=-1) 
  { 
  c = c.substring(0,cx)+c.substring(cx+1); 
  } 
 return(c); 
 } 



// VALIDAR DADOS DO FORMULÁRIO
function WSdadosForm(){
       if ( WSvalidadados() ){
               document.WSdados.submit();
       }
}
