﻿/**
* Admin.Web
*
* @author    Fini AS
* @copyright (c) 2009, by Martin Bakken
* @date      13. February 2009
* @version   1.0
*
/*global Ext, Application */
 
Ext.BLANK_IMAGE_URL = './ext/resources/images/default/s.gif';
Ext.ns('Web');
Web.Login = Ext.extend(Ext.FormPanel, { 
        labelWidth:80
        , frame: true
        ,id: 'loginForm'
        ,title:'Please Login'
        ,width:230
        ,padding:200
        ,defaultType:'textfield'
        ,monitorValid:true
        ,buttons:[{ 
                text:'Login',
                formBind: true,	 
                // Function that fires when user clicks the button 
                handler:this.loginSubmit 
            }] 
	    ,initComponent:function() {
		 Ext.apply(this, {
	        // Specific attributes for the text fields for username / password. 
	        // The "name" attribute defines the name of variables sent to the server.
                items:[{ 
                        id:'loginUsernameField',
                        fieldLabel:'Username', 
                        name:'username', 
                        allowBlank:false 
                    },{ 
                        id:'loginPasswordField',
                        fieldLabel:'Password', 
                        name:'password', 
                        inputType:'password', 
                        allowBlank:false ,
                        listeners:
                        {
	                        specialkey: function(field, el) {
	                            if (el.getKey() == Ext.EventObject.ENTER)
	                                Ext.getCmp('loginForm').loginSubmit();
	                        }
	                    }
                    }]
    	        // All the magic happens after the user clicks the button     
           });
           Web.Login.superclass.initComponent.apply(this, arguments);
	} // eo function initComponent
    ,onRender:function() {
	    Web.Login.superclass.onRender.apply(this, arguments);
   } // eo function onRender

    ,doSubmitCheck:function(e) {
		    if(e.getKey() == e.ENTER){
		        Web.loginSubmit();
		    }
    }
    ,loginSubmit:function()
    {
        Web.loginPanel.getForm().submit({ 
            method:'POST', 
            waitTitle:'Connecting', 
            waitMsg:'Sending data...',
            // 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(){ 
        //                        	Ext.Msg.alert('Status', 'Login Successful!', function(btn, text){
        //			                if (btn == 'ok'){
        //                                var redirect = '/mvc/'; 
        //                                window.location = redirect;
        //                            }
        //			            });
                Web.loginPanel.el.mask('Redirecting...');
                window.location = Web.loginPanel.redirectUrl;
            },

            // 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.alert('Login Failed!', obj.errors.reason); 
                }else{ 
                    Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText); 
                } 
                Web.loginPanel.getForm().reset(); 
            } 
        }); 	
     }
});
Ext.reg('login', Web.Login);         

