 function CtrPesquisaSimples() {
	
	var autoReferencia = this;
	this.setAutoReferencia(this);
	
	this.setId("CtrPesquisaSimples");

	this.instanciarLimite = function() {
		return new LimPesquisaSimples(this);
	}

	// ATRIBUTOS GLOBAIS
	
	// Atributos setados pelo controlador que solicita a pesquisa.
	this.depto = null;              // Depto que sera utilizado. Ex: "DeptoEstoque()". 
	this.pesquisa = null;			// String setada pelo controlador solicitante. EX: "TabelaPreco"  
	this.metodoResposta = null;		// Metodo que retorna o codigo e a desc. do registro escolhido. 
	this.controladorRequisitante = null;
	//*******
	
	this.registroInicial = 0; 		// Define o registro incial para solicitar ao servidor.  
	this.qtdeRegistros = 300; 		// Define quantidade de registros por requisicao.
	this.tamanhoPagina = 30; 		// Tamanho que as paginas serao exibidas na tela.
	this.paginaAtual = 0;    		// Pagina atual que sera exibida.
	this.totalRegistros = 0; 		// Numero total de registros.
	this.inicioPagina = 0;   		// Numero do primeiro registro a ser mostrado.
	this.fimPagina = 0;      		// Numero do ultimo registro a ser mostrado.
 	this.registros = 0;      		// O array de registros a ser exibido.
	this.paginaAtualServidor = 0;   // Pagina do servidor que esta sendo mostrada	
	this.inicioPaginaServidor = 0;  // primeiro registro a ser mostrado.
	this.fimPaginaServidor = 0;     // Ultimo registro a ser mostrado.
	

	this.efetuaPesquisa = function() {
		var depto = eval("new "+this.getDepto());
		var limite = this.getLimite();
		
		var entPesquisa = new EntPesquisa();
		entPesquisa.setCodigo(limite.getCampoCodigo() == "" ? 0 : limite.getCampoCodigo());
		entPesquisa.setDescricao(limite.getCampoNome() == "" ? "%" : limite.getCampoNome() + "%");
		entPesquisa.setQuantidadeRegistros(this.getQtdeRegistros());
		entPesquisa.setRegistroInicial(this.getRegistroInicial());
		entPesquisa.setAtivo(limite.getRadioSituacao());
		entPesquisa.setPesquisa(this.getPesquisa());
		
		limite.escondeDivPesquisa();
		limite.mostrarImagem("barraCarregando");
		depto.pesquisa(entPesquisa, this.metodoRetornoPesquisa);
	}
	
	this.metodoRetornoPesquisa = function(resposta) {
		if (autoReferencia.trataRespostaServidor(resposta.codErro, resposta.mensagem, false)) {
			if (resposta.dados[0].registros != null) {
				arrayRegistros = resposta.dados[0].registros;
				autoReferencia.setTotalRegistros(resposta.dados[0].totalDeRegistros);
				autoReferencia.setRegistros(arrayRegistros);
				autoReferencia.recebeRespostaServidor();
			} else {
				autoReferencia.getLimite().desabilitaBotoesPaginacao();
				autoReferencia.getLimite().desabilitaCampoNumeroPagina();
				autoReferencia.getLimite().mostrarMensagem("alerta", PESQUISA_SEM_RESULTADO);
			}
		}
	}
	
	this.recebeRespostaServidor = function() {
		this.calculaPaginaAtual();
		this.getLimite().imprimePagina(this.getRegistros());
	}
	
	
	// CHAMADO QUANDO UM REGISTRO EH SELECIONADO.
	// O CONTROLADOR REQUISITANTE DEVE OBRIGATORIAMENTE IMPLEMENTAR O METODO "retornoPesquisas".
	this.retornaRegistro = function(codigo, descricao){
		var controladorRequisitante = aplicativo.getControlador(this.getControladorRequisitante());
		controladorRequisitante.retornoPesquisas(codigo, descricao, this.getPesquisa());
	}	
	
	
	// CHAMADO PARA CALCULAR ATRAVES DA PAGINA ATUAL, O PRIMEIRO E O ULTIMO REGISTRO.
	this.calculaPaginaAtual = function() {
		if (this.getPaginaAtual() == 0) {
			this.setInicioPagina(0);
		} else {
			this.setInicioPagina((this.getTamanhoPagina() * this.getPaginaAtual()));
		}
		
		this.setFimPagina((this.getTamanhoPagina() * (1 + this.getPaginaAtual())) - 1);
	}
	
	this.proximaPagina = function() {
		this.setPaginaAtual(this.getPaginaAtual() + 1);
		this.calculaPaginaAtual();
		
		if (this.verificaRequisicaoNova()) {
			this.efetuaPesquisa();
		} else {
			this.getLimite().imprimePagina(this.getRegistros());
		} 
	}

	this.paginaAnterior = function() {
		this.setPaginaAtual(this.getPaginaAtual() - 1);
		this.calculaPaginaAtual();
			
		if (this.verificaRequisicaoNova()) {
			this.efetuaPesquisa();
		} else {
			this.getLimite().imprimePagina(this.getRegistros());
		} 
	}
	
	this.paginaEspecifica = function(pagina) {
		this.setPaginaAtual(pagina);
		this.calculaPaginaAtual();
		var novaPaginaServidor = this.calculaPaginaServidor(pagina);
		
		if (this.getPaginaAtualServidor() != novaPaginaServidor && pagina >= 0) {
			this.setPaginaAtualServidor(novaPaginaServidor);
			this.setRegistroInicial(this.getQtdeRegistros() * this.getPaginaAtualServidor());
			this.efetuaPesquisa();
		}
		else {
			this.getLimite().imprimePagina(this.getRegistros());
		} 
	}
	
	//CALCULA A PAGINA DO SERVIDOR ATRAVES DA PAGINA ESPECIFICA. 
	this.calculaPaginaServidor = function(pagina) {
		var qtdePaginasPorRequisicao = this.getQtdeRegistros() / this.getTamanhoPagina();
		var paginaServidor = Math.ceil((pagina + 1) / qtdePaginasPorRequisicao);
		
		return paginaServidor - 1;
	} 
	// VERIFICA SE EH PRECISO FAZER NOVA REQUISICAO PARA O SERVIDOR.
	this.verificaRequisicaoNova = function() {
		var bValor = false;
		
		if (this.getPaginaAtualServidor() == 0) {
			this.setInicioPaginaServidor(0);
			this.setFimPaginaServidor(this.getQtdeRegistros());
		} 
		else {
			this.setInicioPaginaServidor((this.getQtdeRegistros() * this.getPaginaAtualServidor()));
			this.setFimPaginaServidor(this.getQtdeRegistros() * (this.getPaginaAtualServidor() + 1));
		}
			
		if (this.getInicioPagina() < this.getInicioPaginaServidor() && this.getPaginaAtualServidor() > 0) {  //this.getInicioPaginaServidor()) {
			this.setPaginaAtualServidor(this.getPaginaAtualServidor() - 1);
			this.setRegistroInicial(this.getRegistroInicial() - this.getQtdeRegistros());
			bValor = true;
		}
		else if (this.getInicioPagina() >= this.getFimPaginaServidor()) {
			this.setPaginaAtualServidor(this.getPaginaAtualServidor() + 1);
			this.setRegistroInicial(this.getRegistroInicial() + this.getQtdeRegistros());
			bValor = true;
		}
		return bValor;
	}
	
	// GETTERS E SETTERS
	this.getDepto = function() {
		return this.depto;
	}
	
	this.setDepto = function(valor) {
		this.depto = valor;
	}

	this.getPesquisa = function() {
		return this.pesquisa;
	}
	
	this.setPesquisa = function(valor) {
		this.pesquisa = valor;
	}

	this.getMetodoResposta = function() {
		return this.metodoResposta;
	}
	
	this.setMetodoResposta = function(valor) {
		this.metodoResposta = valor;
	}

	this.getQtdeRegistros = function() {
		return this.qtdeRegistros;
	}
	
	this.setQtdeRegistros = function(valor) {
		this.qtdeRegistros = valor;
	}

	this.getRegistroInicial = function() {
		return this.registroInicial;
	}
	
	this.setRegistroInicial = function(valor) {
		this.registroInicial = valor;
	}
	
	this.getTamanhoPagina = function() {
		return this.tamanhoPagina;
	} 
	
	this.setTamanhoPagina = function(valor) {
		this.tamanhoPagina = valor;
	}

	this.getPaginaAtual = function() {
		return this.paginaAtual;
	} 
	
	this.setPaginaAtual = function(valor) {
		this.paginaAtual = valor;
	}

	this.getTotalRegistros = function() {
		return this.totalRegistros;
	} 
	
	this.setTotalRegistros = function(valor) {
		this.totalRegistros = valor;
	}
	
	this.getInicioPagina = function() {
		return this.inicioPagina;
	} 
	
	this.setInicioPagina = function(valor) {
		this.inicioPagina = valor;
	}

	this.getFimPagina = function() {
		return this.fimPagina;
	} 
	
	this.setFimPagina = function(valor) {
		this.fimPagina = valor;
	}

	this.getRegistros = function() {
		return this.registros;
	} 
	
	this.setRegistros = function(valor) {
		this.registros = valor;
	}

	this.getPaginaAtualServidor = function() {
		return this.paginaAtualServidor;
	} 
	
	this.setPaginaAtualServidor = function(valor) {
		this.paginaAtualServidor = valor;
	}

	this.getInicioPaginaServidor = function() {
		return this.inicioPaginaServidor;
	} 
	
	this.setInicioPaginaServidor = function(valor) {
		this.inicioPaginaServidor = valor;
	}

	this.getFimPaginaServidor = function() {
		return this.fimPaginaServidor;
	} 
	
	this.setFimPaginaServidor = function(valor) {
		this.fimPaginaServidor = valor;
	}
	
	this.getControladorRequisitante = function() {
		return this.controladorRequisitante;
	}
	
	this.setControladorRequisitante = function(objValor) {
		this.controladorRequisitante = objValor;
	}
	
}
CtrPesquisaSimples.prototype = new Controlador;