%function surveyStr = setSurveyPoints(surveyStr,whichMap)
function surveyStr = setSurveyPoints(surveyStr,noteStr,whichMap)

try,
global origin
if nargin == 2, whichMap = 'geo'; end

[lat,lon,latnote,lonnote] = getData(surveyStr,noteStr);

if length(lat) ~= length(lon),
	lasterr = '';
	errorbox('setSurveyPoints failed')
	return
end

dlgTitle = '';
if isfield(surveyStr,'name'), dlgTitle = getfield(surveyStr,'name'); end
while 1,
	
	[inputLon, inputLat, inputHandle] = getPoints(lat,lon,latnote,lonnote,dlgTitle);
	
	if isempty(inputLon) | isempty(inputLat),
		surveyStr = [];
		return
	end
	
	button = questdlg('Are these points okay?','Confirm selection.');
	if strcmp(button,'Yes'),
		break,
	elseif strcmp(button,'Cancel'),
		if ~isempty(inputHandle), delete(inputHandle), end
		surveyStr = [];
		return,
	elseif strcmp(button,'No'),
		if ~isempty(inputHandle), delete(inputHandle), end
	end
end
	
if strcmp(whichMap,'utm'),
	inputLon = inputLon + origin(1);
	inputLat = inputLat + origin(2);
	[inputLon,inputLat] = myutmcalc(inputLon,inputLat,'inverse', ...
		selectGeoid,myutmzone);
	
end

if length(lat) ~= length(inputLat) | length(lon) ~= length(inputLon),
	lasterr = '';
	errorbox('setSurveyPoints failed')
	return
end

for ii=1:length(lat),
	if inputLat(ii) ~= -1 & inputLon(ii) ~= -1,
		surveyStr = setfield(surveyStr,lat{ii},inputLat(ii));
		surveyStr = setfield(surveyStr,lon{ii},inputLon(ii));
	end
end

catch, errorbox('setSurveyPoints failed'), end


%%%%%%%%%%%%%%%%%%
function [lat,lon,latnote,lonnote] = getData(surveyStr,noteStr)
lat = {};
lon = {};
latnote = {};
lonnote = {};
fields = fieldnames(surveyStr);
for ii=1:length(fields),
	if findstr(fields{ii},'lat'),
		if length(lat),
			lat{end+1} = fields{ii};
			if isfield(noteStr,fields{ii}),
				latnote{end+1} = getfield(noteStr,fields{ii});
			else
				latnote{end+1} = 'Worksite latitude';
			end
		else
			lat = {fields{ii}};
			if isfield(noteStr,fields{ii}),
				latnote = {getfield(noteStr,fields{ii})};
			else
				latnote = {'Worksite latitude'};
			end
		end
	elseif findstr(fields{ii},'lon'),
		if length(lon),
			lon{end+1} = fields{ii};
			if isfield(noteStr,fields{ii}),
				lonnote{end+1} = getfield(noteStr,fields{ii});
			else
				lonnote{end+1} = 'Worksite longitude';
			end
		else
			lon = {fields{ii}};
			if isfield(noteStr,fields{ii}),
				lonnote = {getfield(noteStr,fields{ii})};
			else
				lonnote = {'Worksite longitude'};
			end
		end
	end
end


%%%%%%%%%%%%%%%%%%
function [inputLon,inputLat,inputHandle] = getPoints(lat,lon,latnote,lonnote,dlgTitle)

grid on
inputLat = [];
inputLon = [];
inputHandle = [];
for ii=1:length(lat),

	button = questdlg(['Please select ' latnote{ii} ' and ' lonnote{ii} ':  ' ...
		lat{ii} '/' lon{ii} '.'], ...
		dlgTitle,'OK','Skip','Cancel','OK');
	if strcmp(button,'Skip'),
		inputLat = [inputLat; -1];
		inputLon = [inputLon; -1];
		continue,
	elseif strcmp(button,'Cancel'),
		inputLat = [];
		inputLon = [];
		return,
	end

	[iLon,iLat] = ginput(1);
	inputLat = [inputLat; iLat];
	inputLon = [inputLon; iLon];
	hold on,
		jj = find(inputLon ~= -1 & inputLat ~= -1);
		h = [plot(inputLon(jj),inputLat(jj),'k-','LineWidth',2) ...
			plot(inputLon(jj),inputLat(jj),'rx','LineWidth',2)];
	hold off
	inputHandle = [inputHandle; h];
	pause(0.1)
end