
! script name: vprocesstable.d/insertevent
! var_topic: insert event into process table

!A
!A NAME:
!A
!A 	Modify Process Table: Insert Event	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	Place an event into the process table.
!A
!B INSTRUCTIONS:
!B
!B	CHOOSE EVENT TO BE INSERTED: Pick the event to insert.
!B
!B	INSERT BEFORE WHICH ROW, End, New:  Choose the row that
!B		will be after the event.  Choose end to append
!B		the event.  Choose new to pick another event.
!B
!# see also - htdocs/help.d/datatables.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: insert rows ")

DIM Answer$[256]
DIM A$[256]
DIM Destination$[256]
DIM Table$[256]
DIM Name$[256],Name2$[256]
DIM Data_name1$[256],Work_zone1$[256]
DIM A$[256]

Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave
DEG
IF NOT ValidDependency("Amo process table") THEN Leave
IF NOT IsEntityDependent("Amo process table") THEN Leave

WsEventData= 12345
Start:!
Stat=UserSelectBox("EVENTS","SELECTED EVENT",WsEventData,WsUserSelect)
IF Stat=1 THEN Leave

Stat=SetInputFormat("a99","")
Stat=GetValue("CHOOSE EVENT TO BE INSERTED",Name$)
IF (ExitScript(Destination$)) THEN Leave
IF Name$= "CANCEL" THEN Leave

Event=FindUserData(Name$,WsEventData)
IF NOT ValidGeometry(Event) THEN Start

ActivateUndo
Element_loop:!
Stat= SetInputFormat("a1",",E,N,")
Table1=PickEntity("INSERT BEFORE WHICH ROW, End, New ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="N" THEN Start
Is_end= (Answer$="E")
IF NOT Is_end THEN
	IF NOT ValidDependency(Table1) THEN Element_loop
	IF NOT IsEntityDependent(Table1) THEN
		Stat= MessageFileBox("/usr/weber/database/messages/nonvdmprtbl","ERROR")
		GOTO Element_loop
	END IF

	Stat= EntityNameCheck(Table1,Table$)
	IF Table$<>"Amo process table" THEN Element_loop
	T1= LastPickPara

	R1=TableParaRow(Table1,T1)
	IF R1=1 THEN Element_loop

	Stat= GetTableEntryStr("Amo process table",R1,1,Data_name1$)
	Stat= GetTableEntryStr("Amo process table",R1,2,Work_zone1$)
END IF

High_val= 0
Num_rows= NumberTableRows("Amo process table")
FOR J= 2 TO Num_rows
	EntData= AmoTaskTableEvent("Amo process table",J)
	IF ValidGeometry(EntData) THEN
		IF AreEntitiesSame(EntData,Event) THEN
			Stat= GetTableEntryStr("Amo process table",J,2,Answer$)
			K= POS(Answer$,"#")
			IF K THEN
				Curr_val= VAL(Answer$[K+1])
				IF Curr_val> High_val THEN High_val= Curr_val
			END IF
		END IF
	END IF
NEXT J
Inst_name$= "Instance #"&VAL$(High_val+1)

ActivateUndo
Stat= AdvanceUndoNumber("insert event")
Stat= AddItemToUndo(WsUBModifyMode,Table1)

Stat= EraseEntity("Amo process table")
A$="TABLE: Insert event "&Name$
RecordFunctionOn
Stat= RecordDescr(A$)
V1=StringConstant("Amo process table")
V2=StringConstant(Data_name1$)
V3=StringConstant(Work_zone1$)
V4=StringConstant(Name$)
V5=StringConstant(Inst_name$)
E1= FindNamedEntity(V1)
IF Is_end THEN
Stat= ReDimTableRows(E1,1,1)
Stat=InsertIntoTable(E1,V4,NumberTableRows(E1),1)
Stat=InsertIntoTable(E1,V5,NumberTableRows(E1),2)
ELSE
V6= FreeParameter("Inserted before row",ProcessTableRow(E1,V2,V3))
Stat= InsertTableRows(E1,V6,1)
Stat=InsertIntoTable(E1,V4,V6,1)
Stat=InsertIntoTable(E1,V5,V6,2)
END IF
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
Stat= PlotEntity("Amo process table")

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Element_loop

Leave:!
stat= SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
