/**
 * Non Sense Flag Control
 * 
 * @author Thiago Zaranza
 */

function NSFC(formularioPrincipal) 
{
this.modulos=new Array();
this.moduloCorrente=null;

this.divs=new Array();
this.divCorrente=null;

this.componentes=new Array();
this.componenteCorrente=null;

this.formularioPrincipal=null;
this.mensagem=null;

if (formularioPrincipal!=undefined) 
	{
	this.formularioPrincipal=jQuery("form")[0].id;
	formulario=jQuery("form")[0];
	}
};

NSFC.prototype.addModulo=function(modulo) 
{
this.moduloCorrente=modulo;
this.modulos.push(modulo);
};

NSFC.prototype.removeModulo=function(modulo) 
{
this.modulos.remove(modulo);
this.resetModuloCorrente();
};

NSFC.prototype.getModulo=function(nomeModulo) 
{
for(var i=0;i<this.modulos.length;i++)
	if (this.modulos[i].nomeModulo==nomeModulo) 
		{
		this.setModuloCorrente(this.modulos[i]);
		return this.modulos[i];
		}

return null;
};

NSFC.prototype.getModuloCorrente=function() 
{
if (this.moduloCorrente!=null)
	return this.moduloCorrente;
	else 
	{
	this.resetModuloCorrente();
	return this.moduloCorrente;
	}
};

NSFC.prototype.resetModuloCorrente=function() 
{
if (this.modulos.length > 0)
	this.moduloCorrente=this.modulos[0];
	else
	this.moduloCorrente=null;
};

NSFC.prototype.setModuloCorrente=function(modulo) 
{
this.moduloCorrente=modulo;
};

NSFC.prototype.addComponente=function(componente) 
{
this.componentes.push(componente);
};

NSFC.prototype.removeComponente=function(componente) 
{
this.componentes.remove(componente);
};

NSFC.prototype.getComponente=function(nomeComponente) 
{
for (var i=0;i<this.componentes.length;i++)
	if (this.componentes[i].nome==nomeComponente)
		return this.componentes[i];

return null;
};

NSFC.prototype.getComponenteCorrente=function() 
{
if (this.componenteCorrente!=null)
	return this.componenteCorrente;
	else 
	if (this.componentes.length > 0)
		return this.componentes[0];
		else
		return false;
};

NSFC.prototype.buscarComponente=function(idComponente) 
{
// Procura componente no formulário corrente.	
if (this.getModuloCorrente().getFormCorrente().getComponentePorId(idComponente)!=null)
	return this.getModuloCorrente().getFormCorrente().getComponentePorId(idComponente);

// Procura o componente em cada formulário de cada módulo.
for (var i=0;i<this.modulos.length;i++)
	for (var j=0;j<this.modulos[i].forms.length;j++)
		if (this.modulos[i].forms[j].getComponentePorId(idComponente)!=null)
			return this.modulos[i].forms[j].getComponentePorId(idComponente);

// Procura o componente nos formulários de cada aba de cada módulo. 
for (var i=0;i<this.modulos.length;i++)
	for (var j=0;j<this.modulos[i].abas.length;j++)	
		if (this.modulos[i].abas[j].form.getComponentePorId(idComponente)!=null)
			return this.modulos[i].abas[j].form.getComponentePorId(idComponente);

for (var i=0;i<this.divs.length;i++)
   	{
   	if (this.divs[i].form!=null)
   		if (this.divs[i].form.getComponentePorId(idComponente)!=null)
  			return this.divs[i].form.getComponentePorId(idComponente);
	
  	if (this.divs[i].arvore!=null)
   		if (this.divs[i].arvore.id==idComponente)
    	    return this.divs[i].arvore;
  	}
  	
return null;
};

NSFC.prototype.buscarDiv=function(nomeDiv) 
{
// Procura div no módulo corrente.	
if (this.getModuloCorrente()!=null)	
	if (this.getModuloCorrente().getDiv(nomeDiv)!=null)
		return this.getModuloCorrente().getDiv(nomeDiv);

// Procura o componente em cada formulário de cada módulo.
for (var i=0;i<this.modulos.length;i++)
	if (this.modulos[i].getDiv(nomeDiv)!=null)
			return this.modulos[i].getDiv(nomeDiv);

return null;
};

NSFC.prototype.buscarGrid=function(nomeGrid) 
{
// Procura grid no formulário corrente.	
if (this.getModuloCorrente().getFormCorrente().getGrid(nomeGrid)!=null)
	return this.getModuloCorrente().getFormCorrente().getGrid(nomeGrid);

// Procura grid em cada formulário de cada módulo.
for (var i=0;i<this.modulos.length;i++)
	for (var j=0;j<this.modulos[i].forms.length;j++)
		if (this.modulos[i].forms[j].getGrid(nomeGrid)!=null)
			return this.modulos[i].forms[j].getGrid(nomeGrid);

// Procura grid nos formulários de cada aba de cada módulo. 
for (var i=0;i<this.modulos.length;i++)
	for (var j=0;j<this.modulos[i].abas.length;j++)	
		if (this.modulos[i].abas[j].form.getGrid(nomeGrid)!=null)
			return this.modulos[i].abas[j].form.getGrid(nomeGrid);

//Procura grid nos formulários de cada div de cada módulo. 
for (var i=0;i<this.divs.length;i++)	
	if (this.divs[i].form!=null)
		if (this.divs[i].form.getGrid(nomeGrid)!=null)
			return this.divs[i].form.getGrid(nomeGrid);

// Procura grid nas estantes
for (var i=0;i<this.componentes.length;i++)	
	if (this.componentes[i].tipo=="Estante")
		for (var j=0;j<this.componentes[i].listaPrateleiras.length;j++)	
			{
			if (this.componentes[i].listaPrateleiras[j].item!=null)
				{
				if (this.componentes[i].listaPrateleiras[j].item.tipo=="Grid")
					if (this.componentes[i].listaPrateleiras[j].item.nome==nomeGrid)
						return this.componentes[i].listaPrateleiras[j].item;
				}
				else if (this.componentes[i].listaPrateleiras[j].grid!=null)
				{
				if (this.componentes[i].listaPrateleiras[j].grid.nome==nomeGrid)
					return this.componentes[i].listaPrateleiras[j].grid;	
				}	
			}

return null;	
};

NSFC.prototype.buscarForm=function(nomeForm) 
{
// Procura form no módulo corrente.	
if (this.getModuloCorrente().getForm(nomeForm)!=null)
	return this.getModuloCorrente().getForm(nomeForm);

// Procura form em cada formulário de cada módulo.
for (var i=0;i<this.modulos.length;i++)
	if (this.modulos[i].getForm(nomeForm)!=null)
		return this.modulos[i].getForm(nomeForm);

// Procura form nas abas de cada módulo. 
for (var i=0;i<this.modulos.length;i++)
	for (var j=0;j<this.modulos[i].abas.length;j++)	
		if (this.modulos[i].abas[j].form)
			if (this.modulos[i].abas[j].form.nome==nomeForm)
				return this.modulos[i].abas[j].form;

//Procura form nas divs de cada módulo. 
for (var i=0;i<this.divs.length;i++)	
	if (this.divs[i].form!=null)
		if (this.divs[i].form.nome==nomeForm)
			return this.divs[i].form.nome;

return null;
};

NSFC.prototype.fecharDiv=function() 
{
try	
{
this.divCorrente.fechar();
}
catch(e)
	{
	new Erro(e).exibir("Não foi possível fechar a div!");
	}
};

NSFC.prototype.getMensagem=function() 
{
return this.mensagem;
};

NSFC.prototype.setMensagem=function(mensagem) 
{
this.mensagem=mensagem;
};

NSFC.prototype.inicializarModulo=function(nomeModulo) 
{
eval("var modulo=new "+nomeModulo+"()");
this.addModulo(modulo);
return modulo;
};

NSFC.prototype.inicializarComponente=function(nomeComponente) 
{
eval("var componente=new "+nomeComponente+"()");
this.addComponente(componente);
componente.inicializar();
};

NSFC.prototype.habilitarAtalhos=function() 
{
Atalhos();
};

NSFC.prototype.setComponente=function(nomeComponente,valor) 
{
this.getModuloCorrente().getFormCorrente().getComponente(nomeComponente).setValor(valor);
};

NSFC.prototype.get=function(path) 
{
var lista=path.split(".");
var instrucao="this";
var retorno=null;

if (lista.length >= 1) 
	{
	if (lista[0]=="current")
		instrucao+=".getModuloCorrente()";
		else
		instrucao+=".getModulo('"+lista[0]+"')";
	}

if (lista.length >= 2) 
	{
	if (lista[1]=="current")
		instrucao+=".getFormCorrente()";
		else
		instrucao+=".getForm('"+lista[1]+"')";
	}

if (lista.length >= 3)
	instrucao+=".getComponente('"+lista[2]+"')";

eval("retorno="+instrucao);
return retorno;
};

/**
 * Modulo
 * 
 * @classDescription Classe principal do framework. Controla o estado da página
 *                   e os eventos acionados pelo usuário.
 * 
 * @author Thiago Zaranza e Henrique Miranda
 * 
 * Tabela de flags: 
 * 	"inc" Componente deve ser levado em consideração ao incluir * o registro. 
 * 	"atu" Componente deve ser levado em consideração ao atualizar o registro. 
 *	"can" Componente deve ser "limpo" nos eventos <cancelar> ou <novo> da tela. 
 * 	"foc" Componente que deve receber o foco do formulário. 
 * 	"obr" Componente de preenchimento obrigatório. 
 * 	"bus" Componente deve ser levado em consideração em eventos de <busca>. 
 * 	"lwc" Componente deve ter seu conteúdo em letras minúsculas. 
 * 	"upc" Componente deve ter seu conteúdo em letras maiúsculas.
 */

Modulo=function() 
{
this.nomeModulo=null;
this.complementoComando=null;

this.forms=new Array();
this.formCorrente=null;

this.abas=new Array();
this.abaCorrente=null;

this.pilhaForms=new Array();
this.pilhaDivs=new Array();

this.limite=null;
this.nivelArquivo=null;

this.objetoConsulta=null;

this.callbackConsultar=null;
this.callbackDefinirRegistro=null;
};

/**
 * @method construtor
 * 
 * Construtor da classe Modulo. Ao inicializar um módulo é instanciado o Form
 * default e é inicializado como form corrente. O form default deve ter o nome
 * form[nomeModulo].
 * 
 * @param nomeModulo
 *            (string) Nome do módulo.
 * @param limite
 *            (int) Limite usado para paginação de registros.
 * @param nivelArquivo
 *            (int) Path do arquivo com relação à raíz do sistema.
 */
Modulo.prototype.construtor=function(nomeModulo,limite,nivelArquivo) 
{
try
{
this.nomeModulo=nomeModulo;
this.complementoComando=nomeModulo;
this.limite=limite;
this.nivelArquivo=nivelArquivo;

var referencia=this;
if (jQuery("#divAbas").length) 
	{
	jQuery("#divAbas li").each(
		function(li) 
			{
			var aba=new Aba(this.id.replace("aba",""));
			referencia.addAba(aba);
			if (this.className=="abaLigada") 
				{
				referencia.abaCorrente=aba;
				referencia.pilhaForms.push(aba.form);
				}

			jQuery("#"+this.id).click(
				function() 
					{
					var aba=NSFC.get('current').getAba(this.id.replace("aba",""));

					AlternarAbasCSS(this.id);

					NSFC.get('current').abaCorrente=aba;
					NSFC.get('current').formCorrente=aba.form;

					NSFC.get('current').pilhaForms.pop();
					NSFC.get('current').pilhaForms.push(aba.form);
					});
			});

	this.formCorrente=this.abas[0].form;
	} 
	else 
	{
	if (jQuery("#form"+nomeModulo).length)
		{
		this.formCorrente=new Form("form"+nomeModulo);
		this.addForm(this.formCorrente);
		this.pilhaForms.push(this.formCorrente);
		}
		else
		throw("Form \"form"+nomeModulo+"\" não localizado!");
	}
}
catch (e)
	{
	new Erro(e).exibir();
	}
};

Modulo.prototype.setModuloCorrente=function() 
{
NSFC.moduloCorrente=this;
};

Modulo.prototype.addAba=function(objeto) 
{
this.abas.push(objeto);
};

/**
 * Método que adiciona um formulário à lista de forms do Módulo.
 * 
 * @param objetoForm
 *            (Form) Objeto form para ser adicionado em Modulos.
 */
Modulo.prototype.addForm=function(objetoForm) 
{
this.forms.push(objetoForm);
};

/**
 * Método que seta o parâmetro formCorrente e faz o mapeamento de seu HTML.
 * 
 * @param form
 *            (Form) Seta o parâmetro formCorrente do Módulo.
 */
Modulo.prototype.setFormCorrente=function(form) 
{
if (form!=undefined && form!=null)
	this.formCorrente=form;
};

/**
 * Método que retorna o Form acessível.
 * 
 * @return formCorrente (Form) Objeto com o form corrente.
 */
Modulo.prototype.getFormCorrente=function() 
{
if (this.formCorrente!=null)
	return this.formCorrente;
	else 
	if (this.forms.length > 0)
		return this.forms[0];
		else
		return null;
};

/**
 * Método que retorna o Form cujo nome é "nomeForm".
 * 
 * @param nomeForm
 *            (string) Nome do form que deve ser retornado.
 * 
 * @return formCorrente (Form) Objeto com o form corrente.
 */
Modulo.prototype.getForm=function(nomeForm) 
{
for (var i=0;i<this.forms.length;i++)
	if (this.forms[i].nome==nomeForm)
		return this.forms[i];

for(var i=0;i<this.abas.length;i++)
	if (this.abas[i].form.nome==nomeForm)
		return this.abas[i].form;


return null;
};

Modulo.prototype.resetForm=function(nomeForm) 
{
this.formCorrente=this.forms[0];
};

Modulo.prototype.inicializarDiv=function(nomeDiv) 
{
var div=null;

if (this.getDiv(nomeDiv)==null) 
	{
	eval("div=new "+nomeDiv+"()");
	this.addDiv(div);
	} 
	else
	div=this.getDiv(nomeDiv);

return div;
};

/**
 * @method addDiv
 * 
 * Método que adiciona um div à lista de divs do Módulo.
 * 
 * @param objetoDiv
 *            (Div) Objeto form para ser adicionado em Modulos.
 */
Modulo.prototype.addDiv=function(objetoDiv) 
{
NSFC.divs.push(objetoDiv);
};

Modulo.prototype.addGrid=function(objetoGrid) 
{
this.getFormCorrente().addGrid(objetoGrid);
};

/**
 * @method setDivCorrente
 * 
 * Método que seta o parâmetro divCorrente.
 * 
 * @param div
 *            (Div) Seta o parâmetro divCorrente do Módulo.
 */
Modulo.prototype.setDivCorrente=function(div) 
{
NSFC.divCorrente=div;
};

/**
 * @method getDivCorrente
 * 
 * Método que retorna a Div acessível.
 * 
 * @return divCorrente (Div) Objeto com a div corrente.
 */
Modulo.prototype.getDivCorrente=function() 
{
if (NSFC.divCorrente!=null)
	return NSFC.divCorrente;
	else 
	if (NSFC.divs.length > 0)
		return NSFC.divs[0];
		else
		return null;
};

/**
 * @method getDiv
 * 
 * Método que retorna o objeto Div cujo nome é "nomeDiv".
 * 
 * @param nomeDiv
 *            (string) Nome da div que deve ser retornado.
 * 
 * @return div (Div) Objeto com a div de nome "nomeDiv".
 */
Modulo.prototype.getDiv=function(nomeDiv) 
{
this.setModuloCorrente();

for (var i=0;i<NSFC.divs.length;i++)
	if (NSFC.divs[i].nome==nomeDiv)
		return NSFC.divs[i];

return null;
};

Modulo.prototype.getAba=function(nomeAba) 
{
for (var i=0;i<this.abas.length;i++)
	if (this.abas[i].nome==nomeAba)
		return this.abas[i];

return null;
};

/**
 * Método que realiza a requisição para exibir a paginação da grid
 * 
 * @param nomeGrid
 *            (string) Nome da grid
 * @param pagina
 *            (string) Marcador para a página
 *            [primeiraPagina,paginaAnterior,proximaPagina,ultimaPagina]
 */
Modulo.prototype.exibirRegistros=function(nomeGrid,pagina,ordenacao) 
{
if (this.isFunction('exibirRegistros'+this.nomeModulo) && this.exibirRegistros.caller!=this['exibirRegistros'+this.nomeModulo]) 
	this['exibirRegistros'+this.nomeModulo]();
	else
	{
	var grid=null;
	
	if (this.getFormCorrente().getGrid(nomeGrid)!=null)
		this.getFormCorrente().getGrid(nomeGrid).exibirRegistros(pagina,ordenacao);
		else 
		if (NSFC.getComponenteCorrente()!=null) 
			{
			if (typeof NSFC.getComponenteCorrente().getGrid=='function')
				if (NSFC.getComponenteCorrente().getGrid(nomeGrid)!=null)
					NSFC.getComponenteCorrente().getGrid(nomeGrid).exibirRegistros(pagina,ordenacao);
			}
	}
};

/**
 * Resposta da função exibirRegistros
 * 
 * @param requisicao
 *            (object) Objeto que controla a requisição
 * @param nomeGrid
 *            (string) Nome da grid a ser atualizada
 */
Modulo.prototype.respostaExibirRegistros=function(objetoResposta,nomeGrid) 
{
if (this.isFunction('respostaExibirRegistros'+this.nomeModulo) && this.exibirRegistros.caller!=this['respostaExibirRegistros'+this.nomeModulo]) 
	this['respostaExibirRegistros'+this.nomeModulo](objetoResposta,nomeGrid);
	else
	this.getFormCorrente().getGrid(nomeGrid).atualizar(objetoResposta);
};

Modulo.prototype.definirRegistro=function(codigoRecebido,nomeGrid) 
{
if (this.isFunction('definirRegistro'+this.nomeModulo) && this.definirRegistro.caller!=this['definirRegistro'+this.nomeModulo]) 
	this['definirRegistro'+this.nomeModulo]();	
	else
	{		
	var grid =NSFC.buscarGrid(nomeGrid);
	
	if (grid!=null)
		{
		var referencia=this;
	
		var intervalo=window.setInterval(function() 
			{
			try 
			{
			grid.definirRegistro(codigoRecebido,nomeGrid);
	
			if (grid.gridLocalizar==false && grid.consultar!=undefined)
				grid.consultar();
				else
				referencia.getFormCorrente().setFocus();
	
			clearInterval(intervalo);
			} 
			catch (e) 
				{
				;
				}
			},100);
		}	
	}

if (this.callbackDefinirRegistro!=null)
	this.callbackDefinirRegistro.apply(this);
};

Modulo.prototype.getGrid=function(nomeGrid)
{
if (this.getFormCorrente().getGrid(nomeGrid)!=null)
	return this.getFormCorrente().getGrid(nomeGrid);

for (var i=0;i<this.forms.length;i++)
	if (this.forms[i].getGrid(nomeGrid)!=null)
		return this.forms[i].getGrid(nomeGrid);

for (var i=0;i<NSFC.modulos.length;i++)
	for (var j=0;j<NSFC.modulos[i].forms.length;j++)
		if (NSFC.modulos[i].forms[j].getGrid(nomeGrid)!=null)
			return NSFC.modulos[i].forms[j].getGrid(nomeGrid);

if (NSFC.getComponenteCorrente()!=null)
	if(typeof NSFC.getComponenteCorrente().getGrid=="function")
		if(NSFC.getComponenteCorrente().getGrid(nomeGrid)!= null)
			return NSFC.getComponenteCorrente().getGrid(nomeGrid);

return null;
};

Modulo.prototype.consultar=function(objetoConsulta) 
{
if (this.isFunction('consultar'+this.nomeModulo) && this.consultar.caller!=this['consultar'+this.nomeModulo]) 
	this['consultar'+this.nomeModulo]();	
	else
	{
	try 
	{
	var formCorrente=this.getFormCorrente();
	
	this.objetoConsulta=objetoConsulta;
	
	if (this.objetoConsulta==undefined && formCorrente.getObjetoAtivo()!=null)
		this.objetoConsulta=formCorrente.getObjetoAtivo();
	
	if (this.objetoConsulta!=null && formCorrente.ajax!=null) 
		{
		if (this.objetoConsulta.getValor()!=null) 
			{
			var funcaoResposta="";
			var dados="";
			var ajax="";
	
			if (this.abas.length)
				dados="comando=consultarRegistro"+this.abaCorrente.nome;
				else if (this.objetoConsulta.moduloReferenciado!=null)
					dados="comando=consultarRegistro"+this.objetoConsulta.moduloReferenciado;
					else
					dados="comando=consultarRegistro"+this.complementoComando;
			
			dados+="&"+this.objetoConsulta.campoChave+"="+this.objetoConsulta.codigoRegistroDefinido;
			
			if (this.isFunction("respostaConsultar"+this.objetoConsulta.nome))
				funcaoResposta="respostaConsultar"+this.objetoConsulta.nome;
				else				
				funcaoResposta="respostaConsultar";
			
			
			if (this.objetoConsulta.ajax!=null)
				ajax=this.nivelArquivo+this.objetoConsulta.ajax;
				else
				ajax=this.nivelArquivo+formCorrente.ajax;
	
			new Ajax().requisicao(this,ajax,dados,'false',funcaoResposta);
			}
		}
	} 
	catch (e) 
		{
		new Erro(e).exibir('Modulo.consultar()');
		}
	}
};

Modulo.prototype.respostaConsultar=function(objetoResposta) 
{
if (this.isFunction('respostaConsultar'+this.objetoConsulta.nome) && this.respostaConsultar.caller!=this['respostaConsultar'+this.objetoConsulta.nome]) 
	this['respostaConsultar'+this.objetoConsulta.nome](objetoResposta);	
	else
	{
	var obj=this.extrairObjeto(objetoResposta);
	
	if (this.registroEncontrado(objetoResposta)) 
		{
		var propriedades=Reflections.getPropriedades(obj);	
		this.getFormCorrente().limpar();
				
		this.objetoConsulta.registroDefinido=obj;
	
		for(var i=0;i<propriedades.length;i++) 
			{
			var propriedade=propriedades[i];
			var componente=this.getFormCorrente().getComponentePorNome(propriedade);
			
			if (componente!=null) 
				{
				if (componente.getAtributo("referencia")!=undefined)
					componente.recarregar(obj[propriedade]);
					else
					componente.setValor(obj[propriedade]);			
				}
						
			if (this.objetoConsulta.informacoesCadastrais)
				{
				switch (propriedade)
					{
					case "servidorSI": 		 	this.objetoConsulta.informacoesCadastrais.servidorSI=obj[propriedade]; 			break;
					case "dataHoraCadastro":	this.objetoConsulta.informacoesCadastrais.dataHoraCadastro=obj[propriedade];	break;
					case "SUA": 				this.objetoConsulta.informacoesCadastrais.SUA=obj[propriedade]; 				break;
					case "DHUA": 				this.objetoConsulta.informacoesCadastrais.DHUA=obj[propriedade]; 				break;
					}
				}
			}
		
			if (this.callbackConsultar!=null)
				this.callbackConsultar.apply(this);
		}
	}
};

Modulo.prototype.extrairObjeto=function(objetoResposta)
{
if (objetoResposta.root.list!=undefined)
	return objetoResposta.root.list.registros;
	 
if (objetoResposta.root.registro!=undefined)
	return objetoResposta.root.registro;
};

Modulo.prototype.registroEncontrado=function(objetoResposta)
{
if (objetoResposta.root.diversos!=undefined)
	if (objetoResposta.root.diversos.encontrado!=undefined)
		if (objetoResposta.root.diversos.encontrado!=0)
			return true;

return false;
};

Modulo.prototype.limpar=function() 
{
this.setModuloCorrente();

if (this.isFunction('limpar'+this.nomeModulo) && this.limpar.caller!=this['limpar'+this.nomeModulo]) 
	this['limpar'+this.nomeModulo]();
	else 
	this.getFormCorrente().cancelar();
};

Modulo.prototype.novo=function() 
{
if (this.isFunction('novo'+this.nomeModulo) && this.novo.caller!=this['novo'+this.nomeModulo]) 	
	this['novo'+this.nomeModulo]();
	else 
	this.limpar();
};

Modulo.prototype.incluir=function() 
{
if (this.isFunction('incluir'+this.nomeModulo) && this.incluir.caller!=this['incluir'+this.nomeModulo]) 
	this['incluir'+this.nomeModulo]();
	else
	{	
	var formCorrente=this.getFormCorrente();
	var gridCorrente=formCorrente.getGridCorrente();
	
	if (gridCorrente.codigoRegistroDefinido==null) 
		{
		if (formCorrente.verificarCampos()==true) 
			{
			var dados="";
			if (this.abas.length)
				dados="comando=incluir"+this.abaCorrente.nome;
				else
				dados="comando=incluir"+this.complementoComando;
	
			dados+=formCorrente.montarUrl(".inc.")+"&limite="+this.limite;		
	
			new Ajax().requisicao(this,this.nivelArquivo+formCorrente.ajax,dados,'false',"respostaIncluir");
			}
		} 
		else
		alert("Você não se encontra no modo de inclusão! Aperte no botão <Novo>.");
	}
};

Modulo.prototype.respostaIncluir=function(objetoResposta) 
{
if (this.isFunction('respostaIncluir'+this.nomeModulo) && this.respostaIncluir.caller!=this['respostaIncluir'+this.nomeModulo])
	this['respostaIncluir'+this.nomeModulo](objetoResposta);
	else
	{
	if (objetoResposta.root.diversos.acessoNegado=="0" && objetoResposta.root.diversos.inseriu=="1") 
		{
		var formCorrente=this.getFormCorrente();
		var gridCorrente=formCorrente.getGridCorrente();
		var codigoComponentePrincipal=objetoResposta.root.diversos[gridCorrente.campoChave];
		var paginaAtualModulo=objetoResposta.root.diversos.pagina;
	
		if (objetoResposta.root.diversos.inseriu==0)
			this.consultar(gridCorrente);
	
		this.exibirRegistros(gridCorrente.nome,paginaAtualModulo);
		this.definirRegistro(codigoComponentePrincipal,gridCorrente.nome);
		}
	
	window.setTimeout(function() {
		alert(objetoResposta.root.diversos.mensagem);
	},500);
	}
};

Modulo.prototype.excluir=function() 
{
if (this.isFunction('excluir'+this.nomeModulo) && this.excluir.caller!=this['excluir'+this.nomeModulo])
	this['excluir'+this.nomeModulo]();
	else
	{	
	var formCorrente=this.getFormCorrente();
	var gridCorrente=formCorrente.getGridCorrente();
	
	if (gridCorrente.codigoRegistroDefinido!=null) 
		{
		if (confirm("Deseja excluir este registro?")) 
			{
			var dados="";
			if (this.abas.length)
				dados="comando=excluir"+this.abaCorrente.nome;
				else
				dados="comando=excluir"+this.complementoComando;
			
			dados+="&"+gridCorrente.campoChave+"="+gridCorrente.codigoRegistroDefinido;
			new Ajax().requisicao(this,this.nivelArquivo+formCorrente.ajax,dados,'false',"respostaExcluir");
		}
	} 
	else
	alert("Selecione o registro que deseja excluir!");
	}
};

Modulo.prototype.respostaExcluir=function(objetoResposta) 
{
if (this.isFunction('respostaExcluir'+this.nomeModulo) && this.respostaExcluir.caller!=this['respostaExcluir'+this.nomeModulo]) 
	this['respostaExcluir'+this.nomeModulo](objetoResposta);	
	else		
	{	
	var gridCorrente=this.getFormCorrente().getGridCorrente();
	
	if (objetoResposta.root.diversos.acessoNegado==0) 
		{
		if (objetoResposta.root.diversos.excluiu==undefined || objetoResposta.root.diversos.excluiu=="1")
			{
			gridCorrente.total--;
			var totalRegistros=gridCorrente.total;
		
			if (totalRegistros%this.limite==0 && totalRegistros!=0 && gridCorrente.ultimaPagina==0)
				gridCorrente.pagina--;
		
			gridCorrente.codigoRegistroDefinido=null;
			this.exibirRegistros(gridCorrente.nome,gridCorrente.pagina);
			
			this.getFormCorrente().limpar();
			}
		}
	
	window.setTimeout(
		function() 
		{
		alert(objetoResposta.root.diversos.mensagem);
		},500);
	}
};

Modulo.prototype.atualizar=function() 
{
if (this.isFunction('atualizar'+this.nomeModulo) && this.atualizar.caller!=this['atualizar'+this.nomeModulo]) 
	this['atualizar'+this.nomeModulo]();	
	else
	{	
	var formCorrente=this.getFormCorrente();
	var gridCorrente=formCorrente.getGridCorrente();
	
	var codigoComponentePrincipal=gridCorrente.codigoRegistroDefinido;
	
	if (codigoComponentePrincipal!=null) 
		{
		if (formCorrente.verificarCampos()==true) 
			{
			var dados="";
			if (this.abas.length)
				dados="comando=atualizar"+this.abaCorrente.nome;
				else
				dados="comando=atualizar"+this.complementoComando;
	
			dados+=formCorrente.montarUrl(".atu.")+"&"+gridCorrente.campoChave+"="+gridCorrente.codigoRegistroDefinido+"&limite="+this.limite;
			
			new Ajax().requisicao(this,this.nivelArquivo+formCorrente.ajax,dados,'false',"respostaAtualizar");
			}
		} 
		else
		alert("Selecione o registro que deseja atualizar!");
	}
};

Modulo.prototype.respostaAtualizar=function(objetoResposta) 
{
if (this.isFunction('respostaAtualizar'+this.nomeModulo) && this.respostaAtualizar.caller!=this['respostaAtualizar'+this.nomeModulo]) 
	this['respostaAtualizar'+this.nomeModulo](objetoResposta);	
	else
	{	
	var formCorrente=this.getFormCorrente();
	var gridCorrente=formCorrente.getGridCorrente();
	var codigoComponentePrincipal=objetoResposta.root.diversos[gridCorrente.campoChave];
	
	if (objetoResposta.root.diversos.acessoNegado=="0") 
		{
		this.exibirRegistros(gridCorrente.nome,objetoResposta.root.diversos.pagina);
		this.consultar(gridCorrente);
		}
	
	window.setTimeout(function() 
		{
		alert(objetoResposta.root.diversos.mensagem);
		},gridCorrente.tempoTransicao * 5);
	}
};

Modulo.prototype.abrirDiv=function(div) 
{
this.divCorrente=div;

this.pilhaForms.push(this.getFormCorrente());
this.setFormCorrente(this.getDivCorrente().form);
this.getDivCorrente.abrir(this);
};

Modulo.prototype.fecharDiv=function() 
{
this.setFormCorrente(this.pilhaForms.pop());
this.getDivCorrente().fechar();
};

Modulo.prototype.definirLocalizar=function(codigoRecebido,nomeGrid) 
{
this.fecharDiv();

this.definirRegistro(codigoRecebido,this.nomeModulo);
this.retornarPagina(codigoRecebido);

disableMilonicMenu(false);
};

Modulo.prototype.retornarPagina=function(codigoRecebido,nomeGrid) 
{
if (this.isFunction('retornarPagina'+this.nomeModulo) && this.retornarPagina.caller!=this['retornarPagina'+this.nomeModulo]) 
	this['retornarPagina'+this.nomeModulo](codigoRecebido,nomeGrid);	
	else
	{
	var dados="";
	
	var formCorrente=this.getFormCorrente();
	var gridCorrente=null;
	
	if (nomeGrid==undefined)
		gridCorrente=formCorrente.getGridCorrente();
		else
		gridCorrente=formCorrente.getGrid(nomeGrid);
	
	if (this.abas.length)
		dados="comando=retornarPagina"+this.abaCorrente.nome;
		else
		dados="comando=retornarPagina"+this.complementoComando;
	
	dados+="&limite="+this.limite+"&"+gridCorrente.campoChave+"="+codigoRecebido+"&ordenacao="+gridCorrente.ordenacao;
	var funcaoResposta="respostaRetornarPagina"+gridCorrente.nome;
	
	if (!this.isFunction(funcaoResposta))
		funcaoResposta="respostaRetornarPagina";
	
	new Ajax().requisicao(this,this.nivelArquivo+gridCorrente.ajax,dados,'false',funcaoResposta);
	}
};

Modulo.prototype.respostaRetornarPagina=function(objetoResposta) 
{
if (this.isFunction('respostaRetornarPagina'+this.nomeModulo) && this.respostaRetornarPagina.caller!=this['respostaRetornarPagina'+this.nomeModulo]) 
	this['respostaRetornarPagina'+this.nomeModulo](objetoResposta);	
	else
	this.getFormCorrente().getObjetoAtivo().exibirRegistros(objetoResposta.root.diversos.pagina);
};

Modulo.prototype.localizar=function() 
{
if (this.isFunction('localizar'+this.nomeModulo) && this.localizar.caller!=this['localizar'+this.nomeModulo]) 
	this['localizar'+this.nomeModulo]();	
	else
	{	
	var formCorrente=this.getFormCorrente();
	var gridCorrente=formCorrente.getGridCorrente();
	
	if (this.formCorrente.verificarCamposBusca()) 
		{
		this.divCorrente.parametrosPesquisa=formCorrente.montarUrl(".bus.");
		this.exibirRegistros(gridCorrente.nome,1);
		}
	}
};

Modulo.prototype.mostrarTodos=function(objeto) 
{
var formCorrente=this.getFormCorrente();
var gridCorrente=this.formCorrente.getGridCorrente();

if (objeto.checked) 
	{
	this.divCorrente.parametrosPesquisa="";
	formCorrente.cancelar();
	this.exibirRegistros(gridCorrente.nome,1);
	}
};

Modulo.prototype.keyPress=function(evt) 
{
return false;
/*
 * var formCorrente=this.getFormCorrente(); var
 * gridCorrente=this.formCorrente.getGridCorrente();
 * gridCorrente.codigoRegistroDefinido=null;
 */
};

/**
 * Método que verifica a existência de um método no módulo.
 * 
 * @param nomeFuncao  (string) Nome da função a ser verificada
 * 
 * @return (boolean) true se a função existe,false caso contrário.
 */
Modulo.prototype.isFunction=function(nomeFuncao) 
{
return (typeof (this[nomeFuncao])=='function')? true : false;	
};

function Aba(nome) 
{
this.nome=nome;
this.id="aba"+this.nome;
this.idDiv="div"+this.nome;
this.label=jQuery("#"+this.id).text();
this.form=new Form(this.idDiv);
};
