/*
 *
 * Ekinoxe CMS (c) 2009
 *
 *
*/

 var BackOffice = {};

 // ---------------------------------------------------------------------------------------------------
 
 BackOffice.isBusy = false;
 
 BackOffice.Login = {
 
   init : function() {
   
     $('btn-connection').addEvent('mouseup', function(){
       BackOffice.Login.sendForm();
       return false;
     });
     
     document.addEvent('keypress', function(e) {
      if(e.code.toInt() == 13) {
        BackOffice.Login.sendForm();
        return false;
      }
     });
    
     return true;
   
   }
   
   ,sendForm : function() {

      if(BackOffice.isBusy)
        return false;
        
      BackOffice.isBusy = true;
 
      login = $$('input[name=login]')[0];
      passwd = $$('input[name=password]')[0];
      
      var r = new Remote({
        name : 'session'
       ,command : 'open'
       ,params : {
          login: login.value
         ,password : passwd.value
       }
     });
     
     r.addEvent('complete', function() {
       if(this.remote.response.code==1) {
         document.location.href=backofficeRoot;
         BackOffice.isBusy = false;
         return;
       } else {
         $('loginError').set('html', this.remote.response.text).setStyles({'visibility':'visible'});
         BackOffice.isBusy = false;
         return;
       }
       BackOffice.isBusy = false;
     }.bind({login:login,passwd:passwd,remote:r}));
    
     r.addEvent('error', function(remote){
       setTimeout(function() {
         $('loginError').set('html', this.remote.response.text).setStyles({'visibility':'visible'});
         BackOffice.isBusy = false;
       }.bind(this), 300);
     }.bind({login:login,passwd:passwd,remote:r}));
    
     $('loginError')
       .set('html', '<img style="position:absolute;margin: -5px 0 0 0" align="absmiddle" src="'+wwwroot+'/media/system/img/widgets/ajax-load-1.gif" />')
       .setStyles({'visibility':'visible'});
     r.call();
     
     return false;
   
   }
 
 }