/*
	Given a path of the form, "D:\..\prefix_*.*", find all valid
	BMP or JPG images with the given prefix, placing them in an
	indexed list as they are found.  Sort the list by ascending epoch
	and supply the earliest and latest epochs, and the corresponding 
	file names, of the bud images.

 */
int FindInst( char path[] )
{
	static struct _finddata_t inf ;
	static   long search ;
    static    int i, len, found, disordered ;
    static   char *p, fname[256] ;
	static  BUDID B ;

	    found = 0 ;
	InstFound = 0 ;
	InstFirst = 0.0 ;
	InstFinal = 0.0 ;

	  // find the first one

	  if ((search = _findfirst( path, &inf)) != -1L) 
	  {
			  if (		// file is either a jpg or a bmp
				  (strstr(_strlwr(inf.name), ".jpg") != NULL)
				   ||
				  (strstr(_strlwr(inf.name), ".bmp") != NULL)
				 )
					{
						strcpy(fname, inf.name) ;
						len = strlen(fname) ;

						if (ParseBudID(fname, &B, NO_PTH, IS_EXT)) // standard bud file?
						{
						  p = (char *)GlobalAlloc(GPTR, (len + 2) * sizeof(char)) ;

							if (p != NULL)
							{
								InstNames[found] = p ;   
								strcpy(InstNames[found], fname) ;
							}

							InstEpoch[found] = B.JulEp ;
							found++ ;
						}
					}

		  // find the others

		  while (_findnext(search, &inf) == 0)
		  {
			  if (		// file is either a jpg or a bmp
				  (strstr(_strlwr(inf.name), ".jpg") != NULL)
				   ||
				  (strstr(_strlwr(inf.name), ".bmp") != NULL)
				 )
				{
						strcpy(fname, inf.name) ;
						len = strlen(fname) ;

				 if (ParseBudID(fname, &B, NO_PTH, IS_EXT)) // standard bud file?
				 {
						p = (char *)GlobalAlloc(GPTR, (len + 2) * sizeof(char)) ;

						if (p != NULL)
							{
								InstNames[found] = p ;   
								strcpy(InstNames[found], fname) ;
							}

							InstEpoch[found] = B.JulEp ;
							found++ ;
				 }
				}
		}

		_findclose( search ) ;
	  }
	  else
	  {
		  msgbox = 1 ;
		  MessageBox(NULL,"No Instance found","Find and Sort 
Instances",MB_ICONEXCLAMATION|MB_OK) ;
		  msgbox = 0 ;
		  return found ;
	  }
		   // sort the lists into ascending epoch order 

		   disordered = 1 ;
		   while (disordered)
		   {
			disordered = 0 ;
				for (
					 i = 0 ;
					 i < found - 1 ;
				     i++
					)
					{
					if (InstEpoch[i + 1] < InstEpoch[i])
						{
							double tmp ;
							  char t[256] ;

							tmp = InstEpoch[i] ;
							InstEpoch[i] = InstEpoch[i+1] 
;
							InstEpoch[i+1] = tmp ;
							strcpy(t, InstNames[i]) ;
							strcpy(InstNames[i], InstNames[i+1]) ;
							strcpy(InstNames[i+1], t) ;
							disordered = 1 ;
						}
					}
		   }

// supply first and final epochs and names

		   InstFirst = InstEpoch[0] ;
		   InstFinal = InstEpoch[found - 1] ;
		   strcpy(FirstName, InstNames[0]) ;
		   strcpy(FinalName, InstNames[found - 1]) ;

		return found ;
}//End of FindInst()


