 
! script name: amo.d/delworkzones
! topic:	Delete work zones from the Amo workzone execute list.

!A
!A NAME:
!A
!A 	DELETE WORK ZONE (AMO)
!A
!A DESCRIPTION:
!A
!A 	This script deletes workzones from the Amo work zone execute list.
!A

stat=ChangeLabelStr("ScriptNameStatus","Function: delete work zone from amo ")

DIM Answer$[256]
DIM Destination$[256]
DIM D_label$[64]
DIM Choice$[64]
DIM Namelist$[32000]
DIM Name$[128]
DIM Descr$[512]

DeActivateUndo
CALL Convert_old_origins
WsEventData= 12345
WsAmoData= 23051
WsNamedComponent= 7041776

CompList= FindUserData("AMO WORK ZONES",WsAmoData)
IF NOT ValidGeometry(CompList) THEN Leave

Make_list:!
Namelist$=""
Ncomp= GetUserValue(CompList,"Number components:")
FOR I= 1 TO Ncomp
   Stat= GetUserString(CompList,"Component "&VAL$(I)&":",Name$)
   IF POS(Name$,"WZ EVENT ")=1 THEN 
		Namelist$= Namelist$&Name$[10]&","
   ELSE
		Wzone= GetUserValue(CompList,"Component "&VAL$(I)&":")
        	H1= FindZoneOrigin(WsWorkZoneOrigin,Wzone)
        	E1= GetAssocEnt(H1,WsWorkZoneOrigin)
		IF ValidGeometry(E1) THEN
			Zone_num= GetUserValue(E1,"Zone number:")
        		If(Zone_num) THEN
                	   Descr$= UserDataStr(E1,"Description:")
                	   Namelist$= Namelist$&VAL$(Zone_num)&": "&Descr$&","
			END IF
		ELSE
			Namelist$= Namelist$&Name$&":  ,"
        	END IF
   END IF
NEXT I

List_box:!
IF NOT LEN(Namelist$) THEN
	Stat= DeleteEntity(CompList)
        GOTO Leave
END IF
D_label$= "WORK ZONES LIST"
Choice$= "WORK ZONE TO DELETE"
Stat= GeneralListBox(D_label$,Choice$,Namelist$)
Stat=SetInputFormat("a99","")
Element=PickEntity("CHOOSE WORK ZONE TO REMOVE ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="CANCEL" THEN Leave

Ncomp= GetUserValue(CompList,"Number components:")
E1= FindUserData(Answer$,WsEventData)
IF NOT ValidGeometry(E1) THEN
	IF ValidGeometry(Element) THEN
                Wzone= GetComponent(Element)
        ELSE
                Wzone= NumericPrefix(Answer$)
        END IF
	IF Wzone THEN
	   FOR I= 1 TO Ncomp
   		Stat= GetUserString(CompList,"Component "&VAL$(I)&":",Name$)
   		IF NOT (POS(Name$,"WZ EVENT ")=1) THEN
                     Zone_num= GetUserValue(CompList,"Component "&VAL$(I)&":")
		     IF Zone_num= Wzone THEN
		        Stat= DeleteUserField(Complist,"Component "&VAL$(I)&":")
		     END IF
		END IF
	   NEXT I
	END IF
ELSE
	FOR I= 1 TO Ncomp
          Stat= GetUserString(CompList,"Component "&VAL$(I)&":",Name$)
          IF POS(Name$,"WZ EVENT ")=1 THEN
	     Name$= Name$[10]
	     IF Name$= Answer$ THEN
	       	Stat= DeleteUserField(Complist,"Component "&VAL$(I)&":")
 	     END IF
	  END IF
	NEXT I
END IF
Renumber_list:!
	J=0
	FOR I= 1 TO Ncomp
		Stat= GetUserString(CompList,"Component "&VAL$(I)&":",Name$)
		Name$= TRIM$(Name$)
		IF LEN(Name$) THEN
		   J=J+1
		   Stat= PutUserString(CompList,"Component "&VAL$(J)&":",Name$)
		END IF
	NEXT I
	Stat= PutUserValue(CompList,"Number components:",J)
	FOR I= J+1 TO Ncomp
	       	Stat= DeleteUserField(CompList,"Component "&VAL$(I)&":")
	NEXT I
IF NOT GetUserValue(CompList,"Number components:") THEN
	Stat= DeleteEntity(CompList)
	GOTO Leave
END IF
GOTO Make_list

Leave:!
LowerSelectBox
IF (NOT LEN(Destination$)) THEN
         LOAD "typecontrol"
ELSE
         LOAD Destination$
END IF
END

SUB Convert_old_origins
DIM Answer$[256]
DIM Zone_name$[256]

G1= UserDataGroup(WsWorkZoneOrigin)
IF NOT ValidGeometry(G1) THEN SUBEXIT
Num_origins= GroupCount(G1)

FOR I= 1 TO Num_origins
        E1= GroupElement(G1,I)
        IF CheckUserField(E1,"Class id number:",1) THEN Next_i
        Group1= GrpByProp(E1,WsWorkZoneOrigin)
        IF NOT ValidGeometry(Group1) THEN Next_i
        IF NOT GroupCount(Group1) THEN Next_i
	GOTO End_it
Next_i: NEXT I
Stat=DeleteEntity(G1)
SUBEXIT
End_it:	Answer$="/usr/weber/database/messages/convert_old_zones"
	Stat=MessageFileBox(Answer$,"INFORMATION")
	Stat=DeleteEntity(G1)
	Stat=DeleteEntity(Group1)
SUBEND
