/**
 * Classe: Mensage
 *         =======
 *
 *  Propriedades  *******************************
 *    theForm  =>
 *  Metodos Privados  ***************************
 *  Metodos Publicos  ***************************
 *    send      =>
 *    get       =>
 *    seek      =>
 *    _callback =>
 *    _getback  =>
 *    loaded    =>
 */
function Mensage()
{
  /**
   *  Propriedades ==========================================================
   */
  this.objTexto;
  this.statusCMD;
  this.loginUSER;

  /**
   *  Metodos  ==============================================================
   */
   
  /* metodo init  -------------------------------------------------------  */
  this.init = function(){
    this.objTexto = "";
    this.statusCMD = false;
  };

  this.set_login = function(usr)
  {
    this.loginUSER = usr;
  };


/* metodo send  ------------------------------------------------------  */
  this.send = function(txt) {
    var styleTX = findDOM( 'tx', 1 );
    styleTX.visibility = 'hidden';
    //alert('send: ' + txt);
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._backSend",
                           "MsgServer.exec_cmd",
                           txt,
                           false);
  }// End
  
/*
 *  Gerar o Script executável
 *  ========================================================================= //
 */
  this.gerar = function( txt )
  {
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._retGerar",
                           "MsgServer.create_script",
                           txt,
                           false);
  }
  this._retGerar =function( text, callId )
  {
    if( text.indexOf('Ok') == -1 )
    {
      alert( 'Erro na geração do Script' );
    }else
    {
      alert( 'Script gerado com Sucesso.' );
    }
  }

  this.get_ajuda = function( txt )
  {
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._get_ajuda",
                           "MsgServer.get_ajuda",
                           txt,
                           false);
  }
  this._get_ajuda =function( text, callId )
  {
    //alert( text );
    // codigo provisório
    cmd = 'winPage3_Frame.innerHTML = "Objeto Selecionado: '+objSELECTED;
    cmd += '<br>' + text + '";';
    eval(cmd);

  }

/**
 *  Method exeCMD
 */
  this.exeCMD = function( txt )
  {
    var styleTX = findDOM( 'tx', 1 );
    styleTX.visibility = 'hidden';
    //alert('send CMD: ' + txt);
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._statusCMD",
                           "MsgServer.exec_cmd",
                           txt,
                           false);
    //alert('send');
  }// End
  this._statusCMD = function( text, callId )
  {
    //alert( 'ret: ' + text );
    refreshPage(text);
    Mensage.getCompPrj( loginUSER + ':' + nProjeto_ );
    //alert( 'login: '+loginUSER+'  proj: '+nProjeto_);
  }// End


/**
 *  Method getProjetos
 */
  this.getProjetos = function( txt )
  {
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._ret_projetos",
                           "MsgServer.get_projetos",
                           txt,
                           false);
  }// End
  this._ret_projetos = function( text, callId )
  {
    //alert(text);
    var ix_ = text.indexOf( ':' );
    var in_ = 0;

    setTreeView_reset( 'treeView1' );
    
    while( ix_ != -1 )
    {
      addTreeView_Item( 'treeView1', text.substr( in_, (ix_ - in_) ) );
      
      in_ = ix_ +1;
      ix_ = text.indexOf( ':', in_ );
    }
    //alert( 'ret: ' + text );
  }// End


/**
 *  Verifica se existe Projeto aberto na área do Usuário
 */
  this.get_prj_open = function()
  {
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._ret_prj_open",
                           "MsgServer.back_projeto",
                           this.loginUSER,
                           false);
  }//End function
  
/**
 *  trata resposta de Projeto aberto.
 */
  this._ret_prj_open = function( resp, callId )
  {
    if ( resp == 'nil' )
    {
      //alert( 'erro: ' + resp );
    }else
    {
      var ix = resp.indexOf( ':' );
      nProjeto_ = resp.substr( 0, ix);

      if ( resp.substr( ix +1) == 'ok' ||
           resp.substr( ix +1) == 'OK'
         )
      {
        flagEdit_ = false;
      }else
      {
        flagEdit_ = true;
      }// End if()
      
      //alert( nProjeto_ + '  :  ' + flagEdit_ + '  :  ' + resp );
      //Mensage.abrirPrj( loginUSER + ':' + nProjeto_ );
      Mensage._back_proj( 'ok','' );
    }
  }

/* metodo get  --------------------------------------------------------  */
  this.get = function()
  {
    var styleTX = findDOM( 'tx', 1 );

    stTX = styleTX.visibility;
    if( stTX == 'hidden' )
      styleTX.visibility = 'visible';
    else
      styleTX.visibility = 'hidden';

    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._callback",
                           "MsgServer.read",
                           "metodoGet",
                           false);
  }// End
/* metodo _callback  --------------------------------------------------  */
  this._callback = function( text, callId )
  {
    var styleRX = findDOM( 'rx', 1 );
//alert('back: ' + text);
//    var objID = findDOM( 'boxTexto', 0 );
//    objID.value = text;
    //this.boxTexto.value = text;
    var styleRX = findDOM( 'rx', 1 );

    stRX = styleRX.visibility;
    if( stRX == 'hidden' )
      styleRX.visibility = 'visible';
    else
      styleRX.visibility = 'hidden';

    setTimeout('Mensage.get()',1000);
  }// End




/**
 *  getCompPrj()
 *  ------------------------------------------------------------------------- //
 *  solicita os componentes do Projeto aberto
 */
  this.getCompPrj = function( prj_ )
  {
    //alert('getComp. ' + prj_ );
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._back_comp_proj",
                           "MsgServer.get_componente",
                           prj_,
                           false );
    //alert( 'get Comps: ' + prj_ );
  }
  
/**
 *  _back_comp_proj()
 *  ------------------------------------------------------------------------- //
 *  atualiza comboBox com os nomes dos componentes do Projeto
 */
  this._back_comp_proj = function( comps, callId )
  {
    //alert( 'componentes: ' + comps );

    len = comps.length;
    ix_ = comps.indexOf( ':' );

    if( ix_ != -1 )
    {
      iix = 0;
      ixx = 0;
      comboBox_lista[0][iix] = 'page1';
      iix ++;
      while( ix_ != -1 && ix_ < len )
      {
        comboBox_lista[0][iix] = comps.substr( ixx, (ix_ - ixx) );
        ixx = ix_ +1;
        ix_ = comps.indexOf( ':', ix_ +1 );
        iix ++;
        comboBox_itens[0] = iix;
      }// End for()
    }else
    {
      comboBox_lista[0][0] = 'page1';
    }// End if()
    setComboBox_value( 'comboBox1', 'page1' );

    Mensage.get_atributo( loginUSER + ':' + nProjeto_ + ':' + 'page1' );

  }// End function()
  

/**
 *  fecharPrj()
 *  ------------------------------------------------------------------------- //
 *  solicita o fechamento do projeto em edição
 */
  this.fecharPrj = function( prj_ )
  {
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._close_proj",
                           "MsgServer.close_projeto",
                           prj_,
                           false );
  }
  
/**
 *  _close_proj()
 *  ------------------------------------------------------------------------- //
 *  ajusta o ambiente RAD de acordo com o status da requisição de fechamento
 */
  this._close_proj = function( flag, callId )
  {
    if ( flag.toLowerCase() == 'ok' )
    {
      new_page    = 'emBranco.php';
      nProjeto_   = '';
      objSELECTED = '';
      flagEdit_   = false;

      setWinPage_location( 'winPage2', new_page );

      setWinPage_visible( 'winPage2', 'hidden' );

      setSubMenu_status( 'subMenu1', '7', '0' );
      setSubMenu_status( 'subMenu3', '2', '0' );

      setComboBox_reset( 'comboBox1' );
      setFieldSet_reset( 'fieldSet1' );

    }else
    {
      alert( 'ocorreu um Erro durante o fechamento do Projeto...' );
    }
  }
  
  this.salvarPrj = function( prj_ )
  {
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._save_proj",
                           "MsgServer.save_projeto",
                           prj_,
                           false );
    //alert('Salvar');
  }
  this._save_proj = function( flag, callId )
  {
    //alert( 'resp: ' + flag );
    if ( flag.toLowerCase() == 'ok' )
    {
      flagEdit_ = false;
      setSubMenu_status( 'subMenu1', '6', '0' );
      setToolBar_status( 'toolBar1', '3', '0' );

    }// End if()
    //alert( flag );
  }// End function()


  this.novoPrj = function( prj_ )
  {
    if ( nProjeto_ == '' )
    {
      var callId = jsrsCall( "core/rpcServer.Studio.php",
                             "Mensage._new_proj",
                             "MsgServer.new_projeto",
                             prj_,
                             false );
    }else
    {
      if( flagEdit_ )
      {
        Mensage.salvarPrj( loginUSER + ':' + nProjeto_ );
      }else
      {
        Mensage.fecharPrj( loginUSER + ':' + nProjeto_ );
      }// End if()
      
      if ( nFlagCounter == false )
      {
        var nv = nTentativas_;
        nFlagCounter = true;
      }else
      {
        nv --;
      }
      //alert(nv);
      if ( nv = 0 )
      {
        nFlagCounter = false;
        window.clearTimeout( nControle_ );
        alert( 'Erro...(criando novo projeto)' );
      }else
      {
        nControle_ = window.setTimeout( "Mensage.novoPrj( loginUSER  )", nWait_ );
      }// End if()
      
    }// End if()
  }// End Metodo
  this._new_proj = function( flag, callId )
  {
    resp = flag.toLowerCase();
    
    if ( resp.indexOf( 'ok' ) != -1 )
    {
      nProjeto_   = 'novo';
      objSELECTED = 'page1';
      
      new_page = 'desenv/usuarios/' + loginUSER + '/' + nProjeto_ + '.main.php';
      new_page += '?loginUSER=' + loginUSER;

      //alert( flag + ' pagina: ' + new_page );
      
      setWinPage_location( 'winPage2', new_page );
      setWinPage_titulo( 'winPage2', 'Página Principal - ' + nProjeto_ );
      setWinPage_visible( 'winPage2', 'visible' );

      setSubMenu_status( 'subMenu1', '7', '1' );
      setSubMenu_status( 'subMenu3', '2', '1' );

      flagEdit_ = true;
      setSubMenu_status( 'subMenu1', '6', '1' );
      setToolBar_status( 'toolBar1', '3', '1' );

      Mensage.getCompPrj( loginUSER + ':' + nProjeto_ );

    }else
    {
      alert( 'Erro: ' + flag );
    }// End if()
  }

  this.abrirPrj = function( prj_ )
  {
    //alert(prj_ );
    var styleTX = findDOM( 'tx', 1 );
    var styleRX = findDOM( 'rx', 1 );

    styleTX.visibility = 'visible';

    stRX = styleRX.visibility;
    if( stRX == 'hidden' )
      styleRX.visibility = 'visible';
    else
      styleRX.visibility = 'hidden';

    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._back_proj",
                           "MsgServer.open_projeto",
                           prj_,
                           false );
  }

/**
 * ajusta ambiente RAD com o Projeto aberto
 */
  this._back_proj = function( flag, callId )
  {
//alert( 'flag: ' + flag );
    if ( flag == 'ok' )
    {
      objSELECTED = 'page1';

      new_page = 'desenv/usuarios/' + loginUSER + '/' + nProjeto_ + '.main.php';
      new_page += '?loginUSER=' + loginUSER;
      
      // executa o Projeto aberto
      setWinPage_location( 'winPage2', new_page );
      setWinPage_titulo( 'winPage2', 'Pagina Principal - ' + nProjeto_ );
      //setWinPage_tit();

      setWinPage_visible( 'winPage4', 'hidden' );
      setWinPage_visible( 'winPage2', 'visible' );

      setSubMenu_status( 'subMenu1', '7', '1' );
      setSubMenu_status( 'subMenu3', '2', '1' );
//alert(loginUSER + ':' + nProjeto_);
      Mensage.getCompPrj( loginUSER + ':' + nProjeto_ );
      
    }else
    {
      alert( 'Erro' );
    }
  }//


  this.logIN = function()
  {
    var styleTX = findDOM( 'tx', 1 );
    var styleRX = findDOM( 'rx', 1 );
    var hr = new Date();
    
    if ( ( evtMOUSE_H == hr.getHours() &&
           evtMOUSE_M == hr.getMinutes() )   ||
         ( evtKEY_H == hr.getHours() &&
           evtKEY_M == hr.getMinutes() )
        )
    {
      tx_ = "X:" + this.loginUSER;
    }else
    {
      tx_ = "o:" + this.loginUSER;
    }
    
    styleTX.visibility = 'visible';

    stRX = styleRX.visibility;
    if( stRX == 'hidden' )
      styleRX.visibility = 'visible';
    else
      styleRX.visibility = 'hidden';

    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._backlogin",
                           "MsgServer.log_write",
                           tx_,
                           false);
  }

  this._backlogin = function(text, callId)
  {
    setTimeout('Mensage.logIN()',30000);
  }// End


  /* metodo _getback  ---------------------------------------------------  */
  this._getback = function(text, callId) {
    alert(text);
  }// End







  /* metodo seek  -------------------------------------------------------  */
  this.seek = function(){
    var text, callId;
    var objID = findDOM( 'e-mail', 0 );
    var text = objID.value;
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._getback",
                           "MsgServer.seek",
                           text,
                           false);
  }// End



  /* metodo loaded  -----------------------------------------------------  */
  this.loaded = function(){
    var text, callId;
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._getback",
                           "MsgServer.loaded",
                           nameCore,
                           false);
  }// End

  /*
   *  metodo get_winPage()
   *  -------------------------------------------------------------------  //
   */
  this.get_winPage = function( obj ) {
    var styleTX = findDOM( 'tx', 1 );
    styleTX.visibility = 'hidden';
//alert( 'send OBJ: ' + obj );
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._set_winPage",
                           "MsgServer.get_winPage",
                           obj,
                           false);
  }// End
  
  this._set_winPage = function( obj ) {
    //alert( obj );
    //setUp_winPage( obj );
    setWinPage_selected( obj );
  }

/*
 *  metodo set_atributo()
 *  -------------------------------------------------------------------  //
 */
  this.set_atributo = function( obj ) {
//alert( 'send OBJ: ' + obj );
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._ret_atributo",
                           "MsgServer.set_atributo",
                           obj,
                           false);
  }// End

  this._ret_atributo = function( resp )
  {
    //alert( 'OK : ' + resp );
    if( resp.indexOf( 'erro' ) == -1 )
    {
      //refreshPage( 2 );
      
      // se não ocorreu erro na modificação do atributo do objeto
      // na área de trabalho do usuário
      // modifica o atributo do objeto na memória
      winPage2.mudar_atrib( resp );
      
      flagEdit_ = true;
      setSubMenu_status( 'subMenu1', '6', '1' );
      setToolBar_status( 'toolBar1', '3', '1' );
    }else
    {
      alert( 'Mensagem: ' + resp );
    }
  }// End

  /*
   *  metodo get_atributo()
   *  -------------------------------------------------------------------  //
   */
  this.get_atributo = function( obj ) {
    //alert( 'send OBJ: ' + obj );
    var callId = jsrsCall( "core/rpcServer.Studio.php",
                           "Mensage._set_atributo",
                           "MsgServer.get_atributo",
                           obj,
                           false);
  }// End
  
  this._set_atributo = function( obj )
  {
    //alert( 'rec: ' + obj );
    if( obj.substr(0,4) == 'erro' )
    {
      alert( "Erro: " + obj );
    }else
    {
      //alert( 'return: ' + obj );
      setFieldSet_visibleAll( 'fieldSet1', 'hidden' );
      ini = 1;
      ix_obj = obj.indexOf(';');

      while( ix_obj > 0 )
      {
        atrib = obj.substr( 0, ix_obj );
        temp  = obj.substr(ix_obj+1);

        //alert(atrib);
      
        campo = 'fieldSet1_field' + ini;

        ix_separador = atrib.indexOf(':');
        atrib_1 = atrib.substr( 0, ix_separador );
        atrib_2 = atrib.substr( ix_separador+1 );
      
        setFieldSet_value( 1, ini, atrib_2 );
        setFieldSet_label( 1, ini, atrib_1 );
      
        setFieldSet_visible( campo, 'visible' );
        setFieldSet_enable( 0, (ini -1), 'true' )

        ix_obj = temp.indexOf(';');
        obj = temp;

        ini ++;
      }
    }
//    alert( 'ok' );
  }

}// End function Mensage()


var Mensage = new Mensage(); // Pseudo static
//Mensage.get();

