/*
		MINHAS FUNÇÕES JAVASCRIPT

AUTOR: ROBERT DE AZEVEDO LIMA
E-MAIL: ROBERTDEAZEVEDO@YAHOO.COM.BR
*/

/* POP UPS */
function popUrl(url,largura,altura,titulo){
	var janela
	
		janela = window.open(url,'winPopUrl','width = '+ largura +', height = '+ altura);
		janela.document.topMargin = 0;
		janela.document.leftMargin = 0;
		janela.title = titulo;
}

/*================================> CATEGORIA CONTROLES DISPLAY <================================*/

//	MENU DINÂMICO BÁSICO
/*
DESCRIÇÃO:
	Função na qual deverá ser passado o ID do elemento que se deseja esconde e mostrar.
	A Função Identifica se o Objeto está NONE ou INLINE Automáticamente.
	Ao Clicar 1 vez o menu aparece(se estiver oculto) e se clicar novamente oculta.

	PS: Compatível com Netscape e IE.
*/

function mostraMenu(id)
{
	var menu = eval(document.getElementById(id));
	if(menu.style.display == 'none')
	{
		if(navigator.appName.indexOf("etscape") != -1)
		{
			menu.style.display = 'table-cell';
		}
		else
		{
			menu.style.display = 'inline';		
		}
	}
	else
	{
		menu.style.display = 'none';
	}
}
//////////////////////////
/* SUB-MENUS DINÂMICOS
	Ativa e esconde os sub-menus, recebidos pelo parametro idMenu 
VALORES DE ENTRADA:
	idMenu 	-> ID dos menus(TRS) a serem exibidos ou escondidos, separados por ","
COMPATÍVEL:
	ie,netscape,mozilla,m.firefox; 	
AUTHOR:
	Robert de Azevedo, Equipe C10;
	robertdeazevedo@yahoo.com.br;	
*/		
function atvMenu(idMenus)
{
	var mn	= idMenus.split(",");
	var x;

	for(x=0;x<=mn.length-1;x++)
	{
		mn[x]	= eval("document.getElementById('"+ mn[x] +"')");
		if(mn[x].style.display == 'none')
		{
			if(navigator.appName.indexOf("icrosoft") != -1)
			{
				mn[x].style.display = 'inline';
			}
			else
			{
				mn[x].style.display = 'table-row';
			}
		}
		else
		{
			mn[x].style.display = 'none';
		}
	}
}


/*================================> FORMULARIOS <================================*/
/*//////////// FORMULÁRIO INTELIGENTE
	A FUNÇÃO ANALISA UMA PROPRIEDADE QUE DEV SER INSERIDA NAS TAGS HTML( VALID ) // <input type='text' valid='V'>
	VALID	= "N"
		VALIDA SE EXISTEM DADOS PREENCHIDOS E SE OS DADOS INSERIDOS NO CAMPO SÃO SOMENTE NÚMEROS, PONTOS OU VIRGULAS
	
	VALID	= "V"
		VALIDA SE O CAMPO FOI PREENCHIDO OU NÃO
	
	VALID = "" OU SIMPLESMENTE NÃO ACHAR A TAG
		NÃO FAZ VALIDAÇÃO
		

	VALORES DE ENTRADA
	FRM	-> NAME DO FORM A SER ANALISADO
	
	OBS:
		IDENTIFICA AUTOMATICAMENTE SE O NOME DO CAMPO É "email" E FAZ A VERIFICAÇÃO DE EMAIL.
		CASO A VERIFICAÇÃO ESTEJA OK RETORNA TRUE
		CASO CONTRARIO EXIBE MENSAGEM APROPRIADA, DA O FOCU NO CAMPO ONDE OCORREU O ERRO, E RETORNA FALSE
			
	
*/
function vldForm(frm)
{
	var form	= eval('document.'+ frm);
	var qtdCampos	= form.length;
	var x;
	
	for(x=0;x<=qtdCampos-1;x++)
	{
		if(form.elements[x].attributes['valid'] != null)
		{
			if(form.elements[x].attributes['valid'].value == 'n' || form.elements[x].attributes['valid'].value == 'N')
			{
				if(isNaN(form.elements[x].value))
				{
					alert('Por favor preencha os campos corretamente *');
					form.elements[x].focus();
					return(false);
				}
			}
			else
			{
				if(form.elements[x].name.toLowerCase().indexOf("email") != -1)
				{
					if(
						form.elements[x].value.indexOf("@") <= 0  ||
						form.elements[x].value.indexOf(".") == -1 ||
						form.elements[x].value.length <= 5
					)
					{
						alert('E-mail Inválido *');
						form.elements[x].focus();
						return(false)
					}
				}
				else if(form.elements[x].value == '' || form.elements[x].value == ' ')
				{
					alert('Por favor preencha os campos obrigatórios *');
					form.elements[x].focus();
					return(false);
				}
			}
		}
	}
	
	return(true);
}

/*
FUNÇÃO MONTA COMBO

PARAMETROS
	INICIO		-> Número inicial da comboBox
	FIM			-> Número final da comboBox
	NOME		-> Nome do campo
	COMPLEMENTO	-> Escrve o complemente como código
*/
function montaCombo(inicio,fim,nome,complemento)
{
	var x, retorno;
	inicio	= parseInt(inicio);
	fim		= parseInt(fim);

	retorno = "<select name='"+ nome +"' id='"+ nome +"' "+ complemento +">";
		if(fim >= inicio)
		{
			for(x=inicio;x<=fim;x++)
			{
				retorno += "<option value='"+ x +"'>"+ x +"</option>";
			}
		}
		else
		{
			for(x=inicio;x<=fim;x--)
			{
				retorno += "<option value='"+ x +"'>"+ x +"</option>";
			}
		}
	retorno += "</select>";
	return(retorno);
}
// MONTA COMBO PARA ENTRADA DE DATAS
function comboData(nomeCampo,complemento,sDia,sMes,sAno){
	
	var anoInicial, anoAtual, data, dataAtual, anosMais, marcar, x, y, nomeCampo, Sdia, Smes, Sano, edicao, retorno;

			data = new Date();
			anoInicial = data.getFullYear()-5;
			dataAtual = data.getDate() +'/'+ data.getMonth() +'/'+ data.getFullYear();
			anoAtual = data.getFullYear();
			anosMais = parseInt(anoAtual);
			edicao = false;
			retorno	= "";

				if(sDia == undefined && sDia == undefined && sDia == undefined)
				{
					sDia	= data.getDate();
					sMes	= data.getMonth();
					sAno	= data.getFullYear();
				}
				else
				{
				    if(sDia.length < 2) sDia = '0'+ sDia;
				    if(sMes.length < 2) sMes = '0'+ sMes;
				}
			//==================== DIA ====================>
			for(x=1;x<=31;x++){// Escrevendo o Combo com os Dias
				if(x == 1){
					retorno += "<select id='"+ nomeCampo +"cbDia' onChange=\"document.getElementById('"+ nomeCampo +"').value = document.getElementById('"+ nomeCampo +"cbDia').value +'/'+ document.getElementById('"+ nomeCampo +"cbMes').value +'/'+ document.getElementById('"+ nomeCampo +"cbAno').value;\" style='width:40px' "+ complemento +">";
				}
				if(x <= 9){
					y = "0"+ x;
				}
				else{
					y = x;
				}
					if(sDia == y){
						marcar = " selected";
					}
					else{
						marcar = "";
					}
				retorno += "<option value='"+ y +"'"+ marcar +">"+ y +"</option>";
				if(x == 31){
					retorno += "</select> / ";
				}
			}
			//==================== MES ====================>
				for(x=1;x<=12;x++){//Escrevendo o Combo com os Meses;
					if(x == 1){
						retorno += "<select id='"+ nomeCampo +"cbMes' onChange=\"document.getElementById('"+ nomeCampo +"').value = document.getElementById('"+ nomeCampo +"cbDia').value +'/'+ document.getElementById('"+ nomeCampo +"cbMes').value +'/'+ document.getElementById('"+ nomeCampo +"cbAno').value;\" style='width:40px' "+ complemento +">";
					}
					if(x <= 9){
						y = "0"+ x;
					}
					else{
						y = x;
					}
					
						if(sMes == y){
							marcar = " selected";
						}
						else{
							marcar = "";
						}
					retorno += "<option value='"+ y +"'"+ marcar +">"+ y +"</option>";
					if(x == 12){
						retorno += "</select> / ";
					}				
				}
			//==================== ANO ====================>
			for(x=anoInicial;x<=anosMais;x++){
				if(x == anoInicial){
					retorno += "<select id='"+ nomeCampo +"cbAno' onChange=\"document.getElementById('"+ nomeCampo +"').value = document.getElementById('"+ nomeCampo +"cbDia').value +'/'+ document.getElementById('"+ nomeCampo +"cbMes').value +'/'+ document.getElementById('"+ nomeCampo +"cbAno').value;\" style='width:60px' "+ complemento +">";
				}

					if(sAno == x){
						marcar = " selected";
						edicao = true;
					}
					else{
						if(x == anoAtual && edicao == false){
							marcar = " selected";
						}
						else{
							marcar = "";
						}
					}

				retorno += "<option value='"+ x +"'"+ marcar +">"+ x +"</option>";
				if(x == anosMais){
					retorno += "</select>";
				}				
			}
		//================== HIDDEN COM  O VALOR DA DATA ======================>
		if(("" + Sdia).length < 1 || ("" + Smes).length < 1 || ("" + Sano).length < 1){
			retorno += " <input type='hidden' name='"+ nomeCampo +"' id='"+ nomeCampo +"' value='"+ dataAtual +"'>";
		}
		else{
			retorno += " <input type='hidden' name='"+ nomeCampo +"' id='"+ nomeCampo +"' value='"+ sDia +"/"+ sMes +"/"+ sAno +"'>";
		}
		return(retorno);
}

//FUNÇÃO QUE APENAS DEIXA DIGITAR NÚMEROS PONTOS E VÍRGULAS
function somenteNumeros() // PARA EVENTO OnKeyDown
{
	var Tecla = window.event.keyCode;
if (!(	(Tecla > 95 && Tecla < 106) || 
	(Tecla > 44 && Tecla < 58) 		|| 
		(Tecla > 32 && Tecla < 41)  ||
			(
			Tecla == 17  ||
			Tecla == 16  ||
			Tecla == 8   || 
			Tecla == 9   || 
			Tecla == 13  ||
			Tecla == 144 ||
			Tecla == 188 ||
			Tecla == 190 ||
			Tecla == 110 ||
			Tecla == 194 
			)
		)
	)
{window.event.returnValue = false;}
}
