Melrow.Mtw.Manager = function(){

	Ext.Ajax.on('validate',function(response){
	    var resp = eval('(' + response[0].responseText + ')');
	    if (resp['success'] !== true) {
 		    Ext.MessageBox.alert('Melrow TechWeb Error', resp.error, function(){
				document.location.href = '/mtw/logout';
			}, this);
			return false;				
		}
	 },this);	
	
}

Ext.extend(Melrow.Mtw.Manager, Ext.util.Observable, {

 	issueWindow: null,
	grid : null,	
	loginPanel : null,

	renderLogin : function(){
		this.loginPanel = new Ext.form.FormPanel({
		 	frame:true,
	        title: 'Melrow TechWeb Login',
	        bodyStyle:'padding:5px 5px 0',
	        defaults: {width: 140, labelWidth:100},
	        defaultType: 'textfield',				 
	        monitorValid:true,
			items:[{
		     	fieldLabel: 'Account ID',
	            name: 'userid',
	            allowBlank:false		
			},{
				xtype:'textfield',					 
	            fieldLabel: 'Password',
	            name: 'password',
	            allowBlank:false,			            
	            inputType:'password'						
			}],
			buttons: [{
			 	text:'Login',
			 	formBind:true,
				handler: this.login,
				scope:this
			}],
			keys: [{
	            key: Ext.EventObject.ENTER,
	            handler: this.login,
	            scope:this
        	}]  			
			
	    });
	    
	    this.loginPanel.render('login-form');

	},
	
	login : function(f){
	 	var form = this.loginPanel.getForm();
		if (form.isValid()){
		 	form.doAction('submit', {method:'POST', url:'/mtw/login', scope:this, success: function(f,a){
				var res = eval('(' + a.response.responseText + ')');	
				if (!res.error) document.location.href ='/mtw/issues';
				else Ext.MessageBox.alert('Melrow TechWeb Login' , res.error);
			}});
		} else {
			Ext.MessageBox.alert('Melrow TechWeb Login' , 'Invalid Account ID or password. Please try again.');
		}
	},
	
	showPasswordWindow : function(){
		var win;
		if(!win){
            win = new Ext.Window({
                layout:'fit',
                width:380,
                height:160,
                closeAction:'hide',
                resizable:false,
		        title: 'Change Password',
                plain: true,
                items: new Ext.form.FormPanel({
			        bodyStyle:'padding:5px 5px 0',
			        monitorValid:true,
					labelWidth:150, 
					border:false,	        
			        defaults: {width: 180, allowBlank:false,inputType:'password'},
			        defaultType: 'textfield',				 
					items:[{
			            fieldLabel: 'Current password',
			            name: 'pwd_current'
					},{
			            fieldLabel: 'New password',
			            name: 'pwd_new',
			            minLength: 4
					},{
			            fieldLabel: 'New password (again)',
			            name: 'pwd_new_again',
			            minLength: 4	            
					}]
			    }),

                buttons: [{
				 	text:'Change password',
					handler: function(f){
					 	//console.log(win);
					 	var form = win.items.first().getForm();
						if (form.isValid()){
							if (form.findField('pwd_new').getValue() == form.findField('pwd_new_again').getValue()){
							 	var values = form.getValues();
							 	form.doAction('submit', {method:'POST', url:'/mtw/account', success: function(f,a){
									var res = eval('(' + a.response.responseText + ')');	
									if (!res.error) {
									 	Ext.MessageBox.alert('Mtw Change Password', 'Your password has been changed.', function(){
										}, this);
									} else {
										Ext.MessageBox.alert('Mtw Change Password' , res.error);																
									}
								}});
							} else {
								Ext.MessageBox.alert('Mtw Change Password' , 'Your new passwords do not match.');							
							}
						} else {
							Ext.MessageBox.alert('Mtw Change Password' , 'Please correct the fields marked in red.');
						}
					},
					scope:this
				},{
                    text: 'Close',
                    handler: function(){
                        win.hide();
                    }
                }]
            });
        }
        win.show(this);
	},

	renderGrid : function(){
		this.grid = new Melrow.Mtw.IssuesGridPanel();
		this.grid.on('view', this.onOpenIssue, this);
		this.grid.on('error', this.onError, this);
		this.grid.render('mtw-issues-grid');            
	},
	
	onOpenIssue : function(record){
	 	if (!this.issueWindow) this.issueWindow = new Melrow.Mtw.IssueWindow();
	 	this.issueWindow.show(record);
	}
	
});

Melrow.Mtw.Manager = new Melrow.Mtw.Manager();

