Ext.SSL_SECURE_URL="resources/images/default/s.gif"; 
Ext.BLANK_IMAGE_URL="resources/images/default/s.gif";




Login = function(){
   var win,
      form,
      submitUrl = '../pmt/system/login/login.php';
      
   function onSubmit(){
      this.showMask();
      
      form.submit({
         reset: true
      });
   }
   
   return{
      Init:function(){
         Ext.QuickTips.init();
         
//         var logoPanel = new Ext.Panel({
//            baseCls: 'x-plain',
//            id: 'login-logo',
//              region: 'center'
//         });
      
         var formobj = null;
      
         var formPanel = new Ext.form.FormPanel({
              baseCls: 'x-plain',
              baseParams: {
               module: 'login',
               kill : 0
              },
              defaults: {
               width: 190
              },
              defaultType: 'textfield',
              frame: false,
              height: 70,
              id: 'login-form',
              items: [{
                  fieldLabel: 'Email Address',
                  name: 'user',
                  value: ''
              },{
                  fieldLabel: 'Password',
                  inputType: 'password',
                  name: 'pass',
                  value: ''
              }],
              labelWidth:90,
              listeners: {
               'actioncomplete': {
                  fn: this.onActionComplete,
                  scope: this
               },
               'actionfailed': {
                  fn: this.onActionFailed,
                  scope: this
               }
            },
              region: 'center',
              url: submitUrl
          });

         var myWidth;
         var myHeight;

         if( typeof( window.innerWidth ) == 'number' ) { 

            //Non-IE 

            myWidth = window.innerWidth;
            myHeight = window.innerHeight; 

         } else if( document.documentElement && 

            ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 

            //IE 6+ in 'standards compliant mode' 

            myWidth = document.documentElement.clientWidth; 
            myHeight = document.documentElement.clientHeight; 

         } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { 

            //IE 4 compatible 

            myWidth = document.body.clientWidth; 
            myHeight = document.body.clientHeight; 

         }         
         
         win = new Ext.Window({
              buttons: [{
               handler: onSubmit,
               scope: Login,
                  text: 'Login'
              }],
              buttonAlign: 'right',
              closable: false,
              draggable: false,
              pageX : 20,
              height: 150,
              renderTo : 'mol-login',
              id: 'login-win',
              keys: {
               key: [13], // enter key
                 fn: onSubmit,
                 scope:this
              },
              layout: 'border',
              minHeight: 250,
              minWidth: 280,

            x  :  30,
           // y  :  (myHeight / 2) - 20,

              
              plain: false,
              resizable: false,
              items: [
                             //logoPanel,
                             formPanel
              ],
            title: 'Customer Login',
              width: 350,
            bbar              :  new Ext.StatusBar({
                                    id             :  'graph1-status',
                                    statusAlign    :  'right',
                                    items          :  [{
                                    xtype: 'box',
                                    autoEl: {tag: 'a', href: 'http://www.meteringonline.com/pmt/system/login/forgot_password.html', html: 'I forgot my password'}
                                 }]
            })
          });
         
         form = formPanel.getForm();
         
         formPanel.on('render', function(){
            var f = form.findField('user');
            
            if(f){
               f.focus();
            }
         }, this, {delay: 200});
         
          win.show();
      },
      
      hideMask : function(){
         this.pMask.hide();
         win.buttons[0].enable();
      },
      
      onActionComplete : function(f, a){
         this.hideMask();
         if(a && a.result){
            win.destroy(true);
            
            // get the path
            var path = window.location.pathname,
               path = path.substring(0, path.lastIndexOf('/') + 1);
               
            path = '/pmt/';
            // set the cookie
            set_cookie('mol_sessionId', a.result.sessionId, '', path, '', '' );
            set_cookie('memberName', 'User : ' + a.result.usrName);

            path = '/mol/';
            // set the cookie
            set_cookie('mol_sessionId', a.result.sessionId, '', path, '', '' );
            set_cookie('memberName', 'User : ' + a.result.usrName);
            
            // redirect the window
            window.location = '../pmt/';   //path;
            //alert(path);
         }
      },
      
      onActionFailed : function(f, a){
         this.hideMask();
         if (a.result.active == true) {
            formobj = f;  

            Ext.MessageBox.show({
               title    :  'Login Error',
               msg      :  'Your MeteringOnline user has an active session.<br><br><br>Accepting this message might terminate an active user that used the<br>same login username and password settings!<br><br>Click OK to kill the current session. Click Cancel to abort.',
               buttons  :  Ext.MessageBox.OKCANCEL,
               fn       :  this.showResultText,
               scope    :  this,
               icon     :  'ext-mb-error'
            });
         
//            var confirmed = window.confirm("This login credentials have an active session! \nAccepting this message might kill an active user that used \nthe same login name and password! \nClick OK to kill the current session.  Click Cancel to abort.");
//            if (confirmed) {
//               f.baseParams = {module: 'login', kill : 1};
//               alert('Session deactivated! Please press the login button again to gain access to the system');
//            }
         }
      },
      
      showResultText :  function (btn){
         if (btn == 'ok'){

            Ext.MessageBox.show({
               title    :  'Session Termination',
               msg      :  'Your MeteringOnline active session was successfully terminated.<br><br><br>Please press the login button again to gain access to the system',
               buttons  :  Ext.MessageBox.OK,
               scope    :  this,
               icon     :  'ext-mb-info'
            });

            formobj.baseParams = {module: 'login', kill : 1};
         }
      },

      showMask : function(msg){
         if(!this.pMask){
            // using this.pMask, seems that using this.mask caused conflict
              // when this dialog is modal (uses this.mask also)
              this.pMask = new Ext.LoadMask(win.body, {
               msg: 'Please wait...'
              });
         }
         
         if(msg){
            this.pMask.msg = msg;
         }
         this.pMask.show();
         win.buttons[0].disable();
       }
   };
}();

Ext.onReady(Login.Init, Login, true);
