%function nLegs = checkNLegs(nLegs,rng1,rng2,rng3,depth,nGps, ...
%	surfacetime, rpm)
%  nLegs			number of survey legs
%  rng1				track lead-in (meters)
%  rng2				track length (meters)
%  rng3				average track spacing (meters)
%  depth 			depth of survey (meters)
%  nGps				number of gps fixes per leg (#) 
%  surfacetime		time spent on the surface for fix (seconds)
%  rpm 				vehicle rpm (revolutions per minute)
function out = checkNLegs(nLegs,rng1,rng2,rng3,depth,nGps, ...
	surfacetime, rpm)

out = [];
if rpm > 350 | rpm < 250,
	errorbox('Invalid RPM for speed calculation')
	return
end

speed = 1.5; % meters/second
ascenttime = sqrt(depth^2+rng1^2)/speed;
transittime = rng3/speed;
fixtime = 2*ascenttime+surfacetime+transittime;
legtime = rng2/speed;
if nGps > 0,
	legtime = legtime + fixtime*nGps;
elseif nGps < 0,
	legtime = legtime + fixtime/abs(nGps);
end
surveytime = approximate(legtime*nLegs);
defaultnlegs = ceil(10*3600/legtime);

str = [];
str = strvcat(str, 'Assuming vehicle speed = 1.5 meters/second,');
str = strvcat(str, ['  Each leg will take about ' sec2str(approximate(legtime)) ...
	' (HH:MM:SS).']);
str = strvcat(str, ['  The survey will last about '	sec2str(surveytime) ...
	' (HH:MM:SS).']);
str = strvcat(str, ['  A 10 hour survey would cover ' ...
	int2str(defaultnlegs) ' legs.']);
str = strvcat(str,' ');
str = strvcat(str,['Calculate survey with ' int2str(nLegs) ' legs?']);
str = strvcat(str,' ');
button=questdlg(str,'Confirm the number of survey legs:','Yes');

if strcmp(button,'Cancel'),
	return
elseif strcmp(button,'No'),
	prompt{1} = 'Enter desired survey time (hours)';
	prompt{2} = 'OR enter desired number of legs';
	title = 'Enter EITHER survey time OR number of legs';
	answer = inputdlg(prompt,title,1);
	if isempty(answer), return, end
	if ~isempty(answer{1}),
		inputtime = 3600*str2num(answer{1});
		out = ceil(inputtime/legtime);
		out = checkNLegs(out,rng1,rng2,rng3,depth,nGps, ...
			surfacetime,rpm);
	elseif ~isempty(answer{2}),
		out = str2num(answer{2});	
		out = checkNLegs(out,rng1,rng2,rng3,depth,nGps, ...
			surfacetime,rpm);		
	else
		return
	end
else
	out = nLegs;
end