pcwa.core.scope('pcwa.ext.view');
pcwa.ext.view.FormWindow = function FormWindow(config)
{
	this.win = null;
	this.config = config;
	
	if ( !config.width ){ config.width = 400;}
	if ( !config.height ){ config.height = 200;}
	if ( !config.animateTarget ){ config.animateTarget = null;}
	if ( !config.scope ){ config.scope = window;}
	if ( !config.closeOnUpdate ){ config.closeOnUpdate = false;}

	if ( !config.formPanel ){
		config.formPanel = null;
	}else{
		config.formPanel.on	({
								'update': this.onUpdate,
								'change': this.onChange,
								scope: this
							});
	}
};

pcwa.ext.view.FormWindow.prototype = {
	
	createWindow:	function createWindow(){
		if ( this.win ){ delete this.win; }
		
		this.win = new Ext.Window(
		{
			title:	this.config.title || 'Edit',
			layout:	'fit',
			width:	this.config.width,
			height:	this.config.height,
			plain:	true,
			modal:	true,
			shadow:	true,
			items:	this.config.formPanel,
			buttons: [
				{
					id: 'frmSubmit',
					name: 'frmSubmit',
					text: 'Save Changes',
					disabled: true,
					listeners:	{	'click':	function(){
													this.performUpdate(false);
												},
									scope:		this
								}
				},
				{
					id: 'frmClose',
					name: 'frmClose',
					text: 'Close',
					listeners:	{	'click':	function(){
													this.win.close();
												},
									scope:		this
								}
				}
			],
			listeners:	{	'beforeclose':	function(p){
												if ( this.config.formPanel.isDirty() ){
													Ext.Msg.show({
														title:'Save Changes?',
														msg: 'You have pending changes. Would you like to save your changes before closing?',
														buttons: Ext.Msg.YESNOCANCEL,
														fn: this.processSavePrompt,
														scope: this,
														animEl: this.config.animateTarget,
														icon: Ext.Msg.QUESTION
													});
													return false;
												}
											},
							'close':		function(p){
												if ( this.config.onClose && typeof this.config.onClose == 'function' ){
													this.config.onClose.call(this.config.scope, this.config.formPanel.getRecord());
												}
											},
							scope:			this
			}
		});
	},
	
	onUpdate:			function onUpdate(rec, store, lastUpdateOp, params){
							if ( this.config.onUpdate && typeof this.config.onUpdate == 'function' ){
								this.config.onUpdate.call(this.config.scope, rec, store, lastUpdateOp);
							}
							if ( params.closeOnUpdate === true || this.config.closeOnUpdate === true ){
								this.win.close();
							}else{
								this.win.buttons[0].disable();
							}
						},
	onChange:			function onChange(fld, newVal, rec, store){ this.win.buttons[0].enable();},
	performUpdate:		function performUpdate(closeOnUpdate){
							this.config.formPanel.update({closeOnUpdate: closeOnUpdate});
						},
	postInit:			function postInit(){ this.config.formPanel.loadData();},
	processSavePrompt:	function processSavePrompt(btn){
							if ( btn == 'yes' ){
								this.performUpdate(true);
							}else{
								if ( btn == 'no' ){
									this.config.formPanel.reset();
									this.win.close();
								}
							}
						},
	show:				function show(){
							if ( this.win == null ){ this.createWindow();}
							this.win.show(this.config.animateTarget, this.postInit, this);
						}
}

