Ext.namespace("Ext.ss.form");
Ext.ss.form.FormPanel = Ext.extend(Ext.form.FormPanel, {
    frame: false,
    header: false,
    border: false,
    statusBarPlugin: false,
    autoScroll: true,
    anchor: '100% 100%',
    bodyStyle: 'padding:10px 10px 0px 10px;background-color:#FFFFFF;',
    footerStyle: 'background-color:#FFFFFF;',
    labelWidth: 90,
    deferTime: 500,
    statusBar: null,
    app: null,
    initComponent : function() {
		var statusBarConfig = {id: this.id + '-statusbar'};
		if(this.statusBarPlugin == true) {
			statusBarConfig.plugins = new Ext.ux.ValidationStatus({form:this.id});
		}
		this.statusBar = new Ext.ux.StatusBar(statusBarConfig);
		// super
	    Ext.ss.form.FormPanel.superclass.initComponent.call(this);
	},
    getStatusBar : function() {
    	return this.statusBar;
    },
	buildUI: function(){
        return [{
            text: Ext.ss.locale.Common.prototype.saveButtonText,
            iconCls: 'silk-disk',
            handler: this.onUpdate,
            scope: this
        }, {
            text: Ext.ss.locale.Common.prototype.resetButtonText,
            iconCls: 'silk-arrow-undo',
            handler: this.onReset,
            scope: this
        }];
    },
    onUpdate : function(btn, ev) {
		var theForm = this;
        if(theForm.getForm().isValid()) {
			theForm.getStatusBar().showBusy(Ext.LoadMask.prototype.msg);
			theForm.getEl().mask();
			theForm.getForm().submit( {
				exception : function (form, action) {
					theForm.getStatusBar().setStatus({
	                    text:action.result.errors.desc,
	                    iconCls:'x-status-error',
	                    clear: true
	                });
					theForm.getEl().unmask();
				},
				failure : function(form, action) {
					var desc = '';
					var res = action.result;
					
					if(res.errors.validator) {
						var repText = '';
						var field = null;
						if(res.errors.field) {
							var repText = res.errors.field;
							field = theForm.getForm().findField(res.errors.field);
							if(field) {
								repText = field.fieldLabel;
							}
						} else if(res.errors.desc) {
							repText = res.errors.desc;
						}

						if(Ext.ss.locale.Common.prototype[res.errors.validator + 'Text']) {
							desc = String.format(Ext.ss.locale.Common.prototype[res.errors.validator + 'Text'],repText)
						} else {
							desc = '[' + res.errors.validator + ']';
						}
						if(field) {
							field.markInvalid(desc);
						}
					} else if(res.errors.desc) {
						desc = res.errors.desc;
					}
					
					theForm.getStatusBar().setStatus({
	                    text: desc,
	                    iconCls: 'x-status-error',
	                    clear: true
	                });
					theForm.getEl().unmask();
				},
				success : function(form, action) {
					theForm.getStatusBar().setStatus({
	                    text: Ext.ss.locale.Common.prototype.successServerText,
	                    iconCls: 'x-status-valid',
	                    clear: true
	                });
					theForm.onSuccess.defer(theForm.deferTime,theForm,[theForm,action.result.data]);
				}
			});
        };
    },
    onReset : function(btn, ev) {
    	if(this.getForm().isDirty()) {
	        this.fireEvent('update', this, this.getForm().getValues());
	        this.getForm().reset();
    	}
    },
    onSuccess: function(theComp,result) {
    	if(theComp.record)
    	{
    		theComp.getForm().updateRecord(theComp.record);
    		theComp.record.id = result[0][theComp.idProperty];
    		theComp.record.set(theComp.idProperty,result[0][theComp.idProperty]);

	    	if(theComp.record.phantom) {
	    		theComp.insertNewRecord(theComp.record);
	        }
	    	theComp.record.phantom = false;
	    	theComp.record.commit();
	    	theComp.closeTab(theComp);
    	} else {
    		theComp.getEl().unmask();
    	}
    }
});
