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

DECLARE FUNCTION PrintSetup, PopUp, Get_Status, PrintSetupNew, Environment_Services, SRP_Array, Dialog_Box
DECLARE SUBROUTINE Set_Status, Msg

$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	= 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
	
    If Count(PrintPath, @FM) EQ 1 then
        (Name, Path) using @FM = PrintPath
        Path = Path[1, ' ']
        Name = '\\' : Path : '\' : Name
        PrintPath = 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

If Count(PrintPath, @FM) EQ 1 then
    (Name, Path) using @FM = PrintPath
    Path = Path[1, ' ']
    Name = '\\' : Path : '\' : Name
    PrintPath = Name
end

RETURN PrintPath


