function [A,numprofiles,numpoints,closed]=addinsideprofile(A,numprofiles,numpoints,closed,prevplot,root);
%ADDINSIDEPROFILE - add apoint inside a profile
%
%   this script is used by expmaster as an elementary operation
%   on an ARGUS profile
%
%   Usage:
%      [A,numprofiles,numpoints,closed]=addinsideprofile(A,numprofiles,numpoints,closed,prevplot,root)
		
	%some checks
	if numprofiles==0
		disp('no profile present')
		return
	end	   
	if numpoints<2
		disp('at least two points are required')
		return
	end	   
	hold on
	loop=1;

	%plot squares
	for i=1:numprofiles
		plot(A(i).x,A(i).y,'-rs','MarkerSize',10);
	end

	while loop

		%first step, select a segment
		title('click on a segment, RETURN to exit','FontSize',14)
		[xi,yi] = ginput(1);

		%first click
		if ~isempty(xi)

			%get the closest segment
			[profsel indsel]=closestsegment(A,numprofiles,xi,yi);

			%check that at least one segment exists
			if indsel==0
				disp('at least two points in one profile are required')
				return
			end

			%highlight selected segment
			plot([A(profsel).x(indsel) A(profsel).x(indsel+1)],[A(profsel).y(indsel) A(profsel).y(indsel+1)],'b-','MarkerSize',10);

			%next click
			title('click on the new point''s location, RETURN to exit','FontSize',14)
			[xi,yi,but] = ginput(1);

			%second click
			if ~isempty(xi)

				%add point to A
				A(profsel).x=[A(profsel).x(1:indsel,1); xi; A(profsel).x(indsel+1:end,1)];
				A(profsel).y=[A(profsel).y(1:indsel,1); yi; A(profsel).y(indsel+1:end,1)];
				numpoints=numpoints+1;

				%plot new profile
				undoplots(prevplot);
				for i=1:numprofiles
					plot(A(i).x,A(i).y,'-rs','MarkerSize',10);
				end

			else
				%RETURN->exit
				return
			end
		else
			%RETURN-> exit
			return
		end
	end
end
