
	Xsn.FormDialog = function(config){
		Ext.apply(this, config);
		this.addEvents({'load':true});
		this.dialog;
		this.init();
	}		

	Ext.extend(Xsn.FormDialog, Ext.util.Observable, {

		init: function(){
			var dialog = new Ext.LayoutDialog(Ext.id(), {
                modal: true,
                width: this.width || 350,
                height: this.height || 225,
                shadow:true,
                autoCreate: {tag: 'form', method:'POST', onSubmit:false},
				title: this.title || '',
                animateTarget:null,
                draggable:false,
    	        closable: false,
    	        resizable:false,
	            collapsible:false,                
                center: {
                    titlebar: false,
					autoScroll:false,
					tabPosition:'top'
                }
            });
            dialog.addKeyListener(27, dialog.hide, dialog);
            dialog.addButton(this.okButtonText || 'Ok', this.onSave, this);
		    dialog.addButton(this.cancelButtonText || 'Annuleer', dialog.hide, dialog);
			this.dialog = dialog;
		},

		onSave : function(){
		 	if (this.form.getForm().isValid()){
				vals = this.form.getForm().getValues();
				Ext.applyIf(vals, this.form.getForm().baseParams);					 	 
				this.hide();				
				if (this.callback) this.callback(vals);
			} else {
				Ext.MessageBox.alert(this.dialog.title, 'Het formulier is onjuist ingevuld.');
			}
		},
		
		setForm : function(form){
		 	var tabs = form.getContainers();
		 	var layout = this.dialog.getLayout();
		 	
		 	while (layout.getRegion('center').getActivePanel()) layout.getRegion('center').remove(layout.getRegion('center').getActivePanel());

		 	for (var i=0;i<tabs.length;i++){
				layout.add('center', new Ext.ContentPanel(tabs[i].getEl(), {title:tabs[i].title}));				
			}
			form.render(this.dialog.getEl());
			this.form = form;
		},

		getDialogForm : function(){
			return this.form;
		},

		show : function (callback, src){
		 	this.callback = callback;
			this.dialog.show(src);				
		},
		
		hide : function (){
            this.dialog.hide();			
		},
		
		getEl : function(){
			return this.dialog.getEl();
		}
		
	});

