Ext.ns('blablar');
Ext.QuickTips.init();

Ext.ux.IFrameComponent = Ext.extend(Ext.BoxComponent,
{
   onRender : function(ct, position)
   {
      this.el = ct.createChild(
      {
         tag : 'iframe', id : 'iframe-' + this.id, frameBorder : 0, src : this.url
      }
      );
   }
}
);


function ventanaLogin()
{
   var login = new Ext.FormPanel(
   {
      labelWidth : 70,
      url : 'login.aspx',
      frame : true,

      bodyStyle : 'padding:5px;',
      // title : 'Ingresa con tu cuenta de correo',
      defaultType : 'textfield',
      monitorValid : true,
      // Specific attributes for the text fields for username / password.
      // The "name" attribute defines the name of variables sent to the server.
      items : [
      {
         xtype : 'box', autoEl :
         {
            tag : 'div', html : '<div class="app-msg"><img class="img-msg" src="images/32user-icon.png" />Ingresa con tu cuenta de correo</div>'
         }
      }
      ,
      {
         fieldLabel : 'Correo',
         name : 'loginUsername',
         width : 150,
         vtype : 'email',
         spellcheck : false,
         allowBlank : false,
         validationEvent : false
      }
      ,
      {
         fieldLabel : 'Contrase&ntilde;a',
         name : 'loginPassword',
         width : 150,
         inputType : 'password',
         allowBlank : false,
         validationEvent : false
      }

      ],

      // All the magic happens after the user clicks the button
      buttons : [
      {
         text : '&iquest;Olvidaste tu contrase&ntilde;a?',
         handler : function()
         {
            recuperarPassword();
         }
      }
      ,
      {
         text : 'Aceptar',
         formBind : true,
         // Function that fires when user clicks the button
         handler : function()
         {
            login.getForm().submit(
            {
               method : 'POST',
               waitTitle : 'Espere...',
               waitMsg : 'Autentificando...',

               // Functions that fire (success or failure) when the server responds.
               // The one that executes is determined by the
               // response that comes from login.asp as seen below. The server would
               // actually respond with valid JSON,
               // something like : response.write "{ success: true}" or
               // response.write "{ success: false, errors: { reason: 'Login failed. Try again.' }}"
               // depending on the logic contained within your server script.
               // If a success occurs, the user is notified with an alert messagebox,
               // and when they click "OK", they are redirected to whatever page
               // you define as redirect.

               success : function()
               {
                  var redirect = 'default.aspx';
                  window.location = redirect;
               }
               ,

               // Failure function, see comment above re : success and failure.
               // You can see here, if login fails, it throws a messagebox
               // at the user telling him / her as much.

               failure : function(form, action)
               {
                  if(action.failureType == 'server')
                  {
                     obj = Ext.util.JSON.decode(action.response.responseText);
                     Ext.Msg.show({icon: 'cant-msg-icon', title: 'Aviso', msg: obj.errors.reason, buttons: Ext.Msg.OK});
                  }
                  else
                  {
                     Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText);
                  }
                  login.getForm().reset();
               }
            }
            );
         }
      }
      ]
   }
   );

   var login_win = new Ext.Window(
   {
      layout : 'fit',
      width : 300,
      height : 180,
      // closable : true,
      closeAction : 'hide',
      resizable : false,
      plain : true,
      border : false,
      modal : true,
      items : [login]
   }
   );

   login_win.show();
}

function recuperarPassword()
{
   var olvidadizo = new Ext.FormPanel(
   {
      labelWidth : 70,
      url : 'resetpwd.aspx',
      frame : true,
      title : 'Recuperaci&oacute;n de Contrase&ntilde;a',
      defaultType : 'textfield',
      monitorValid : true,
      items : [
      {
         fieldLabel : 'Correo',
         name : 'correo',
         width : 150,
         vtype : 'email',
         allowBlank : false
      }
      ,
      {
         fieldLabel : 'N&uacute;mero Celular',
         name : 'celular',
         width : 150,
         vtype : 'alphanum',
         allowBlank : false
      }
      ,
      {
         fieldLabel : 'C&oacute;digo de Validaci&oacute;n'
         , xtype : 'box'
         , anchor : ''
         , isFormField : true
         , autoEl :
         {
            tag : 'div', children : [
            {
               tag : 'img'
               , qtip : 'Escribe este texto en el recuadro de abajo.'
               , src : './images/validar.aspx?nc=' + new Date().getTime()
            }
            ,
            {
               tag : 'div'
               , style : 'margin:0 0 0 0'
               , html : ''
            }
            ]
         }
      }
      ,
      {
         name : 'checkcode',
         xtype : 'textfield',
         allowBlank : false,
         vtype : 'alphanum',
         maxLength : 10,
         isFormField : true,
         width : 120
      }
      ]
   }
   );

   var forgot_win = new Ext.Window(
   {
      layout : 'fit',
      width : 300,
      height : 245,
      closable : true,
      resizable : false,
      plain : true,
      border : false,
      modal : true,
      items : [olvidadizo],
      buttons : [
      {
         text : 'Aceptar',
         formBind : true,
         // Function that fires when user clicks the button
         handler : function()
         {
            olvidadizo.getForm().submit(
            {
               method : 'POST',
               waitTitle : 'Espere...',
               waitMsg : 'Espere...',
               success : function()
               {
                  Ext.Msg.alert('Aviso', 'Hemos restablecido tu contrase&ntilde;a.<br>En breve recibir&aacute;s un correo electronico indicandote cual es.<br>Recuerda cambiarla cuando ingreses la pr&oacute;xima vez.');
                  var redirect = 'default.aspx';
                  window.location = redirect;
               }
               ,
               failure : function(form, action)
               {
                  if(action.failureType == 'server')
                  {
                     obj = Ext.util.JSON.decode(action.response.responseText);
                     Ext.Msg.alert('Aviso', obj.errors.reason);
                  }
                  else
                  {
                     Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText);
                  }
                  olvidadizo.getForm().reset();
               }
            }
            );
         }
      }
      ]
   }
   );
   forgot_win.show();
}

function cambiaPassword()
{
   var cambiarpwd = new Ext.FormPanel(
   {
      labelWidth : 70,
      url : 'cambiarpwd.aspx',
      frame : true,
      title : 'Cambiar tu Contrase&ntilde;a',
      defaultType : 'textfield',
      monitorValid : true,
      items : [
      {
         fieldLabel : 'Contrase&ntilde;a Actual',
         name : 'claveActual',
         width : 150,
         inputType : 'password',
         allowBlank : false
      }
      ,
      {
         fieldLabel : 'Nueva Contrase&ntilde;a',
         name : 'claveNueva',
         width : 150,
         inputType : 'password',
         allowBlank : false
      }
      ,
      {
         fieldLabel : 'Confirmar Contrase&ntilde;a',
         name : 'claveConfirma',
         width : 150,
         inputType : 'password',
         allowBlank : false
      }
      ],
      buttons : [
      {
         text : 'Aceptar',
         formBind : true,
         // Function that fires when user clicks the button
         handler : function()
         {
            cambiarpwd.getForm().submit(
            {
               method : 'POST',
               waitTitle : 'Espere...',
               waitMsg : 'Espere...',
               success : function()
               {
                  var redirect = 'default.aspx';
                  window.location = redirect;
               }
               ,


               failure : function(form, action)
               {
                  if(action.failureType == 'server')
                  {
                     obj = Ext.util.JSON.decode(action.response.responseText);
                     Ext.Msg.alert('Aviso', obj.errors.reason);
                  }
                  else
                  {
                     Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText);
                  }
                  cambiarpwd.getForm().reset();
               }
            }
            );
         }
      }
      ]
   }
   );

   var chgpwd_win = new Ext.Window(
   {
      layout : 'fit',
      width : 300,
      height : 245,
      closable : true,
      resizable : false,
      plain : true,
      border : false,
      modal : true,
      items : [cambiarpwd]
   }
   );

   chgpwd_win.show();
}

function bienvenida()
{
   var welcome_win = new Ext.Window(
   {
      layout : 'fit',
      id : 'welcomeWindow',
      title : 'Publicidad - Se cierra en 10 segundos',
      width : 330,
      height : 310,
      closable : false,
      resizable : false,
      draggable : false,
      plain : true,
      border : true,
      modal : true,
      items : [
      new Ext.ux.IFrameComponent(
      {
         id : id, url : 'publicidad.aspx?seccion=Bienvenida', width : 310, height : 260
      }
      )      ]
   }
   );

   welcome_win.show();
   setTimeout( "closeWelcome()", 20 * 1000 );

}

function closeWelcome()
{
   var welcome_win = Ext.getCmp('welcomeWindow');
   welcome_win.close();
   // primeraVez();
}



function EstatusSistema()
{
   var ServiceStatus = new Ext.FormPanel(
   {
      labelWidth : 130,
      id : 'EstatusPDA',
      frame : true,
      // monitorValid : true,
      // ENTRADA
      items : [
      {
         xtype : 'datefield',
         fieldLabel : 'Estado del sistema el',
         format : 'd/m/Y',
         readOnly : true,
         name : 'fechaStatus'
      }
      ,
      {
         xtype : 'timefield',
         fieldLabel : 'Reportado a las',
         name : 'horaStatus',
         readOnly : true,
         width : 60
      }
      ,
      {
         xtype : 'textfield',
         fieldLabel : 'Env&iacute;o de Mensajes',
         name : 'cellStatus',
         readOnly : true,
         width : 100
      }
      ,
      {
         xtype : 'textfield',
         fieldLabel : 'Provedor Celular',
         name : 'cellService',
         readOnly : true,
         width : 120
      }
      ,
      {
         xtype : 'textfield',
         fieldLabel : 'Mensajes en Cola',
         name : 'pendientes',
         readOnly : true,
         width : 60
      }
      ]
   }
   );


   // VENTANA PRINCIPAL
   var sched_win = new Ext.Window(
   {
      title : 'Estado del Servicio',
      layout : 'fit',
      width : 310,
      height : 220,
      iconCls : 'status-icon',
      // closable : true,
      resizable : false,
      plain : true,
      // border : true,
      draggable : true
      , modal : false
      , closeAction : 'close'
      , resizable : false
      , shadow : true
      , items : [ServiceStatus]
   }
   );

   sched_win.show();

   var act = Ext.getCmp('EstatusPDA');
   act.getForm().load(
   {
      url : 'getStatus.aspx', waitTitle : 'Espera...', waitMsg : 'Obteniendo estado del servicio...'
   }
   );
}


function smallEstatusSistema()
{
   var ServiceStatus = new Ext.FormPanel(
   {
      labelWidth : 130,
      id : 'EstatusPDA_small',
      frame : true,
      renderTo : 'smallStatus',
      // monitorValid : true,
      // ENTRADA
      items : [
      {
         xtype : 'textfield',
         fieldLabel : 'Env&iacute;o de Mensajes',
         name : 'cellStatus',
         readOnly : true,
         width : 100
      }
      ,
      {
         xtype : 'textfield',
         fieldLabel : 'Mensajes en Cola',
         name : 'pendientes',
         readOnly : true,
         width : 60
      }
      ]
   }
   );


   var act = Ext.getCmp('EstatusPDA_small');
   act.getForm().load(
   {
      url : 'getStatus.aspx', waitTitle : 'Espera...', waitMsg : 'Obteniendo estado del servicio...'
   }
   );
}



function primeraVez()
{
   Ext.Msg.show(
   {
      title : '&iexcl;Bienvenido!',
      //renderTo: 'hostess',
      icon: 'welcome-msg-icon',
      msg : 'Bienvenido a <b>blablar.com</b>, el mejor sitio para enviar mensajes de texto<br>a cualquier celular de cualquier compa&ntilde;ia de M&eacute;xico, completamente gratis.<br>Para enviar mensajes, es necesario que <b>ingreses con tu cuenta</b>.<br>Si aun no tienes cuenta, crea una. Es r&aacute;pido y gratis.',
      buttons :
      {
         no : 'Iniciar Sesi&oacute;n'
         , yes : 'Crear mi cuenta'
         //         , cancel : '&iquest;Que es blablar?'
      }
      , fn : function (btn)
      {
         switch (btn)
         {
            case 'no' :
               ventanaLogin();
               break;
            case 'yes' :
               nvacta_win.show();
               break;
            case 'cancel' :
         }
      }
   }
   );
}


function avisos()
{


   var welcome_win = new Ext.Window(
   {
      layout : 'fit',
      id : 'notifyWin',
      title : 'Notificaciones - Se cierra en 10 segundos',
      width : 415,
      height : 265,
      closable : false,
      resizable : false,
      draggable : false,
      plain : true,
      border : true,
      modal : false,
      items : [
      {
         // title : '&iquest;Necesitas ayuda?'
         // , iconCls : 'help-icon'
bodyStyle : 'padding:10px 10px 10px 10px',

         autoLoad :
         {
            url : './aviso2.htm'
         }
      }
      ]
   }
   );

   welcome_win.show();
   setTimeout( "closeNotify()", 26 * 1000 );

}

function closeNotify()
{
   var welcome_win = Ext.getCmp('notifyWin');
   welcome_win.close();

}

