// Java Document
Ext.onReady(function() {
    //
	    var login = new Ext.form.FormPanel({
	        baseCls: 'x-plain',
	        layout:'absolute',
	        defaultType: 'textfield',
	        items: [
		        {
		            x: 0,
		            y: 5,
		            xtype:'label',
		            text: 'Username:'
		        },
		        {
		            x: 60,
		            y: 0,
		            id: 'uid',
		            name: 'uid',
		            anchor:'100%',
		            enableKeyEvents: true,
		            listeners: {
		                specialkey: function(field, el){
		                	if (el.getKey() == Ext.EventObject.ENTER){
		                		Ext.getCmp('login_button').fireEvent('click')	
		                	}
		                }
		            }
		        },
		        {
		            x: 0,
		            y: 35,
		            xtype:'label',
		            text: 'Password:'
		        },
		        {
		            x: 60,
		            y: 30,
		            name: 'pwd',
		            inputType:'password', 
		            anchor: '100%',
		            enableKeyEvents: true,
		            listeners: {
		                specialkey: function(field, el){
		                	if (el.getKey() == Ext.EventObject.ENTER){
		                		Ext.getCmp('login_button').fireEvent('click')	
		                	}
		                }
		            }
		        }
	        ]
	    });
	//
	    var forgot = new Ext.form.FormPanel({
	        baseCls: 'x-plain',
	        layout:'absolute',
	        defaultType: 'textfield',
	        items: [
		        {
		            x: 0,
		            y: 5,
		            xtype:'label',
		            text: 'Email:'
		        },
		        {
		            x: 60,
		            y: 0,
		            name: 'email',
		            anchor:'100%'  // anchor width by percentage
		        }
	        ]
	    });
	//
	    var forgot_window = new Ext.Window({
	        title: 'Retrieve Password',
	        width: 300,
	        height:150,
	        minWidth: 300,
	        minHeight: 150,
	        layout: 'fit',
	        plain: true,
			modal: true,
			closable: false,
	        bodyStyle:'padding:5px;',
	        buttonAlign:'center',
	        items: forgot,
	        buttons: [
	        	{
		            text: 'Get Login Info',
					handler:function(){ 
						forgot.getForm().submit({ 
							method:'POST', 
							waitTitle:'Connecting', 
							waitMsg:'Sending data...',
							url:'_class/class_users/get_login_info.php',
							success:function(action){ 
								forgot_window.hide(); 
    							window1.show();
								Ext.Msg.alert('Success', 'Your new login information has been sent to your email address.');
							},	
							failure:function(form, action){ 
								obj = Ext.util.JSON.decode(action.response.responseText); 
								Ext.Msg.alert('No Record Found!', obj.errors.reason); 
								forgot.getForm().reset(); 
							}
						});
					}
	        	},
	        	{
	        		text: 'Cancel',
					handler:function(){ 
						forgot_window.hide();
						window1.show();
					}
	        	}
	        ]
	    });
    //
	    var window1 = new Ext.Window({
	        title: 'System Login',
	        width: 300,
	        height:150,
	        minWidth: 300,
	        minHeight: 150,
	        layout: 'fit',
	        plain: true,
			modal: true,
			closable: false,
	        bodyStyle:'padding:5px;',
	        buttonAlign:'center',
	        items: login,
	        buttons: [
	        	{
	        		id: 'login_button',
		            text: 'Login',
		            listeners: {
		                click: function(){
	                       login.getForm().submit({ 
								method:'POST', 
								waitTitle:'Connecting', 
								waitMsg:'Sending data...',
								url:'_class/class_users/login.php',
								success:function(action){ 
									window1.close();
									Ext.MessageBox.show({
									  msg: 'Logging in...',
									  progressText: 'Redirecting...',
									  width:300,
									  wait:true,
									  waitConfig: {interval:100}
									});
									var redirect = 'index.php'; 
									window.location = redirect;
								},	
								failure:function(form, action){ 
									obj = Ext.util.JSON.decode(action.response.responseText); 
									Ext.Msg.show({
									   title:'Login Failed!',
									   msg: obj.errors.reason,
									   buttons: Ext.Msg.OK,
									   fn: resetLogin,
									   icon: Ext.MessageBox.ERROR
									});	
								}
							});
		                }
		            },
					handler:function(){ 
						login.getForm().submit({ 
							method:'POST', 
							waitTitle:'Connecting', 
							waitMsg:'Sending data...',
							url:'_class/class_users/login.php',
							success:function(action){ 
								window1.close();
								Ext.MessageBox.show({
								  msg: 'Logging in...',
								  progressText: 'Redirecting...',
								  width:300,
								  wait:true,
								  waitConfig: {interval:100}
								});
								var redirect = 'index.php'; 
								window.location = redirect;
							},	
							failure:function(form, action){ 
								obj = Ext.util.JSON.decode(action.response.responseText); 
								//Ext.Msg.alert('Login Failed!', obj.errors.reason); 
								Ext.Msg.show({
								   title:'Login Failed!',
								   msg: obj.errors.reason,
								   buttons: Ext.Msg.OK,
								   fn: resetLogin,
								   icon: Ext.MessageBox.ERROR
								});								
								
							}
						});
					}
	        	},
	        	{
	        		text: 'Forgot?',
					handler:function(){ 
						window1.hide();
						forgot_window.show();
					}
	        	}
	        ]
	    });
	//
    	window1.show();
    	Ext.getCmp('uid').focus('',10);
/*
 *
 */
function resetLogin(){
	login.getForm().reset();
	Ext.getCmp('uid').focus('',10);
}

/*


*/
});