Ext.onReady(function(){
				
    Ext.QuickTips.init();
 
	// Create a variable to hold our EXT Form Panel. 
	// Assign various config options as seen.	 
    var login = new Ext.FormPanel({ 
        labelWidth:80,
        url:BASE_URL+INDEX+'admin/login', 
		//url:BASE_URL+INDEX+'login', 
        frame:true, 
        title:'Sistema Tiger Sport Team', 
        defaultType:'textfield',
		monitorValid:true,
	// Specific attributes for the text fields for username / password. 
	// The "name" attribute defines the name of variables sent to the server.
        items:[new Ext.form.ComboBox({
						hiddenName: 'id',
						fieldLabel:'N&uacute;cleo',
						store: new Ext.data.JsonStore({
							//autoLoad: true,
							url: BASE_URL+INDEX+'admin/empresas',
							root: 'results',
							id: 'id',
							fields: ['id', 'nome']
						}),
						triggerAction: 'all',
						allowBlank:false,
						blankText: 'Campo obrigat&oacute;rio',
						selectOnFocus:true,							
						valueField:'id',
						displayField: 'nome',
						width: 300
			}),{ 
                fieldLabel:'Usu&aacute;rio', 
                name:'login',
				id:'login', 
                allowBlank:false,
				inputType:'textfield',
				blankText: 'Campo obrigat&oacute;rio',
				value:'Seu login',
				width: 160
            },{ 
                fieldLabel:'Senha', 
                name:'senha', 
                id:'senha', 
                inputType:'password',
				blankText: 'Campo obrigat&oacute;rio',
                allowBlank:false,
				width: 160
            }],
 
	// All the magic happens after the user clicks the button     
        buttons:[{ 
                text:'Entrar no sistema',
                formBind: true,	 
                // Function that fires when user clicks the button 
                handler:function(){ 
                    login.getForm().submit({ 
                        method:'POST', 
                        waitTitle:'Conectando...', 
                        waitMsg:'Enviando dados...',
 
			// 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', 'Sucesso no login!', function(btn, text){
				   			//	if (btn == 'ok'){
									var redirect = BASE_URL+INDEX+'inicio'; 
									window.location = redirect;
                             //   }
				       		//});
                   		},
 
			// 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 Falhou!', obj.errors.reason); 
                            }else{ 
                                Ext.Msg.alert('Aten&ccedil;&atilde;oo!', 'Autentica&ccedil;&atilde;o falhou : ' + action.response.responseText); 
                            } 
                            login.getForm().reset(); 
                        } 
                    }); 
                } 
            }] 
    });
 
 
	// This just creates a window to wrap the login form. 
	// The login object is passed to the items collection.       
    var win = new Ext.Window({
        layout:'fit',
        width:420,
        height:170,
        closable: false,
        resizable: false,
        plain: false,
        border: false,
        items: [login]
	});
	win.show();
	$('#login').focus(function(){
		$('#login').val('');
	});
	
	$('#senha').focus(function(){
		$('#senha').val('');
	});
	
});


