// Install and Remove scripts for MCFE Tracker Manager
	
InstallScript := func(partFrame)
begin
	// create a new locale without commas in numbers
	// locale is always based on user's current preferences
	//
	local mcfeLoc;
	local loc := GetLocale();
	mcfeLoc := { _proto:		loc,
				title:			kMCFElocName,
				localeSym:		kMCFElocSymbol,
				numberformat:	{
					decimalpoint:	".",
					groupSepStr:	"",
					groupWidth:		3,
					minusPrefix:	"-",
					minusSuffix:	"",
					currencyPrefix:	loc.numberformat.currencyPrefix,
					currencySuffix:	loc.numberformat.currencySuffix,
					decimalLeadingZ: kLeadZero
				},
			};
	call kAddLocaleFunc with (mcfeLoc);

	// register the auxiliary Tracker Button for Notes
	//
	RegAuxButton(kAuxTrackerSymbol,{
		destApp: 'paperroll,
		_proto: protoPopupButton,
		text: "Tracker",
		popup: [],
		viewBounds: RelBounds(0,0,40,10),
		trkman:	nil, // Tracker Manager
		trk:	nil, // the Tracker
		cInfo:	nil, // Caret Info
		nTrys:	0,	 // used in getData
		maxTrys: 6,
		nt: "Tracker Button",
		
		// build popup menu using Tracker details from TrackerManager
		//
		ButtonClickScript: func()
		begin
			popup := [];
			trkman := GetRoot().('|TrackMan:UKC_MCFE|);
			if trkman = nil then
			begin
				:Notify(kNotifyAlert, nt, 
					"TrackerManager missing! Make sure it is installed.");
			end
			else begin
				AddArraySlot ( popup, {item: "Trackers", pickable: true, index: 0,} );
				AddArraySlot ( popup, 'pickseparator );

				if trkman:currentPseudoSym() then
					AddArraySlot ( popup, {item: "Context", pickable: true, index: 1,} );

				if trkman:currentGPSSym() then
				begin
					AddArraySlot ( popup, {item: "Position", pickable: true, index: 2,} );
					local t := GetRoot().(trkman:currentGPSSym());
					if t then
					begin
						local inf := t:getTrackerType();
						if inf then
							if (BAnd(inf.info, kSupportsPseudoRange) <> 0) or
							 (BAnd(inf.info, kSupportsCarrierPhase) <> 0) then
				 				AddArraySlot ( popup, {item: "GPS data", pickable: true, index: 3,} );
				 	end;
				end;

				if trkman:currentAttitudeSym() then
					AddArraySlot ( popup, {item: "Attitude", pickable: true, index: 4,} );
				
				if trkman:currentEnvSym() then
					AddArraySlot ( popup, {item: "Environment", pickable: true, index: 5,} );
					
				if Length ( popup ) = 0 then
				begin
					:Notify(kNotifyAlert, nt, 
						"No trackers found. Use TrackerManager to select default trackers.");
				end
			end;
			inherited:?ButtonClickScript();
		end,

		// get data from the Tracker
		//
		getData: func(all)
		begin
			local txt := trk:getFormattedData(all);
			if txt then
			begin
				SetCaretInfo(cInfo.view,cInfo.info);
				InsertItemsAtCaret({insertItems: {text: txt, styles: 9218,}});
				GetRoot():SysBeep();
				GetRoot():SysBeep();
			end
			else begin
				if nTrys < maxTrys then
				begin
					nTrys := nTrys + 1;
					AddDelayedCall(func(n) self:getData(n), [all], 5000);
				end else
					:Notify(kNotifyAlert, nt, "Data not available.");
			end;
		end,
		
		PickActionScript: func(item)
		begin
			cInfo := GetCaretInfo();
			if cInfo then
			begin
				trk := nil;
				nTrys := 0;
				local index := popup[item].index;
				local alldata := nil;
				if index = 0 then
					trkman:Open();
				else if index = 1 then
					trk := trkman:TrackerOpenConnect ( kPseudoTracker );
				else if index = 2 then
					trk := trkman:TrackerOpenConnect ( kLocationTracker );
				else if index = 3 then
				begin
					trk := trkman:TrackerOpenConnect ( kLocationTracker );
					alldata := true;
				end else if index = 4 then
					trk := trkman:TrackerOpenConnect ( kAttitudeTracker );
				else if index = 5 then
					trk := trkman:TrackerOpenConnect ( kEnvTracker );
				if trk then
					:getData(alldata);
			end else
				:Notify(kNotifyAlert, nt,
						"Click in note to place caret at required insertion point, then press Tracker button again.");

			inherited:?PickActionScript(item);
		end,
	});
end;

RemoveScript := func(partFrame)
begin
	// remove the auxiliary button from Notes
	UnRegAuxButton(kAuxTrackerSymbol);

	// remove the 'no commas in numbers' locale
	call kRemoveLocaleFunc with (kMCFElocSymbol);
end;
