	PRO KeySearch, key, vec, sxtp=sxtp, sxtf=sxtf, hxt=hxt, $
			wbs=wbs, bcs=bcs
;	-------------------------------------------------------------
;+						20-Aug-1991
;	Name:
;		KeySearch
;	Purpose:
;		Decode item, operator, and value from 'key' parameter,
;		apply the search to the input data vectors and return
;		the new data vectors.  A single success vector is always
;		returned for each call; a multiple item search begins
;		with the first item in the list which is "AND" to the
;		results of the next item search and so on.
;	CALLING SEQUENCE:
;		KeySearch, key, vec, [sxtp=sxtp, sxtf=sxtf, hxt=hxt, 
;					wbs=wbs, BCS=bcs]
;	Input:
;		key 	item, operator and value for Search
;	Input/Output:
;		vec	data vector to be searched and updated
;	KeyWord-input/output:
;		sxtp	flag giving the type of data vector
;		sxtf	flag giving the type of data vector
;		bcs 	flag giving the type of data vector 
;		hxt 	flag giving the type of data vector 
;		wbs 	flag giving the type of data vector 
;	History:
;		written 14-May-91 by GAL.
;		6-Aug-1991, updated for changes to formal parameters 
;		on calls to ParsKey and KaySea.
;		13-Aug-91, updated logic for multiple instrument search
;		20-Aug-91, updated for wbs and hxt as seperate records
;-
;	---------------------------------------------------------------
;	ON_ERROR, 2	;force a return to caller on error

	keys = Str2Arr(key, delimit=';')	;use ';' to parse keys
	siz_keys = SIZE(keys)
	nKeys = siz_keys(siz_keys(0)+2)	;total # elements
	match = 1			;assume all found

	FOR i= 0, nKeys-1 DO BEGIN	;do for all requests
	  IF (match) THEN BEGIN
	    ParsKey, keys(i), item, op, val, op2=op2, val2=val2, $
				op3=op3, val3=val3

;	    Is This a valid ITEM:
	    IF (KEYWORD_SET(sxtp) OR KEYWORD_SET(sxtf)) THEN BEGIN
	      sxt = 1			;set sxt data present flag
	    ENDIF
	    itemStat = Ck_Item(item, BCS=bcs, HXT=hxt, SXT=sxt, WBS=wbs)
	    IF (itemStat) THEN BEGIN	;valid search Key found	    
	      IF (sxt) THEN BEGIN	;SXT sea ok
	        idx = SXTKaySea(vec, item, op, val, op2=op2, val2=val2,$
				op3=op3, val3=val3)
	        IF (idx(0) ne -1) THEN BEGIN
	          vec = vec(idx)	;update search vector for next
					;call
	        ENDIF ELSE BEGIN 	;NO matches FOUND!!!
	          vec = LONG(-1)
	          match = 0		;skip all other requests
	        ENDELSE
	      ENDIF			;for SXT sea
	    ENDIF ELSE BEGIN            ;pass all back i.e. no search
              vec = vec			
	    ENDELSE			;end of itemStat check

	  ENDIF				;for "match" check
	ENDFOR

	RETURN
	END
