// TABELAS	
function Tipo_Tabelas(quantidade,nome)
{
	this.Quant = quantidade
	this.Nome = nome
	this.Campos = new Tipo_Campos
}

// 	CAMPOS (tipo 1 = sting; tipo 2 = inteiro)
function Tipo_Campos(nome,descricao,tipo,requerido,listagem)
{
	this.Nome = nome
	this.Descricao = descricao
	this.Tipo = tipo
	this.Requerido = requerido
	this.Listagem = listagem
}

//	FUNCOES DE RETORNO DAS TABELAS
function Get_Quantidade(tabela) { return tabela.Quant }
function Get_Nome_Tabela(tabela) { return tabela.Nome }

//	FUNCOES DE RETORNO DOS CAMPOS
function Get_Nome(tabela,x) { return tabela.Campos[x].Nome }
function Get_Descricao(tabela,x) { return tabela.Campos[x].Descricao }
function Get_Tipo(tabela,x) { return tabela.Campos[x].Tipo }
function Get_Requerido(tabela,x) { return tabela.Campos[x].Requerido }
function Get_Listagem(tabela,x) { return tabela.Campos[x].Listagem }


// 	CARREGA UM SELECT (objeto) COM OS CAMPOS QUE PODEM SER VISUALIZADOS NA TELA DE LISTAGEM 
function Carrega_Campos(tabela, objeto){
	posicao=0;
	for (cont=0; cont < Get_Quantidade(tabela); cont++){
		if ( Get_Listagem(tabela,cont)== 1 ){
     		objeto.options[posicao]= new Option(Get_Nome(tabela,cont), Get_Tipo(tabela,cont)+"-"+Get_Descricao(tabela,cont));
			posicao++;
		}
	}
}

// 	CARREGA UM SELECT (objeto) COM OS CAMPOS QUE PODEM SER VISUALIZADOS NA TELA DE LISTAGEM 
function Carrega_Criterios(tabela, objeto, campo){
	if(campo.options.length > 1){
		for(cont=0; cont < campo.options.length; cont++){
			if ( campo.options[cont].selected ){
				tipo= campo.options[cont].value.substring(0, campo.options[cont].value.indexOf('-'));
				Muda_Criterio(objeto, tipo);
			}
		}
	}
	else{
		tipo= campo.value.substring(0, campo.value.indexOf('-'));
		Muda_Criterio(objeto, tipo);
	}
}

//	FUNCAO QUE APENAS RETIRA O TIPO DE VALOR ANTER DE CHAMAR MUDAR_CRITERIO
function Pre_Muda_Criterio(objeto, valor){
	tipo= valor.substring(0, valor.indexOf('-'));
	Muda_Criterio(objeto, tipo);
}

//	FUNCAO QUE MODIFICA O SELECT DE CRITERIOS DE ACORDO COM O TIPO
function Muda_Criterio(objeto, tipo){
	while(objeto.options.length>0) objeto.options[0]=null;
		
	if (tipo=="string"){ 
       	objeto.options[0]=new Option("Contém", 1);
       	objeto.options[1]=new Option("Começa com", 2);
       	objeto.options[2]=new Option("Exata", 3);
	}
	else if (tipo=="int"){ 
       	objeto.options[0]=new Option(">", 1);
       	objeto.options[1]=new Option("<", 2);
       	objeto.options[2]=new Option("=", 3);
	}
}

function Apaga_Select(objeto){
	while(objeto.options.length>0) objeto.options[0]=null;
}

//FUNCAO QUE CONFERE SE TODOS OS CAMPOS OBRIGATÓRIOS DO CADASTRO ESTAO PREENCHIDOS
function Verifica_Obrigatorios(tabela, string_formulario, formulario, pagina, adicional){
	mensagem="Alguns dos campos obrigatórios não foram preenchidos:\n";
/*
	for (cont=0; cont < Get_Quantidade(tabela); cont++){
		if(Get_Requerido(tabela,cont) == 1){
			valor= eval(string_formulario+".in"+Get_Descricao(tabela,cont)+".value");
			if(valor == "")
				mensagem += "   - " + Get_Nome(tabela,cont) + "\n";
		}
	}
	*/
	if(mensagem.length > 54)
		alert(mensagem);
	else
		Movimentacao(formulario, pagina, adicional);
}

//	FUNCAO QUE MUDA O SRC DE UMA TAG IMG(OBJETO)
function Muda_Imagem(objeto, nova_imagem){
		objeto.src=nova_imagem;
}

// MUDA A COR DO OBJETO (objeto) ONFOCUS E ONBLUR
function MudaCor(objeto, bg) {
  if (objeto.style) 
		objeto.style.backgroundColor = bg;
}
function Focu(objeto) {
  var bgcor = "#ffffff";
  MudaCor(objeto, bgcor);
}
function Sair(objeto) {
  var bgcor =  "#EFEFEF";
  MudaCor(objeto, bgcor);
}
function SairListagem(objeto,cor) {
  MudaCor(objeto, cor);
}

//	FUNCAO QUE CHAMA O ARQUIVO DE MOVIMENTACAO
function Movimentacao(formulario, pagina, adicional){
	if(formulario!=""){
		formulario.action= pagina+adicional;
		formulario.submit();
	}
	else
		window.location=pagina+adicional;
}

//	FUNCAO QUE FILTRA PELA INICIAL DE UMA PAGINA DE LISTAGEM
function Filtra_Inicial(inicial, pagina, adicional){
	window.location= pagina+"?Inicial="+inicial+adicional;
}

//	FUNCAO QUE ATIVA O BOTAO SEM FLTRO DO FILTRO NAS TELAS DE LISTAGEM
function Sem_Filtro(formulario, pagina, adicional){
	formulario.inFiltroValor.value="";
	formulario.action= pagina+"?SemFiltro=1"+adicional;
	formulario.submit();
}

//	FUNCAO QUE ATIVA O BOTAO DE FLTRO DO FILTRO NAS TELAS DE LISTAGEM
function Filtrar(formulario, pagina, adicional){
	formulario.action= pagina + adicional;
	formulario.submit();
}

// SELECIONA UM SELECT (lista) DE ACORDO COM UM VALOR (valor)
function Seleciona_Option(valor,lista){
// Verifica se o valor e um numero
	cont=0
	achou=false
// Verifica se existe algum option com o codigo desejado
	for(cont=0;cont<lista.options.length;cont++)
	{	
		if (valor==lista.options[cont].value)
		{
			achou=true
			break
		}
	}
// Se Existe seleciona, caso contrario limpa o select
	if (achou)
		lista.options[cont].selected=true 
	else 
		lista.options[0].selected=true 
}

//FUNCAO QUE ABRE UMA NOVA JANELA DE ACORDO OS PARAMETROS PASSADOS
function Pesquisa(pagina, largura, altura, dis_left, dis_top){
	window.open(pagina,"","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=no,width="+largura+",height="+altura+",top="+dis_top+",left="+dis_left);
}

//FUNCAO QUE CHAMA A VISUALIZACAO DO REGISTRO DA LISTAGEM
function Visualizar(pagina, registro, adicional){
	window.location= pagina+"?Registro="+registro+adicional;
}

