          SUBROUTINE (EM.DATA,PASSER,E.BODY,E.ATTACH,ERR.MSG)
** Version# 106.0002[16] - 02/12/2014 - 01:47pm - TSMITH - eclipse
*** V106.0002 Change - Custom Coding . - 02/12/2014 - TSMITH - eclipse
*** V106.0001 Change - Custom Coding . - 10/29/2013 - TSMITH - eclipse

*** Subroutine - EMAIL.DATA.GET
*-------------------------------------------------------------------------*
*** Gathers email data and calls the User display screen EMAIL.SEND,
*** which is used to allow the user to make changes before sending.
*-------------------------------------------------------------------------*
*** EM.DATA<1> - CN Bill To  INIT.BT
*** EM.DATA<2> - CN Ship To  INIT.ST
*** EM.DATA<3> - BRanch  INIT.BR
*** EM.DATA<4> - OID, Order ID
*** EM.DATA<5> - GEN value position of currrent generation of order
*** EM.DATA<6> - Order Status for process
*** EM.DATA<7> - Info only call ... DO NOT SEND EMAIL
*** EM.DATA<8> - Print style override
*** EM.DATA<9> - Coming from activity trigger maintenance
*** EM.DATA<10>- Statement info (SEL.OPTS:VM:ASOF:VM:AGDT:VM:SBRS)
*** EM.DATA<11>- A list of OID's used for logging only
***            - If the first value is 'USER' then the second value is
***            - a user for logging instead of logging to an OID
*** EM.DATA<12>- A corresponding list of GEN's used for logging only
*** PASSER<1>  - Email Address
*** PASSER<2>  - Customer Number (e.g. Entity ID)
*** PASSER<3>  - Contact ID
*** PASSER<4>  - Receipiant Name
*** PASSER<5>  - Subject
*** PASSER<6>  - Attachment path
*** PASSER<7>  - CC, Carbon Copies
*** PASSER<8>  - BCC, Blind Carbon Copies
*** PASSER<9>  - Flag to Send Attachment in Body
*** PASSER<10> - Flag that EMAIL.DATA.GET will send the email
***            - this is needed when sending attachments.
*** PASSER<11> - Flag to not call user for prompt
*** PASSER<12> - From Name (domain name will be system defined)
***            - If not passed will default to @LOGNAME
*** PASSER<13> - EMAIL.SEND cannot override attachment flag (PASSER<9>)
*** PASSER<14> - this will be the
***            - message keyed in by user if need to pass to other pgms.
*** PASSER<15> - User BCC address for copies of EMAIL (from SEC-57)
*** PASSER<16> - User BCC Subject Prefix (from SEC-58)
*** PASSER<17> - ATTACHMENT path/key reference e.g. HOLD.FILE:nnnnn
***            - for display in EMAIL.SEND options
*** PASSER<18> - ATTACHMENT type - .txt (default), .html, .doc, .xls
*** PASSER<19> - ATTACHMENT body type if sending attachment in Body
*** PASSER<20> - Sending email as a company HTML template
*** PASSER<21> - ATTACHMENT filename (optional, will default to
*** PASSER<22> - File name of template to be used for sent email
***            - USER.ID-###.TXT  where ### is a number starting at 100
*** PASSER<28> - Specified Hold File (&HOLD&) ID for the report that is
***            - going to be emailed via Eclipse Forms.
*** PASSER<29> - Flag indicating this is an Extract.
***            - 1 = Print Price Sheet Extract   (EF and non-EF)
***            - 2 = All other reports and/or holdfile extract (EF Only)
*** E.BODY     - Body of Email, optional
*** E.ATTACH   - Attachment File of Email, optional
*** ERR.MSG    - Errors to pass back to calling pgm
*-------------------------------------------------------------------------*
*** COMMON Variables:
***       PHANTOM.PROC and JAVA.PROC$ are read but not changed
*-------------------------------------------------------------------------*

          ERR.MSG = ''

          * If phantom or java, do not display error message
          IF PHANTOM.PROC OR JAVA.PROC$ THEN
             DISP.ERR = NO
          END ELSE
             DISP.ERR = YES
          END

          * check for add on auth
          UT.SEC3 22,AUTH.OK,,DISP.ERR
          IF NOT(AUTH.OK) THEN
             ERR.MSG = 'Outbound Email is not authorized!'
             RETURN
          END

          UT.OPEN.FILE 'VOC',VOCFILE,ERR.MSG,YES
          IF ERR.MSG THEN RETURN

          UT.OPEN.FILE 'MSG-OUT',MOUTFILE,ERR.MSG,YES
          IF ERR.MSG THEN RETURN

          CRLF = CHAR(13):CHAR(10) ;* used to convert FM before email send

          OID             = EM.DATA<4>
          OID.LIST        = EM.DATA<11>
          GEN             = EM.DATA<5>
          GEN.LIST        = EM.DATA<12>
          IF OID.LIST<1,1>='USER' THEN
             LOG.USER = OID.LIST<1,2>
             OID.LIST = ''
          END ELSE
             LOG.USER = ''
          END
          IF NOT(OID.LIST) THEN
             OID.LIST = OID
             GEN.LIST = GEN
          END
          STATUS          = EM.DATA<6>
          INFO.ONLY.CALL  = EM.DATA<7>
          STMT.INFO       = EM.DATA<10>
          USER.NOT.PROMPT = PASSER<11>
          MBX.BODY        = E.BODY
          IF MBX.BODY = '' AND PASSER<14> THEN
             MBX.BODY = RAISE(PASSER<14>)
             IF JAVA.PROC$ AND PASSER<19> = 'HTML' THEN
                BODY.LINE.CT = DCOUNT(MBX.BODY,VM)
                FOR JJ = 1 TO BODY.LINE.CT
                   * convert the typed body into html.
                   IF TRIM(MBX.BODY<1,JJ>) THEN
                      MBX.BODY<1,JJ> = CHANGE(MBX.BODY<1,JJ>,' ','&nbsp;')
                   END ELSE
                      MBX.BODY<1,JJ> = ''
                   END
                NEXT JJ
                MBX.BODY = CHANGE(MBX.BODY,VM,'<br>':CRLF)
             END
          END

          ATTACH.PATH = ''
          EXT.HOLD.ID = PASSER<28>

          * Check if need to move the HOLD attachment to msg-out for solar
          IF JAVA.PROC$ THEN
             GOSUB CHK.SOLAR.ATTACH
             IF ERR.MSG THEN RETURN
          END

          SUBJECT       = PASSER<5>
          FROM.ADDR     = PASSER<12>

          * if a from address wasn't passed in, try to set one
          * (only do so if coming from Activity Trigger Maint)
          IF NOT(FROM.ADDR) AND EM.DATA<9> THEN
             EMAIL.GET.FROM.ADDRESS FROM.ADDR
             PASSER<12> = FROM.ADDR
          END

          * Control file to store valid email suffix attachment types
          READ VAL.TYPS FROM CTRLFILE,'EMAIL.SUFFIX.TYPES' ELSE VAL.TYPS=''

          GOSUB CHECK.TIFF
          * Prepare data for user prompts
          IF NOT(USER.NOT.PROMPT) THEN
             PASSER<6> = ''    ;* EMAIL Attachment Path

             * Screen to prompt user for input
             MATBUILD SV.CUS FROM CUS ;* Save reigning Customer record.

             EMAIL.SEND PASSER,MBX.BODY

             IF F12 THEN RETURN

             IF MBX.BODY THEN
                PASSER<14> = LOWER(MBX.BODY)
                E.BODY     = MBX.BODY
             END

             * reset sending data after return from prompts in Emailsend
             * for send in this program.
             SUBJECT       = PASSER<5>
             FROM.ADDR     = PASSER<12>

             MATPARSE CUS FROM SV.CUS
          END

          IF USE.ECLIPSE.FORMS$ THEN
             IF INFO.ONLY.CALL THEN RETURN
             CN = EM.DATA<2>
             IF NOT(CN) THEN CN = EM.DATA<1>

             IF NOT(PASSER<17>) THEN
                PASSER<17> = PASSER<21>:PASSER<18>
             END

             FEXT = OCONV(PASSER<18>,'MCU')

             * If PASSER<29> is set to '2', then we are coming from the
             * holdfile.  Prepare extract data for emailing.
             IF PASSER<29> = '2' AND FEXT # '.PDF' THEN
                * Add ',' or '~~' to delimit columns for .CSV & .XML
                IF FEXT = '.CSV' OR FEXT = '.XML' THEN
                   GOSUB ADD.DELIMS

                   * Convert delimited file into XML format
                   IF FEXT = '.XML' THEN
                      FORMS.CONVERT.TO.XML E.ATTACH,PASSER<21>,XML,ERR.MSG

                      E.ATTACH = XML
                   END
                END
             END

             IF ((STMT.INFO AND CN) OR OID OR EXT.HOLD.ID) THEN
                GOSUB EMAIL.FORM
                RETURN
             END
          END

          IF NOT(INFO.ONLY.CALL) OR JAVA.PROC$ THEN
             * If emailing an order as a tiff, replace the attachment
             * with a tiff image of the order
             IF EMAIL.TIFF THEN
                GOSUB CREATE.TIFF
             END
             CHK.ADDR = PASSER<1>
             EMAIL.SET.ADDR CHK.ADDR,EM.DATA<4>,EM.DATA<2>,EM.DATA<1>,EM.DATA<5>
             PASSER<1> = CHK.ADDR
             IF PASSER<1> THEN
                BODY.TYPE = ''
                IF PASSER<9> THEN
                   E.ATTACH = DELETE(E.ATTACH,1)
                   MBX.BODY<-1> = E.ATTACH
                   BODY.TYPE = PASSER<19>   ;* Either HTML or TXT
                   E.ATTACH = ''
                END ELSE
                   BODY.TYPE = PASSER<19>
                END

                * Remove VM and FM and change to CRLF for send
                MBX.BODY = CHANGE(MBX.BODY,SSSVM,@FM)
                MBX.BODY = CHANGE(MBX.BODY,SSVM,@FM)
                MBX.BODY = CHANGE(MBX.BODY,SVM,@FM)
                MBX.BODY = CHANGE(MBX.BODY,@VM,@FM)
                MBX.BODY = CHANGE(MBX.BODY,@FM,CRLF)
                MBX.BODY = CHANGE(MBX.BODY,CHAR(255),'')

                IF E.ATTACH AND NOT(EMAIL.TIFF) THEN
                   * If not a .txt document then delete the report title
                   * from the attachment so it will display properly
                   IF PASSER<18> AND OCONV(PASSER<18>,'MCU') # '.TXT' THEN
                      LOCATE PASSER<18> IN VAL.TYPS<1> SETTING NADA THEN
                         E.ATTACH = DELETE(E.ATTACH,1)
                      END
                   END

                   E.ATTACH = CHANGE(E.ATTACH,SSSVM,@FM)
                   E.ATTACH = CHANGE(E.ATTACH,SSVM,@FM)
                   E.ATTACH = CHANGE(E.ATTACH,SVM,@FM)
                   E.ATTACH = CHANGE(E.ATTACH,@VM,@FM)
                   E.ATTACH = CHANGE(E.ATTACH,@FM,CRLF)

                   READV ATTACH.PATH FROM VOCFILE,'MSG-OUT',2 ELSE
                      ATTACH.PATH = '/u2/msg-out'
                   END
                   ASEQ = 100

TEST.ATT:          IF PASSER<21> THEN
                      ATT.ID  = PASSER<21>
                      CONVERT ' ' TO '' IN ATT.ID
                      CONVERT '/' TO '' IN ATT.ID
                      CONVERT '\' TO '' IN ATT.ID
                      IF ASEQ # 100 THEN ATT.ID := '-':ASEQ
                   END ELSE
                      ATT.ID = USER.ID:'-':ASEQ
                   END
                      TO.LIST = PASSER<1>
                   * We are only expecting a suffix on the path if
                   * PASSER<18> is set.  If not, we default to .txt
                   IF PASSER<18> THEN
                      ATT.ID := PASSER<18>
                   END ELSE
                      IF PASSER<19> = 'HTML' THEN
                         ATT.ID := '.html'
                      END ELSE
                      IF FIELD(TO.LIST,"@",2)='gpsupplyco.com' OR FIELD(TO.LIST,"@",1)='awallace' OR TO.LIST = 'randy.allison@nhyates.com' THEN
                         ATT.ID := '.csv'
                      END ELSE
                         ATT.ID := '.txt'
                   END
                 END
                END
                   OPENSEQ 'MSG-OUT',ATT.ID TO MOUTSEQFILE THEN
                      ASEQ += 1
                      CLOSESEQ MOUTSEQFILE
                      GOTO TEST.ATT
                   END ELSE
                      IF OS.TYPE$ = 'NT' THEN       ;* Windows NT Version
                         ATTACH.PATH := '\':ATT.ID
                      END ELSE
                         ATTACH.PATH := '/':ATT.ID
                      END
                      E.ATTACH.SV = E.ATTACH
                      UT.REP.STR E.ATTACH,CRLF,AM
                      LN.CT = DCOUNT(E.ATTACH,AM)
                      FOR JJ = 1 TO LN.CT
                         WRITESEQ E.ATTACH<JJ> ON MOUTSEQFILE ELSE NULL
                      NEXT JJ
                      E.ATTACH = E.ATTACH.SV
                      CLOSESEQ MOUTSEQFILE
                   END
                END

                * chk if 'Email Letterhead' option has been selected
                IF PASSER<20> THEN
                   GOSUB LOAD.TEMPLATE
                END

                * send the email to all To, CC and BCC addresses
                TO.LIST       = PASSER<1>

*-------------------------------------------------------------------------*
*Begin code to BCC all outgoing Bids to the associated salesperson
                SLS.EMAIL = TRANS('INITIALS',LED(72)<1,GEN>,55,'X')
                IF LED(6)<1,GEN>= 'B' THEN
                BCC.LIST = SLS.EMAIL
                END ELSE
                BCC.LIST = PASSER<8>
                END
*-------------------------------------------------------------------------*

                CC.LIST       = PASSER<7>
                THIS.SUBJECT  = SUBJECT
                THIS.MBX.BODY = MBX.BODY
                THIS.TO.LIST  = TO.LIST
                GOSUB SEND.IT

                * setup a display list of all emails
                DISP.ALL.LIST = TO.LIST:VM:CC.LIST:VM:BCC.LIST

                DISP.ALL.LIST = TRIM(DISP.ALL.LIST,VM)
                CONVERT VM TO ', ' IN DISP.ALL.LIST

                * Send the email to the 'Sender BCC' - this is the
                * email address specified on the Email Options screen.
                * Note! This is different than the 'BCC' user input in
                * that this is meant to be an audit user email.
                * Because it contains additional info, it must be sent
                * separately.
                IF PASSER<15> THEN
                   THIS.TO.LIST  = PASSER<15>
                   CC.LIST       = ''
                   BCC.LIST      = ''
                   THIS.SUBJECT  = SUBJECT
                   THIS.MBX.BODY = MBX.BODY
                   THIS.MBX.BODY<-1> = ' '
                   THIS.MBX.BODY<-1> = 'Message Sent To: ': DISP.ALL.LIST

                   * append BCC Sender prefix, if it exists
                   IF PASSER<16> THEN
                      THIS.SUBJECT = PASSER<16>:' ':SUBJECT
                   END
                   GOSUB SEND.IT
                END

                * log the sent email
                CN = EM.DATA<2>                  ;* Ship-To Entity
                IF CN = '' THEN CN = EM.DATA<1>  ;* Bill-To Entity
                IF CN THEN
                   TYPE = 'E'
                   LOG.ID = CN
                   GOSUB LOG.IT
                END ELSE IF LOG.USER THEN
                   TYPE = 'U'
                   LOG.ID = LOG.USER
                   GOSUB LOG.IT
                END
             END

             IF EMAIL.TIFF THEN
                * Remove temporary files containing pcl and tiff data
                CMD = 'rm -f "':DOC.PATH:'" "':TIFF.PATH:'"'
                IF OS.TYPE$ = "NT" THEN
                   GOSUB FORMAT.NT.CMD
                END
                MSG = EXECUTE.OS.COMMAND(CMD)

                * Delete the batch created for NT sites
                IF OS.TYPE$ = "NT" THEN
                   MSG = EXECUTE.OS.COMMAND('rm ':MOUT.DIR:TEMP.FILE)
                END
             END
             IF E.ATTACH THEN
                * Note - this gets created by MOUTSEQFILE
                DELETE MOUTFILE,ATT.ID
             END
          END

FINISH:   *** exit this routine
          RETURN
*-------------------------------------------------------------------------*
LOAD.TEMPLATE: *** load the email template

          FORM.NAME = 'Default Email Template'
          WOE.GET.FORM.RESP 'E',FORM.NAME,TEMP.BODY

          ID = 'HTML~':PASSER<22>
          EMAIL.TEMPLATE.GET.RESP COMPANY.NAME,HOME.URL,LOGO,LINKS,ID
          UT.REP.STR TEMP.BODY,'&COMPANY.NAME&',COMPANY.NAME
          UT.REP.STR TEMP.BODY,'&HOME.URL&',HOME.URL
          UT.REP.STR TEMP.BODY,'&LOGO&',LOGO
          UT.REP.STR TEMP.BODY,'&LINKS&',LINKS

          * insert the current MBX.BODY into the template
          MBX.BODY = CHANGE(MBX.BODY,CHAR(13),'<br>':CRLF)
          MBX.BODY = CHANGE(MBX.BODY,' ','&nbsp;')
          UT.REP.STR TEMP.BODY,'&BODY&',MBX.BODY

          * reset MBX.BODY
          MBX.BODY  = TEMP.BODY
          BODY.TYPE = 'HTML'

          RETURN
*-------------------------------------------------------------------------*
SEND.IT:  *** send the email

          * If attaching as a tiff, remove any HTML breaks from
          * the body to ensure that it's plain text
          IF EMAIL.TIFF THEN
             * crlf may have been already converted to <br>crlf
             * if so, convert it back the get any <br> not followed by
             *crlf
             THIS.MBX.BODY = CHANGE(THIS.MBX.BODY,'<br>':CRLF,CRLF)
             THIS.MBX.BODY = CHANGE(THIS.MBX.BODY,'<br>',CRLF)
             THIS.MBX.BODY = CHANGE(THIS.MBX.BODY,'&nbsp;',' ')
          END

          NETMAIL.OUT THIS.TO.LIST,THIS.SUBJECT,THIS.MBX.BODY,FROM.ADDR,BCC.LIST,CC.LIST,ATTACH.PATH,,BODY.TYPE

          RETURN
*-------------------------------------------------------------------------*
LOG.IT:   *** log the email send

          * if an order was attached, log the email send to the change log
          IF OID.LIST THEN
             OID.COUNT = DCOUNT(OID.LIST,VM)
             FOR OID.INDEX = 1 TO OID.COUNT
                THIS.OID = OID.LIST<1,OID.INDEX>
                THIS.GEN = GEN.LIST<1,OID.INDEX>
                IF THIS.GEN THEN GEN.MV = THIS.GEN ELSE GEN.MV = 1
                OE.CMT = 'E-mailed To: ':DISP.ALL.LIST
                OE.ADD.COMMENT THIS.OID,GEN.MV,OE.CMT
             NEXT OID.INDEX
          END

          IF ATTACH.PATH THEN ATTCH.PTH = SUBJECT ELSE ATTCH.PTH = ''

          UPDATE.EMAIL.LOG TYPE,LOG.ID,TO.LIST,FROM.ADDR,CC.LIST,BCC.LIST,ATTACH.PATH,MBX.BODY

          * update the tracking log
          DISP.TO.LIST  = TO.LIST
          DISP.CC.LIST  = CC.LIST
          DISP.BCC.LIST = BCC.LIST

          CONVERT VM TO ', ' IN DISP.TO.LIST
          CONVERT VM TO ', ' IN DISP.CC.LIST
          CONVERT VM TO ', ' IN DISP.BCC.LIST

          IF TYPE='U' THEN
             CMT       = 'User ID   : ':LOG.ID
          END ELSE
             CMT       = 'Entity ID : ':LOG.ID
          END
          CMT<1,-1> = 'To        : ':DISP.TO.LIST
          CMT<1,-1> = 'CC        : ':DISP.CC.LIST
          CMT<1,-1> = 'BCC       : ':DISP.BCC.LIST
          CMT<1,-1> = 'Sender BCC: ':PASSER<15>
          CMT<1,-1> = 'From      : ':FROM.ADDR
          CMT<1,-1> = 'Subject   : ':SUBJECT
          IF ATTCH.PTH THEN
             CMT<1,-1> = 'Attachment: ':ATTCH.PTH
          END
          ELOG     = ''
          ELOG<67> = 'SYSTEM'
          JOB.TRACKING.CMT LOG.ID,CMT,,ELOG

          RETURN
*-------------------------------------------------------------------------*
CHECK.TIFF: *** Determine if the email should be sent as a tiff attachment
          EMAIL.CHECK.TIFF 'INV',OID,GEN,,ORD.TIFF
          EMAIL.CHECK.TIFF 'STMT',,,EM.DATA<2>,STMT.TIFF

          * Add the email bid message as the body of the e-mail if this
          * is a bid and is going to be sent as a tiff or pdf. We have to
          * include it in the body of the order since the tiff/pdf uses the
          * printed form. Only from Eterm.  Solar checks this control
          * other ways.
          IF STATUS = 'B' AND ORD.TIFF AND NOT(JAVA.PROC$) THEN
             READ EMAIL.BID.MSG FROM CTRLFILE,'EMAIL.BID.MSG' ELSE
                EMAIL.BID.MSG = ''
             END
             MBX.BODY := VM:VM:EMAIL.BID.MSG
          END

          * Only use tiff mode if the email contains an order/statement
          EMAIL.TIFF = NO
          IF (OID # '' AND ORD.TIFF) OR (STMT.INFO<1,2> # '' AND STMT.TIFF) THEN
             PASSER<17> = SUBJECT
             PASSER<18> = '.tiff'
             PASSER<19> = 'TIFF'
             PASSER<9>  = ''
             TIFF.EXT   = '.tif'
             IF ORD.TIFF<2> # '' THEN
                TIFF.EXT = ORD.TIFF<2>
             END
             IF OID THEN
                EMAIL.TIFF = ORD.TIFF<1>
             END ELSE
                EMAIL.TIFF = STMT.TIFF<1>
             END
          END

          * If we are not emailing a tiff for an order/statment, check
          * to see if we need to create it for a report extract.
          IF NOT(EMAIL.TIFF) THEN
             IF PASSER<29> AND OCONV(PASSER<18>,'MCU') = '.PDF' THEN
                EMAIL.TIFF = 3       ;* Create a PDF
                TIFF.EXT   = '.tif'
             END
          END

          RETURN
*-------------------------------------------------------------------------*
CREATE.TIFF: *** Create a tiff image for an order

          * Do not attempt to re-generate the document if we are doing
          * a report extract.
          IF NOT(PASSER<29>) THEN
             GOSUB GEN.HOLD.DOC

             * Return now if nothing was produced
             IF ERR.MSG THEN RETURN TO FINISH
          END ELSE
             DOC.ID = EXT.HOLD.ID
          END

          * Build complete hold file and msg-out paths
          HOLD.DIR = ''
          MOUT.DIR = ''
          OPEN '&HOLD&' TO TEMPFILE THEN
             STATUS FILE.STAT FROM TEMPFILE THEN
                HOLD.DIR = FILE.STAT<20>
             END
             CLOSE TEMPFILE
          END
          OPEN 'MSG-OUT' TO TEMPFILE THEN
             STATUS FILE.STAT FROM TEMPFILE THEN
                MOUT.DIR = FILE.STAT<20>
             END
             CLOSE TEMPFILE
          END
          UNIX.PID.GET PID
          IF OS.TYPE$ = "NT" THEN
             HOLD.DIR := '\'
             MOUT.DIR := '\'
          END ELSE
             HOLD.DIR := '/'
             MOUT.DIR := '/'
          END
          DOC.PATH = HOLD.DIR:DOC.ID

          BEGIN CASE
          CASE EXT.HOLD.ID
             PDF.DOC.ID  = PASSER<17>
             TIFF.DOC.ID = PASSER<21>:TIFF.EXT
          CASE OID
             PDF.DOC.ID  = OID:'_':PID:'.pdf'
             TIFF.DOC.ID = OID:'_':PID:TIFF.EXT
          CASE OTHERWISE
             PDF.DOC.ID  = 'Statement_':PID:'.pdf'
             TIFF.DOC.ID = 'Statement_':PID:TIFF.EXT
          END CASE
          TIFF.PATH = MOUT.DIR:TIFF.DOC.ID
          PDF.PATH = MOUT.DIR:PDF.DOC.ID

          * Generate the tiff using vsifax with the file output option
          TIFF.ERROR = NO
          FOUND.IMAGE = NO
          IF EMAIL.TIFF = 2 OR EMAIL.TIFF = 4 THEN
             *** Using fax form, custom code may use Vsifax @+IMAGE
             GOSUB CHECK.FOR.IMAGE
          END
          IF FOUND.IMAGE THEN
             *** pcltofif does not handle @+IMAGE
             CMD = 'vfx -o "':TIFF.PATH:'" -F "pcl" "':DOC.PATH:'"'
             TIFF.CMD = 'vfx'
          END ELSE
             CMD = '$VSIFAX/lbin/pcltotif -h off -o "':TIFF.PATH:'" "':DOC.PATH:'"'
             TIFF.CMD = 'pcltotif'
          END
          IF OS.TYPE$ = "NT" THEN
             GOSUB FORMAT.NT.CMD
          END

          MSG.NUM = 1; *** In case we need to log
          GOSUB DO.VSIFAX.CMD
          IF NOT(MSG) THEN
             WAIT.COUNT = 0
             LOOP
                OPENSEQ TIFF.PATH TO WAIT.FD THEN
                   CLOSESEQ WAIT.FD
                   EXIT
                END ELSE
                   IF WAIT.COUNT > 20 THEN
                      MSG = TIFF.CMD:' failed to create file'
                      EXIT
                   END
                   WAIT.COUNT += 1
                   SLEEP 5
                END
             REPEAT
          END
          IF MSG THEN
             * Log the error
             LOG.MSG = MSG
             GOSUB LOG.ERROR

             TIFF.ERROR = YES
             IF NOT(PHANTOM.PROC) AND NOT(JAVA.PROC$) THEN
                DISP.MSG = 'VSIFAX E-mail Error: ':MSG
                ERR.MESS 2,2,DISP.MSG
IN$$1:          INP K,0,0,0
             END
          END
          * Delete the batch created for NT sites
          IF OS.TYPE$ = "NT" THEN
             MSG = EXECUTE.OS.COMMAND('rm ':MOUT.DIR:TEMP.FILE)
          END

          * Convert the tiff to a PDF file
          IF NOT(TIFF.ERROR) AND (EMAIL.TIFF = 3 OR EMAIL.TIFF = 4) THEN
             CMD = '$VSIFAX/lbin/tiftopdf -o ':PDF.PATH:' ':TIFF.PATH
             IF OS.TYPE$ = "NT" THEN
                GOSUB FORMAT.NT.CMD
             END

             MSG.NUM = 2
             GOSUB DO.VSIFAX.CMD

          * Delete the batch created for NT sites
             IF OS.TYPE$ = "NT" THEN
                NT.MSG = EXECUTE.OS.COMMAND('rm ':MOUT.DIR:TEMP.FILE)
             END

             IF NOT(MSG) THEN
                WAIT.COUNT = 0
                LOOP
                   OPENSEQ PDF.PATH TO WAIT.FD THEN
                      CLOSESEQ WAIT.FD
                      EXIT
                   END ELSE
                      IF WAIT.COUNT > 20 THEN
                         MSG = 'tiftopdf failed to create file'
                         EXIT
                      END
                      WAIT.COUNT += 1
                      SLEEP 5
                   END
                REPEAT
             END
             IF MSG THEN
                * Log the error
                LOG.MSG = MSG
                GOSUB LOG.ERROR

                IF NOT(PHANTOM.PROC) AND NOT(JAVA.PROC$) THEN
                   DISP.MSG  = 'PDF conversion error, '
                   DISP.MSG := 'Email sent as a Tiff document'
                   DISP.MSG<-1> = 'VSIFAX E-mail Error: ':MSG
                   ERR.MESS 2,2,DISP.MSG
IN$$3:             INP K,0,0,0
                END
             END ELSE
                * remove the tiff, a PDF will now be sent instead
                CMD = 'rm ':TIFF.PATH
                IF OS.TYPE$ = "NT" THEN
                   GOSUB FORMAT.NT.CMD
                END
                MSG = EXECUTE.OS.COMMAND(CMD,YES)
                * Delete the batch created for NT sites
                IF OS.TYPE$ = "NT" THEN
                   MSG = EXECUTE.OS.COMMAND('rm ':MOUT.DIR:TEMP.FILE)
                END

                TIFF.PATH = PDF.PATH ;* Change the attachment path to PDF
             END
          END


          * Set the attachment path to the tiff file
          ATTACH.PATH = TIFF.PATH
          E.ATTACH = ''

          * If no body was entered, replace it with the tiff default
          TEST.BODY = MBX.BODY
          TEST.BODY = CHANGE(TEST.BODY,'&nbsp;','')
          TEST.BODY = CHANGE(TEST.BODY,CHAR(10),'')
          TEST.BODY = CHANGE(TEST.BODY,CHAR(13),'')
          TEST.BODY = CHANGE(TEST.BODY,'<br>','')
          IF TRIM(TRIM(TRIM(TEST.BODY,VM),AM)) = '' THEN
             IF OID THEN
                READ MBX.BODY FROM CTRLFILE,'EMAIL.ORDER.TIFF.MSG' ELSE
                   MBX.BODY = ''
                END
             END ELSE
                READ MBX.BODY FROM CTRLFILE,'EMAIL.STATEMENT.TIFF.MSG' ELSE
                   MBX.BODY = ''
                END
             END
             UT.REP.STR MBX.BODY,VM,' '
          END

          RETURN
*-------------------------------------------------------------------------*
DO.VSIFAX.CMD: ** Do a Vsifax command with possible retry
          MSG = EXECUTE.OS.COMMAND(CMD,YES)
          IF NOT(MSG) THEN RETURN
          LOG.MSG = MSG; ***   Save this in case the command works the next tme
          *** try again, Csifax gets 'intermitant' errors
          MSG = EXECUTE.OS.COMMAND(CMD,YES)
          IF MSG THEN RETURN; ** Give up
          MSG.NUM += 100; ** For 'temporary errors'
          GOSUB LOG.ERROR
          RETURN
*-------------------------------------------------------------------------*
CHECK.FOR.IMAGE: *** Check for custom code using @+IMAGE
          FOUND.IMAGE = NO
          OPENSEQ '&HOLD&',DOC.ID TO SOURCE ELSE RETURN
          LOOP
             READSEQ TEST FROM SOURCE ELSE EXIT
             IF INDEX(TEST,'@+IMAGE',1) THEN
                FOUND.IMAGE = YES
                EXIT
             END
          REPEAT

          CLOSESEQ SOURCE
          RETURN
*-------------------------------------------------------------------------*
GEN.HOLD.DOC: *** Generate the Order/Statement document to the holdfile.

          * Set DRPT for hold file printing
          DRPT     = ''
          DRPT<27> = 'HOLD'

          IF EMAIL.TIFF = 2 OR EMAIL.TIFF = 4 THEN
             * Fake "faxing" mode so that printing routines will use
             * their IF FAX.FLAG logic instead of printed. This is for
             * sites that want the "fax" version of forms to be emailed.
             DRPT<29> = YES
          END

          * Clear PRINTER.ID to prevent any attempt to use macros loaded
          * on a physical printer during SOE.PRINT.ORDER or AR.STATEMENT
          SV.PRINTER.ID = PRINTER.ID
          PRINTER.ID = ''

          * Set printer to a hold file document. This is done this way
          * to avoid any changes to printer.on, printer.off, and the
          * programs they call, so do not replace it with calls to those
          * those programs.
          GET.NEW.ID 'NEXT.DOC',DOC.ID,,,RPTFILE
          WIDTH = 80
          COMMAND = 'SETPTR 0,':WIDTH:',999,,,3,NOHEAD,NOEJECT,NOFMT,BRIEF,BANNER ':DOC.ID
          EXECUTE COMMAND CAPTURING MSG
          PRINTER ON
          PRINTER.ON$ = YES
          PRT.ON      = YES

          IF OID THEN                      ;* Create the order document
             PSTYLE.OVRD = EM.DATA<8>
             IF PSTYLE.OVRD = '' THEN
                PSTYLE.GET.ID OID,GEN,PSTYLE.ID,STATUS
             END ELSE
                PSTYLE.ID = PSTYLE.OVRD
             END
             IF OID[1,1] = 'P' OR STATUS = 'Y' THEN
                POE.PRINT.ORDER OID,GEN,STATUS,PSTYLE.ID,DRPT,PRT.ON
             END ELSE IF OID[1,1] = 'T' THEN
                TOE.PRINT.ORDER OID,GEN,STATUS,PSTYLE.ID,PRT.ON,DRPT
             END ELSE
                SOE.PRINT.ORDER OID,GEN,,STATUS,PSTYLE.ID,PRT.ON,DRPT
             END
          END ELSE                         ;* Create the statement
             ST.CN   = EM.DATA<2>
             IF NOT(ST.CN) THEN ST.CN = EM.DATA<1>
             SEL.OPTS  = RAISE(STMT.INFO<1,1>)
             ASOF      = STMT.INFO<1,2>
             AGDT      = STMT.INFO<1,3>
             SBRS      = RAISE(STMT.INFO<1,4>)
             MIN.BAL   = STMT.INFO<1,5>
             BATCH.FLG = STMT.INFO<1,6>
             ADDL.INFO = MIN.BAL:AM:BATCH.FLG
             STMT.CT   = ''
             AR.STATEMENT SEL.OPTS,ST.CN,ASOF,AGDT,SBRS,PRT.ON,STMT.CT,DRPT,1,,ADDL.INFO

             IF NOT(STMT.CT) THEN
                * Zero balance flag, set so that the email count won't
                * be incremented in AR.PHR.STATEMENT and so that
                * AR.DVR.STATEMENT will show the zero balance error if
                * this was a single email.
                ERR.MSG = 'Zero Balance'
             END
          END

          * Printer off
          PRINTER OFF
          PRINTER CLOSE
          PRINTER.ON$ = ''

          * Restore PRINTER.ID
          PRINTER.ID = SV.PRINTER.ID

          RETURN
*-------------------------------------------------------------------------*
FORMAT.NT.CMD: *** Convert a command with quotes to work with NT

          NT.CMD = CHANGE(CMD,"$VSIFAX",'"%VSIFAX%"')
          * quote the environment variable since it may contain spaces
          CONVERT '/' TO '\' IN NT.CMD
          * For NT, write out a temp file containing the command because
          * DOS /C gets hopelessly confused by any single or double
          * quotes passed directly to it around parameters
          OPEN 'MSG-OUT' TO TEMPFILE THEN
             TEMP.FILE = 'vfxcmd':TIME():'.BAT'
             WRITE '' ON TEMPFILE,TEMP.FILE
             CLOSE TEMPFILE
             OPENSEQ 'MSG-OUT',TEMP.FILE TO TEMPFILE THEN
                WRITESEQ NT.CMD ON TEMPFILE THEN
                   CLOSESEQ TEMPFILE
                   CMD = MOUT.DIR:TEMP.FILE
                END
             END
          END

          RETURN
*-------------------------------------------------------------------------*
CHK.SOLAR.ATTACH: *** Move &HOLD& Attachments for solar

          IF E.ATTACH OR PASSER<17>[1,10] # 'HOLD.FILE:' THEN
             RETURN
          END

          * We need to copy this from &HOLD& to msg-out because the
          * client doesn't do this for us (binary files are hosed
          * during solar comm, and besides, no point in transferring
          * file to client just to pass it back in here)
          DOC.ID = PASSER<17>[11,LEN(PASSER<17>)-10]
          READ RPT.DATA FROM RPTFILE,DOC.ID ELSE
             ERR.MSG = "Unable to read ":DOC.ID:" from REPORTS!"
             RETURN
          END
          IF RPT.DATA<8> # '' AND RPT.DATA<64> = '' THEN
             IS.REPORT = YES
          END ELSE IS.REPORT = NO
          OPENSEQ '&HOLD&',DOC.ID TO HOLDSEQFILE ELSE
             ERR.MSG = "Unable to read ":DOC.ID:" from &HOLD&!"
             RETURN
          END
          EDOC.ID = RPT.DATA<64>
          IF EDOC.ID = '' THEN EDOC.ID = DOC.ID
          ATT.ID = EDOC.ID

          * We are only expecting a suffix on the path if
          * PASSER<18> is set.  If not, we default to .txt
          IF PASSER<18> THEN
             SUFFIX = PASSER<18>
          END ELSE
             IF PASSER<19> = 'HTML' THEN
                SUFFIX = '.html'
             END ELSE
                SUFFIX = '.txt'
             END
          END

          ATT.ID := SUFFIX

TRY.AGN1: READV TEST FROM MOUTFILE,ATT.ID,0 THEN
             NAP 55
             ATT.ID = TIME():'-':EDOC.ID:SUFFIX
             GOTO TRY.AGN1
          END
          WRITEV '' ON MOUTFILE,ATT.ID,1

          OPENSEQ 'MSG-OUT',ATT.ID TO MOUTSEQFILE ELSE
             ERR.MSG = "Error while opening attachment file"
             RETURN
          END

          IF IS.REPORT THEN
             HDR.LINES = DCOUNT(RPT.DATA<8>,VM)
             FOR HX = 1 TO HDR.LINES
                WRITESEQ RPT.DATA<8,HX> ON MOUTSEQFILE ELSE
                   ERR.MSG = "Error writing to MSG-OUT! (1)"
                   RETURN
                END
             NEXT HX
          END

          * Copy the file.  Must do this using BLK, as could be
          * a binary file
          LOOP
             READBLK BDATA FROM HOLDSEQFILE,1024 ELSE EXIT
             WRITEBLK BDATA ON MOUTSEQFILE ELSE
                ERR.MSG = "Error writing to MSG-OUT! (2)"
                RETURN
             END
          REPEAT

          CLOSESEQ MOUTSEQFILE

          READV ATTACH.PATH FROM VOCFILE,'MSG-OUT',2 ELSE
             ERR.MSG = "Unable to find MSG-OUT in VOC!"
             RETURN
          END

          IF OS.TYPE$ = 'NT' THEN SLSH = '\' ELSE SLSH = '/'
          ATTACH.PATH := SLSH:ATT.ID

          * If we are to include the file in the body of the email,
          * then read it in from msg-out for later use
          IF PASSER<9> THEN
             READ E.ATTACH FROM MOUTFILE,ATT.ID ELSE E.ATTACH = ''
          END

          RETURN
*-------------------------------------------------------------------------*
EMAIL.FORM:
          BEGIN CASE
          CASE EXT.HOLD.ID
             * If this is an extract, call the extract routine for
             * all file extensions except PDF.
             IF PASSER<29> AND OCONV(PASSER<18>,'MCU') # '.PDF' THEN
                FORMS.EXTRACT.FROM.HOLD E.ATTACH,PASSER
             END ELSE
                FORMS.PRINT.FROM.HOLD EXT.HOLD.ID,,PASSER,,YES
             END
          CASE STMT.INFO
             SEL.OPTS  = RAISE(STMT.INFO<1,1>)
             ASOF      = STMT.INFO<1,2>
             AGDT      = STMT.INFO<1,3>
             SBRS      = RAISE(STMT.INFO<1,4>)
             MIN.BAL   = STMT.INFO<1,5>
             BATCH.FLG = STMT.INFO<1,6>
             ADDL.INFO = MIN.BAL:AM:BATCH.FLG

             FORMS.CHECK.STMT.PRINT SEL.OPTS,CN,ASOF,AGDT,SBRS,PRT.FLAG,HDR,ITM,FTR,ADDL.INFO
             IF PRT.FLAG THEN
                STATEMENT.DELIVER CN,LOWER(HDR),LOWER(ITM),LOWER(FTR),,PASSER
             END
          CASE OTHERWISE
             PSTYLE.OVRD = EM.DATA<8>
             BEGIN CASE
             CASE OID[1,1] = 'S'
                BEGIN CASE
                CASE STATUS = 'I'
                   DOC.ID = 'INVOICE'
                   USER.DOC.ID = '** INVOICE **'
                   DOC.TYPE = 'I'
                CASE STATUS = 'B'
                   DOC.ID = 'SALES.BID'
                   DOC.TYPE = 'B'
                   USER.DOC.ID = '** QUOTATION **'
                CASE STATUS = "T"
                   DOC.ID = "SALES.PICK"
                   DOC.TYPE = "T"
                   USER.DOC.ID = "** PICK TICKET"
                CASE STATUS = "O"
                   DOC.ID = "SALES.SHIP"
                   DOC.TYPE = "O"
                   USER.DOC.ID = "** SHIP TICKET"
                CASE STATUS = "M"
                   SOE.DELIVER.ORDER.SUMMARY OID,,PASSER
                   RETURN
                CASE STATUS = 'Y'
                   DOC.ID   = 'PURCHASE.DIRECT'
                   USER.DOC.ID   = '** DIRECT SHIPMENT P/O **'
                   DOC.TYPE = 'D'
                CASE OTHERWISE
                   DOC.ID = 'SALES.ACK'
                   USER.DOC.ID = '** ACKNOWLEDGEMENT **'
                   DOC.TYPE = 'S'
                END CASE
             CASE OID[1,1] = 'P'
                BEGIN CASE
                CASE STATUS = 'R'
                   DOC.ID   = 'PURCHASE.REGISTER'
                   USER.DOC.ID   = '** RECEIVING REGISTER **'
                   DOC.TYPE = 'R'
                CASE STATUS = 'B'
                   DOC.ID   = 'PURCHASE.BID'
                   USER.DOC.ID  = '** REQUEST FOR QUOTATION **'
                   DOC.TYPE = 'B'
                CASE OTHERWISE
                   DOC.ID = 'PURCHASE.ORDER'
                   USER.DOC.ID   = '** PURCHASE ORDER **'
                   DOC.TYPE = 'O'
                END CASE
             CASE OID[1,1] = 'T'
                PRINT.RECEIVER = NO
                * Check to see if it's a Receiving Register, if so, check
                * the control record for current ship branch to see if a
                * Receiving Register should print of a PO Receiver.
                CONTROL.CHK = 'PRINT.XFER.RECVR~':EM.DATA<3>
                IF STATUS   = 'R' THEN
                   READV NADA FROM CTRBFILE,CONTROL.CHK,0 THEN
                      PRINT.RECEIVER = YES
                   END
                END
                DOC.TYPE = STATUS
                BEGIN CASE
                CASE PRINT.RECEIVER
                   USER.DOC.ID = 'Purchase Reciver'
                   DOC.ID   = 'PO.RECEIVER'
                CASE STATUS = 'B'
                   DOC.ID   = 'TRANSFER.QUOTE'
                   USER.DOC.ID   = 'Transfer Quote'
                CASE STATUS = 'O'
                   DOC.ID   = 'TRANSFER'
                   USER.DOC.ID = 'Transfer'
                CASE STATUS = 'S'
                   DOC.ID = 'TRANSFER.SHIPPER'
                   USER.DOC.ID = 'Transfer Shipper'
                CASE STATUS = 'R'
                   DOC.ID   = 'TRANSFER.REGISTER'
                   USER.DOC.ID = 'Transfer Register'
                END CASE
             CASE OTHERWISE
                DOC.ID = STATUS
             END CASE

             IF USE.EF.PRINT.LOG$ THEN
                OE.LOG.PRINT OID,GEN,DOC.TYPE:VM:1,"SKIP",REPRINT
             END ELSE
                OE.LOG.PRINT OID,GEN,DOC.TYPE:VM:1,USER.DOC.ID:' E-mail',REPRINT
             END

             FORMS.DELIVER OID,GEN,DOC.ID,PSTYLE.OVRD,,PASSER,,REPRINT
          END CASE
          RETURN
*-------------------------------------------------------------------------*
LOG.ERROR:
          LOG.REC = OCONV(DATE(),"D2/") : " ":OCONV(TIME(),"MTH")
          LOG.REC<2> = CMD
          LOG.REC<3> = PHANTOM.PROC:VM:JAVA.PROC$
          LOG.REC<4> = LOG.MSG
          WRITE.LAST.ACTION LOG.REC,'ATTACH.ERROR':MSG.NUM
          RETURN
*-------------------------------------------------------------------------*
ADD.DELIMS: *** Add delimiters if .CSV or .XML.
          READ RPT.DATA FROM RPTFILE,EXT.HOLD.ID ELSE RETURN

          FORMAT = RAISE(RAISE(RPT.DATA<54,8>))
          IF FORMAT = '' THEN
             FORMAT = PC.COLUMNS('&HOLD&,':EXT.HOLD.ID)
          END

          CONVERT VM TO AM IN E.ATTACH

          COLS = DCOUNT(FORMAT,AM)
          TB = CHAR(9)
          BEGIN CASE
          CASE OCONV(PASSER<18>,'MCU') = '.XLS'
             DELIMTR = TB
          CASE OCONV(PASSER<18>,'MCU') = '.CSV'
             DELIMTR = ','
          CASE OCONV(PASSER<18>,'MCU') = '.XML'
             DELIMTR = '~~'
          CASE OTHERWISE
             DELIMTR = ''
          END CASE

          LN.CTR  = DCOUNT(E.ATTACH,AM)
          FOR XX = 1 TO LN.CTR
             WRK.STR = ''
             FOR YY = 1 TO COLS
                WRK = E.ATTACH<XX>[FORMAT<YY,1>,FORMAT<YY,2>]

                * If the data contains the delimiter and double quote ("),
                * replace the delimiter with a space.  If there is not a
                * double quote within the data, put double quotes around
                * the data.
                IF INDEX(WRK,DELIMTR,1) THEN
                   IF INDEX(WRK,'"',1) THEN
                      CONVERT DELIMTR TO ' ' IN WRK
                   END ELSE
                      WRK = '"':WRK:'"'
                   END
                END
                IF YY # COLS THEN
                   WRK := DELIMTR
                END

                WRK.STR := WRK
             NEXT YY

             E.ATTACH<XX> = WRK.STR
          NEXT XX

          RETURN
*-------------------------------------------------------------------------*
!TSMITH~02/12/14~13:47
