COMPILE FUNCTION Test_Printer_Select(PrinterID,RetDefault)
#pragma precomp SRP_PreCompiler

DECLARE FUNCTION Test_PrintSetup, PopUp, Get_Status, PrintSetupNew, Environment_Services, SRP_Array, Utility
DECLARE FUNCTION SRP_Run_Command
DECLARE SUBROUTINE Set_Status, Msg

$INSERT APP_INSERTS
$INSERT POPUP_EQUATES
$INSERT LOGICAL
$INSERT PRINTSETUP_EQUATES

/*
	Routine to Select Printer and Port for use in OIPI Set_Printer("INIT",...) calls. 
	
	2/4/2000 by jch  * * * *
*/

IF NOT(ASSIGNED(PrinterID))		THEN PrinterID = ''
IF NOT(ASSIGNED(RetDefault))	THEN RetDefault = ''

Printers	= ''
Default		= ''
PrintCount	= Test_PrintSetup(PRN_GET$,'',Printers,Default)	;* OI routine returns printer names, driver, port information
 
DefPrinter	= Default[1,',']
DefPort		= FIELD(Default,',',3)
DefLine		= DefPrinter:' on ':DefPort

IF RetDefault THEN
	SWAP ' on ' WITH @FM IN DefLine
	RETURN DefLine
END

PopLines = ''											;* Pretty up printer names and ports for a popup
SkipPopup = 0

CONVERT @LOWER_CASE TO @UPPER_CASE IN PrinterID

FOR I = 1 TO PrintCount
	Printer = Printers<I>[1,',']
	Port	= FIELD(Printers<I>,',',3)

	IF Printer NE '' THEN
		TestPrinter = Printer
		CONVERT @LOWER_CASE TO @UPPER_CASE IN TestPrinter
		If (IndexC(TestPrinter, PrinterID, 1) GT 0) then SkipPopup = I
*		IF TestPrinter = PrinterID THEN SkipPopup = I
	END
	If Port NE '' then
		PopLines<1,I> = Printer:' on ':Port
	end else
		PopLines<1,I> = Printer
	end
NEXT I

IF SkipPopup THEN
	PrintPath = PopLines<1,SkipPopup>
	SWAP ' on ' WITH @FM IN PrintPath
	
    // This creates a well formated printer name using any UNC paths that are returned in the above popup.
    If Count(PrintPath, @FM) EQ 2 then
        (Name, Path, Port) using @FM = PrintPath
        Path = Path[1, ' ']
        Name = '\\' : Path : '\' : Name
        PrintPath<1> = Name
    end
	
	RETURN PrintPath
END

LOCATE DefLine IN PopLines USING @VM SETTING DefPos ELSE DefPos = ''

PopLines = SRP_Array('SortRows', PopLines, 'AL1', 'LIST', @VM, '', False$)

TypeOver = ''
TypeOver<PDISPLAY$>	= PopLines
TypeOver<PFORMAT$>	= '1':@SVM:'60':@SVM:'L':@SVM:'L':@SVM:'':@SVM:'Printer'
TypeOver<PMODE$>	= 'L'
TypeOver<PTYPE$>	= 'E'
TypeOver<PTITLE$>	= 'Select Report Destination...'
IF DefPos NE '' THEN
	TypeOver<PINITSELECT$> = DefPos
END

Set_Status(0)
PrintPath = PopUp(@WINDOW,TypeOver)	;* Pick from popup
IF PrintPath = '' THEN RETURN ''
SWAP ' on ' WITH @FM IN PrintPath	;* Put in format for passing Set_Printer('INIT'...)

// This creates a well formated printer name using any UNC paths that are returned in the above popup.
If Count(PrintPath, @FM) EQ 2 then
	(Name, Path, Port) using @FM = PrintPath
	Path = Path[1, ' ']
	Name = '\\' : Path : '\' : Name
	PrintPath<1> = Name
end

RETURN PrintPath

