﻿var cor_primaria  = '#FFF5D0';

function $$(p_div_id) {
 if(document.layers) 		
    return document.layers[p_div_id]; 
 if(document.getElementById) 
 	return document.getElementById(p_div_id);
 if(document.all) 
 	return document.all[p_div_id]; 
 if(document[p_div_id]) 
    return document[p_div_id]; 

  return ( false );
}
/* Biblioteca : utilLib.js
	Autor : Jefferson Petilo( jpetilo@riabrazil.com.br )
*/
// Gerenciador de eventos
function addEvent(element, eventType, lamdaFunction, useCapture) {
  if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}
// Recupera a p�gina corrente
function getCurrentPage() {
 var caminho = window.location.pathname;
 var pagina  = caminho.substring( caminho.lastIndexOf('/') + 1 );
 return ( pagina );
}
// Atualiza a p�gina pai
function fnc_reload() {
 try {
   opener.location.reload();
 } catch ( e ) { 
 	return ( false );  
 } 
}
// Atualiza a p�gina pai e fecha filha
function fnc_fecha_dialog() {
 try {	
  	var janela = ( window.opener )?window.opener:window.dialogArguments; 
    janela.location.reload();
	window.close();
 } catch ( e ) { 
 	return ( false );  
 } 
}
// Recupera a refer�ncia do div( cross-browser )
function fnc_referencia_div( p_div_id ) {
  //Netscape layers	
  if( document.layers ) 		return document.layers[ p_div_id ]; 
  //DOM; IE5, NS6, Mozilla, Opera
  if( document.getElementById ) return document.getElementById( p_div_id );
  //Proprietary DOM; IE4
  if( document.all ) return document.all[ p_div_id ]; 
  //Netscape alternative
  if( document[p_div_id] ) return document[p_div_id]; 

  return ( false );
}
// oculta o div
function fnc_mostra_div( p_div_id ) {
  if( !fnc_referencia_div( p_div_id ) ) return;
  
  fnc_referencia_div( p_div_id ).style.display = '';
}
// mostra o div
function fnc_oculta_div( p_div_id ) {
  if( !fnc_referencia_div( p_div_id ) ) return;	
  fnc_referencia_div( p_div_id ).style.display = 'none';
}

function initCap( str ) { 
 var str = str.substring(0,1).toUpperCase() + str.substring(1,str.length).toLowerCase(); 
 return str; 
} 
function getAbsolutePos(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

// Centraliza o div na p�gina
function fnc_centro( p_janela ) {				
  var jsdlg = 215;		
  var w,h;
  var left, top;
  
  // se o banner n�o estiver dispon�vel
  if( !fnc_referencia_div( p_janela ) ) return; 
  
  if (self.innerHeight) { // all except Explorer
	w = self.innerWidth;
	h = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
   // Explorer 6 Strict Mode
	w = document.documentElement.clientWidth;
	h = document.documentElement.clientHeight;
  } else if (document.body) {
  	 // other Explorers
	w = document.body.clientWidth;
	h = document.body.clientHeight;
  }
   fnc_referencia_div( p_janela ).style.left = left = (w/2)-(415/2);
   fnc_referencia_div( p_janela ).style.top  = top  = (h/2)-((jsdlg+85)/2);
}

// Altera a cor da linha
function swapBG( el, BG1, BG2 ) {
  el.style.backgroundColor = ( el.style.backgroundColor == BG1 ) ? BG2 : BG1;
}

// mostra coment�rio de ajuda
function showAjuda( div, titulo, descricao ) {
 
 if( !fnc_referencia_div( div )) return;
 
 var ajuda 					= fnc_referencia_div( div ).style;
	 ajuda.display 			= 'inline';
  	 ajuda.position 		= 'absolute';
  	 ajuda.width 			= '170';
  	 ajuda.backgroundColor 	= 'lightyellow';
  	 ajuda.border 			= 'dashed 1px black';
  	 ajuda.padding 			= '10px';
  	 ajuda.innerHTML 		= '<b>' + titulo + '</b><br><img src=/_imagens/graficos/spacer.gif width="1" height="5"><br><div style="padding-left:10; padding-right:5">' + descricao + '</div>';
}
// oculta o coment�rio de ajuda
function hideAJuda( div ) {
  if( !fnc_referencia_div( div )) return;
  fnc_referencia_div( div ).style.display = 'none';
}

function fnc_mostrar_tooltip(p_objeto, p_texto, p_alinhamento, p_cor_fundo) {
 if( !p_texto ) return; // se o hint n�o estiver dispon�vel n�o executa o tooltip
 
 var deslocamento;
 var size		= ( !p_objeto.style.width.split('px')[0] )?p_objeto.size * 4:p_objeto.style.width.split('px')[0];;
 var posicao    = getAbsolutePos( p_objeto );
 var tip    	= document.createElement('div');
	 tip.id 	= 'tip';
	 tip.style.backgroundColor 	 = '#ffffcc';
	 tip.style.color 	 		 = '#000000';
	 tip.style.border 		 	 = 'solid black 1px';    
	 tip.style.fontFamily 	 	 = 'Geneva';
	 tip.style.fontSize 		 = '10px';
	 tip.style.zIndex			 = '551';
	 tip.style.display			 = 'none';
	 tip.style.width			 = eval( p_texto.length * 5.2) + "px";
	 tip.style.height			 = ( p_texto.length > 100)?40  + "px":20+ "px";
	 tip.style.position			 = 'absolute';
	 tip.innerHTML 				 = String.fromCharCode(32,32) + p_texto;
	 
	 
 switch ( p_alinhamento.toLowerCase() ){ 
	case "top" : 
		tip.style.top  = eval(posicao.y - tip.offsetHeight) + "px";
		tip.style.left = posicao.x + "px"; 
	break;
	case "left" :
	    deslocamento = parseInt( posicao.x - tip.offsetWidth) + parseInt( size ) + 35;
		tip.style.top  = posicao.y + "px";
		tip.style.left = deslocamento + "px";
	break;
	case "bottom" :
		tip.style.top  = eval(posicao.y + tip.offsetHeight) + "px";
		tip.style.left =  posicao.x + "px";
	break;
	case "right" :
		tip.style.top  = posicao.y + "px";
		tip.style.left = eval( posicao.x + tip.offsetWidth ) + "px";
	break;
 }
 document.body.appendChild( tip );
  tip.style.display ='';
}

function fnc_ocultar_tooltip(){ 
  if(!fnc_referencia_div('tip')) return;
  document.body.removeChild(  fnc_referencia_div('tip') );
}
// Alterna a cor de fundo do elemento
function setBackgroundColor( e ) {
  var target;
  if (window.event) { // IE5.5 and below
    target = window.event.srcElement;
  } else if (e) {     // N4+ && DOM compliant
    target = e.target;
  } else {
    target = "unsupported";
  }
  if( target.style ) {
    target.style.backgroundColor = cor_primaria;
	fnc_mostrar_tooltip( target ,target.getAttribute('hint'),'left', target.style.backgroundColor );
  }
}
// Limpa a cor de fundo do elemento
function clearBackgroundColor( e ) {
  var target;
  if (window.event) { // IE5.5 and below
    target = window.event.srcElement;
  } else if (e) {     // N4+ && DOM compliant
    target = e.target;
  } else {
    target = "unsupported";
  }
  
  if( target.style ) {
    target.style.backgroundColor = '';
	fnc_ocultar_tooltip();
  }
}

function fnc_mudar_fonte( p_objeto, p_tipo ) {
 var tamanho = 13;

if ( p_tipo == 'mais')  {
  	if(tamanho < 19) tamanho +=2;
 } else {
	if(tamanho > 6 ) tamanho-=2;
 }
 
 fnc_referencia_div( p_objeto ).style.fontSize = tamanho + 'px';
}

function fnc_contar_caracteres( p_campo, p_contador, p_max_char,p_nao_inserir_texto ) {
  
  if( p_campo.value.length > p_max_char ) {
  	  p_campo.value     = p_campo.value.substr(0, p_max_char);
	  p_contador.value = p_max_char - p_campo.value.length; 
 } else 
	  p_contador.value = p_max_char - p_campo.value.length;

 if (p_nao_inserir_texto == null ){
 	p_nao_inserir_texto=false;
 }
 var posicao    				  = getAbsolutePos( p_contador );
 var texto  					  = (p_nao_inserir_texto ? "" : 'Caracteres restantes');
 var comentario 				  = document.createElement('span');
 	 comentario.id				  = p_campo.name;
	 comentario.style.fontStyle	  = 'normal';
	 comentario.style.letterSpacing = '1px';
	 comentario.style.fontFamily  = 'Arial';
	 comentario.style.fontSize 	  = '10px';
	 comentario.style.zIndex	  = '551';
	 comentario.style.position	  = 'absolute';
	 comentario.style.color		  = 'black';
	 
	 comentario.innerHTML 		  = String.fromCharCode(32,32) + texto;
	 comentario.style.top  		  = eval( posicao.y + 2 ) + "px";
	 comentario.style.left 		  = eval( 37 + posicao.x + comentario.offsetWidth ) + "px";
	
	if( p_contador.value ==  p_max_char ) document.body.appendChild( comentario );
}

function bloqueiaLetras(){
	var codTecla = parseInt(window.event.keyCode);
	var tecla = String.fromCharCode(codTecla);
	
	if (!(codTecla >= 48 && codTecla <= 57 ) && ( !(codTecla == 44 ) && !(codTecla == 46 ) ) ) {
		window.event.keyCode=0;
	}  
}

function getFCKEditorContent( p_fck_instancia ){
	var oEditor = FCKeditorAPI.GetInstance( p_fck_instancia ) ;
	var oDOM    = oEditor.EditorDocument ;
	var content = oDOM.body.innerHTML;

  return( content );
}	


function setBGColor( e ) {
  var target;
  if (window.event) { // IE5.5 and below
    target = window.event.srcElement;
  } else if (e) {     // N4+ && DOM compliant
    target = e.target;
  } else {
    target = "unsupported";
  }
  if( target.style ) {
    target.style.backgroundColor = cor_primaria;
   // $showTip(target ,target.getAttribute('comentario'),'left', cor_primaria );
	
  }
}
// Limpa a cor de fundo do elemento
function clearBGColor( e ) {
  var target;
  if (window.event) { // IE5.5 and below
    target = window.event.srcElement;
  } else if (e) {     // N4+ && DOM compliant
    target = e.target;
  } else {
    target = "unsupported";
  }
  
  if( target.style ) {
    target.style.backgroundColor = '';
   // $hideTip();	
	
  }
} 


function $show( p_id ) {
  if( !$$( p_id ) ) return;

  // visibility
  if( $$( p_id ).style.visibility.length ){ 
	  $$( p_id ).style.visibility = 'visible';
 } else { 
  // display
   if(  $$( p_id ).style.display =='block' ){ 
	   $hide(p_id);
	  return;
   }
   $$( p_id ).style.display = 'block';
 }
}

function $hide( p_id ) {
  if( !$$( p_id ) ) return;	
  // visibility
  if( $$( p_id ).style.visibility.length ){ 
	  $$( p_id ).style.visibility = 'hidden';
  } else {
  // display
	  $$( p_id ).style.display    = 'none';
  }
}

function $showTip(p_objeto, p_texto, p_alinhamento, p_cor_fundo) {
 if( !p_texto ) return; 
 
 var deslocamento;
 var size		= ( !p_objeto.style.width.split('px')[0] )?p_objeto.size * 4:p_objeto.style.width.split('px')[0];;
 var posicao    = getAbsolutePos( p_objeto );
 var tip    	= document.createElement('div');
	 tip.id 	= 'tip';
	 tip.style.backgroundColor 	 = '#D3DCD9';
	 tip.style.color 	 		 = '#000000';
	 tip.style.border 		 	 = 'solid black 1px';    
	 tip.style.fontFamily 	 	 = 'Geneva';
	 tip.style.fontSize 		 = '10px';
	 tip.style.zIndex			 = '551';
	 tip.style.display			 = 'none';
	 tip.style.width			 = eval( p_texto.length * 5.2) + "px";
	 tip.style.height			 = ( p_texto.length > 100)?40  + "px":20+ "px";
	 tip.style.position			 = 'absolute';
	 tip.innerHTML 				 = String.fromCharCode(32,32) + p_texto;
	 
	 
 switch ( p_alinhamento.toLowerCase() ){ 
	case "top" : 
		tip.style.top  = eval(posicao.y - tip.offsetHeight) + "px";
		tip.style.left = posicao.x + "px"; 
	break;
	case "left" :
	    deslocamento = parseInt( posicao.x - tip.offsetWidth) + parseInt( size ) + 75;
		tip.style.top  = posicao.y + "px";
		tip.style.left = deslocamento + "px";
	break;
	case "bottom" :
		tip.style.top  = eval(posicao.y + tip.offsetHeight) + "px";
		tip.style.left =  posicao.x + "px";
	break;
	case "right" :
		tip.style.top  = posicao.y + "px";
		tip.style.left = eval( posicao.x + tip.offsetWidth ) + "px";
	break;
 }
 document.body.appendChild( tip );
  $show('tip');
}

function $hideTip(){ 
  if(!$$('tip')) return;
  document.body.removeChild( $$('tip'));
}
function fnc_change_parent_background(obj,bgcolor) {
var tr=obj.parentNode.parentNode;
	tr.style.backgroundColor=(obj.checked)? 'pink' : 'white';
}
function Lpad( p_numero ){ 	
  return (( p_numero <= 9) ? ('0' + p_numero) : p_numero ) 
}	

function limitChar( string , count ){
 var novo_texto  = ( string.length >= count )? string.substr( string, count ) + '...' : string;
		
 return ( novo_texto );
}
function swapBG( el, BG1, BG2 ) {
try{  el.style.backgroundColor = ( el.style.backgroundColor == BG1 ) ? BG2 : BG1; } catch(e){ }
}

function isYear(anoCorrente){
 var numAnosIntervalo = 150;
 var anoAtual 	 	  = Number( new Date().getFullYear() );
 var anoCorrente 	  = Number(anoCorrente);
 
 return ( anoCorrente > anoAtual - numAnosIntervalo && anoCorrente < anoAtual + numAnosIntervalo  );
}
function fnc_compara_data( data1, data2 ) {
 var v_ano1 = data1.getYear();
 var v_ano2 = data2.getYear();
 var v_mes1 = data1.getMonth();
 var v_mes2 = data2.getMonth();
 var v_dia1	= data1.getDate();
 var v_dia2	= data2.getDate();

	// verifica se os anos são diferentes
	if ( v_ano1 > v_ano2 ) { return -1; }
	if ( v_ano2 > v_ano1 ) { return  1; }
	// verifica se os meses são diferentes
	if ( v_mes1 > v_mes2 )  { return -1; }
	if ( v_mes2 > v_mes1 )  { return  1; }
	// verifica se os dias são diferentes
	if ( v_dia1 > v_dia2 )  { return -1; }
	if ( v_dia2 > v_dia1 )  { return  1; }

	// se a data for igual
	return ( 0 );
 }

DateFromString = function( a ) {
	var p_data  = a.split( '/' );
	
	for ( var i = 0 ; i < p_data.length ; i++ ) p_data[ i ] =  p_data[ i ] ;

	return( new Date( p_data[ 2 ] , p_data[ 1 ] - 1 , p_data[ 0 ] ) );
}

DataHoraFromString = function( a , b ) {
	var p_data  = a.split( '/' );
	var p_hora  = b.split( ':' );
	
	for ( var i = 0 ; i < p_data.length ; i++ ) p_data[ i ] =  p_data[ i ] ;
	for ( var i = 0 ; i < p_hora.length ; i++ ) p_hora[ i ] =  p_hora[ i ] ;

	return( new Date( p_data[ 2 ] , p_data[ 1 ] - 1 , p_data[ 0 ] , p_hora[ 0 ] , p_hora[ 1 ] ) );
}

// onblur="CopiarValor('f_cd_area','f_area')"
function CopiarValor( p_campo_origem, p_campo_destino ){
  var frm		   = form('principal');
  var campo_origem = frm.elements[ p_campo_origem ];
  var campo_destino= frm.elements[ p_campo_destino ];

 if(campo_origem.value.trim().length > 0 && campo_destino.value.trim().length == 0 ){ 
   campo_destino.value = campo_origem.value;
 } 
return false;
}

function getRadioSelecionado( p_grupo ) {
  if(typeof p_grupo == 'string') p_grupo = eval('document.principal.'+p_grupo);

  if( p_grupo[ 0 ] ) { 
	for(var i = 0; i < p_grupo.length; i++ )
		if( p_grupo[ i ].checked ) return ( i ); // returna posição do elemento do array que est' + String.fromCharCode( 224 ) + ' selecionado
  } else 
	 if( p_grupo.checked ) return ( 0 );  // retorna posição do primeiro elemento
		return ( -1 ); // retorna que não existe elemento selecionado
}

// getRadioSelecionadoValor(f_radio_grupo)

// getCheckboxValor('f_check_grupo')
function getCheckboxValor( p_grupo ) {
 var lista_valores = '0'; 
 var grupo   	   = ( typeof p_grupo != 'string')?p_grupo:form('principal').elements[ p_grupo ];

 if(typeof grupo == 'undefined') return lista_valores;

 var vLength = grupo.length; 

 if( typeof vLength == 'undefined' ) 
	if(grupo.checked)
		return grupo.value;
	else
		return lista_valores;

 for( var i=0, vTotal=vLength; i<vTotal; i++) 
 	 if( grupo[ i ].checked ) 
		 lista_valores += ',' + grupo[ i ].value;

 return lista_valores;
}

function fnc_tabnext( p_campo_origem , p_campo_destino )
{
  if ( p_campo_origem.getAttribute && p_campo_origem.value.length == p_campo_origem.getAttribute("maxlength") )
	   p_campo_destino.focus();
}

function selecionaTodosCheckbox( p_campo_referencia, p_campo_destino ) {
	var campo_destino = document.principal[p_campo_destino];
	var campo_origem  = document.principal[ p_campo_referencia ];
	var vLength		  = campo_destino.length;

	 if( typeof campo_destino == 'undefined') return;

	 if( typeof vLength == 'undefined'){ 
		campo_destino.checked = campo_origem.checked;
	 } else {
		for( var i = 0; i < vLength; i++ )
			campo_destino[i].checked = campo_origem.checked;
	 }
}