;+
; NAME        : EIT_SUB_FIXOFF
;
; PURPOSE     :
;
; CATEGORY    :
;
; EXPLANATION : 
;
; SYNTAX      : 
;
; EXAMPLES    : none
; 
; CALLED BY   :
;
; CALLS TO    : none
;
; ARGURMENTS  :	none
;
; KEYWORDS    : none
;
; OUTPUTS     : none
;
; COMMON      : none
;
; RESTRICTIONS: none
;
; SIDE EFFECTS: none
;
; PROJECT     : SOHO - EIT
;
; HISTORY     : V1,  Elaine Einfalt (HSTX)
; 	1999-march  Einfalt - - passing group to more routines for V5 modal
;
; CONTACT     : eit@xanado.nascom.nasa.gov
;-

function EIT_SUB_FIXOFF, sub_ids=sub_ids, offset=offset, $
			 extent=extent,   cam_ro=cam_ro,  group=group

   ;
   ; If necessary, adjust offset so that the the subfield pattern will
   ; fit on the screen.
   ; Offset is the upper left corner of the greatest extent of the pattern
   ; So check if pattern goes of bottom or right side.
   ;

   ; Already know that it can't go off the top or left side,
   ; because the offset is defined at the top left corner.
   ; 1997/6/25 Apparently it can move off the top and crash.

   if offset lt 0 then offset = offset + 32

   ; Given the the subfield pattern and an offset, 
   ; will the right most block fit on the screen.

   blocks = where(sub_ids ne 0, n_blk)  ; subfield indices
   if n_blk eq 0 then return,1

   columns = blocks mod 32		; get the columns of each field
   rows = blocks/32			; rows of each field

   ; get the first and last columns used by the subfield pattern
   ; get the top and bottom rows used by the subfield pattern

   fstcol = min(columns, left_ind)
   lstcol = max(columns, right_ind)

   botrow = max(rows, bot_ind)
   toprow = min(rows, top_ind)


   diffcol = lstcol - fstcol		; width of pattern - 1
   req_col = offset mod 32		; offset's requested column
   max_col = req_col < (31 - diffcol)


   diffrow = botrow - toprow		; height of the pattern - 1 
   req_row = offset/32			; offset's requested row
   max_row = req_row < (31 - diffrow)


   ; Combine max_col and max_row to get validated offset

   offset = 32 * max_row + max_col

   ; When cam_ro is present this is from a Ready button press so
   ; any subfield pattern must lay completely within the read out area.
   ; cam_ro = (p1row, p1col, p2row, p2col, rowsum, colsum)
   

   if n_elements(cam_ro) gt 0 then begin

      offset_toprow = max_row			; after offset is applied
      offset_botrow = max_row + diffrow
      offset_fstcol = max_col			; after offset is applied
      offset_lstcol = max_col + diffcol

      cam_el = str_sep(cam_ro,',')		; (p1row,p1col,p2row,p2col)
      if offset_toprow lt cam_el(0) or $
	 offset_botrow gt cam_el(2) or $
	 offset_fstcol lt cam_el(1) or $
	 offset_lstcol gt cam_el(3) then begin

	 ack = respond_widg($
		mess=[ $
		'The readout area ('+ strtrim(cam_el(1),2) + ',' + $
		strtrim(cam_el(0),2) + ',' + strtrim(cam_el(3),2) + $
		',' + strtrim(cam_el(2),2) +') is misaligned or', $
		'not large enough to completely include the ' + $
		'subfield pattern at the current offset.', $
	      ' ', $
	      'The subfield pattern maximum extent is: ' + $
		strtrim(offset_fstcol,2) + ',' + strtrim(offset_toprow,2) +$
		','+ $
		strtrim(offset_lstcol,2) + ',' + strtrim(offset_botrow,2)], $ 
	butt='Acknowledge', /row, title='Misaligned Readout Area', $
	group=group)


	 return, 0			; force to a different value so it 
					; fails validity test in EIT_LOAD_KEYTST
      endif


   endif


return, 1
end

