;+
; NAME
;          EIS_CHECK_TL_INTEGRITY
;
; PURPOSE
;          This function checks that there are NO studies in the timeline
;          database that are NOT in the study database. It checks that
;          the highest study id (ID field in the study database) is greater
;          than the corresponding highest study id (ST_ID field in the
;          timeline database). Returns 0/1 on fail/pass.
;
; NOTES
;          This check ensures that the studies in the timeline database do
;          actually exist in the study database.
;          If this condition is false, then the PT will NOT be invoked!
;
; CATEGORY
;          EIS Timeline Planning
;
; INPUTS
;          None
;
; WRITTEN
;          John A Rainnie, RAL, 26-Aug-2008
;
; HISTORY
;          v0.1 JAR 26-Aug-2008
;               Added planning_db
;
;
;-
;______________________________________________________________________________
FUNCTION eis_check_tl_integrity

;replace call to astrolib
defsysv,'!DEBUG', 0
defsysv,'!TEXTUNIT', 0
defsysv,'!PRIV', 2
defsysv,'!TEXTOUT', EXIST = EXIST
;
; Check all studies in timeline_db are in the study database
;
; Get the highest number study_id in the timeline_db
status = eis_open_db(/TIME)
dbext , -1 , 'ST_ID' , tl_st_id_list
highest_tl_study_id = MAX(tl_st_id_list)       ;299
dbclose
; Get the highest number study_id in the study database
status = eis_open_db(/STUDY)
dbext , -1 , 'ID' , st_id_list
highest_st_study_id = MAX(st_id_list)       ;302
dbclose

IF (highest_tl_study_id GT highest_st_study_id) THEN BEGIN
    BEEP
    mText = ["There are studies in the timeline that don't exist"    , $
            "in the study database. Aborting!"]
    d = DIALOG_MESSAGE(mText , /ERROR)
    RETURN , 0
ENDIF

RETURN , 1

END