When is a document valid
Hello,
I seem to be tripping up on some very basic stuff.
We install an event listener to detect selection changes. This works well if we manually close the palette. If we quit while the palettte is open we get an error saying we are accessing a document when there are not any open.

Here is the error. There is a document window.
The following code demonstrates the problem. In my opinion the alert should not display the documnet name when quiting but it does.
What is the best test to see if a document is valid.
Thanks.
P.
#targetengine "paltester";
main();
function main()
{
var w = new Window( "palette", "pal test" );
w.onShow = function ()
{
app.addEventListener("afterSelectionChanged", selectionChangedFunction);
return;
}
w.onClose = function ()
{
app.removeEventListener("afterSelectionChanged", selectionChangedFunction);
return;
}
w.show ();
function selectionChangedFunction ( )
{
if(app.documents.length <= 0 )
return;
alert ( app.documents[0].name );
return;
}
}