Ext.ns('Ext.pv.notification');
Ext.pv.notification.NotificationForm = Ext.extend(Ext.form.FormPanel, {
	autoScroll : true,
	border : true,
	labelAlign : 'left',
	bodyStyle : 'padding: 20 10 10 10px',
	defaultType : 'textfield',

	// Localized strings
	typeLabel : 'Notification Type',
	bodyLabel : 'Custom Text',
	
	contextPath : null,
	personId : 0,
	record : null,

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

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

		// super
		Ext.pv.notification.NotificationForm.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();
		}
	},
	
	/**
	 * load
	 * @param {int} notificationId
	 */
	load : function(notificationId) {
		if (this.dataStore==null) {
			this.dataStore = new Ext.data.Store({
				url: this.contextPath + '/siteAdmin/family/loadNotification.json',
				reader : new Ext.data.JsonReader({
					idProperty : 'notificationId',
					root : 'rows',
					fields : [
					    {name : 'notificationId'},
					    {name : 'personId'},
					    {name : 'typeId'},
					    {name : 'typeTitle'},
					    {name : 'enabled'},
					    {name : 'baseDate'},
					    {name : 'nextSendDate'},
					    {name : 'subject'},
					    {name : 'body'}
					]
				}),
				form: this,
				listeners : { 
					load : function(store, records, options) {
						if (records.length>0) {
							this.form.loadRecord(records[0]);
						}
					}
				}
			});
		}
		this.dataStore.load({
			params: {
				personId: this.personId,
				notificationId: notificationId
			}
		});
	},
	
	/**
	 * buildform
	 * 
	 * @private
	 */
	buildFormFields : function() {
		
		// Create the form fields
		return [ {
			xtype : 'hidden',
			name : 'notificationId'
		}, {
			xtype : 'hidden',
			name : 'personId'
		}, {
			fieldLabel : this.typeLabel,
			name : 'typeTitle',
			maxLength : 256,
			allowBlank : this.readonly || false,
			width : 300,
			readOnly : true,
			style: {marginBottom: '15px', marginTop: '0px'}
		}, {
			xtype : 'pvhtmleditor',
			fieldLabel : this.bodyLabel,
			name : 'body',
			allowBlank : false,
			width : 550,
			height : 250,
			readOnly : this.readonly
		} ];
	}

});
