EMA.Alert.form.User = Ext.extend(Ext.FormPanel, {
	labelWidth: 105,
    frame:true,
    bodyStyle:'padding:5px 5px 0',
    width: 390,
    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: 'Dealer #',
	            name: 'data[User][id]',
        		xtype: 'displayfield',
        		value: this.populate ? this.data['User.id'] : ''
	        },{
	            name: 'data[User][id]',
        		xtype: 'hidden',
        		value: this.populate ? this.data['User.id'] : ''
	        },{
	            name: 'data[User][role_id]',
        		xtype: 'hidden',
        		value: 1
	        }, {
	        	id: 'firstname',
	            fieldLabel: 'First Name',
	            name: 'data[User][firstname]',
        		xtype: 'textfield',
        		value: this.populate ? this.data['User.firstname'] : '',
        		allowBlank: false
	        }, {
	        	id: 'lastname',
	            fieldLabel: 'Last Name',
	            name: 'data[User][lastname]',
        		xtype: 'textfield',
        		value: this.populate ? this.data['User.lastname'] : '',
        		allowBlank: false
	        }, {
	        	id: 'username',
	            fieldLabel: 'Username',
	            name: 'data[User][username]',
        		xtype: 'textfield',
        		value: this.populate ? this.data['User.username'] : '',
        		allowBlank: false
	        }
        ];
        
        if (!this.populate) {
        	this.items = this.items.concat({
	        	id: 'password',
	            fieldLabel: 'Password',
	            name: 'data[User][password]',
        		xtype: 'textfield',
        		vtype: 'password',
        		inputType: 'password',
        		allowBlank: false
	        }, {
	        	id: 'passwordConfirm',
	            fieldLabel: 'Confirm password',
	            name: 'data[User][repassword]',
        		xtype: 'textfield',
        		vtype: 'password',
        		inputType: 'password',
        		initialPasswordField: 'password',
        		allowBlank: false
	        });
        }
		
		this.buttons = [
			{
		        text: 'Save',
		        listeners: {
		        	scope: this,
		        	click: function() {
		        		this.onSubmit();
		        	}
		        }
		    }, {
		        text: 'Cancel',
		        listeners: {
		        	scope: this.parentWindow,
		        	click: function(e) {
		        		this.hide();
		        	}
		        }
			}
		];
	    
		EMA.Alert.form.User.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'
			});
		}
	}	
});
