/**
 * 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( "bin/rpcServer.Studio.php",
                           "Mensage._backSend",
                           "MsgServer.exec_cmd",
                           txt,
                           false);
  }// End
  
/*  Gerar o Script executável  */
  this.gerar = function( txt )
  {
    var callId = jsrsCall( "bin/rpcServer.Studio.php",
                           "Mensage._retGerar",
                           "MsgServer.create_script",
                           txt,
                           false);
  }
  this._retGerar =function( text, callId )
  {
    alert( text );
  }

  this.get_ajuda = function( txt )
  {
    var callId = jsrsCall( "bin/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( "bin/rpcServer.Studio.php",
                           "Mensage._statusCMD",
                           "MsgServer.exec_cmd",
                           txt,
                           false);
  }// 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( "bin/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



  this.get_prj_open = function()
  {
    var callId = jsrsCall( "bin/rpcServer.Studio.php",
                           "Mensage._ret_prj_open",
                           "MsgServer.back_projeto",
                           this.loginUSER,
                           false);
  }//End function
  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_ );
      //Mensage.abrirPrj( loginUSER + ':' + nProjeto_ );
      Mensage._back_proj( 'ok','' );
    }
  }

/* metodo get  --------------------------------------------------------  */
  this.get = function()
  {
    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( "bin/rpcServer.Studio.php",
                           "Mensage._callback",
                           "MsgServer.read",
                           "metodoGet",
                           false);
  }// End
/* metodo _callback  --------------------------------------------------  */
  this._callback = function( text, callId )
  {
//alert('back: ' + text);
//    var objID = findDOM( 'boxTexto', 0 );
//    objID.value = text;
    //this.boxTexto.value = text;
    setTimeout('Mensage.get()',1000);
  }// End




  this.getCompPrj = function( prj_ )
  {
//alert( 'getComp: ' + prj_ );
    var callId = jsrsCall( "bin/rpcServer.Studio.php",
                           "Mensage._back_comp_proj",
                           "MsgServer.get_componente",
                           prj_,
                           false );
  }
  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()
  

  this.fecharPrj = function( prj_ )
  {
    var callId = jsrsCall( "bin/rpcServer.Studio.php",
                           "Mensage._close_proj",
                           "MsgServer.close_projeto",
                           prj_,
                           false );
  }
  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( 'Erro' );
    }
  }
  
  this.salvarPrj = function( prj_ )
  {
    var callId = jsrsCall( "bin/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( "bin/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( "bin/rpcServer.Studio.php",
                           "Mensage._back_proj",
                           "MsgServer.open_projeto",
                           prj_,
                           false );
  }
  this._back_proj = function( flag, callId )
  {
//    alert( flag );
    if ( flag == 'ok' )
    {
      objSELECTED = 'page1';

      new_page = 'desenv/usuarios/' + loginUSER + '/' + nProjeto_ + '.main.php';
      new_page += '?loginUSER=' + loginUSER;

      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' );

      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( "bin/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( "bin/rpcServer.Studio.php",
                           "Mensage._getback",
                           "MsgServer.seek",
                           text,
                           false);
  }// End



  /* metodo loaded  -----------------------------------------------------  */
  this.loaded = function(){
    var text, callId;
    var callId = jsrsCall( "bin/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( "bin/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( "bin/rpcServer.Studio.php",
                           "Mensage._ret_atributo",
                           "MsgServer.set_atributo",
                           obj,
                           false);
  }// End

  this._ret_atributo = function( resp )
  {
    if( resp.indexOf( 'erro' ) == -1 )
    {
      // alert( 'OK : ' + obj );
      //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( 'Erro: ' + resp );
    }
  }// End

  /*
   *  metodo get_atributo()
   *  -------------------------------------------------------------------  //
   */
  this.get_atributo = function( obj ) {
//alert( 'send OBJ: ' + obj );
    var callId = jsrsCall( "bin/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( 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();

