<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 ] )
      {
        dbgrid_modo_edit[ idx ] = getDBgrid_coluna(objID);
        
        setDBgrid_edit_enable( objID );
      }// End
    }// End
    window.event.cancelBubble = true;
  }// 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 == 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()
  
//
//  dbGrid_mouseMove()
//
  function dbGrid_mouseMove( objID )  
  {  
    if( objID.indexOf('dbGrid') != -1 )  
    {  
    
    }  
  }// End function()
  
//  ------------------------------------------------------------  //
//  Lista de Métodos do Componente                                //
//  Conjunto de funções para manipular as propriedades do Objeto  //
//  ------------------------------------------------------------  //


//
//  coloca a coluna em modo de Edição
//
  function setDBgrid_edit_enable( objID )
  {
    objDOM = findDOM( objID, 0 );
    objDOM.readOnly = false;

    objSTYLE = findDOM( objID, 1 );
    objSTYLE.color='#FF0000';
    objSTYLE.fontWeight='bold';
    objSTYLE.backgroundColor='#EEEEEE';
  }// End

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

    objSTYLE = findDOM( objID, 1 );
    objSTYLE.color='#000000';
    objSTYLE.fontWeight='normal';
  }// End


//
//  Funções auxiliares
//  ========================================================================= //
//
 
//
//  retorna o numero da linha da grid
//
  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
//
  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 )
  {
    ret = null;
    ix  = 0;
    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
    return ret;
  }// End

//
//  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
 *  ------------------------------------------------------------------------  //
*/
  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);

    //
    //  desmarca linha corrente anterior  ----------------------------------  //
    //
    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);
    //
    //  Marca a nova linha Corrente  ---------------------------------------  //
    //
    for( ix = 1; ix <= nrc; ix ++ )
    {
      nCol = nObj + '_col_' + nrl + '_' + ix;
      objDOM = findDOM( nCol, 1 );

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

    if( objID.indexOf( 'dbgrid1' ) != -1 )
    {
/**
 *  reload dbGrid2
 */
    keyValue = getDBgrid_key_value( nObj, idx );
    arr = keyValue.split( '*' );
    key = 'dbgrid2:' + arr[0];
    //alert( key );
    dataAccess.getFields( 'dbgrid2' );
    dataAccess.getResult( key  );
    }// End
  }// End function setDBgrid_linha( objID )
  
/**
 *  Evento keyPress
 */
  function dbgrid_keyPress( objID, cod )
  {
    if( objID != '' )
    {
      if( objID.indexOf( 'dbgrid' ) != -1 &&
          objID.indexOf( '_col_' ) != -1
        )
      {
      }// 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()
 *  ========================================================================= //
 */
  function getDBgrid_col_value( objID )
  {
    objDOM = findDOM( objID,0 );
    
    return objDOM.value;
  }// 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
  }
//  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;
}


/**
 */
function resetDBgrid( dbGrid )
{
//alert('passo 0 : ' + dbGrid);
  var arrField = new Array();
//alert('passo 1');
  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;
}

/**
 *  Incluir uma linha em brando no final da Gride.
 */
function incDBgrid_nova_linha( dbGrid )
{
  objDOM = findDOM( dbGrid, 0 );

  alert( dbGrid );
}
  
  
  
  
</script> 

<?php


?>
