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

EMA.Alert.form.Login = 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;
		this.callback = config.callback;
		this.items = [
	        {
	        	id: 'username',
	            fieldLabel: 'Username',
	            name: 'data[User][username]',
	            allowBlank: false,
        		xtype: 'textfield'
	        }, {
	        	id: 'password',
	            fieldLabel: 'Password',
	            name: 'data[User][password]',
        		xtype: 'textfield',
        		allowBlank: false,
        		//vtype: 'password',
        		inputType: 'password'
        		//initialPasswordField: 'password'
	        }
        ];
		
		this.buttons = [{
			text: 'Login',
		        listeners: {
		        	scope: this,
		        	click: function() {
		        		this.onSubmit();
		        	}
		        },
				scale: 'large',
				width: '150'
		}];
	    
		EMA.Alert.form.Login.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) {
					//console.log('xxx');
					//console.log(Ext.getCmp('username'));
					console.log(callback);
					console.log(action);
					callback(action.result.id);
	    	    },
	    	    failure: function(form, action) {
	    	    	//parentWindow.hide();
	    	    	Ext.Msg.alert('Failed', action.result.errorMessage);
					var decoded = Ext.decode(action.response.responseText);
					if (decoded.errorMessage != "") {
						Ext.getCmp('password').markInvalid(decoded.errorMessage);
					}
	    	    	console.log('username next');
	    	    	console.log(Ext.getCmp('username'));
	    	    },
				waitMsg: 'Processing'
			});
		}
	}	
});
