Ext.ns('Ext.pv.fandf');
Ext.pv.fandf.SelectColumnsForm = Ext.extend(Ext.form.FormPanel, {
	frame : false,
	border : false,
	bodyStyle: 'padding: 10px;',
	labelWidth : 75,
	monitorValid : true,
	autoScroll : true,
	defaults: {
		xtype: 'fieldset',
		collapsible: false
	},

	// Localized strings
	columnConfigTitle : 'Specify column positions',
	firstnameLabel : 'First Name',
	lastnameLabel : 'Last Name',
	emailLabel : 'Email',
	alertTitle : 'Message',
	invalidInputMessage : 'Form is invalid.',
	invalidColumnIndexMessage : 'Column indexes must be greater than zero.',

	personId : 0,
	contextPath : null,
	
	record : null,


	/**
	 * initComponent
	 * 
	 * @protected
	 */
	initComponent : function() {

		// Build the form fields
		this.items = this.buildFormFields();

		// super
		Ext.pv.fandf.SelectColumnsForm.superclass.initComponent.call(this);
	},
	
	/**
	 * loadRecord
	 * 
	 * @param {Record} rec
	 */
	loadRecord : function(rec) {
		this.record = rec;
		if (rec != null) {
			this.getForm().loadRecord(rec);
		} else {
			this.getForm().reset();
		}
	},
	
	/**
	 * isValid
	 * 
	 * @public
	 */
	isValid : function() {
		// Ensure vtype validation passes
		if (!this.getForm().isValid()) {
			Ext.Msg.alert(this.alertTitle,this.invalidInputMessage); 
			return false;
		}
		
		// Ensure column index are greater than zero
		var idx1 = this.getForm().findField('firstnameIndex').getValue();
		var idx2 = this.getForm().findField('lastnameIndex').getValue();
		var idx3 = this.getForm().findField('emailIndex').getValue();
		if (idx1<=0 || idx2<=0 || idx3<=0) {
			Ext.Msg.alert(this.alertTitle,this.invalidColumnIndexMessage); 
			return false;
		}

		return true;
	},
	
	/**
	 * buildform
	 * 
	 * @private
	 */
	buildFormFields : function() {
		
		var items = [{
			xtype : 'hidden',
			name : 'personId',
			value : this.personId
		},{
			title: this.columnConfigTitle,
			defaults: {
				xtype: 'numberfield',
				width : 40,
				allowBlank : false,
				style: {marginBottom: '0px', marginTop: '0px'}
			},
			items :[{
				fieldLabel : this.firstnameLabel,
				name : 'firstnameIndex',
				value : '1'
			},{
				fieldLabel : this.lastnameLabel,
				name : 'lastnameIndex',
				value : '2'
			},{
				fieldLabel : this.emailLabel,
				name : 'emailIndex',
				value : '3'
			}]
		}];
		
		return items;
	}
	
});
