var info = {
   init: function()
   {
      var pThis = this;
        
      $('#disconnect').click(function()
      {
         login.disconnect();
      });
   },
   
   display: function()
   {
      AccountService.getUserInfo(
      {    
         callback: function(result) 
         { 
            var info = JSON.decode(result); 
            $('#name').html(info['firstName'] + ' ' + info['lastName']);      
         },
         timeout: 10000,
         errorHandler: function(message) { }
      });
   }
}

var login = {
   loginName: '',
   password: '',
   result: '',
   
   init: function()
   {
      $('#loginName').val(this.loginName);
      $('#password').val(this.password);
      
      var pThis = this;
      
      $('#connect').click(function()
      {
         pThis.connect();
      });
      
      $('#password').keyup(function(e)
      {
         if (e.keyCode == 13)
            pThis.connect();
      });
      
      $('#loginName').focus(); 
   },

   connect: function()
   {
      this.loginName = $('#loginName').val();
      this.password = $('#password').val();
      
      var pThis = this;
      
      if (this.loginName == '' || this.password == '')
         return;
      
      AccountService.logIn(this.loginName, this.password,
      {    
        callback: function(result) { pThis.result = JSON.decode(result); pThis.processResult(); },
        timeout: 10000,
        errorHandler: function(message) { $('#errorBox').animate({height: '40px'}, 180, function() {$('#loginProblem').show();} ); }
      });
   },
   
   processResult: function()
   {
      $('#loginError,#loginDisabled,#tooManyTry').hide();
      
      if (this.result['result'] == false)
      {
         if (this.result['reason'] == 'usrPwdInvalid')
         {   
            $('#loginError').show(); 
            $('#errorBox').animate({height: '23px'});
         }
         else if (this.result['reason'] == 'userDisabled')
         {
            $('#loginDisabled').show();
            $('#errorBox').animate({height: '23px'});
         }
         else if (this.result['reason'] == 'tooManyTry')
            $('#errorBox').animate({height: '45px'}, 180, function() { $('#tooManyTry').show();} );   
         else
            $('#loginError').show();
         /*else if (this.result['reason'] == 'multipleSession')
            $('#loginDisabled').show();*/
      }
      else
      {
         if (this.result['customer'])
         {
            new URI('/../../../customer.htm').go();
            return;
         }
         
         if (this.result['mustRegister'])
            $('#errorBox').animate({height: '45px'}, 180, function() { $('#mustRegister').show();} );
         else
         {  
            var platform = '';
            
            if (Browser.Platform.mac) platform = 'Mac';
            else if (Browser.Platform.win) platform = 'Windows';
            else if (Browser.Platform.linux) platform = 'Linux';
            else if (Browser.Platform.ios) platform = 'iOS';
            else if (Browser.Platform.android) platform = 'Android';
            else if (Browser.Platform.weboc) platform = 'WebOS';
            else platform = 'Other';
            
            AccountService.saveBrowserStats(Browser.name, Browser.version, platform, screen.width, screen.height, this.result['userLogId'], '',
            {    
               callback: function(result) {},
               timeout: 10000,
               errorHandler: function(message) { }
            });

            new URI('/../../../account.htm').go();
         }
      }
   },
   
   disconnect: function()
   {
      AccountService.disconnect(
      {    
         callback: function(result) 
         {
            new URI('/../../../index.htm').go();
         },
         timeout: 10000,
         errorHandler: function(message) { }
      });
   }
}
      

