//ALTERAÇÃO DA COR DOS BOTÕES DO MENU
function f_botao( p_situacao, p_objeto )
{
	if ( p_situacao == 'on')
		p_objeto.src = p_objeto.src.replace(/off/, "on");
	if ( p_situacao == 'off')
		p_objeto.src = p_objeto.src.replace(/on/, "off");
}

//CLICK BOTÕES DO MENU
function f_botao_click( p_link )
{
	if (p_link == 'voltar')
		window.history.back();
	else if (p_link == 'imprimir')
		window.print();
	else
	document.location.href = p_link;
}

//CLICK BOTÕES DO MENU
function f_legenda( p_legenda )
{
	document.getElementById("span_legenda").innerHTML = p_legenda;
}

//TRIM
function f_trim( str )
{
	var i = 0;
	var j = str.length;
	while(str.charCodeAt(i) == 32)
		i++;
	while(str.charCodeAt(j - 1) == 32)
		j--;
	if(i >= j)
		return("");
	return(str.substring(i, j));
}

//FORMATAR DATA onKeyPress="return f_formatar_data(event,this);"
function f_formatar_data(evento, Campo){

	var charCode = (navigator.appName == "Netscape") ? evento.which : evento.keyCode;
	
	if (!(charCode == 13 || charCode == 8 || charCode == 0 || charCode == 47 || (charCode>=48 && charCode<=57)))
		return false;

	if(charCode != 0 && charCode != 47) {
		if(Campo.value.length == 2 || Campo.value.length == 5){
			Campo.value += "/" ;
		}
	}

	return true;
}	

function f_validar_data( p_campo )
{
	var reDate = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/; //dd/mm/aaaa
	eval("reDate = reDate");
	if (!reDate.test(p_campo.value) && (p_campo.value != null && p_campo.value != "")) {
		window.alert("Data inválida.");
		p_campo.focus();
		return false;
	}
}


//VALIDA ENTRE DATAS
function f_validar_entre_datas( pObjetoDataInicial, pObjetoDataFinal )
{
	pObjetoDataInicial.value
	pObjetoDataFinal.value

	var dataini = new Date( pObjetoDataInicial.value.substring(6,10), pObjetoDataInicial.value.substring(3,5)-1, pObjetoDataInicial.value.substring(0,2) );
	var datafim = new Date( pObjetoDataFinal.value.substring(6,10), pObjetoDataFinal.value.substring(3,5)-1, pObjetoDataFinal.value.substring(0,2) );

	if (dataini > datafim)
	{
		window.alert("Data inicial não pode ser maior que data final.");
		pObjetoDataInicial.focus();
		return false;
	}
}

//TRANSFORMA EM MAIUSCULO
function f_texto_upper( pObjeto )
{
	var strNome = new String(pObjeto.value);
    pObjeto.value = strNome.toUpperCase();
    return true;
}

//PERMITE SOMENTE NUNEROS
function f_texto_numero(evento, Campo)
{
	var charCode = (navigator.appName == "Netscape") ? evento.which : evento.keyCode;
	if (!(charCode == 13 || charCode == 8 || charCode == 0 || charCode == 47 || (charCode>=48 && charCode<=57)))
		return false;
	return true;
}	

//VALIDA NUNEROS
function f_valida_texto_numero( p_campo )
{
	var reDigits = /^\d+$/;
	if (!reDigits.test(p_campo.value) && (p_campo.value != null && p_campo.value != ""))
	{
		p_campo.value = '';
		p_campo.focus();
	}
}


//MAXLENGTH DO TEXTAREA
function f_maxlength_textarea(textarea,div_indicador,tamanho)
{
	if(textarea.value.length > tamanho)
	{
		textarea.value = textarea.value.substring(0,tamanho-1);
		return false;
	}
	document.getElementById(div_indicador).innerHTML = tamanho - textarea.value.length;		
	return true;
}		

//VALIDAR E-MAIL
function f_validar_email( p_campo )
{
	var reEmail = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	eval("reEmail = reEmail");
	if (!reEmail.test(p_campo.value) && (p_campo.value != null && p_campo.value != ""))
	{
		window.alert("E-mail inválido.");
		p_campo.focus();
		return false;
	}
}

//EXIBIR E ESCONDER CONTEUDO DE TABELA
//p_total_tr => total de tr que contem na página
//p_total_tr = 0 => não abre somente uma por vez
function f_tabela_retratil( p_id, p_total_tr )
{
	//esconde todos
	for(ids = 1; ids <= p_total_tr; ids++) 
	{
		if ( p_id != ids )
			document.getElementById("tr_" + ids).style.display = 'none';
	}
	
	if ( document.getElementById("tr_" + p_id).style.display == 'none' )
		document.getElementById("tr_" + p_id).style.display = 'inline';
	else
		document.getElementById("tr_" + p_id).style.display = 'none';
}

//ALTERA A COR DA LINHA QUANDO PASSAR MOUSE EM CIMA
function f_grid_selecionar( p_estilo, p_tr )
{
	if ( p_tr.className == p_estilo ){
		p_tr.className = p_tr.value;
	}
	else{
		p_tr.value = p_tr.className;
		p_tr.className = p_estilo;
	}
}

//MENHU FLUANTE (ACOMPANHAR SCROLL
function f_div_menu_flutuante(iX,iY,id,alturaInicial)
{
		var L=dD.getElementById?dD.getElementById(id):dD.all?dD.all[id]:dD.layers[id];

		this[id+'O']=L;

		if(dD.layers)
			L.style=L;

		L.nX=L.iX=iX;
		L.nY=L.iY=iY;

		L.P=function(x,y){this.style.left=x+px;
						  this.style.top=y+px;};

		L.Fm=function(){var pX, pY;

						pX=(this.iX >=0)?0:nn?innerWidth:nn&&dH.clientWidth?dH.clientWidth:dB.clientWidth;
						pY=nn?pageYOffset:nn&&dH.scrollTop?dH.scrollTop:dB.scrollTop;

						if (navigator.appName == "Netscape") 
						{
							if(self.pageYOffset < alturaInicial)
								pY = alturaInicial;
						}
						else 
						{ 
							if(window.document.body.scrollTop < alturaInicial)
								pY = alturaInicial;
						}
						if(this.iY<0)
							pY+=nn?innerHeight:nn&&dH.clientHeight?dH.clientHeight:dB.clientHeight;

						if( document.getElementById("f_div_menu_flutuante").offsetTop > (alturaInicial-2) )
							document.getElementById("f_div_menu_flutuante").style.visibility = 'visible';
						else
							document.getElementById("f_div_menu_flutuante").style.visibility = 'hidden';

						this.nX+=.1*(pX+this.iX-this.nX);
						this.nY+=.1*(pY+this.iY-this.nY);
						this.P(this.nX,this.nY);

						setTimeout(this.id+'O.Fm()',0);
						};
		return L;
}

//ABRIR JANELA
function f_abre_janela( p_url, p_parameter, p_name, x, y)
{
  var showpop;
  
  var LeftPosition;
  var TopPosition;
  
  LeftPosition = (screen.width) ? (screen.width-x)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-y)/2 : 0;
  
  newwindow = window.open(p_url + '?' + p_parameter,p_name,'top='+ TopPosition +',left='+ LeftPosition +'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,menubar=no,width='+ x +',height='+ y );
  
  if (window.focus) {newwindow.focus()}

}

//EXECUTA O AJAX COMO REQUEST
function f_AjaxRequest(method_, id_, url_, pars_, after_, msg_)
{
	$(id_).innerHTML = msg_;
	
	new Ajax.Request(url_, 
					 {
						method: method_,
						parameters: pars_,
						onSuccess: function(ajax_) 
						{
							$(id_).update(ajax_.responseText);
							if (after_ != "")
								eval(after_);
						},
						onFailure: function(ajax_)
						{
							alert('Um erro ocorreu ao tentar efetuar esta operação.');
							if (aopDebug)
								$(id_).update(ajax_.responseText);
						}
					 });
}
