PROGRAM CONFIG
VAR
	IndexValid : BOOL;
	ConfigIndex : INT;
END_VAR
// This code manages configurations. Configurations can
// be loaded from externally visiable tags into an internal
// configuration array or from the internal array into 
// external tags. 
// External tags are defined in the ExternalWellCompleteConfig tag
// Set the LoadConfigInternalToExternal tag to move a config
// from the internal array RealWellConfigurations to the
// ExternalWellCompleteConfig tag. 
// Set the LoadConfigExternalToInternal tag to move a config
// from the external tag to the internal array
// Use the ExternalConfigNumber tag to select which 
// tag to work with. This tag should be in the range 
// of 1 to 32, assuming an array of 32 well configurations

// Validate the index. We can't do anything unless the index is valid
ConfigIndex := GVL.ExternalConfigNumber - 1;
IF(ConfigIndex < 0 OR GVL.ExternalConfigNumber > 31) THEN
	IndexValid := FALSE;
ELSE
	IndexValid := TRUE;
END_IF

IF(IndexValid = TRUE) THEN
	// The index is valid, we can try to do something
	IF(GVL.LoadConfigInternalToExternal = TRUE) THEN
		// clear the request, it's been processed
		GVL.LoadConfigInternalToExternal := FALSE;
		// move the config from the internal array 
		// to the externally available tag
		GVL.ExternalWellCompleteConfig := GVL.RealWellConfigurations[ConfigIndex];
	ELSIF(GVL.LoadConfigExternalToInternal = TRUE) THEN
		// clear the request, it's been processed
		GVL.LoadConfigExternalToInternal := FALSE;
		// move the config from external tag to the 
		// internal array. The configuration MUST 
		// be validated externally, or validation can 
		// be added here.
		GVL.RealWellConfigurations[ConfigIndex] := GVL.ExternalWellCompleteConfig;
		// set the status. Automatically enable
		GVL.RealWellTestConfigStatus[ConfigIndex].Enabled := TRUE;
		GVL.RealWellTestConfigStatus[ConfigIndex].Loaded := TRUE;
	END_IF
END_IF