/* ================================================================= */
/*

*
*	Given the id of an entry from a vgroup vg, looks in vg for the next
*	entry after it, and returns its id.
*	The call Vgetnext (vg,-1) returns the id of the FIRST entry in the vgroup.
*
*  Vgetnext will look at only VSET elements in the vgroup.
*  To look at all links in a vgroup, use Vgettagrefs instead.
*
*	RETURNS -1 if error
*	RETURNS the id of the next entry( 0 or +ve integer)  in the vgroup.
*
*	This id is actually the "ref" of the entry's "tag/ref".
*
*/

PUBLIC int Vgetnext (vg,id)           /*@-@*/

VGROUP	*vg;
int 		id;					/* actual id of an entry in the vgroup vg */
{
	int 	i;

	if (id < -1) return(FAIL);
	if (vg == NULL) return(FAIL);
	if (vg->otag != VGDESCTAG) return(FAIL);

	if (vjv) {
		sprintf(sjs,"#Vgetnext:vg->nvelt is %d\n",vg->nvelt);
		zj;
	}
	if (vg->nvelt  == 0) return(FAIL); 			/* nothing in vg */

	if (id == -1) {
		if ((vg->tag[0]==VGDESCTAG) || (vg->tag[0]==VSDESCTAG)) 
				return(vg->ref[0]); 		/* id of first entry */
		}
	
	/* look in vg for id */
	for(i=0;i<vg->nvelt;i++) 
		if ((vg->tag[i]==VGDESCTAG) || (vg->tag[i]==VSDESCTAG)) {
			if(vg->ref[i] == id) {
				if (i == (vg->nvelt - 1) )
					return(FAIL);
				else  {
					if ((vg->tag[i+1]==VGDESCTAG) ||
						 (vg->tag[i+1]==VSDESCTAG)) 
						return(vg->ref[i+1]);		/* return the id of next entry */
					else  return (-1); 
					}
			}
		}

	return (FAIL);

} /* Vgetnext  */

