function wo_mast_sched_comm(Branch, CurParam, CaseSens )
declare function set_property, fieldcount, get_property, msg, dialog_box
declare function editcell
declare function send_message, utility, key_sort, start_window, entid, repository
declare subroutine end_dialog


$INSERT MSG_EQUATES
$INSERT POPUP_EQUATES
$INSERT LOGICAL
$INSERT WO_LOG_EQU
$INSERT WO_STEP_EQU


equ Yellow$ to 65535
equ White$  to 16777215

ReturnVar = 0
Branches = 'PREV,NEXT,CANCEL,CLEARPOS,SET_WO_SCHEDULED'
convert ',' to @fm in Branches
locate Branch in Branches using @fm setting Bpos then
   on Bpos gosub PREV, NEXT, CANCEL, CLEARPOS, SET_WO_SCHEDULED
end else
   Void = msg( '', 'Invalid Branch ':Bpos:' passed to wo_mast_sched_comm' )
end
return ReturnVar
*============================================================================*
PREV:
   *DEBUG
   * CurParam = the string the user entered to search for
   * LastPos is Column:@fm:Row of the last found string
   SchedInfo = get_property( 'WO_MAST_SCHED.SCHED_INFO', 'ARRAY' )   
   LastPos = get_property( 'WO_MAST_SCHED', '@LastPos' )
   if LastPos else
      LastPos = fieldcount( SchedInfo, @fm ):@fm:get_property( 'WO_MAST_SCHED.REACTOR_CNT', 'TEXT' )+1
   end
   Col = LastPos<1>
   Row = LastPos<2>
   SchedInfo = get_property( 'WO_MAST_SCHED.SCHED_INFO', 'ARRAY' )
   * we know they are searching back so only search on LastPos backward
   SchedInfo = field( SchedInfo, @fm, 1, Col ) ;* start with one and get up to cur column
   if Row-1 = 0 then
      TColData = ''
   end else
      TColData = field( SchedInfo<Col>, @vm, 1, Row-1 )
   end
   SchedInfo<Col> = TColData
   Found = false$
   for i = Col to 1 step -1
      ThisColumn = SchedInfo<i>
      if CaseSens else
         convert @lower_case to @upper_case in ThisColumn
         convert @lower_case to @upper_case in CurParam
         * because count is case sensitive yuchhhhhhhhhhhhhhhhhhh!!!!
      end   
      CurParamCnt = count( ThisColumn, CurParam )
      if CurParamcnt then
	      if CaseSens then
	         Fpos = index( ThisColumn, CurParam, CurParamCnt )
	      end else
	         Fpos = indexc( ThisColumn, CurParam, CurParamCnt )
	      end      
	      if Fpos then
	         TColumn = ThisColumn[1,Fpos]
	         RowFound = fieldcount( TColumn, @vm )
	         LastPos = i:@fm:RowFound
	         Void = set_property( 'WO_MAST_SCHED', '@LastPos', LastPos )         
	         Void = set_property( 'WO_MAST_SCHED.SCHED_INFO', 'SELPOS', LastPos )
             Void = send_message( 'WO_MAST_SCHED.SCHED_INFO', 'COLOR_BY_POS', Col, Row, White$ )
             Void = send_message( 'WO_MAST_SCHED.SCHED_INFO', 'COLOR_BY_POS', i, RowFound, Yellow$ )                 
	         Found = true$
	      end
	  end
     until Found
   next i
   if Found else
      MsgInfo = ''
      MsgInfo<micon$> = '!'
      MsgInfo<mtext$> = quote(CurParam):' not found...'
      Void = msg( '', MsgInfo )
      *Void = set_property( 'WO_MAST_SCHED', '@LastPos', '' )         
   end
return
*============================================================================*
NEXT:
   *debug
   * CurParam = the string the user entered to search for
   * LastPos is Column:@fm:Row of the last found string
   LastPos = get_property( 'WO_MAST_SCHED', '@LastPos' )
   if LastPos else
      LastPos = 1:@fm:0
   end
   Col = LastPos<1>
   Row = LastPos<2>
   SchedInfo = get_property( 'WO_MAST_SCHED.SCHED_INFO', 'ARRAY' )
   * we know they are searching forward so only search on LastPos forward
   SchedInfo = field( SchedInfo, @fm, Col, 99999 )
   BegFields = Col - 1
   if BegFields then
      SchedInfo = str( @fm, BegFields ):SchedInfo
   end
   TColData = field( SchedInfo<Col>, @vm, Row+1, 999 )
   BegValues = Row
   if BegValues then
      TColData = str( @vm, BegValues ):TColData
      SchedInfo<Col> = TColData
   end
   Found = false$
   CCnt = fieldcount( SchedInfo, @fm )
   for i = Col to CCnt
      ThisColumn = SchedInfo<i>
      if CaseSens then
         Fpos = index( ThisColumn, CurParam, 1 )
      end else
         Fpos = indexc( ThisColumn, CurParam, 1 )
      end      
      if Fpos then
         TColumn = ThisColumn[1,Fpos]
         RowFound = fieldcount( TColumn, @vm )
         LastPos = i:@fm:RowFound
         Void = set_property( 'WO_MAST_SCHED', '@LastPos', LastPos )         
         Void = set_property( 'WO_MAST_SCHED.SCHED_INFO', 'SELPOS', LastPos )
         Void = send_message( 'WO_MAST_SCHED.SCHED_INFO', 'COLOR_BY_POS', Col, Row, White$ )
         Void = send_message( 'WO_MAST_SCHED.SCHED_INFO', 'COLOR_BY_POS', i, RowFound, Yellow$ )                 
         Found = true$
      end 
     until Found
   next i
   if Found else
      MsgInfo = ''
      MsgInfo<micon$> = '!'
      MsgInfo<mtext$> = quote(CurParam):' not found...'
      Void = msg( '', MsgInfo )
      *Void = set_property( 'WO_MAST_SCHED', '@LastPos', '' )         
   end
return
*============================================================================*
CANCEL:
   LastPos = get_property( 'WO_MAST_SCHED', '@LastPos' )
   if LastPos then
      Col = LastPos<1>
      Row = LastPos<2>
      Void = send_message( 'WO_MAST_SCHED.SCHED_INFO', 'COLOR_BY_POS', Col, Row, White$ )
   end
   Void = set_property( 'WO_MAST_SCHED', '@LastPos', '' )
   end_dialog( @window, '' ) 
return
*============================================================================*
CLEARPOS:
   LastPos = get_property( 'WO_MAST_SCHED', '@LastPos' )
   if LastPos then
      Col = LastPos<1>
      Row = LastPos<2>
      Void = send_message( 'WO_MAST_SCHED.SCHED_INFO', 'COLOR_BY_POS', Col, Row, White$ )
   end
   Void = set_property( 'WO_MAST_SCHED', '@LastPos', '' )
return
*============================================================================*
SET_WO_SCHEDULED:
	SchedArray = get_property( @window:'.SCHED_INFO', 'ARRAY' )
	WOsToUpdate = ''
	Cols = fieldcount( SchedArray, @fm )
	Rows = fieldcount( SchedArray<1>, @vm )
	
	for Col = 1 to Cols
		for Row = 1 to Rows
			Reactor = Row
			
			ThisCol = SchedArray<Col,Row>
			TWOs = iconv( ThisCol, '[WO_VALID,1]' )
			* PASSING A 1 TO PREVENT ERROR MESSAGES
			convert ';' to @fm in TWOs
			Wcnt = fieldcount( TWOs, @fm )
			for W = 1 to Wcnt
				ThisWo = TWOs<W>
 				IF INDEX(ThisWo,'.',1) THEN
					WOStepKey = ThisWo
				END ELSE
					WOStepKey = ThisWo:'.1'
				END
            
				LOCATE WOStepKey IN WOsToUpdate<1> USING @VM SETTING Fpos ELSE
					WOsToUpdate = INSERT(WOsToUpdate,1,Fpos,0,WOStepKey)
					WOsToUpdate = INSERT(WOsToUpdate,2,Fpos,0,Reactor)
				END
			NEXT W		   
		NEXT Row
	NEXT Col
	
	WOStepKeys = WOsToUpdate<1>
	CONVERT '.' TO '*' IN WOStepKeys
	WOsToUpdate<1> = WOStepKeys
	
	SchedStatus = XLATE( 'WO_Step', WOsToUpdate<1>, WO_STEP_SCHEDULED$, 'X' )
	Scnt		= FIELDCOUNT( WOsToUpdate<1>, @VM )
	
	OPEN 'WO_STEP' TO WOStepTable ELSE
		Void = msg( '', 'Unable to open WO_STEP...' )
		return 0
	END
		
	IF Scnt > 2 THEN
		Def = ''
		Def<MCAPTION$>		= 'Scheduling Work Orders...'
		Def<MTYPE$>			= 'G'
		Def<MEXTENT$>		= Scnt
		Def<MTEXTWIDTH$>	= 600
		MsgUp = Msg(@WINDOW,Def)
	END ELSE
		MsgUp = ''
	END

	LockErrs = ''
	FOR I = 1 TO Scnt
		ThisWoLog	= WOsToUpdate<1,I>
		ReactNo		= WOsToUpdate<2,I>
		
		IF MsgUp NE '' THEN
			Msg(@WINDOW, MsgUp, I, MSGINSTUPDATE$)
		END
		
		ThisSchedStat = SchedStatus<1,I>
		IF ( ThisSchedStat = true$ ) ELSE
			
			LOCK WOStepTable, ThisWoLog THEN
				READ WOStepRec FROM WOStepTable,ThisWoLog THEN
					WOStepRec<WO_STEP_SCHEDULED$> = 1
					
					CurrDate = OCONV(Date(),'D4/')
					CurrTime = OCONV(Time(),'MTH')
					WOStepRec<WO_STEP_SCHED_DTM$> = ICONV(CurrDate:' ':CurrTime,'DTM')
					
					WRITE WOStepRec ON WOStepTable,ThisWoLog ELSE
						Void = msg( '', 'Unable to open WO_STEP...' )
						RETURN 0
					END
				END
				UNLOCK WOStepTable,ThisWoLog ELSE
					Void = msg( '', 'Unable to unlock WO_STEP ':ThisWoLog )
					RETURN 0
				END
			END ELSE
				LockErrs<-1> = ThisWoLog
			END
		END
	NEXT I
	
	IF MsgUp NE '' THEN Msg(@WINDOW,MsgUp)
	
	if ( LockErrs <> '' ) then
	   swap @fm with ', ' in LockErrs
	   MsgInfo = ''
	   Tvar = 'The following WO(s) ':LockErrs:' could not be set to scheduled, due to them being '
	   Tvar:= 'in use.  You will have to manuall set them to scheduled or try at a later time.'
	   MsgInfo<mtext$> = Tvar
	   MsgInfo<micon$> = '!'
	   Void = msg( '', MsgInfo )
	end
return
*============================================================================*

