// JavaScript Document

var resposta = 0;

function conferetudo(formulario)

{

	var texto, msgaux, erro; 

	var frm = document.form_cadastro;

	var nomeClasse;

	

	erro = false;

	nomeClasse = "campoCadastroErro";



	//msgaux='Os campos selecionados precisam ser verificados';

	msgaux='Os campos em destaque no formulário precisam ser verificados';	



	for (i=0;i<frm.elements.length;i++)

	{ 

		if(frm.elements[i].type!="radio" && frm.elements[i].type!="checkbox")

			frm.elements[i].className = "";

		else

			frm.elements[i].className ="";

	} 



	login = '';

	login_conf = '';



	texto = frm.login.value;

	login = frm.login.value;

	if (texto.length < 4) { 

		frm.login.className  = nomeClasse;			

		erro=true;

	}



	texto = frm.login_conf.value;

	login_conf = frm.login_conf.value;

	if (texto.length < 4) { 

		frm.login_conf.className  = nomeClasse;			

		erro=true;

	}	

			

	if(login != "" && login_conf!="")

	{

		if(login != login_conf){ 

			frm.login.className  = nomeClasse;

			frm.login_conf.className  = nomeClasse;		

			erro=true;

		}

	}

	//****************************//

		

	texto = frm.pwd.value;

	senha = frm.pwd.value;

	if (texto.length < 6 || texto.length > 12) { 

		frm.pwd.className  = nomeClasse;			

		erro=true;

	}

	

	texto = frm.pwd_conf.value;

	senha_conf = frm.pwd_conf.value;

	if (texto.length < 6 || texto.length > 12) { 

		frm.pwd_conf.className  = nomeClasse;			

		erro=true;

	}

		

	if(senha != "" && senha_conf!="")

	{

		if(senha != senha_conf){ 

			frm.pwd.className  = nomeClasse;

			frm.pwd_conf.className  = nomeClasse;		

			erro=true;

		}

	}

	

	//****************************//



	texto = frm.nome.value;

	if (texto.length < 1){

		frm.nome.className  = nomeClasse;	

		erro=true;				

	}		



	if(!frm.cpf.value.isCPF()){ // prototypes.js

		frm.cpf.className  = nomeClasse;		

		erro=true;				

	}

	

	/*

	if (!radio('sexo')) {

		frm.sexo.className=nomeClasse;		

		erro=true;				

		alert(frm.sexo.value);

	}

	*/

	texto = (frm.sexo[0].checked || frm.sexo[1].checked)

	if (!texto){

		frm.sexo[0].className = nomeClasse;

		frm.sexo[1].className = nomeClasse;		

		erro=true;		

	}	



	texto = frm.razao_social.value;

	if (texto.length < 1){

		frm.razao_social.className  = nomeClasse;	

		erro=true;				

	}

	

	if(!frm.cnpj.value.isCNPJ()){ // prototypes.js

		frm.cnpj.className  = nomeClasse;		

		erro=true;				

	}

		

	//if(!$('cpf').value.isCPF()){

	

	if (!frm.email.value.isMail())

	{

		frm.email.className  = nomeClasse;

		erro=true; 	

	}



	texto = frm.ddd01.value;

	if (texto.length < 2) {

		frm.ddd01.className  = nomeClasse;

		erro=true;

	}

	

	texto = frm.tel01.value;

	if (texto.length < 7) {

		frm.tel01.className  = nomeClasse;

		erro=true;

	}

		

	texto = frm.endereco.value;

	if (texto.length < 1){

		frm.endereco.className  = nomeClasse;		

		erro=true;				

	}



	texto = frm.numero.value;

	if (texto.length < 1){

		frm.numero.className  = nomeClasse;		

		erro=true;				

	}

	

	texto = frm.bairro.value;

	if (texto.length < 1){

		frm.bairro.className  = nomeClasse;		

		erro=true;				

	}



	texto = frm.id_estado.value;

	if (texto == -1){

		frm.id_estado.className = nomeClasse;		

		erro=true;				

	}

	

	texto = frm.id_cidade.value;

	if (texto == -1){

		frm.id_cidade.className = nomeClasse;		

		erro=true;				

	}	



	texto = frm.cep.value;

	if (texto.length < 5){ 

		frm.cep.className  = nomeClasse;		

		erro=true;				

	}	



	texto = frm.cep2.value;

	if (texto.length < 3){

		frm.cep2.className  = nomeClasse;		

		erro=true;				

	}			



	if(frm.concordo.checked==false)

	{

		//alert("Você precisa concordar com o Regulamento para poder se cadastrar.");

		frm.concordo.className  = nomeClasse;		

		erro=true;			

	}

	

 // Verifica se ouve erro

	if (erro == false)

	{

		//frm.submit();

		testaDuplicidade(frm.cpf.value, frm.cnpj.value, frm.login.value);

	}

	else

	{

		alert(msgaux);

    }

}



function testaDuplicidade(vCampo01, vCampo02, vCampo03) 

{

	var xml = new xmlConnection();

	if (xml.create()) 

	{

		xml.setMethod('POST');

		xml.setURL('testaduplicidadeXML.php');

		xml.setChildName('return');

		

		xml.addParameters('campo01',vCampo01);

		xml.addParameters('campo02',vCampo02);

		xml.addParameters('campo03',vCampo03);

		

		xml.onComplete = function() 

		{

			if (this.getAttByName('erro',0) == '') {

				document.form_cadastro.submit();

			} 

			else 

			{

				alert(this.getAttByName('erro',0));

			}

		};

		xml.execute();

	}

};

$ = function(e){ return document.getElementById(e); }

var verificaRazaoSocial = function(){
	if($('cnpj').value == "") return;
	
	var xml = new xmlConnection();
	if (xml.create()){
		xml.setMethod('POST');
		xml.setURL('consultaDocumento.php');
		xml.setChildName('SinteseCadastral');		
		xml.onComplete = function(){
			try{
				var nome = this.getXML().getElementsByTagName('Nome')[0].firstChild.nodeValue;
				var ativo = this.getXML().getElementsByTagName('SituacaoDocumento')[0].firstChild.nodeValue;
				
				if(ativo.substr(0,5) != "ATIVA"){
					$('btProcurar').style.display = "block";
					alert('CNPJ Inválido!');
					return false;
				}
				
				$('razao_social').value = nome;
			}catch(e){
				this.onError();
			}
		}
		xml.onError = function(){
			alert('Favor entrar em contato no SAC (16)2101-0190.');
		}
		xml.addParameters('documento',$('cnpj').value);
		
		$('btProcurar').style.display = "none";
		xml.execute();
	}
}
