<script language="javascript">
<?php
  include( create_dbgrid_control( $modo_RAD ) );
?>

//
//  dbGrid_mouseClick()
//
  function dbGrid_mouseClick( objID )
  {  
    //alert('clik Obj: ' + objID);
    if( objID.indexOf('dbgrid') != -1 &&
        objID.indexOf('_col_') != -1
      )
    {  
//alert( getDBgrid_coluna(objID) );
      setDBgrid_linha( objID );
    }// End
  }// End function dbGrid_mouseClick( objID )
  
//
//  dbGrid_mouseDown()
//
  function dbGrid_mouseDown( objID )
  {
    if( objID.indexOf('dbgrid') != -1 &&
        objID.indexOf('_col_') != -1
      )
    {

    }
  }// End function()

/**
 *  dbGrid_mouseDblClick()
 */
  function dbGrid_mouseDblClick( objID )
  {
    if( objID.indexOf('dbgrid') != -1 &&
        objID.indexOf('_col_')  != -1
      )
    {
      idx = getDBgrid_index( objID );
      if( dbgrid_edit[ idx ] )
      {
        setDBgrid_edit_enable( objID, idx );
      }// End if( dbgrid_edit[ idx ] )

      event.cancelBubble = true;
      event.returnValue = false;
    }// End if( objID.indexOf('dbgrid') != -1 )
  }// End function()

//
//  dbGrid_mouseUp()
//
  function dbGrid_mouseUp( objID )
  {  
    if( objID.indexOf('dbgrid') != -1 )
    {  
    
    }  
  }// End function()
  
//
//  dbGrid_mouseOver()
//
  function dbGrid_mouseOver( objID )
  {  
    if( objID.indexOf('dbgrid') != -1 &&
        objID.indexOf('_col_') != -1
      )
    {
      idx = getDBgrid_index( objID );
      nrl = getDBgrid_linha( objID );
      nrc = getDBgrid_coluna( objID );
//alert('linha'+nrl+' ' );
      objDOM = findDOM( objID, 1 );
      
      if( dbgrid_edit[ idx ] &&
          dbgrid_linha_corrente[ idx ] == nrl &&
          dbgrid_modo_edit[ idx ] == nrc
        )
      {
        objDOM.backgroundColor='#EEEEEE';
      }else
      {
        objDOM.backgroundColor='#FFFFDF';
        objDOM.color='#000080';
        objDOM.fontWeight='bold';
      }// End if()

//      objDOM.color='#000080';
//      objDOM.fontWeight='bold';
    }  
  }// End function()
  
//
//  dbGrid_mouseOut()
//
  function dbGrid_mouseOut( objID )
  {  
    if( objID.indexOf('dbgrid') != -1 &&
        objID.indexOf('_col_') != -1
      )
    {  
      idx = getDBgrid_index( objID );
      old = dbgrid_linha_corrente[ idx ];
      nrl = getDBgrid_linha( objID );
      nrc = getDBgrid_coluna( objID );

      objDOM = findDOM( objID, 1 );
      //objDOM.color='#000000';
      //objDOM.fontWeight='normal';
      if( nrl == old )
      {
        if( dbgrid_edit[ idx ] &&
            dbgrid_modo_edit == 0
          )
        {
          objDOM.backgroundColor='#FFFF99';
          objDOM.color='#000000';
          objDOM.fontWeight='normal';
        }else
        {
          if( dbgrid_modo_edit[ idx ] == nrc )
          {
            objDOM.backgroundColor='#EEEEEE';
          }else
          {
            objDOM.backgroundColor='#FFFF99';
            
            objDOM.color='#000000';
            objDOM.fontWeight='normal';
          }// End if( dbgrid_modo_edit == nrc )
        }// End
      }else
      {
        objDOM.backgroundColor='#FFFFFF';
        objDOM.color='#000000';
        objDOM.fontWeight='normal';
      }// End
    }// End
  }// End function()
  
  
//  ------------------------------------------------------------  //
//  Lista de Métodos do Componente                                //
//  Conjunto de funções para manipular as propriedades do Objeto  //
//  ------------------------------------------------------------  //


/**
 *  seta o status de Edição da linha corrente
 *
 *  true = Modo Edição
 *  false = modo Exibição
 */
  function setDBGrid_edit_line( objID, status )
  {
    var idx = getDBgrid_index( objID );
    var nrc = dbgrid_nr_coluna[ idx ];
    var nrl = dbgrid_linha_corrente[ idx ];

/*
    if( status )
    {
      readOnly = false;
    }else
    {
      readOnly = true;
    }
*/
    readOnly = !status;
    corFont1 = '#FF0000';
    corBack1 = '#EEEEEE';
    corFont2 = '#000000';
    corBack2 = '#FFFF99';

    for( ix = nrc; ix > 0; ix -- )
    {
      nCol = objID + '_col_' + nrl + '_' + ix;
      objDOM = findDOM( nCol, 0 );
      
      colTipo = objDOM.getAttribute( 'colTipo' );
      if( colTipo == 'field' )
      {
        objDOM.readOnly = readOnly;
        objDOM.style.color= corFont1;
        objDOM.style.backgroundColor=corBack1;
      }else
      {
        objDOM.readOnly = true;
        objDOM.style.color= corFont2;
        objDOM.style.backgroundColor=corBack2;
        //alert( colTipo );
      }
      objDOM.focus();
    }
  }// End function setDBGrid_edit_line( objID )

//
//  coloca a coluna em modo de Edição
//
  function setDBgrid_edit_enable( objID, idx )
  {
    dbgrid_modo_edit[ idx ] = getDBgrid_coluna( objID );
    
    objDOM = findDOM( objID, 0 );
    objDOM.focus();
    objDOM.setActive();
    objDOM.readOnly = false;
    objDOM.style.color='#FF0000';
    objDOM.style.backgroundColor='#EEEEEE';
    
    
//alert(objID+' '+objDOM.value );

  }// End function setDBgrid_edit_enable( objID )

//
//  coloca a coluna em modo de exibição
//
  function setDBgrid_edit_disable( objID )
  {
    var idx = getDBgrid_index( objID );
    var objDOM = null;
    
    dbgrid_modo_edit[idx] = 0;
    
    objDOM = findDOM( objID, 0 );
    objDOM.readOnly = true;

    objDOM.style.color='#000000';
    objDOM.style.fontWeight='normal';
  }// End function setDBgrid_edit_disable( objID )


//
//  Funções auxiliares
//  ========================================================================= //
//
 
//
//  retorna o numero de linhas da grid
//
  function getDBgrid_nr_linhas( objID )
  {
    var idx  = getDBgrid_index( objID );
    var nObj = dbgrid_ID[ idx ] + '_nrLinhas';
    objDOM = findDOM( nObj, 0 );

    return parseInt( objDOM.value );
  }// End

//
//  retorna o numero da linha da grid atraves do ID da Coluna
//
  function getDBgrid_linha( objID )
  {
    ix1 = objID.indexOf( '_col_' ) +5;
    ix2 = objID.indexOf( '_', ix1 );

    return parseInt(objID.substring( ix1, ix2 ));
  }// End

//
//  retorna o numero da coluna da grid atraves do ID da Coluna
//
  function getDBgrid_coluna( objID )
  {
    ix1 = objID.indexOf( '_col_' ) + 5;
    ix2 = objID.indexOf( '_', ix1 ) + 1;
    return parseInt( objID.substr( ix2 ) );
  }// End

/**
 *  retorna o indice do dbgrid
 */
  function getDBgrid_index( objID )
  {
    var ret = null;
    var ix  = 0;
    if( typeof( objID ) == 'object' ||
        typeof( objID ) == 'string'
      )
    {
      if( objID.indexOf('dbgrid') != -1 )
      {
        ix = objID.indexOf('_');
        if( ix != -1 )
        {
          idx = objID.slice( 6, ix );
        }else
        {
          idx = objID.slice( 6 );
        }// End
        ret = parseInt( idx ) -1;
      }// End if( objID.indexOf('dbgrid') != -1 )
    }// End if( typeof() == 'object' )
    return ret;
  }// End function getDBgrid_index( objID )

//
//  retorna o ID da dbgrid
//
  function getDBgrid_ID( objID )
  {
    ret = null;
    ix  = 0;
    if( objID.indexOf('dbgrid') != -1 )
    {
      ret = objID.slice( 0, objID.indexOf('_') );
    }// End
    return ret;
  }// End


/**
 *  seta a linha Corrente atraves do ID da Coluna
 *  ------------------------------------------------------------------------  //
 */
  function setDBgrid_linha( objID )
  {
//    var ix = 0;
    var nmCol = null;
    var idx = getDBgrid_index( objID );
    var nrl = getDBgrid_linha( objID );
    var old = dbgrid_linha_corrente[ idx ];
    var nrc = dbgrid_nr_coluna[ idx ];
    var nObj = getDBgrid_ID( objID );

    var nrc_i = getDBgrid_coluna( objID );
    //
    //  desmarca linha corrente anterior  ----------------------------------  //
    //
    setDBgrid_linha_off( idx );

    //
    //  Marca a nova linha Corrente  ---------------------------------------  //
    //
    setDBgrid_linha_on( idx, nrl, nrc_i );
    
    //dbgrid_linha_corrente[ idx ] = nrl;
//alert(nObj);
    //dbGrid_refresh( 'dbgrid2' );
  }// End function setDBgrid_linha( objID )

/**
 *  desativa a linha Corrente
 */
  function setDBgrid_linha_off( idx )
  {
    var nmCol = null;
    var old   = dbgrid_linha_corrente[ idx ];
    var nrc   = dbgrid_nr_coluna[ idx ];
    var nObj  = dbgrid_ID[ idx ];
    if( old != 0 )
    {
      for( ix = 1; ix <= nrc; ix ++ )
      {
        nCol = nObj + '_col_' + old + '_' + ix;
        objDOM = findDOM( nCol, 0 );
        objDOM.readOnly = true;
        objDOM.style.backgroundColor='#FFFFFF';

        if( ix == dbgrid_modo_edit[idx] && dbgrid_modo_edit[idx] > 0  )
        {
          nmCol = nCol;
        }// End if()
      }// End for( ix = 1; ix <= nrc; ix ++ )
      if( nmCol != null )
      {
        //setDBgrid_edit_disable( nmCol );
      }// End if( nmCol != null )
    }// End if( old != 0 )
  }// end function setDBgrid_linha_off( idx )

/**
 *  seta a linha indicada como a linha corrente
 *  idx => indice do DBGrid
 *  nrl => numero da linha
 */
//  function setDBgrid_linha_on( idx, nrl, nrc_i )
  function setDBgrid_linha_on( idx, nrl )
  {
 //alert(nrc_i);
    var nmCol = null;
    var nrc   = dbgrid_nr_coluna[ idx ];
    var old   = dbgrid_linha_corrente[ idx ];
    var nObj  = dbgrid_ID[ idx ];

    for( ix = 1; ix <= nrc; ix ++ )
    {
      nCol = nObj + '_col_' + nrl + '_' + ix;
      objDOM = findDOM( nCol, 0 );
      objDOM.readOnly = false;
      objDOM.setActive();
      objDOM.readOnly = true;

      if( ix == dbgrid_modo_edit[ idx] )
      {
        objDOM.style.backgroundColor='#EEEEEE';
      }else
      {
        objDOM.style.backgroundColor='#FFFF99';

        if( nrl == old )
        {
        }else
        {
          if( old > 0 )
          {
            nObj_old = nObj + '_col_' + old + '_' + ix;
          //alert(nObj_old);
          //setDBgrid_edit_enable( nObj_old );
          }// End if( old > 0 )
          //dbgrid_modo_edit[ idx ] = 0;
        }// End if( nrl == old )
      }// End if( ix == dbgrid_modo_edit[ idx] )
    }// End for( ix = 1; ix <= nrc; ix ++ )

    dbgrid_linha_corrente[ idx ] = nrl;
  }// end function setDBgrid_linha_on( idx )



/**
 *
 */
  function setDBgrid_readOnly( idx, status )
  {
    var nrc  = dbgrid_nr_coluna[ idx ];
    var nObj = dbgrid_ID[ idx ];
    var nrl  = getDBgrid_nr_linhas( nObj )

    for( l = 1; l <= nrl; l++ )
    {
      for( c = 1; c <= nrc; c++ )
      {
        nCol = nObj + '_col_' + l + '_' + c;
        objDOM = findDOM( nCol, 0 );
        objDOM.readOnly = status;
      }// End for( c = 1; c <= nrc; c++ )
    }// End for( l = 1; l <= nrl; l++ )
  }// End function setDBgrid_readOnly( idx, status )

/**
 *  Evento keyPress
 */
  function dbgrid_keyPress( objID, cod )
  {
    if( objID != '' )
    {
      if( objID.indexOf( 'dbgrid' ) != -1 &&
          objID.indexOf( '_col_' ) != -1
        )
      {
        alert('ok');
      }// End if()
    }// End if( objID != '' )
  }// End function dbgrid_keyPress( objID, cod )





//  ------------------------------------------------------------  //
//  Lista de Métodos do Componente                                //
//  Conjunto de funções para manipular as propriedades do Objeto  //
//  ------------------------------------------------------------  //



/**
 *  getDBgrid_col_value_by_name()
 *  ------------------------------------------------------------------------  //
 *  Parametros de entrada:
 *    dbGrid = ID do dbgrid
 *    nameCol = nome da Coluna
 *  retorna o valor da coluna corrente do dbGrid, pelo nome da Coluna
 */
  function getDBgrid_col_value_by_name( dbGrid, nameCol )
  {
    idxDB = getDBgrid_index( dbGrid );
    val = '';

    for( nr = 1; nr <= dbgrid_nr_coluna[ idxDB ]; nr ++ )
    {
      idCol   = dbGrid + '_col_' + dbgrid_linha_corrente[ idxDB ] + '_' + nr;
      objDOM = findDOM( idCol,0 );
      nCol = objDOM.getAttribute( 'colName' );
      if( nCol == nameCol )
      {
        val = objDOM.value;
//        alert(nCol+' :: '+nameCol+' = '+val);
      }// end if( nCol == nameCol )
    }// End for( nr = 1; nr < dbgrid_nr_coluna[ idxDB ]; nr ++ )
    return val;
  }// End function getDBgrid_col_value_by_name( dbGrid, nameCol )

/**
 *  SUM (totaliza uma coluna da grid pelo nome da coluna
 */
  function getDBgrid_col_sum_by_name( dbGrid, nameCol )
  {
    var idxDB = getDBgrid_index( dbGrid );
    var linDB = getDBgrid_nr_linhas( dbGrid );
    var val = 0;
//alert(linDB);
    for( nl = 1; nl <= linDB; nl ++ )
    {
      for( nr = 1; nr <= dbgrid_nr_coluna[ idxDB ]; nr ++ )
      {
        idCol  = dbGrid + '_col_' + nl + '_' + nr;
        objDOM = findDOM( idCol,0 );
        nCol = objDOM.getAttribute( 'colName' );
//alert( idCol );
        if( nCol == nameCol )
        {
          //alert( objDOM.value.toFixed(4) );
          val = val + parseFloat(objDOM.value);
        }// end if( nCol == nameCol )
      }// End for( nr = 1; nr < dbgrid_nr_coluna[ idxDB ]; nr ++ )
    }
    return val.toFixed(4);
  }// End function getDBgrid_col_sum_by_name( dbGrid, nameCol )



/**
 *  getDBgrid_col_value_ID()
 *  ------------------------------------------------------------------------- //
 * retorn o valor da coluna
 */

  function getDBgrid_col_value( objID )
  {
    var objDOM = findDOM( objID, 0 );
    var ret = "";
    if( objDOM == null )
    {
      //alert( 'obj: '+objID + ' = ' + objDOM );
    }else
    {
      ret = objDOM.value;
    }
//    return objDOM.value;
    return ret;
  }// End

/**
 *  getDBgrid_name()
 */
  function getDBgrid_name( objID )
  {
    return( objID.substr( 0, objID.indexOf('_') ) );
  }// End function getDBgrid_name()
 
/**
 *  getDBgrid_index()
 *  ========================================================================= //
 */
//  function getDBgrid_index( objID )
//  {
//    ix = objID.indexOf('_');
//    if( ix != -1 )
//    {
//      idx = objID.slice( 6, ix );
//    }else
//    {
//      idx = objID.slice( 6 );
//    }// End
//    return (parseInt(idx) - 1);
//  }// End
  
/**
 *  setDBgrid_update()
 *  ========================================================================= //
 */
  function setDBgrid_update( objID )
  {
    var ix = getDBgrid_index( objID );
    var cmd = null;
    var nObj = getDBgrid_name(objID);
    var objDOM = findDOM( objID, 0 );
//var loginUSER = "othuki";
//alert('1: ' + objID );
    if( dbgrid_edit[ ix ] &&
        dbgrid_linha_corrente[ ix ] > 0  )
    {
      $key_ = getDBgrid_key_value( nObj, ix );
//alert($key_);
      cmd = loginUSER                      + ':' +
            nObj                           + ':' +
            dbgrid_pk[ix]                  + ':' +
            $key_                          + ':' +
            objDOM.getAttribute('colName') + ':' +
            getDBgrid_col_value( objID );
      //alert( 'cmd: ' + cmd);
      dataAccess.upDate( cmd );
    }// End if()
    
  }// End function setDBgrid_update( objID )


/**
 *  retorna os value´s do(s) Campo(s) Chave da linha Corrente (PK)
 */
function getDBgrid_key_value( nObjDB, ixDB )
{
  var ret = '';

/**
 *  conta numero de campos chaves
 */
  campoKey = dbgrid_pk[ixDB];
  arrKey = new Array();
  arrKey = campoKey.split('*');
  nrKey  = arrKey.length;
  iix    = 0;
  for( ind = 1; ind <= dbgrid_nr_coluna[ ixDB ]; ind ++ )
  {
    nmCol = nObjDB + '_col_' + dbgrid_linha_corrente[ixDB] + '_' + ind;
    objDOM = findDOM( nmCol, 0 );

    for( iv = 0; iv <= nrKey; iv ++ )
    {
      if( objDOM.getAttribute('colName') == arrKey[iv] )
      {
        if(iix > 0) ret += '#';
        ret += objDOM.value;
        iix ++;
      }// End
    }// End
  }//End for( ind = 1; ind <= dbgrid_nr_coluna[ ixDB ]; ind ++ )
//  alert(ret);
  return ret;
}// End


function setDBgrid_visible( dbGrid, status )
{
  var objDOM = findDOM( dbGrid, 1 );
  if( status )
  {
    novoStatus = 'inherit';
  }else
  {
    novoStatus = 'hidden';
  }
  objDOM.visibility = novoStatus;
}


/**
 *  resetDBgrid()
 */
function resetDBgrid( dbGrid )
{
//alert('passo 0 : ' + dbGrid);
  var arrField = new Array();
  var key_ = '';

//    nObj = getDBgrid_ID( dbGrid );
    idx  = getDBgrid_index( dbGrid );
//    keyValue = getDBgrid_key_value( dbGrid, idx );
//alert('master: '+dbgrid_master[ idx ]);
    if( dbgrid_master[ idx ] == '' )
    {
//alert('blank');
      key_ = '';
    }else
    {
      ids = null;
      for( ix = 0; ix < dbgrid_obj; ix++ )
      {
        if( dbgrid_dataset[ix] == dbgrid_master[ idx ] )
        {
          ids = ix;
        }// End
      }// End for( ix = 0; ix < dbgrid_obj; ix++ )
      if( ids != null)
      {
//alert('dataSet: '+dbgrid_ID[ids]+' : '+dbgrid_masterKey[ idx ]);
        key_ = getDBgrid_col_value_by_name( dbgrid_ID[ids], dbgrid_masterKey[ idx ] );
      }// End if( ids != null)
    }// End if( dbgrid_master[ idx ] == '' )
    
//alert('key: '+key_);
//alert('key[ '+keyValue +' ]');

//alert('passo 1:'+dbGrid);

  dataAccess.getFields( dbGrid );
  key = loginUSER + ':' + dbGrid + ':' + key_;
//  key = dbGrid + ': ';
  dataAccess.getResult( key  );
//alert('key::  '+key);


//  var cmd = dbGrid + "_onLoad();";
//alert('cmd: '+cmd);
//  eval( cmd );


/*
  arrField[0] = 'Codigo';
  arrField[1] = 'Componente';
  arrField[2] = '%';
  arrField[3] = 'field4';

  dataAccess.getFields( dbGrid );
//alert('passo 2');

  var objDOM = findDOM( dbGrid, 0 );
  var tmp = '';

  tmp += '<table id="' + dbGrid + '_table" cellpadding="2" cellspacing="1">';
  tmp += '  <thead>';
  tmp += '  <tr id="' + dbGrid + '_tit">';
  tmp += '    <th id="' + dbGrid + '_th" class="' + dbGrid + '_tit">';
  tmp += '      <span id="' + dbGrid + '_num" class="' + dbGrid + '_col">';
  tmp += '        &nbsp; &nbsp; ';
  tmp += '      </span>';
  tmp += '    </th>';

  for( ix = 0; ix < 3; ix++ )
  {
    iix = ix +1;
    tmp += '    <th id="' + dbGrid + '_th_' + iix + '" class="' + dbGrid + '_tit">';
    tmp += '      <input id="' + dbGrid + '_tit_' + iix+'" type="text" readOnly="true"';
    tmp += '             value="'+arrField[ix] + '" class="' + dbGrid + '_col_' + iix+'">';
    tmp += '    </th>';
  }
  tmp += '  </tr>';
  tmp += '</thead>';
  tmp += '';
  tmp += '';
  tmp += '';
  tmp += '</table>';

  objDOM.innerHTML = tmp;
*/
}// End function resetDBgrid( dbGrid )


/**
 *  Deletar a linha Corrente
 */
function setDBgrid_delete_linha( dbGrid )
{
  var campos = '';
  idx_grid = getDBgrid_index( dbGrid );
  nrLinha = dbgrid_linha_corrente[ idx_grid ];

  dataSet = dbgrid_dataset[ idx_grid ];
  nrCol = dbgrid_nr_coluna[ idx_grid ];
  for( nix = 1; nix <= nrCol; nix ++ )
  {
    nCol = dbGrid + '_col_' + nrLinha + '_' + nix;
    objDOM = findDOM( nCol, 0 );
    nameCol = objDOM.getAttribute( 'colName' );
//    alert( nCol);
    campos += nameCol + ':' + getDBgrid_col_value( nCol );

    if( nix < nrCol )
    {
      campos += '#';
    }// End if( nix < nrCol )
  }// End for( nix = 1; nix <= nrCol; nix ++ )

  str_ = loginUSER + ':' + dbGrid + ':' + nrLinha + ':Del#' + campos;
alert( str_ );
//  alert( nrLinha + '#' + campos);
  dataAccess.setRecord( str_ );
}// End function delDBgrid_linha( dbGrid )


/**
 *  executa um post no Banco de Dados  -------------------------------------  //
 */
 function setDBgrid_post( dbGrid )
 {
  var campos = '';
  idx_grid = getDBgrid_index( dbGrid );
  nrLinha = dbgrid_linha_corrente[ idx_grid ];

  dataSet  = dbgrid_dataset[ idx_grid ];
  idx_data = getTable_index( dataSet );
//alert( idx_data );
//return;
  if( table_master[ idx_data ] == '' )
  {
    // alert(table_master[ idx_data ]);
  }else
  {
    masterKey = table_master_key[ idx_data ];

    valKey = getDBgrid_col_value_by_name( 'dbgrid1', masterKey );
//alert( valKey );

    //campos = masterKey + ':' + valKey+'#';
//alert( 'key: '+campos );
  }// End if( table_master[ idx_data ] == '' )
  
  nrCol = dbgrid_nr_coluna[ idx_grid ];
  for( nix = 1; nix <= nrCol; nix ++ )
  {
    nCol = dbGrid + '_col_' + nrLinha + '_' + nix;
    objDOM = findDOM( nCol, 0 );
    nameCol = objDOM.getAttribute( 'colName' );
    tipoCol = objDOM.getAttribute( 'colTipo' );
//    alert( nCol);

/**
 *  seleciona os campos do tipo field para enviar ao servidor
 */
    if( tipoCol == 'field' )
    {
      campos += nameCol + ':' + getDBgrid_col_value( nCol );
    }// End if( tipoCol == 'field' )

    if( nix < nrCol )
    {
      campos += '#';
    }// End if( nix < nrCol )
  }// End for( nix = 1; nix <= nrCol; nix ++ )

  len = campos.length -1;
  while( campos.slice( len ) == '#' )
  {
    len -= 1;
    campos = campos.slice( 0,len );
  }// End while( campos.slice( len ) == '#' )
//alert( campos );

  str_ = loginUSER + ':' + dbGrid + ':' + nrLinha + ':Upd#' + campos;
//alert( 'teste 1: '+str_ );
  //alert( nrLinha + '#' + campos );
  dataAccess.setRecord( str_ );
 }// End function setDBgrid_post( dbGrid )
 
 
/**
 *  Incluir uma linha em brando no final da Grid  --------------------------  //
 */
function setDBgrid_nova_linha( dbGrid )
{
  nIns = dbGrid + '_insert';
  nInp = dbGrid + '_col_0_1';
  objDOM = findDOM( nIns, 1 );
  objDOM.visibility='inherit';

  idx_grid = getDBgrid_index( dbGrid );
  dbgrid_linha_corrente[ idx_grid ] = 0;
//alert(nInp);
  objDOM = findDOM( nInp, 0 );
  objDOM.focus();

//  alert( dbGrid );
}// End function setDBgrid_nova_linha( dbGrid )


/**
 *  Deletar a linha em brando no final da Grid.
 */
function delDBgrid_nova_linha( dbGrid )
{
  idx_grid = getDBgrid_index( dbGrid );
  if( dbgrid_linha_corrente[ idx_grid ] == 0 )
  {
    nIns = dbGrid + '_insert';
    objDOM = findDOM( nIns, 1 );
    objDOM.visibility='hidden';

    nrLinha = getDBgrid_nr_linhas( dbGrid );

    nInp = dbGrid + '_col_'+nrLinha+'_1';
    dbgrid_linha_corrente[ idx_grid ] = nrLinha;

    objDOM = findDOM( nInp, 0 );
    objDOM.focus();
    setDBgrid_linha_on( idx_grid, nrLinha );
  }// End if( dbgrid_linha_corrente[ idx_grid ] == 0 )
//  alert( dbGrid );
}

/**
 *  dbGrid_refresh()
 *  ------------------------------------------------------------------------  //
 *  realiza um refresh nos valores dos campos da grid
 *  atualizando os campos do tipo Calculado
 */
function dbGrid_refresh( objID )
{
  var dbg_id = findDOM( objID + '_nrLinhas',0 );
  var dbg_idx = getDBgrid_index( objID );
  var linha_corrente = dbgrid_linha_corrente[ dbg_idx ];
//alert( objID+' linhas: ' + dbg_id.value );
//alert(dbgrid_nr_coluna[1]);
//alert(linha_corrente);

  nrl = dbg_id.value;
  str = '';

/**
 *  laço da quantidade de linhas do dbGrid
 */
  for( ix1 = 1; ix1 <= nrl; ix1 ++ )
  {
    nrc = dbgrid_nr_coluna[1];
    dbgrid_linha_corrente[ dbg_idx ] = ix1;

    // laço da quantidade de colunas do dbGrid  //
    for( ix2 = 1; ix2 <= nrc; ix2 ++ )
    {
      nCol_ = objID + '_col_' + ix1 + '_' + ix2;
      col_id = findDOM( nCol_, 0 );
      tipo = col_id.getAttribute('colTipo');
      nCol = col_id.getAttribute( 'colName' );

      if( tipo == 'calculado' )
      {
        nFor_ = objID + '_formula_' + ix1 + '_' + ix2;
        //alert(nFor_);
        forID = findDOM( nFor_, 0 );

        str = forID.innerHTML;
//alert('formula['+nCol+']: '+str);
        if( str != '' )
        {
          // retira os espaços em branco
          spc = str.indexOf( ' ' );
          while( spc != -1 )
          {
            if( spc == 1 )
            {
              str = str.slice( spc+1 );
            }else
            {
              str = str.slice( 0,spc ) + str.slice( spc +1 );
            }
            spc = str.indexOf( ' ' );
          }//
//alert( 'formula: '+str );

/**
 *  monta a formula  -------------------------------------------------------  //
 */
          ix  = findOPR( str, 0 );
          ixx = findOPR( str, ix+1 );
//alert(str+' : '+ix+' : '+ixx);
          pr1 = str.slice( 0, ix );
          ix_ = ix +1;
          oprMath1 = str.slice( ix, ix_ );
          if( ixx != -1 )
          {
            pr2 = str.slice( ix_, ixx );
          }else
          {
            pr2 = str.slice( ix_ );
          }

          tmp = parseFloat( pr1 );
          if( isNaN( tmp ) )
          {
            pr1 = eval( pr1);
          }else
          {
            pr1 = tmp;
          }// End if( isNaN( tmp ) )

//alert( 'pr2: '+pr2 );
            var tmp2 = parseFloat( pr2 );
            if( isNaN( tmp2 ) )
            {
              pr2 = eval( pr2 );
            }else
            {
              pr2 = tmp2;
            }
//alert( 'pr2.1:'+pr2 );

          if( ixx != -1 )
          {
            ixx_ = (ixx + 1);
            oprMath2 = str.slice( ixx, ixx_ );
//alert( oprMath2 );
            pr3 = str.slice( ixx_ );
//if( pr3 == 'col' )
//alert('pr3: '+pr3);
//alert('ok:'+pr3 );
              tmp = parseFloat( pr3 );
              if( isNaN( tmp ) )
              {
                pr3 = eval( pr3 );
              }else
              {
                pr3 = tmp;
              }
//alert( oprMath2+' :  '+pr3);
            linha = 'val = ( ' + pr1 + oprMath1 + pr2 + oprMath2 + pr3 + ' )';
            //alert(linha);
          }else
          {
            linha = 'val = ( ' + pr1 + oprMath1 + pr2 + ' )';
          }


//alert( 'cmd:'+linha );
          //eval( 'val = ( ' + pr1 + opr + pr2 + ' )' );
          eval( linha );
          col_id.value = val.toFixed(4);
        }// End if( str != '' )
      }// End if( tipo == 'calculado' )
    }// End for( ix2 = 1; ix2 <= nrc; ix2 ++ )
  }// End for( ix1 = 1; ix1 <= nrl; ix1 ++ )

/**
 *  restaura a linha corrente
 */
  dbgrid_linha_corrente[ dbg_idx ] = linha_corrente;

}// End function dbGrid_refresh( objID )
  
/*
    if( old != 0
        && old != nrl
      )
    {
      for( ix = 1; ix <= nrc; ix ++ )
      {
        nCol = nObj + '_col_' + old + '_' + ix;
        objSTYLE = findDOM( nCol, 1 );
        objSTYLE.backgroundColor='#FFFFFF';

        if( ix == dbgrid_modo_edit[idx] && dbgrid_modo_edit[idx] > 0  )
        {
          nmCol = nCol;
        }// End if()
      }// End for( ix = 1; ix <= nrc; ix ++ )
      if( nmCol != null )
      {
        setDBgrid_edit_disable( nmCol );
      }// End
    }// End if( old != 0 )
*/
//alert(nrc);


function findOPR( str, s )
{
  var ix1 = str.indexOf( '/', s );
  var ix2 = str.indexOf( '*', s );
  var ix3 = str.indexOf( '+', s );
  var ix4 = str.indexOf( '-', s );
//alert(str+' '+ix1+' : '+ix2+' : '+ix3+' : '+ix4);
  var ix_ = 1000;
  if( ix1 != -1 && ix1 < ix_ )
    ix_ = ix1;
  if( ix2 != -1 && ix2 < ix_ )
    ix_ = ix2
  if( ix3 != -1 && ix3 < ix_ )
    ix_ = ix3;
  if( ix4 != -1 && ix4 < ix_ )
    ix_ = ix4
  if(ix_ == 1000)
    ix_ = -1;
  return( ix_ );
}// End
</script> 


<?php


?>
