Ext.namespace('EMA.Alert.form');

EMA.Alert.form.Password = Ext.extend(Ext.FormPanel, {
	labelWidth: 75,
    frame:true,
    bodyStyle:'padding:5px 5px 0',
    width: 350,
    defaults: {width: 230},
    defaultType: 'textfield',
    waitMsgTarget: true,
	constructor: function(config) {
		this.url = config.url;
		this.populate = config.populate;
		this.parentWindow = config.parentWindow;
		 /*
    	 * Fill form with value of our Account
    	 */
		this.data = config.data;
		this.callback = config.callback;
		this.items = [
		    {
	            fieldLabel: 'User #',
	            name: 'data[User][id]',
        		xtype: 'displayfield',
        		value: this.data['User.id']
	        },{
	            name: 'data[User][id]',
        		xtype: 'hidden',
        		value: this.data['User.id']
	        }, {
	        	id: 'password',
	            fieldLabel: 'Password',
	            name: 'data[User][password]',
        		xtype: 'textfield',
        		vtype: 'password',
        		inputType: 'password'
	        }, {
	        	id: 'passwordConfirm',
	            fieldLabel: 'Confirm password',
	            name: 'data[User][repassword]',
        		xtype: 'textfield',
        		vtype: 'password',
        		inputType: 'password',
        		initialPasswordField: 'password'
	        }
        ];
		
		this.buttons = [
			{
		        text: 'Update',
		        listeners: {
		        	scope: this,
		        	click: function() {
		        		this.onSubmit();
		        	}
		        }
		    }, {
		        text: 'Cancel',
		        listeners: {
		        	scope: this.parentWindow,
		        	click: function(e) {
		        		this.hide();
		        	}
		        }
			}
		];
	    
		EMA.Alert.form.Password.superclass.constructor.apply(this, arguments);
	},
	onSubmit: function () {
		var parentWindow = this.parentWindow;
		var transactionForm = this.getForm();
		var callback = this.callback;
		
		if (this.getForm().isValid()) {
			this.getForm().submit({
				success: function(form, action) {
					callback(action.result.id);
	    	    },
	    	    failure: function(form, action) {
	    	    	parentWindow.hide();
	    	    	Ext.Msg.alert('Failed', action.result.errorMessage);
	    	    },
				waitMsg: 'Processing'
			});
		}
	}	
});
