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

	// Localized strings
	fileSelectionTitle : 'Choose file',
	fileLabel : 'File',
	hasHeaderLabel : 'This file contains a header record',
	delimiterConfigTitle : 'Specify column delimiter',
	delimiterLabel : 'Delimiter',
	commaLabel : 'Comma',
	tabLabel : 'Tab',
	alertTitle : 'Message',
	invalidInputMessage : 'Form is invalid.',
	fileRequiredMessage : 'You must select a file.',
	invalidFileTypeMessage : 'The supported file types are .txt and .csv.',
	browseButton  : "Browse...",
	
	personId : 0,
	contextPath : null,
	
	record : null,


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

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

		// super
		Ext.pv.fandf.UploadFileForm.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 file is selected
		if (!this.fileChooser.isFileSelected()) {
			Ext.Msg.alert(this.alertTitle,this.fileRequiredMessage); 
			return false;
		}
		
		// Ensure file type is supported
		if (!this.fileChooser.isValidFileType(['txt','csv'])) {
			Ext.Msg.alert(this.alertTitle,this.invalidFileTypeMessage); 
			return false;
		}

		return true;
	},
	
	/**
	 * buildform
	 * 
	 * @private
	 */
	buildFormFields : function() {
		
		this.fileChooser = new Ext.form.FileUploadField({
			fieldLabel: this.fileLabel,
			name : 'fileChooser',
			blank : false,
			width:200,
			buttonText: this.browseButton
		});
		
		var items = [{
			xtype : 'hidden',
			name : 'personId',
			value : this.personId
		},{
			title: this.fileSelectionTitle,
			defaultType: 'textfield',
			items :[this.fileChooser,{
				xtype : 'hidden',
				name : 'fileName',
				value : 'csv.txt'
			},{
				xtype : 'checkbox',
				name : 'hasHeader',
				fieldLabel : '',
				labelSeparator : '',
				boxLabel : this.hasHeaderLabel
			}]
		}, {
			title: this.delimiterConfigTitle,
			defaultType: 'textfield',
			items :[{
				xtype: 'radiogroup',
				fieldLabel: this.delimiterLabel,
				columns: [100, 100, 100],
				items: [
					{boxLabel: this.commaLabel, name: 'delim', inputValue: 'comma', checked: true},
					{boxLabel: this.tabLabel, name: 'delim', inputValue: 'tab'}
					//,{boxLabel: 'Other', name: 'delim', inputValue: 'other'}
				]
			}
			// If anyone ever asks for 'Other' here it is
			//,{
			//	fieldLabel : 'Other',
			//	name : 'other_delim',
			//	maxLength : 1,
			//	width : 40,
			//	allowBlank : true,
			//	disabled : true,
			//	style: {marginBottom: '0px', marginTop: '0px'}
			//}
			]
		}];
		
		return items;
	}
	
});
