
function verifica_ramo(valor){
	
	if(valor == "outros"){
		
		document.getElementById("cad_outro_ramo").style.display = "";
		
	}else{
		
		document.getElementById("cad_outro_ramo").style.display = "none";
	}
}


function limpa_divs_erro() {
	
	var array_divs = document.form_users_adm.getElementsByTagName("div");
	
	for(var d = 0 ; d < array_divs.length ; d++){

		var divs_erro = array_divs[d].getAttribute("id").split("_");

		if(divs_erro[0] == "erro"){

			var esconde_divs = "";

			for(var j = 0 ; j < divs_erro.length ; j++){
				if(j < divs_erro.length-1){
					esconde_divs += divs_erro[j] + "_";
				}else{
					esconde_divs += divs_erro[j];
				}
			}

			document.getElementById(esconde_divs).style.visibility	= 'hidden'		;
			document.getElementById(esconde_divs).style.position	= 'absolute'	;
			document.getElementById(esconde_divs).style.top			= '0'			;

		}
	}

	document.getElementById("mensagem_usuario").style.visibility	= 'hidden'		;
	document.getElementById("mensagem_usuario").style.position		= 'absolute'	;
	document.getElementById("mensagem_usuario").style.top			= '0'			;

}

function check_pessoa(pessoa) {
	
	
	if(pessoa == "pessoa_juridica"){
		document.getElementById("pessoa_fisica").style.display = "none";
		document.getElementById("pessoa_juridica").style.display = "";
		document.getElementById("exp_pessoa_juridica").style.display = "";
		document.getElementById("exp_pessoa_fisica").style.display = "none";
		
	} else {
		document.getElementById("pessoa_fisica").style.display = "";
		document.getElementById("exp_pessoa_fisica").style.display = "";
		document.getElementById("exp_pessoa_juridica").style.display = "none";
		document.getElementById("pessoa_juridica").style.display = "none";
	}
	
	limpa_divs_erro();
	
}

function validacao_cadastro(pagina){

  	//limpa_divs_erro();

	var valor = document.form_users_adm.elements;
	var validausersArray = new Array(valor.length-2);
	
	if(pagina == 'site'){
		var elemento_pessoa = 1;
	}else{
		var elemento_pessoa = 0;
	}

	for (var j = 0 ; j < valor.length-2 ; j++){

		var campo = document.form_users_adm.elements[j].name;
		var valor_campo = document.form_users_adm.elements[j].value;
		
		if(document.form_users_adm.elements[elemento_pessoa].checked == true){
			var pessoa = "pessoa_fisica";
		}else{
			var pessoa = "pessoa_juridica";
		}
		
		if(campo == "formulario"){
			continue;
		}

		validausersArray[campo] = valor_campo;

	}

	var url_get = "";
	for(i in validausersArray) {
		url_get += "&" + i + "=" + validausersArray[i];
	}

	url="validacao.php?pessoa="+pessoa+url_get;
	
	Dados(url,"null");

}

function Dados(valor,call) {
	
	// verifica se o browser tem suporte a ajax
	try {
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex) {
			try {
				ajax = new XMLHttpRequest();
			}
			catch(exc) {
				alert("Esse browser não tem recursos para uso do Ajax");
				ajax = null;
			}
		}
	}

	// se tiver suporte ajax
	if(ajax) {

	  	// deixa apenas o elemento 1 no option, os outros são excluídos
		// document.forms[0].produto.options.length = 1;

		idCadastro = document.getElementById("cadastro");
		
		ajax.open("GET", valor, true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		ajax.onreadystatechange = function() {

		//enquanto estiver processando...emite a msg de carregando
		if(ajax.readyState == 1) {
			idCadastro.value = "Validando..";
			
		}else{
			idCadastro.value = "Cadastrar";
		}

		//após ser processado - chama função processXML que vai varrer os dados
		if(ajax.readyState == 4 ) {
			
			// caso nao esteja vindo da função reload_captcha
			if (call == "null"){
				
				if(ajax.responseXML) {
					processXML(ajax.responseXML);
				}else {
					//caso não seja um arquivo XML emite a mensagem abaixo
					idCadastro.value = "Cadastrar";
				}
				
			}
			   
	    }

	   }

	   ajax.send(null);

	} // Fecha function Ajax

} // Fecha function Dados

function processXML(obj){

   // pega a tag erro
   var dataArrays = obj.getElementsByTagName("erro");
   
  	// total de elementos contidos na tag erros
	if(dataArrays.length > 0) {

		// percorre o arquivo XML para extrair os dados
		for(var i = 0 ; i < dataArrays.length ; i++) {

			var item = dataArrays[i];
			var erro_nome =  item.getElementsByTagName("nome_erro")[0].firstChild.nodeValue;
			var texto_erro =  item.getElementsByTagName("texto")[0].firstChild.nodeValue;
			
			document.getElementById('erro_'+erro_nome).style.visibility	= 'visible'	;
			document.getElementById('erro_'+erro_nome).style.position	= 'static'	;
			document.getElementById('erro_'+erro_nome).innerHTML 		= texto_erro;

		}

	} else {
	    // caso o XML volte vazio, ou seja, não tenha erro.....dá o submit no formulário
		document.form_users_adm.submit();
	}

}

function reload_captcha() {
	var div = document.getElementById("div_captcha");
	
	aleat = Math.random() * 1800
    aleat = Math.round(aleat) 	
    
	div.innerHTML = "";
	url = "inc/limpa_session_captcha.php?limpa=1";
	Dados(url,"reload_captcha");
	div.innerHTML = "<img src=\"./inc/captcha.php?n=5&"+aleat+"\" alt=\"CAPTCHA\" />";
}
