// NEWSLETTER
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function validarNewsletter() {
	if(document.getElementById("newsEmail").value == "") {
		document.getElementById("newsEmail").style.border = "2px solid red";
		document.getElementById("newsEmail").focus();
		return false;
	}
	if(document.getElementById("newsEmail").value.indexOf("@", 0) == -1 || document.getElementById("newsEmail").value.indexOf(".", 0) == -1) {
		document.getElementById("newsEmail").style.border = "2px solid red";
		document.getElementById("newsEmail").focus();
		return false;
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// COMENTARIOS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function validarComentario() {
	if(document.getElementById("comAssunto").value == "") {
		document.getElementById("comAssunto").style.border = "2px solid red";
		document.getElementById("comAssunto").focus();
		return false;
	}
	if(document.getElementById("comComentario").value == "") {
		document.getElementById("comComentario").style.border = "2px solid red";
		document.getElementById("comComentario").focus();
		return false;
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// CONTATO
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function validarContato() {
	if(document.getElementById("contatoNome").value == "") {
		document.getElementById("contatoNome").style.border = "2px solid red";
		document.getElementById("contatoNome").focus();
		return false;
	}
	if(document.getElementById("contatoEmail").value == "") {
		document.getElementById("contatoEmail").style.border = "2px solid red";
		document.getElementById("contatoEmail").focus();
		return false;
	}
	if(document.getElementById("contatoMensagem").value == "") {
		document.getElementById("contatoMensagem").style.border = "2px solid red";
		document.getElementById("contatoMensagem").focus();
		return false;
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// FALE CONOSCO
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function validarFaleConosco() {
	if(document.getElementById("contatoNome").value == "") {
		document.getElementById("contatoNome").style.border = "2px solid red";
		document.getElementById("contatoNome").focus();
		return false;
	} else {
		document.getElementById("contatoNome").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("contatoEmail").value == "" || document.getElementById("contatoEmail").value.indexOf("@", 0) == -1 || document.getElementById("contatoEmail").value.indexOf(".", 0) == -1) {
		document.getElementById("contatoEmail").style.border = "2px solid red";
		document.getElementById("contatoEmail").focus();
		return false;
	} else {
		document.getElementById("contatoEmail").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("contatoCategoria").value == "selecione") {
		document.getElementById("contatoCategoria").style.border = "2px solid red";
		document.getElementById("contatoCategoria").focus();
		return false;
	} else {
		document.getElementById("contatoCategoria").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("contatoAssunto").value == "") {
		document.getElementById("contatoAssunto").style.border = "2px solid red";
		document.getElementById("contatoAssunto").focus();
		return false;
	} else {
		document.getElementById("contatoAssunto").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("contatoMensagem").value == "") {
		document.getElementById("contatoMensagem").style.border = "2px solid red";
		document.getElementById("contatoMensagem").focus();
		return false;
	} else {
		document.getElementById("contatoMensagem").style.border = "1px solid #7f9db9";
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// IMPRESSAO
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var valorFonte = 12;
var valorMinimo = 10;
var valorMaximo = 20;

function iniciarImpressao(condicao, retorno) {
	if(condicao == "comum") {
		document.getElementById("imprimir").style.width = "100%";
	}
	document.getElementById("ferramentas").style.display = "none";
	window.print();
	finalizarImpressao(retorno);
}
function finalizarImpressao(retorno) {
	if(window.confirm("Página imprimida com sucesso!\n\nDeseja fechar está janela?")) {
		window.close();
	} else {
		window.location = retorno;
	}
}
function cancelarImpressao() {
	window.close();
}
function fonte(acao) {
	if(valorFonte <= valorMaximo) {
		document.getElementById("ferramentas").style.fontSize = 12 + "px";
		if(acao == "aumentar" && valorFonte < valorMaximo) {
			document.getElementById("imprimir").style.fontSize = (valorFonte + 2) + "px";
			valorFonte = (valorFonte + 2);
		} else if(acao == "diminuir" && valorFonte > valorMinimo) {
			document.getElementById("imprimir").style.fontSize = (valorFonte - 2) + "px";
			valorFonte = (valorFonte - 2);
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// GERENCIAR FONTE
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function gerenciarFonte(acao, opcoes, container) {
	if(acao == "normal") {
		document.getElementById(container).style.fontSize = 12 + "px";
		valorFonte = 12;
	}
	if(valorFonte <= valorMaximo) {
		document.getElementById(opcoes).style.fontSize = 12 + "px";
		if(acao == "aumentar" && valorFonte < valorMaximo) {
			document.getElementById(container).style.fontSize = (valorFonte + 2) + "px";
			valorFonte = (valorFonte + 2);
		} else if(acao == "diminuir" && valorFonte > valorMinimo) {
			document.getElementById(container).style.fontSize = (valorFonte - 2) + "px";
			valorFonte = (valorFonte - 2);
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// INDIQUE
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function validarIndique(formulario) {
	if(document.getElementById("indNome").value == "") {
		document.getElementById("indNome").style.border = "2px solid red";
		document.getElementById("indNome").focus();
		return false;
	} else {
		document.getElementById("indNome").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("indEmail").value == "" || 
	   document.getElementById("indEmail").value.indexOf("@", 0) == -1 || document.getElementById("indEmail").value.indexOf(".", 0) == -1) {
		document.getElementById("indEmail").style.border = "2px solid red";
		document.getElementById("indEmail").focus();
		return false;
	} else {
		document.getElementById("indEmail").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("indNomeAmigo").value == "") {
		document.getElementById("indNomeAmigo").style.border = "2px solid red";
		document.getElementById("indNomeAmigo").focus();
		return false;
	} else {
		document.getElementById("indNomeAmigo").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("indEmailAmigo").value == "" || 
	   document.getElementById("indEmailAmigo").value.indexOf("@", 0) == -1 || document.getElementById("indEmailAmigo").value.indexOf(".", 0) == -1) {
		document.getElementById("indEmailAmigo").style.border = "2px solid red";
		document.getElementById("indEmailAmigo").focus();
		return false;
	} else {
		document.getElementById("indEmailAmigo").style.border = "1px solid #7f9db9";
	}
	indicar(formulario);
}

function indicar(formulario) {

	var indNome = document.forms[formulario].indNome.value;
	var indEmail = document.forms[formulario].indEmail.value;
	var indEmailAmigo = document.forms[formulario].indEmailAmigo.value;
	var indNomeAmigo = document.forms[formulario].indNomeAmigo.value;
	var indMensagem = document.forms[formulario].indMensagem.value;
	var indSetor = document.forms[formulario].indSetor.value;
	var indSetorId = document.forms[formulario].indSetorId.value;
	var indSetorNome = document.forms[formulario].indSetorNome.value;
	var indSetorUrl = document.forms[formulario].indSetorUrl.value;

	parametros = "&indNome=" + indNome + "&indEmail=" + indEmail + "&indEmailAmigo=" + indEmailAmigo + "&indNomeAmigo=" + indNomeAmigo + "&indMensagem=" + indMensagem + "&indSetor=" + indSetor + "&indSetorId=" + indSetorId + "&indSetorNome=" + indSetorNome + "&indSetorUrl=" + indSetorUrl;
	
	ajax.open("POST", PORTAL_MODULOS + "indique/indique.php?acao=indicar", true);
	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax.setRequestHeader("Content-length", parametros.length);
	ajax.setRequestHeader("Connection", "close");
	
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {	
			
			exibirElemento("sucesso", "fast");
			ocultarElemento("indique", "slow");
			document.getElementById("sucesso").innerHTML = "<p><strong>A sua indicação foi realizada com sucesso!</strong></p>";
			
			var indNome = document.forms[formulario].indNome.value = "";
			var indEmail = document.forms[formulario].indEmail.value = "";
			var indEmailAmigo = document.forms[formulario].indEmailAmigo.value = "";
			var indNomeAmigo = document.forms[formulario].indNomeAmigo.value = "";
			var indMensagem = document.forms[formulario].indMensagem.value = "";
			
			ocultarElementoPorTempo("ocultarElemento('sucesso')", 5000);
			
		} else {
			
			exibirElemento("sucesso", "fast");
			document.getElementById("sucesso").innerHTML = "<p><strong>Efetuando indicação, por favor aguarde...</strong></p>";
			
		}
	}
	ajax.send(parametros);
	
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// VERIFICAR EMAIL
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function verificarEmail(url, email) {
	ajax.open("GET", PORTAL_INCLUDES_MODULOS + "cadastro.php?acao=verificar&email=" + email, true);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {
			if(ajax.responseText == "0") {
				document.getElementById("statusVerificacao").innerHTML = "Email dispon&iacute;vel";
				document.getElementById("iconePreloader").src = url + "imagens/icones/sucesso.png";
				document.getElementById("preloader").style.display = "none";
				document.getElementById("userEmailStatus").value = "1";
			} else {
				document.getElementById("statusVerificacao").innerHTML = 'Email existente, <a href="' + url + 'recuperar-senha/" title="Recuperar Senha">recuperar senha</a>.';
				document.getElementById("iconePreloader").src = url + "imagens/icones/erro.png";
				document.getElementById("preloader").style.display = ""
				document.getElementById("userEmailStatus").value = "0";
			}
		} else {
			document.getElementById("statusVerificacao").innerHTML = "Consultando email, por favor aguarde.";
			document.getElementById("iconePreloader").src = url + "imagens/preloader.gif";
			document.getElementById("preloader").style.display = "none";
		}
	}
	ajax.send(null);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// TIPO CADASTRO
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function tipoCadastro(tipo) {
	document.getElementById("userTipoCadastroSelecionado").value = tipo;
	if(tipo == "fisica") {
		document.getElementById("cadastroPessoaFisica").style.display = "block";
		document.getElementById("cadastroPessoaJuridica").style.display = "none";
	} else {
		document.getElementById("cadastroPessoaFisica").style.display = "none";
		document.getElementById("cadastroPessoaJuridica").style.display = "block";
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// VALIDAR CADASTRO
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function validarCadastro(tipo) {
	
	if(tipo == "fisica") {
	
		if(document.getElementById("userNome").value == "") {
			document.getElementById("userNome").style.border = "2px solid red";
			document.getElementById("userNome").focus();
			return false;
		} else {
			document.getElementById("userNome").style.border = "1px solid #7f9db9";
		}
		if(document.getElementById("userSobrenome").value == "") {
			document.getElementById("userSobrenome").style.border = "2px solid red";
			document.getElementById("userSobrenome").focus();
			return false;
		} else {
			document.getElementById("userSobrenome").style.border = "1px solid #7f9db9";
		}
		if(document.getElementById("userNascimento").value == "" || document.getElementById("userNascimento").value == "__/__/____") {
			document.getElementById("userNascimento").style.border = "2px solid red";
			document.getElementById("userNascimento").focus();
			return false;
		} else {
			document.getElementById("userNascimento").style.border = "1px solid #7f9db9";
		}
		if(document.getElementById("userRG").value == "") {
			document.getElementById("userRG").style.border = "2px solid red";
			document.getElementById("userRG").focus();
			return false;
		} else {
			document.getElementById("userRG").style.border = "1px solid #7f9db9";
		}
		if(document.getElementById("userCpfCnpj").value == "" || document.getElementById("userNascimento").value == "__/__/____") {
			document.getElementById("userCpfCnpj").style.border = "2px solid red";
			document.getElementById("userCpfCnpj").focus();
			return false;
		} else {
			document.getElementById("userCpfCnpj").style.border = "1px solid #7f9db9";
		}	
		if(document.getElementById("userEstadoCivil").value == "") {
			document.getElementById("userEstadoCivil").style.border = "2px solid red";
			document.getElementById("userEstadoCivil").focus();
			return false;
		} else {
			document.getElementById("userEstadoCivil").style.border = "1px solid #7f9db9";
		}
	
	} else {
		
		if(document.getElementById("userEmpRazaoSocial").value == "") {
			document.getElementById("userEmpRazaoSocial").style.border = "2px solid red";
			document.getElementById("userEmpRazaoSocial").focus();
			return false;
		} else {
			document.getElementById("userEmpRazaoSocial").style.border = "1px solid #7f9db9";
		}
		if(document.getElementById("userEmpNomeContato").value == "") {
			document.getElementById("userEmpNomeContato").style.border = "2px solid red";
			document.getElementById("userEmpNomeContato").focus();
			return false;
		} else {
			document.getElementById("userEmpNomeContato").style.border = "1px solid #7f9db9";
		}
		if(document.getElementById("userEmpCNPJ").value == "") {
			document.getElementById("userEmpCNPJ").style.border = "2px solid red";
			document.getElementById("userEmpCNPJ").focus();
			return false;
		} else {
			document.getElementById("userEmpCNPJ").style.border = "1px solid #7f9db9";
		}
		if(document.getElementById("userEmpIE").value == "") {
			document.getElementById("userEmpIE").style.border = "2px solid red";
			document.getElementById("userEmpIE").focus();
			return false;
		} else {
			document.getElementById("userEmpIE").style.border = "1px solid #7f9db9";
		}
		
	}	
	
	if(document.getElementById("userTelefone").value == "" || document.getElementById("userTelefone").value == "(__) ____-____") {
		document.getElementById("userTelefone").style.border = "2px solid red";
		document.getElementById("userTelefone").focus();
		return false;
	} else {
		document.getElementById("userTelefone").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("userEndereco").value == "") {
		document.getElementById("userEndereco").style.border = "2px solid red";
		document.getElementById("userEndereco").focus();
		return false;
	} else {
		document.getElementById("userEndereco").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("userCep").value == "") {
		document.getElementById("userCep").style.border = "2px solid red";
		document.getElementById("userCep").focus();
		return false;
	} else {
		document.getElementById("userCep").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("userBairro").value == "") {
		document.getElementById("userBairro").style.border = "2px solid red";
		document.getElementById("userBairro").focus();
		return false;
	} else {
		document.getElementById("userBairro").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("userEstado").value == "selecione") {
		document.getElementById("userEstado").style.border = "2px solid red";
		document.getElementById("userEstado").focus();
		return false;
	} else {
		document.getElementById("userEstado").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("userCidade").value == "selecione") {
		document.getElementById("userCidade").style.border = "2px solid red";
		document.getElementById("userCidade").focus();
		return false;
	} else {
		document.getElementById("userCidade").style.border = "1px solid #7f9db9";
	}	
	if(document.getElementById("userEmail").value == "" || document.getElementById("userEmail").value.indexOf("@", 0) == -1 || document.getElementById("userEmail").value.indexOf(".", 0) == -1) {
		document.getElementById("userEmail").style.border = "2px solid red";
		document.getElementById("userEmail").focus();
		return false;
	} else {
		document.getElementById("userEmail").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("userEmailStatus").value == "0") {
		document.getElementById("userEmail").style.border = "2px solid #901e78";
		document.getElementById("userEmail").focus();
		return false;
	} else {
		document.getElementById("userEmail").style.border = "1px solid #7f9db9";
	}
	if(document.getElementById("userSenha").value == "") {
		document.getElementById("userSenha").style.border = "2px solid red";
		document.getElementById("userSenha").focus();
		return false;
	} else {
		document.getElementById("userSenha").style.border = "1px solid #7f9db9";
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// CARREGAR CIDADES
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function carregarCidades(estado) {
 	var selectCidades = document.getElementById("userCidade");
	ajax.open("GET", PORTAL_INCLUDES_MODULOS + "cidades.php?acao=carregar&estado=" + estado, true);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {
			var listarCidades = ajax.responseText.split(",");
			for(i = 0; i < selectCidades.options.length; i++) {
				selectCidades.options.remove(i);
			}
			for(i = 0; i < listarCidades.length-1; i++) {
				var listarCidadesOptions = listarCidades[i].split("|");
				selectCidades.options[i] = new Option(listarCidadesOptions[1], listarCidadesOptions[0]);
			}
			selectCidades.disabled = 0;
		} else {
			selectCidades.options.option[0] = new Option("Carregando cidades, por favor aguarde...", "Carregando cidades, por favor aguarde...");
		}
	}
	ajax.send(null);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
