var elemento;
var cor1;
var cor2;
var oPopup = window.createPopup();	

function getCurrentPage()
{
 var caminho = window.location.pathname;
 var pagina  = caminho.substring( caminho.lastIndexOf('/') + 1 );
 
 return ( pagina );
}

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;
    }
}

function fnc_centro( p_janela )
{				
  var jsdlg = 400;		
  var w,h;
  var left, top;
  
  if( !document.getElementById( 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;
  }
	
   document.getElementById( p_janela ).style.left = left = (w/2)-(600/2);
   document.getElementById( p_janela ).style.top  = top  = (h/2)-((jsdlg)/2);
}

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;
}

function fnc_mostra_div( p_div_id )
{
  fnc_referencia_div( p_div_id ).style.display = '';
}

function fnc_oculta_div( p_div_id )
{
  fnc_referencia_div( p_div_id ).style.display = 'none';
}

function fnc_ajuste_iframe( p_id_iframe )
{
   document.getElementById( p_id_iframe ).style.height = document.getElementById( p_id_iframe ).document.body.scrollHeight + 25
}

function fnc_length_editor( p_instancia )
{
	var oEditor = FCKeditorAPI.GetInstance( p_instancia ) ;
	var oDOM    = oEditor.EditorDocument ;
	var iLength ;

    if ( document.all )	// caso for internet explorer 
		iLength = oDOM.body.innerText.length ;
	else // Outros navegadores.
	{
		var r = oDOM.createRange() ;
		r.selectNodeContents( oDOM.body ) ;
		iLength = r.toString().length ;
	}
  return( iLength );
}

function swapBG( el, BG1, BG2 )
{
  el.style.backgroundColor = ( el.style.backgroundColor == BG1 ) ? BG2 : BG1;
}

function fnc_cor_tablesort( p_id_tabela )
{
 v_corpo   = document.getElementById( p_id_tabela ).getElementsByTagName('TBODY')[ 0 ];

 for ( var i = 0; i < v_corpo.rows.length; i++ )
 {
	v_cor = v_corpo.rows[ i ].rowIndex;
	
	if ( v_cor % 2 == 1 )
	{
	  v_corpo.rows[ i ].style.background  = '#FFFFFF';
	  v_corpo.rows[ i ].title 	 	      = '#FFFFFF';
		
	} else {
	   v_corpo.rows[ i ].style.background = '#EEEEEE';
	   v_corpo.rows[ i ].title 		  	  = '#EEEEEE';
	}
  }
}	

function fnc_carregar_imagens()
{
  var imagem  = [];
  for ( i = 0; i < fnc_carregar_imagens.arguments.length; i++ )
  {
	imagem[ i ] 	= new Image();
	imagem[ i ].src = fnc_carregar_imagens.arguments[ i ];
  }
}

function fnc_velocidade( p_conexao )
{
 var velocidade = 0;
  
 switch( p_conexao )
 {
   case 'cd56'    :  velocidade  =   57600;  	break;
   case 'adsl128' :  velocidade  =  131328;		break;
   case 'adsl256' :  velocidade  =  262400; 	break;
   case 'adsl512' :  velocidade  =  524544; 	break;
   case 'adsl768' :  velocidade  =  786688; 	break;
   case 't1'      :  velocidade  = 1048832; 	break;
 }
  return( velocidade );
}

function fnc_tempo_download( p_tamanho, p_conexao )
{
 var segundos	 = ( (  p_tamanho * 10 ) / fnc_velocidade( p_conexao ) );
 var hora 		 =  segundos   / 3600;
 var minuto   	 =  segundos   / 60;
 var segundo 	 =  Math.round( segundos % 60  );
 var temp 		 = '';
 // strings
 var sHora        = ( hora >= 2 )?' horas ':' hora ';
 var sMinuto      = ( minuto >= 2 )?' minutos ':' minuto ';
 var sSegundo	 = '';

 if ( minuto >= 60 ) minuto  = minuto % 60;
 if ( hora   >=  1 ) temp   += Math.round( hora )+''+sHora;

 if(  segundo  < 60 )
 {
   // se o temp tempo restante for menos que 60 segundos
   if ( minuto >= 1 )	temp   += Math.round( minuto  + 1 ) +''+ sMinuto;
   
   if( segundo > 1 )
	  sSegundo = ' segundos ';
   else if( segundo == 1 ) 	  
	  sSegundo = ' segundo ';
   else if( segundo == 0 ) 	  
	  sSegundo = '';
	  
	temp +=  Math.round( segundo )+''+sSegundo; 

  } else {
	 if ( minuto >= 1 )	temp   += Math.round( minuto  + 1 )+''+sMinuto;
  }	
  
  if( temp == 0 ) temp = 'Download imediato ';

 return ( temp );
}  

function fnc_grupo( p_objeto_id )
{
  operacao = p_objeto_id.split('_');

  switch( operacao[ 0 ] )
  {
	  case 'btnshow' :   
		document.getElementById( p_objeto_id ).style.display 					= 'none';
		document.getElementById( 'btnhide_' +  operacao[ 1 ] ).style.display 	= '';
		document.getElementById(  operacao[ 1 ] ).style.display 				= '';
  	 break;
	 case 'btnhide' :   
		document.getElementById( p_objeto_id ).style.display 				    = 'none';
		document.getElementById(  operacao[ 1 ] ).style.display 				= 'none';
		document.getElementById( 'btnshow_' +  operacao[ 1 ] ).style.display 	= '';
		break;
  }
}

function fnc_gerencia_grupo( p_objeto )
{
	
 var objeto   = document.getElementById( p_objeto );
 var grupo 	  = document.getElementById( p_objeto.replace('botao_','') );
 var operacao = ( grupo.style.display == 'none')?'mostrar':'ocultar';

  switch( operacao )
  {
   case 'mostrar' :
		 objeto.onclick		  = function(){ fnc_gerencia_grupo( this.id ) }
		 objeto.src			  = '/_imagens/icones/16x16/down.gif';
		 objeto.style.display = '';
		 grupo.style.display  = '';
   break;
   
   case 'ocultar' :   
		 objeto.onclick		  = function(){ fnc_gerencia_grupo( this.id ) }
		 objeto.src			  = '/_imagens/icones/16x16/up.gif';
		 objeto.style.display = '';
		 grupo.style.display  = 'none';
	break;
  }
}

function isPainel( p_painel )
{ 
 if( !document.getElementById( p_painel ) ) return; 
 
  document.getElementById( p_painel ).style.margin = '10px';  
  fnc_mostra_div( p_painel );  
}

function isPaginaEditavel( p_pagina )
{ 
  var primeiro_trecho = p_pagina.substring( 0, p_pagina.indexOf('_')  );
  var ultimo_trecho   = p_pagina.substring( p_pagina.lastIndexOf('_') + 1, p_pagina.lastIndexOf('.')  );

 return( ( primeiro_trecho == 'editar' && ultimo_trecho == 'detalhes')?true:false );

}

function fnc_reload()
{
 try
 {
   opener.location.reload();
 } catch ( e ) {
	return false; 
 } 
}

function swapBG( el, BG1, BG2 )
{
  el.style.backgroundColor = ( el.style.backgroundColor == BG1 ) ? BG2 : BG1;
}

function showPopup( current , e , comentario )
{
  var oPopupBody						 = oPopup.document.body;
	  oPopupBody.style.backgroundColor 	 = cor_primaria;
	  oPopupBody.style.border 		 	 = "solid black 1px";    
	  oPopupBody.style.fontFamily 	 	 = "Verdana, Geneva, Arial, Helvetica, sans-serif";
	  oPopupBody.style.fontSize 		 = "10px";
  var altura 		= 1;
  var largura 	 	= 320;
  var deslocamento  = ( parseInt( current.offsetHeight ) + 5 );
	
	 if( comentario.length > 50 )
	  altura  = Math.ceil( comentario.length / 50 );
	else
	  largura = Math.ceil( comentario.length * 10 );
 
	oPopupBody.innerHTML = comentario; 
	
	switch( current.type )
	{
		case 'select-one': 
		case 'select-multiple': 
			  deslocamento = ( altura == 1 )? ( -1 * ( parseInt( current.offsetHeight  )+ 5 ) ) : ( -1 * ( parseInt( current.offsetHeight ) + ( 10 * altura ) ) );
		break;		
	}
	
   oPopup.show( 0 , deslocamento , largura, 15 * altura , current );
}

function setBackgroundColor( e )
{
  try
  {
	  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 ) target.style.backgroundColor = cor_primaria;
	  if( target ) showPopup( target, window.event , target.hint );
	  
 } catch ( e ) {
	return false; 
 } 
}

function clearBackgroundColor( e )
{
  try
  {
	
  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 ) target.style.backgroundColor = '';
  if(oPopup.isOpen == true){oPopup.hide();}

 } catch ( e ) {
	return false; 
 } 

}

function fnc_paginacao( p_valor )
{
  with( document.forms[ 0 ] )
  {
	registro_pagina.value  = p_valor;  
	operacao.value		   = 1;
	submit();
  } 
}

function fnc_detalhes_ordem_servico( p_ordem_servico_id )
{
top.location = 'detalhes_ordem_servico.cfm?ordem_servico_id='+ p_ordem_servico_id;	
}

