PAMWF.FormDialog = Class.create( PAMWF.Dialog, {
	_form: null,
	_dm: null,
	_tips: new Array(),
	
	initialize: function ( $super, url, datagrid )
	{
		if (typeof(datagrid) == 'object')
		{
			this._dm = datagrid;
		}
		
		$super( url );
	},
	
	startSuccess: function ( $super, t )
	{
		var fields = null;
		
		if (this._dm != null)
		{
			this._dm._requestInProgress = false;
			var fields = this._dm._fields;
		}
		else if ( t.headerJSON && t.headerJSON.fields )
		{
			var fields = t.headerJSON.fields;
		}
		
		$super(t);
		
		try
		{
			this._myDiv.select('.Tooltip').each(function(s){
				this._tips.push(new PAMWF.Tooltip(s));
			}.bindAsEventListener(this));
		} catch ( e ){}
		
		// Get the form
		this._form = new PAMWF.Form( this, this._myDiv.select('FORM')[0], fields );
	},
	
	_submitSuccess: function (t)
	{		
		if ( typeof(t.responseXML) == 'object' && this._dm )
		{
			this._dm._parseRow( t );
		}
		else
		{
			this._close();
		}
	},
	
	_close: function ( $super, event )
	{
		if (this._form._changed)
		{
			if( confirm( "You have made changes to this form. Are you sure you want to close it?"))
			{
				if (confirm ( "Do you want to save the changes now?"))
				{
					$('submit').click();
				}
				else
				{
					$super( event );
				}
			}
		}
		else
		{
			$super( event );
		}
	},
	
	_cleanup: function( $super )
	{
		$super();
		
		if (this._dm != null)
		{
			this._dm._endDialog();
		}
		
		this._tips.each(function(s){s.destroy();});
	},
	
	startFailure: function ( $super, t) 
	{
		if (this._dm != null)
		{
			this._dm._requestInProgress = false;
		}
		
		$super(t);
	}
});