Ext.ns('Ext.pv.notification');
Ext.pv.notification.NotificationFormDialog = Ext.extend(Ext.Window, {
	width: 750,
	height: 470,
	resizable: true,
	autoScroll: true,
	modal: true,
	layout: 'border',
	constrain: true,
	stateful: false,
	closable: true,
	onEsc: Ext.emptyFn,
	
	// Localized strings
	title: 'Edit Scheduled Notification',
	instructionText: 'Use this form to add a personal note to the email that will be sent to the Family and Friends. Click on the preview button to see how the final email will look when it is sent out.',
	previewButtonText : 'Preview',
	okButtonText : 'OK',
	cancelButtonText : 'Cancel',
	alertTitle : 'Message',
	invalidMessage : 'Form is invalid.',
	
	contextPath : null,
	personId : 0,
    record : null,

    /**
     * initComponent
     * @protected
     */
    initComponent : function() {
	
		// Initialize the form fields
        this.form = new Ext.pv.notification.NotificationForm({
        	region : 'center',
        	border : true,
        	margins: {top:0, right:5, bottom:5, left:5},
        	contextPath : this.contextPath,
        	personId : this.personId,
        	readonly: false,
        	record: this.record
        });
        this.items = [{
				layout : 'fit',
				region : 'north',
				height : 60,
				bodyCssClass: 'infoBox',
				html : this.instructionText,
				margins: {top:5, right:5, bottom:5, left:5}
			},this.form
		];
        
        this.buttonAlign = 'left';
        this.buttons = [{
	        	text: this.previewButtonText,
	        	handler: this.onPreview,
	        	scope: this
	        },"->",{
	        	text: this.okButtonText,
	        	handler: this.onOk,
	        	scope: this
	        },{
	        	text: this.cancelButtonText,
	        	handler: this.onCancel,
	        	scope: this
	        }
        ];
        
        this.on(
        	'show',
        	function() {
        		// Initialize form data
        		this.form.loadRecord(this.record);
        	},
        	this
        );
        
        this.addEvents({
            /**
             * @event saved
             * Fires when a record is successfully saved
             * @param {Record} the forms record object
             */
            saved : true
        });
        
        // super
        Ext.pv.notification.NotificationFormDialog.superclass.initComponent.call(this);
    },
    
    load : function(notificationId) {
    	this.form.load(notificationId);
    },
    
    onPreview : function() {
    	var dlg = new Ext.Window({
    		frame : true,
    		modal : true,
    		title : this.previewButtonText,
    		width : 835,
    		height : 500,
    		autoScroll : true,
    		autoLoad : {
    			url : this.contextPath + '/siteAdmin/family/previewNotification.html',
    			method : 'POST',
    			nocache : true,
    			scripts : false,
    			discardUrl : true,
    			params : {
    				personId : this.personId,
    				notificationId : this.form.getForm().findField('notificationId').getValue(),
    				body : this.form.getForm().findField('body').getValue()
    			}
    		},
    		buttons : [{
    			text : this.okButtonText,
    			handler : function() {
    				dlg.close();
    			},
    			scope: this
    		}]
    	});
    	dlg.show();
    },
    
    onOk : function() {
    	this.saveRecord();
    },
    
    onCancel : function() {
    	this.close();
    },
    
    /**
     * saveRecord
     */
    saveRecord : function() {
    	
        if (!this.form.getForm().isValid()) {
        	Ext.Msg.alert(this.alertTitle,this.invalidMessage); 
            return false;
        }

        var myThis = this;
        this.form.getForm().submit({
            clientValidation: true,
            url: myThis.contextPath + '/siteAdmin/family/saveNotification.do',
            success: function(form, action) {
        		myThis.fireEvent('saved', action.result.notificationId);
	        	myThis.close();
            },
            failure: function(form, action) {
            	Ext.Msg.alert(myThis.alertTitle,action.result.error);
            }
        });
    }

});
