Ext.SSL_SECURE_URL="resources/images/default/s.gif"; 
Ext.BLANK_IMAGE_URL="resources/images/default/s.gif";

Login = function(){
	var win,
		form,
		submitUrl = 'source/login/login.php';
		
	function onSubmit(){
	
		form.submit({
			waitMsg: 'Aspetta ...',
			reset: true,
			success: Login.Success,
			scope: Login
		});
	}
	
	return{
		Init:function(){
			Ext.QuickTips.init();
			
			var logoPanel = new Ext.Panel({
				baseCls: 'x-plain',
				id: 'login-logo',
		        region: 'center'
			});
		
			var formPanel = new Ext.form.FormPanel({
		        baseCls: 'x-plain',
		        baseParams: {
		        	module: 'login'
		        },
		        defaults: {
		        	width: 200
		        },
		        defaultType: 'textfield',
		        frame: false,
		        height: 70,
		        id: 'login-form',
		        items: [{
		            fieldLabel: 'Utente',
		            name: 'user',
		            value: ''
		        },{
		            fieldLabel: 'Parola chiave',
		            inputType: 'password',
		            name: 'pass',
		            value: ''
		        }],
		        labelWidth:120,
		        region: 'south',
		        url: submitUrl
		    });
			var registerPanel = new Ext.form.FormPanel({
		        baseCls: 'x-plain',
		        baseParams: {
		        	module: 'register'
		        },
		        defaults: {
		        	width: 200
		        },
		        defaultType: 'textfield',
		        frame: false,
		        height: 70,
		        id: 'register-form',
		        items: [{
		            fieldLabel: 'Utente',
		            name: 'newuser',
		            value: ''
		        },{
		            fieldLabel: 'Email',
		            name: 'useremail',
		            value: '' 		            
		        }],
		        labelWidth:120,
		        region: 'south',
		        url: 'source/login/register.php'
		    });


            var btnLogin = new Ext.Button({

		        handler: function(){
            		form = formPanel.getForm();		        	
		        form.submit({
				waitMsg:'Aspetta...',
				url: 'source/login/login.php',
				reset:true,
				success:Login.Success,
				scope:Login
			});
		        	},
		        	scope: Login,
		            text: 'Entra'

            
            });

            var btnCreateAccount = new Ext.Button({
                text: 'Registra',
                tooltip: 'Registra unnuovo utente',
                handler: function() {
                    winReg.show();
                    win.hide();
                }
            });
            
            var btnRegister = new Ext.Button({
                text: 'Registra',
                tooltip: 'Registra nuovo utente',
                type:'submit',
                handler: function() {
            	form = registerPanel.getForm();	
                    form.submit({
                        waitTitle: "Aspetta...",
                        waitMsg: "Sto registrando...",
                        url: 'source/login/register.php',
                        method: 'POST',
                        success:Login.Success,
                        scope:Login
                    });
                }
            });
            
            var btnCancel = new Ext.Button({
                text: 'Ritorna',
                tooltip: 'Ritorna a login',
                handler: function() {
                    winReg.hide();
                    win.show();
                }
            });


		
		   win = new Ext.Window({
		        buttons: [
		         	btnLogin,
		         	btnCreateAccount
		        ],
		        buttonAlign: 'right',
		        closable: false,
		        draggable: true,
		        height: 250,
		        id: 'login-win',
		        keys: {
		        	key: [13], // enter key
			        fn: onSubmit,
			        scope:this
		        },
		        layout: 'border',
		        minHeight: 250,
		        minWidth: 530,
		        plain: false,
		        resizable: false,
		        items: [
		        	logoPanel,
		        	formPanel
		        ],
				title: 'Entra Alhoo Desktop',
		        width: 530
		    });

		   winReg = new Ext.Window({
		        buttons: [
		               btnRegister,
			       btnCancel
	  	        ],
		        buttonAlign: 'right',
		        closable: false,
		        draggable: false,
		        height: 250,
		        id: 'register-win',
		        keys: {
		        	key: [13], // enter key
			        fn: onSubmit,
			        scope:this
		        },
		        minHeight: 250,
		        minWidth: 530,
		        plain: false,
		        resizable: false,
		        items: [
		        	registerPanel
		        ],
			title: 'Registra un Alhoo Desktop tutto tuo',
		        width: 530
		    });



			form = formPanel.getForm();
			
			formPanel.on('render', function(){
				var f = form.findField('user');
				
				if(f){
					f.focus();
				}
			}, this, {delay: 200});
			
		    win.show();
		    winReg.show();
		    winReg.hide();
		},
		
		Success: function(f,a){
			if(a && a.result){
				win.destroy(true);
				
				// get the path
				var path = window.location.pathname,
					path = path.substring(0, path.lastIndexOf('/') + 1);
					
				// set the cookie
				set_cookie('key', a.result.key, '', path, '', '' );
				set_cookie('user', a.result.user, '', path, '', '' );
				set_cookie('memberName', a.result.name, '', path, '', '' );
				set_cookie('memberType', a.result.type, '', path, '', '' );
				//set_cookie('memberGroup', a.result.group, '', path, '', '' );
				
				// redirect the window
				window.location = path;
			}else{
				winReg.hide();
                                win.show();	
                                Ext.MessageBox.show({
				title: 'Benvenuto nuovo utente',
				msg: 'registrazione eseguta con successo. <br /><br /> Riceverete un email con le istruzioni.<br /> <br />Clicate "OK" per tornare nel login. ',
				buttons: Ext.MessageBox.OK,
				icon: Ext.MessageBox.INFO
				});

			
			}

		}







	};




}();

Ext.BasicForm.prototype.afterAction=function(action, success){
	this.activeAction = null;
	var o = action.options;
	if(o.waitMsg){
		Ext.MessageBox.updateProgress(1);
		Ext.MessageBox.hide();
	}
	if(success){
		if(o.reset){
			this.reset();
		}
		Ext.callback(o.success, o.scope, [this, action]);
		this.fireEvent('actioncompleted', this, action);
	}else{
		Ext.callback(o.failure, o.scope, [this, action]);
		this.fireEvent('actionfailed', this, action);
	}
}

Ext.onReady(Login.Init, Login, true);