﻿TBS.login.loginForm = new Object();

TBS.login.loginForm = function () {
        
        this.userTextField = new Ext.form.TextField({id: 'txtUserLogin', fieldLabel: 'Username',  hideLabel:false, allowBlank:false});
        this.userPasswordField = new Ext.form.TextField({id: 'txtPassLogin', fieldLabel: 'Password', allowBlank:false, hideLabel:false, inputType: 'password'});
        this.loginButton = new Ext.Button({id: 'btnLoginSubmit', text: ' Submit ', cls: 'x-btn-text-icon', icon: TBS.settings.rootFolder + '/images/icons/key_go.gif'});
        this.panel = null;
        
        this.render = function(container) {
        
                            var main = this;     
                             
                            this.loginButton.on('click',  function(event) {loginHandler(main)});  

                            this.panel = new Ext.Panel({    
                                    id: 'pnlLogin',                                      
                                    frame:true,                                   
                                    layout: 'form',
                                    title: 'System Login',                                    
                                    width: 250,                  
                                    items: [this.userTextField, this.userPasswordField],
                                    buttons: [this.loginButton]
                            });                            
                            
                            this.panel.render(container);
                                
                  }
                  
        var loginHandler = function(mainreference) {   
        
      
                    var waitBox = Ext.MessageBox.wait('Logging in, Please wait...');
           
                    var request = { username : mainreference.userTextField.getValue(), password : mainreference.userPasswordField.getValue() };  
                        
                    Ext.Ajax.request( 
                                {   
                                url:  TBS.settings.rootFolder + '/json.svc/Login',
                                params: Ext.util.JSON.encode(request), 
                                failure:function(response,options){
                                 
                                    waitBox.hide();                                                
                                    var o = Ext.util.JSON.decode(response.responseText);
                                    Ext.MessageBox.alert('Error',o.Message); 
                                    
                                },     
                                                                 
                                success:function(response,options){                                            
                                
                                    waitBox.hide();                                                
                                    waitBox2 = Ext.MessageBox.wait('Login successful, redirecting...');
                                    var o = Ext.util.JSON.decode(response.responseText);  
                                    TBS.auth.currentUser = o.d;
                                    
                                    if (TBS.auth.currentUser.ParentRole.AccessLevel == 1000) {
                                    
                                     
                                        document.location.href = TBS.settings.customerPage;
                                    
                                    } else {
                                    
                                        document.location.href = TBS.settings.adminPage;                                                
                                    }
                                 
                                }                                      
                             } 
                        );    
         }
}