
Ext.ns('blablar');
Ext.QuickTips.init();

function renderPost(value, p, record)
{
   var post = record.data['contents'].replace(/(<([^>]+)>)/ig, "");
   // strip html

   if (post.length > 200)
   {
      post = post.substr(0, 200) + ' [...]';
      // shorten it
   }

   return String.format('<p><b>{0}</b><br />{1}</p>', value, post);
}


function nuevoAmigo()
{
   var nuevo = new Ext.FormPanel(
   {
      labelWidth : 80,
      url : './agregarAmigo.aspx',  // en ejemplo
      iconCls : 'gear-icon',
      frame : true,
      monitorValid : true,
      // ENTRADA
      items : [
      {
         xtype : 'numberfield',
         fieldLabel : 'Celular',
         name : 'Celular',
         emptyText : 'Sin 044/045',
         width : 100,
         vtype : 'alphanum',
         maxLength : 10,
         minLength : 10,
         maxLengthText : 'El numero de caracteres m&iacute;nimo es  10',
         minLengthText : 'El numero de caracteres m&acute;ximo es  10',
         allowBlank : false
      }
      ,
      {
         xtype : 'textfield',
         fieldLabel : 'Nombre',
         emptyText : 'Nombre completo del contacto',
         name : 'Nombre',
         width : 260,
         maxLength : 150,
         maxLengthText : 'El numero de caracteres m&aacute;ximo es de 150',
         allowBlank : false
      }
      ,
      {
         fieldLabel : 'Diminutivo',
         name : 'NombreCorto',
         xtype : 'textfield',
         emptyText : 'Diminutivo ',
         allowBlank : false,
         vtype : 'alphanum',
         maxLength : 10,
         width : 120
      }
      ]
   }
   );
   var agregarAmigo_win = new Ext.Window(
   {
      title : 'Agregar Amigo (N&uacute;mero Frecuente)',
      layout : 'fit',
      width : 400,
      height : 180,
      iconCls : 'friends-icon',
      closable : true,
      resizable : false,
      plain : true,
      // border : true,
      draggable : true
      , modal : true
      , closeAction : 'close'
      , resizable : false
      , shadow : true
      , items : [nuevo]
      , buttons : [
      {
         text : 'Guardar',
         formBind : true,
         // FUNCION DE BOTON PARA ENVIAR MENSAJE
         handler : function()
         {
            nuevo.getForm().submit(
            {
               method : 'POST',
               waitTitle : 'Espere',
               waitMsg : 'Guardando...',

               // FUNCION PARA ENVIO DE MENSAJE

               success : function()
               {
                  // Ext.Msg.alert('Aviso', 'Mensaje enviado.');
                  agregarAmigo_win.close();
               }
               ,

               // VALIDACIONES

               failure : function(form, action)
               {
                  if(action.failureType == 'server')
                  {
                     obj = Ext.util.JSON.decode(action.response.responseText);
                     Ext.Msg.alert('Fallo el env&iacute;o de mensaje', obj.errors.reason);
                  }
                  else
                  {
                     Ext.Msg.alert('Warning!', 'Server is unreachable : ' + action.response.responseText);
                  }
               }
            }
            );
         }
      }
      ]
   }
   );
   agregarAmigo_win.show();
}

function cargarAmigo(r)
{
   var nuevo = new Ext.FormPanel(
   {
      labelWidth : 80,
      id: 'nuevo',
      url : './agregarAmigo.aspx',  // en ejemplo
      iconCls : 'gear-icon',
      frame : true,
      monitorValid : true,
      // ENTRADA
      items : [
      {
         xtype : 'numberfield',
         fieldLabel : 'Celular',
         name : 'Celular',
         emptyText : 'Sin 044/045',
         width : 100,
         vtype : 'alphanum',
         maxLength : 10,
         minLength : 10,
         maxLengthText : 'El numero de caracteres m&iacute;nimo es  10',
         minLengthText : 'El numero de caracteres m&acute;ximo es  10',
         allowBlank : false
      }
      ,
      {
         xtype : 'textfield',
         fieldLabel : 'Nombre',
         emptyText : 'Nombre completo del contacto',
         name : 'Nombre',
         width : 260,
         maxLength : 150,
         maxLengthText : 'El numero de caracteres m&aacute;ximo es de 150',
         allowBlank : false
      }
      ,
      {
         fieldLabel : 'Diminutivo',
         name : 'NombreCorto',
         xtype : 'textfield',
         emptyText : 'Diminutivo ',
         allowBlank : false,
         vtype : 'alphanum',
         maxLength : 10,
         width : 120
      }
      ]
   }
   );

   var agregarAmigo_win = new Ext.Window(
   {
      title : 'Actualizar Amigo (N&uacute;mero Frecuente)',
      layout : 'fit',
      width : 400,
      height : 180,
      iconCls : 'friends-icon',
      closable : true,
      resizable : false,
      plain : true,
      // border : true,
      draggable : true
      , modal : true
      , closeAction : 'close'
      , resizable : false
      , shadow : true
      , items : [nuevo]
      , buttons : [
      {
         text : 'Guardar',
         formBind : true,
         // FUNCION DE BOTON PARA ENVIAR MENSAJE
         handler : function()
         {
            nuevo.getForm().submit(
            {
               method : 'POST',
               waitTitle : 'Espere',
               waitMsg : 'Guardando...',

               // FUNCION PARA ENVIO DE MENSAJE

               success : function()
               {
                  Ext.Msg.alert('Aviso', 'Se guardo con exito');
                  agregarAmigo_win.close();
                  //var t = Ext.getCmp('listaAmigos');
                  //t.reload();
               }
               ,

               // VALIDACIONES

               failure : function(form, action)
               {
                  if(action.failureType == 'server')
                  {
                     obj = Ext.util.JSON.decode(action.response.responseText);
                     Ext.Msg.alert('Fallo el env&iacute;o de mensaje', obj.errors.reason);
                  }
                  else
                  {
                     Ext.Msg.alert('Warning!', 'Server is unreachable : ' + action.response.responseText);
                  }
               }
            }
            );
         }
      }
      ]
   }
   );

   agregarAmigo_win.show();
   var act = Ext.getCmp('nuevo');
   var cel = r.get('celular');
   act.getForm().load(
   {
      url : 'obtenDatosAmigo.aspx?celular=' + cel, waitTitle : 'Cargando...', waitMsg : 'Cargando...'
   }
   );
}

blablar.listaAmigos = Ext.extend(Ext.grid.GridPanel,
{
   initComponent : function()
   {
      var config =
      {
         trackMouseOver : true
         , columns : [
         {
            header : 'No'
            , width : 20
            , resizable : false
            , menuDisabled : true
            , sortable : true
            , dataIndex : 'lineaNo'
         }
         ,
         {
            header : 'Nombre'
            , width : 180
            , resizable : false
            , menuDisabled : true
            , sortable : true
            , dataIndex : 'nombre'
         }
         ,
         {
            header : 'Nombre Corto'
            , menuDisabled : true
            , width : 80
            , sortable : true
            , dataIndex : 'nombreCorto'
         }
         ,
         {
            header : "Celular"
            , menuDisabled : true
            , width : 90
            , sortable : true
            , dataIndex : 'celular'
         }
         ]
         , viewConfig :
         {
            forceFit : true, enableRowBody : true, getRowClass : this.getRowClass
         }
         , loadMask :
         {
            msg : 'Cargando ...'
         }
      }
      ;

      this.on('rowdblclick', function (grid, rowIndex, e)
      {
         var r = grid.getStore().getAt(rowIndex);
         cargarAmigo(r);
      }
      );

      Ext.apply(this, Ext.apply(this.initialConfig, config));


      this.bbar = new Ext.PagingToolbar(
      {
         store : this.store
         , displayInfo : true
         , pageSize : 25
         , displayMsg : 'Mostrando del {0} al {1}, {2} en total'
         , emptyMsg : "No has enviado mensajes."

      }
      );
      blablar.listaAmigos.superclass.initComponent.apply(this, arguments);

      // load the store at the latest possible moment
      this.on(
      {
         afterlayout :
         {
            scope : this, single : true, fn : function()
            {
               this.store.load(
               {
                  params :
                  {
                     start : 0, limit : 25
                  }
               }
               );
            }
         }

      }
      );

   }
   // eo function initComponent
   /* , getRowClass : function(record, rowIndex, p, store)
   {
   p.body = record.get('mensaje');
   return p.body ? 'x-grid3-row-with-body' : '';
   } */

   , onRender : function()
   {
      this.store.load(
      {
         params :
         {
            start : 0, limit : 25
         }
      }
      );
      blablar.listaAmigos.superclass.onRender.apply(this, arguments);
   }
   // eo function onRender
}
);

Ext.reg('listaAmigos', blablar.listaAmigos);


function cargaListaAmigos()
{

   var lstAmigos_win = new Ext.Window(
   {
      title : 'Tus amigos (N&uacute;meros Frecuentes)',
      layout : 'fit',
      width : 610,
      height : 400,
      iconCls : 'friends-icon',
    renderTo: document.hostess,
      // closable : true,
      resizable : false,
      plain : true,
      // border : true,
      draggable : true
      , modal : false
      , closeAction : 'close'
      , resizable : false
      , shadow : true
      , items : [
      {
         xtype : 'listaAmigos'
         , store : new Ext.data.JsonStore(
         {
            id : 'listaAmigos'
            , totalProperty : 'totalCount'
            , root : 'rows'
            , url : './listarAmigos.aspx'
            // , sm : new Ext.grid.RowSelectionModel({singleSelect : true}
            , sm : new Ext.grid.RowSelectionModel(
            {
               singleSelect : true
               , listeners :
               {
                  rowselect : function(sm, rowIndex, record)
                  {
                     selRecordStore = record;
                  }
                  ,
                  rowdblclick : function(grid, rowIndex, e)
                  {
                     selRecordStore = record;
                  }
               }
            }
            )
            , fields : [
            {
               name : 'lineaNo', type : 'auto'
            }
            ,
            {
               name : 'nombre', type : 'string'
            }
            ,
            {
               name : 'nombreCorto', type : 'string'
            }
            ,
            {
               name : 'celular', type : 'string'
            }
            ,
            {
               name : 'id', type : 'string'
            }
            ]
         }
         )
         , width : 610
         , height : 363
      }
      ]
      , buttons : [
      {
         text : 'A&ntilde;adir n&uacute;mero',
         // formBind : true,
         // FUNCION DE BOTON PARA ENVIAR MENSAJE
         handler : function()
         {
            nuevoAmigo();
         }
      }
      ]
   }
   );

   lstAmigos_win.show();
}

