
// ===============================================================================
// SETAR O ARQUIVO DE PROCESSAMENTO GENÉRICO QUE O AJAX UTILIZA NAS FUNÇÕES: =====
// ===============================================================================
var ARQUIVO_PROCESSAMENTO_GERAL_AJAX = RAIZ_SITE + "biblioteca/processamento.php";
// ===============================================================================

function funcoesObjetoClicavel() {

	// NOME DAS FUNÇÕES PRONTAS: ===============================================
	this.nome_funcao_pronta = Array();
	this.nome_funcao_pronta['Exibir'] 					= 'exibir';
	this.nome_funcao_pronta['Alterar'] 					= 'alterar';
	this.nome_funcao_pronta['Salvar como...']			= 'salvarComo';
	this.nome_funcao_pronta['Ativar'] 					= 'requisitarAtivacao';
	this.nome_funcao_pronta['Desativar'] 				= 'requisitarDesativacao';
	this.nome_funcao_pronta['Excluir'] 					= 'requisitarExclusao';
	this.nome_funcao_pronta['Enviar login e senha'] 	= 'requisitarEnvioLoginSenha';
	this.nome_funcao_pronta['Logar como este usuário'] 	= 'requisitarLogin';

	// ÍCONES DAS FUNÇÕES PRONTAS: =============================================
	this.icone_funcao_pronta = Array();
	this.icone_funcao_pronta['Exibir'] 					= 'visualizacao';
	this.icone_funcao_pronta['Alterar'] 				= 'edicao';
	this.icone_funcao_pronta['Salvar como...']			= 'salvar';
	this.icone_funcao_pronta['Ativar'] 					= 'ativar';
	this.icone_funcao_pronta['Desativar'] 				= 'desativar';
	this.icone_funcao_pronta['Excluir'] 				= 'exclusao';
	this.icone_funcao_pronta['Enviar login e senha']	= 'envio';
	this.icone_funcao_pronta['Logar como este usuário']	= 'usuario';
	
	// FRASES DE CONFIRMAÇÃO OU ALERT: =========================================
	this.pergunta_confirmacao_exclusao;
	this.frase_confirmacao_envio_login_senha;
	this.frase_confirmacao_login;

	// VALIDAÇÕES: =============================================================
	this.validarIdBase = function() {
		if(!this.id_base) {
			alert('ERRO INTERNO: Não foi especificado o id do objeto clicável.');
			return false;
		}
		return true;
	}
	this.validarNomeIdBase = function() {
		if(!this.nome_id_base) {
			alert('ERRO INTERNO: Não foi especificado o nome do id do objeto clicável.');
			return false;
		}
		return true;
	}
	this.validarDivPreloader = function() {
		if(!this.id_div_preloader) {
			alert('ERRO INTERNO: Não foi especificado o nome da div de preloader para excluir.');
			return false;
		} 
		if(!document.getElementById(this.id_div_preloader + this.id_base) && !document.getElementById(this.id_div_preloader)) {
			alert('ERRO INTERNO: Não foi encontrado no HTML a div do preloader "' + this.id_div_preloader + this.id_base + '" nem "' + this.id_div_preloader + '".');
			return false;
		}
		return true;
	}
	this.validarNome = function() {
		if(!this.nome) {
			alert('ERRO INTERNO: Não foi especificado o nome.');
			return false;
		}
		return true;
	}
	// EXIBIR DETALHAMENTO: ====================================================
	this.exibir = function() {
		if(this.validarNomeIdBase() && this.validarIdBase()) {
			document.location = 'detalhamento.php?' + this.nome_id_base + '=' + this.id_base;
		}
	}

	// IR PARA PÁGINA DE ALTERAÇÃO: ============================================
	this.alterar = function() {
		if(this.validarNomeIdBase() && this.validarIdBase()) {
			document.location = 'cadastro.php?acao=alterar&' + this.nome_id_base + '=' + this.id_base + '&pagina_resultante=listagem';
		}
	}

	// SALVAR COMO: ============================================================
	this.salvarComo = function() {
		if(this.validarNomeIdBase() && this.validarIdBase()) {
			document.location = 'cadastro.php?acao=duplicar&' + this.nome_id_base + '=' + this.id_base + '&pagina_resultante=listagem';
		}
	}

	// ATIVAMENTO AJAX: ==========================================================
	this.requisitarAlteracaoStatus = function(status) {
		
		if(typeof(preloaderPagina) == 'object') {
			preloaderPagina.ocultar();
		}
		
		this.ocultarMenu();

		if(!this.validarNomeIdBase() || !this.validarIdBase() || !this.validarDivPreloader()) {
			return false;
		}
		
		// EXECUTAR AJAX:
		local_preloader = this.id_div_preloader + this.id_base;
		document.getElementById(local_preloader).style.display = 'block';
		modo_debugger_ajax = false;
		url = ARQUIVO_PROCESSAMENTO_GERAL_AJAX;
		parametros = 'acao=alterar_status&nome_dado=' + this.nome_id_base + '&' + this.nome_id_base + '=' + this.id_base + '&status=' + status;
		objetoRequisicaoDados = new requisicaoDados(url, parametros, this.resultadoAlteracaoStatus, cancelarAjax, local_preloader, '');
	}
	this.resultadoAlteracaoStatus = function() {
		if(resultado_ajax == 'sucesso') {
			atualizarPagina();
		} else {
			alert('Erro interno na alteração de status.');
		}
	}
	this.requisitarAtivacao = function() {
		this.requisitarAlteracaoStatus('ativo');
	}
	this.requisitarDesativacao = function() {
		this.requisitarAlteracaoStatus('inativo');
	}

	// EXCLUSÃO AJAX: ==========================================================
	this.requisitarExclusao = function() {
		this.ocultarMenu();
		if(typeof(preloaderPagina) == 'object') {
			preloaderPagina.ocultar();
		}

		if(!this.validarNomeIdBase() || !this.validarDivPreloader() || !this.validarDivPreloader()) {
			return false;
		}

		// CONFIRMAR EXCLUSÃO: 
		if(!this.pergunta_confirmacao_exclusao) {
			this.pergunta_confirmacao_exclusao = 'Deseja realmente excluir permantentemente este cadastro?';
		}
		confirmacao = confirm(this.pergunta_confirmacao_exclusao);
		// EXECUTAR AJAX:
 		if(confirmacao) {
			// O preloader pode ser "preloader_imagem23", onde 23 é o id, mas também pode ser só "preloader_imagem"
			local_preloader = this.id_div_preloader + this.id_base;
			if(!document.getElementById(local_preloader)) {
				local_preloader = this.id_div_preloader;
			}
			document.getElementById(local_preloader).style.display = 'block';
			modo_debugger_ajax = false;
			url = ARQUIVO_PROCESSAMENTO_GERAL_AJAX;
			parametros = 'acao=excluir&nome_dado=' + this.nome_id_base + '&' + this.nome_id_base + '=' + this.id_base;
			objetoRequisicaoDados = new requisicaoDados(url, parametros, this.resultadoExclusao, cancelarAjax, local_preloader, '');
		}
	}

	this.resultadoExclusao = function() {
		if(resultado_ajax == 'sucesso') {
			atualizarPagina();
		} else {
			alert('Erro interno na exclusão.');
		}
	}
	
	// ENVIAR LOGIN E SENHA: ===================================================
	this.requisitarEnvioLoginSenha = function() {
		this.ocultarMenu();
		if(typeof(preloaderPagina) == 'object') {
			preloaderPagina.ocultar();
		}
		if(!this.validarNomeIdBase() || !this.validarDivPreloader() || !this.validarDivPreloader()) {
			return false;
		}

		// CONFIRMAR ENVIO: 
		if(!this.frase_confirmacao_envio_login_senha) {
			this.validarNome();
			this.frase_confirmacao_envio_login_senha = 'Deseja enviar login e senha para ' + this.nome + '?';
		}
		confirmacao = confirm(this.frase_confirmacao_envio_login_senha);

		// EXECUTAR AJAX:
		if(confirmacao) {
			modo_debugger_ajax = false;
			url = ARQUIVO_PROCESSAMENTO_GERAL_AJAX;
			parametros = 'acao=enviar_login_senha&nome_dado=' + this.nome_id_base + '&' + this.nome_id_base + '=' + this.id_base;
			local_preloader = this.id_div_preloader + this.id_base;
			document.getElementById(local_preloader).style.display = 'block';
			objetoRequisicaoDados = new requisicaoDados(url, parametros, this.resultadoEnvioLoginSenha, cancelarAjax, local_preloader, '');
		}
	}
	this.resultadoEnvioLoginSenha = function() {
		if(resultado_envio_login_senha == 'sucesso') {
			alert("Nome de usuário e senha enviados para " + email_destinatario + ".");
		} else {
			alert('Houve algum erro ao tentar enviar o nome de usuário e senha. \nIsso pode ser ocasionado por instabilidade na conexão da internet, congestionamento no servidor de e-mail ou endereço de e-mail digitado incorretamente durante o cadastro. Confira o e-mail e tente novamente.');
		}
	}
	
	// LOGAR COMO USUÁRIO: ===================================================
	this.requisitarLogin = function() {
		this.ocultarMenu();
		if(typeof(preloaderPagina) == 'object') {
			preloaderPagina.ocultar();
		}
		if(!this.validarNomeIdBase() || !this.validarDivPreloader() || !this.validarNome()) {
			return false;
		}

		// CONFIRMAR ENVIO: 
		if(!this.frase_confirmacao_login) {
			this.validarNome();
			this.frase_confirmacao_login = 'Deseja logar como se você fosse ' + this.nome + '?';
		}
		confirmacao = confirm(this.frase_confirmacao_login);

		// EXECUTAR AJAX:
		if(confirmacao) {
			modo_debugger_ajax = false;
			url = ARQUIVO_PROCESSAMENTO_GERAL_AJAX;
			parametros = 'acao=logar_como&nome_dado=' + this.nome_id_base + '&' + this.nome_id_base + '=' + this.id_base;
			local_preloader = this.id_div_preloader + this.id_base;
			document.getElementById(local_preloader).style.display = 'block';
			objetoRequisicaoDados = new requisicaoDados(url, parametros, this.resultadoLogin, cancelarAjax, local_preloader, '');
		}
	}
	this.resultadoLogin = function() {
		if(resultado_logar_como == 'sucesso') {
			if(!this.pagina_resultado_login) {
				this.pagina_resultado_login = (RAIZ_SITE != '' ? RAIZ_SITE+'index.php' : 'index.php');
			}
			document.location = this.pagina_resultado_login;
		} else {
			alert('Houve algum erro ao tentar logar como ' + this.nome + '.');
			window.location.reload();
		}
	}
}

objetoClicavel.prototype = new funcoesObjetoClicavel();
