FBASF ;AISC/JLG - ICD10 DIAGNOSIS CODE ASF (Advanced Search Functionality) ;3/26/2012
 ;;3.5;FEE BASIS;**139**;JAN 30, 1995;Build 127
 ;;Per VA Directive 6402, this routine should not be modified.
 ;
 ; Reference to API $$CODEN^ICDEX supported by ICR #5747
 ;
 ;Prompt for ICD10 diagnosis entries  
 ; params, 1-Diagnosis prompt
 ;         2-prompt line number (null if no number)
 ;         3-allow up arrow (^) flag (optional) -if this is set to "Y" then the up arrow will be accepted for early exit
 ;         4-allow deletion of DX field? (optional)  -if this is set to "Y", @ is an acceptable entry
 ;         5-allow forcing a field to be required (optional)  -if this is set to "Y", the field will be forced to be required
ASKICD10(DXPRMPT,LNNUM,ALWUPA,ALDEL,ALFREQ) ;
 N FBOUT,FBDC,ICDRET,FBTMP,FBPRMPT  S FBDC=""
 S FBPRMPT=DXPRMPT_LNNUM
 S ICDRET=$$EN(EDATE,FBDC) ; EDATE must be assigned prior to calling this s/r. It represents 'date of interest'
 D EXIT
 Q ICDRET ;returns the value of ien or -1
 ; 
EN(EFFDATE,X) ; -- params 1-date of interest 2-diagnosis code
 N FBQUIT,FBRETV,FBPARAM,FBCSYS,FBOUT,FBDFN
 D SETPARAM(.FBPARAM) ; set screen messages
 S FBDT=EFFDATE,FBFILE=DP,FBIEN=DA,FBDFLT="",FBRETV=0,FBOUT=""
 S:$D(DFN) FBDFN=DFN
 ; 161.01 is the sub-field authorization in fee basis patient file
 S:FBFILE="161.01" FBDFLT=$$GETDC^FBASFU(FBFILE,FBDFN,FBIEN)
 ; 162.7 is the unauthorized claim funds file
 S:FBFILE="162.7" FBDFLT=$$GETDCUC^FBASFU(FBFILE,FBIEN)
 S:FBDFLT']"" FBDFLT=$$GETVAL^FBASFU(FBFILE,FBIEN,FBPARAM("FIELD_NAME")) ; set default value if applicable
 ;
EN1 ; 
 S FBRETV=$$DIAG10(FBDT,FBDFLT,.FBPARAM)
 I (FBRETV']"")!(FBRETV<0) Q FBRETV
 I FBRETV="@" Q FBRETV ; don't print labels for deletions
 S FBRETV=$$PRTICD10^FBASFU(FBRETV) ; prints ICD code and description to the screen
 S FBRETV=$P($P(FBRETV,"^"),";")
 G:FBRETV=-1 EN1
 Q FBRETV ; returns IEN file #80 or -1
 ;//---------
 ;The entry point for ICD-10 diagnosis search functionality
 ;input parameters :
 ; FBDT - date of interest
 ; FBDFLT - default values for the search string (can be a code by default)
 ; FBPARAM - parameters/string constants (see SETPARAM for details)
 ;returns ICD-10 code selected by the user:
 ;  IEN file #80;ICD code value^description
 ; results
 ; or -1 if invalid data(press enter)
 ; "" if not found 
 ; or -2 if time out
 ; or -3 if ^ or ^^
 ; or -4 in ICD10 if the usre answered NO for the question "Do you wish to continue(Y/N)?"
 ; or -5 if deletion of DX field is attempted
 ; 
DIAG10(FBDT,FBDFLT,FBPARAM) ;
 N FBINP,FBTMP,FBREQFLDMP
 S:'$D(ALWUPA) ALWUPA="N" ; up arrow allow flag
 S:'$D(ALDEL) ALDEL="N" ; delete allow flag
 S:'$D(ALFREQ) ALFREQ="N" ; force required allow flag
ASKAGAIN ;
 S FBINP=$$SRCHSTR(FBPARAM("SEARCH_PROMPT"),FBPARAM("HELP ?"),FBPARAM("HELP ??"),FBDFLT)
 ;user should enter at least 2 characters
 I FBINP'<0 I $L($P(FBINP,U,2))'>1 W !!,FBPARAM("ENTER MORE") W:$L(FBPARAM("ENTER MORE2"))>0 !,FBPARAM("ENTER MORE2") W ! G ASKAGAIN
 ; return values from SRCHSTR function ... $D(DTOUT) -2, $D(DUOUT) -3, Y["^" -3, Y="" -1, otherwise 0_U_Y
 Q:FBINP=-2 FBINP ; timed out
 Q:(ALWUPA="Y")&(FBINP=-3) FBINP ; "^" entered
 S FBREQFLD=$$REQFLD^FBASFU(FBFILE,FBPARAM("FIELD_NAME"))
 I ((ALFREQ="Y")&(FBINP=-5)) S FBREQFLD=0
 I ((FBINP=-5)&('FBREQFLD)) W FBPARAM("REQUIRED") G ASKAGAIN
 I ALDEL="Y",FBINP=-5,$G(FBDFLT)="" S ALDEL="N"
 I ALDEL="Y",FBINP=-5 N FBYN D  Q:FBYN=1 "@" G ASKAGAIN
 . S FBYN=$$QUESTION^FBASF(2,"SURE YOU WANT TO DELETE")
 . I FBYN'=1 W FBPARAM("NOTHING DELETED")
 I FBINP=-5 W "??" G ASKAGAIN
 I ((FBREQFLD=-1)&(FBINP=-3)) W !,FBPARAM("EXIT NOT ALLOWED") G ASKAGAIN
 Q:((FBREQFLD=-1)&(FBINP'[U)) FBINP ; if not a required field and NOT a valid search string for icd code
 I FBINP=-1 D   ; if a space is entered for a required field
 . W "??"
 . I FBPARAM("SEARCH_PROMPT")["ADMITTING DIAGNOSIS" W !,FBPARAM("ENTER ADM DIAG")
 I ((FBREQFLD=0)&(FBINP=-1)) G ASKAGAIN ;space entered for required field
 I FBINP=-3 W !,FBPARAM("EXIT NOT ALLOWED") G ASKAGAIN ;^ entered for all ICD fields
 S FBTMP=$$STATCHK^FBASFU($P(FBINP,U,2),FBDT) ; check if icd code is inactive
 G:FBTMP=-1 ASKAGAIN ; If icd code is inactive
 N FBMATCH S FBMATCH=$$ISMATCH($P(FBINP,U,2))
 S FBINP=$$LEXICD10($P(FBINP,U,2),FBDT,.FBPARAM)
 G:FBINP=-4 ASKAGAIN ; if the threshold for the results is reached and user wants to refine search criteria
 I FBINP']"" W !,FBPARAM("NO MATCHES FOUND") I FBPARAM("SEARCH_PROMPT")["ADMITTING DIAGNOSIS" W !,"  ",FBPARAM("ENTER ADM DIAG")
 G:FBINP']"" ASKAGAIN
 G:FBINP=-1 ASKAGAIN
 Q FBINP_"^"_FBMATCH
 ;
 ;input parameter - diagnosis code
 ;Returns 0 (zero) if diagnosis code is an exact match, otherwise return -1
ISMATCH(FBDCDE) ;
 N FBMFLG S FBMFLG=-1 ;set default to -1
 S:$$CODEN^ICDEX(FBDCDE,80)>0 FBMFLG=0
 Q FBMFLG
 ;--------------
 ;The entry point for ICD-10 diagnosis search functionality
 ;can be called from applications directly
 ; Supported ICR 5681 ($$DIAGSRCH^LEX10CS)
 ;input parameters :
 ; FBTXT - search string
 ; FBDATE - date of interest
 ; FBPAR - array with text messages and other string constants
 ;returns ICD-10 code selected by the user:
 ;  IEN file #80;ICD code value^description
 ; or 
 ; "" if not found 
 ; -1 if exit : ^ or ^^
 ; -2 if continue searching
 ;
LEXICD10(FBTXT,FBDATE,FBPAR) ; ICD-10 Search
 N FBLVTXT
 ;parameters check
 S FBDATE=+$G(FBDATE)
 I FBDATE'?7N Q -1
 S FBTXT=$G(FBTXT)
 Q:'$L(FBTXT) -1
 N FBNUMB
 S FBNUMB=$$FREQ^LEXU(FBTXT)
 I FBNUMB>$$MAX^LEXU(30) D  I $$QUESTION(2,FBPARAM("WISH CONTINUE"),FBPARAM("YES OR NO"))'=1 Q -4
 . D FORMWRIT(FBPAR("EXCEEDS MESSAGE1")_FBTXT_FBPAR("EXCEEDS MESSAGE2")_FBNUMB_FBPAR("EXCEEDS MESSAGE3")_FBTXT_""".",0)
 . D FORMWRIT("",2)
 ;new and set variables
 N DIROUT,DUOUT,DTOUT,FBEXIT,FBICDNT
 N FBRETV,FBXX,FBLEVEL
 S FBRETV=""
 S FBEXIT=0
 S FBLEVEL=1,FBLVTXT(FBLEVEL)=FBTXT ;level 1 stores the original search string
 ; main loop
 F  Q:FBEXIT>0  D
 .K FBICDY
 .;get the search string from the current level and call LEX API
 .S FBICDY=$$DIAGSRCH^LEX10CS(FBLVTXT(FBLEVEL),.FBICDY,FBDATE,30)
 .S:$O(FBICDY(" "),-1)>0 FBICDY=+FBICDY
 .; Nothing found
 .I +FBICDY'>0 S FBEXIT=1 S FBXX=-1 Q
 .; Single match found for partial text search
 .I FBMATCH<0,FBLEVEL=1,FBICDY=1 S FBMATCH=0
 .; display the list of items and ask the user to select the item from the list
 .S FBXX=$$SEL^FBASFL(.FBICDY,8)
 .; if ^ was entered 
 .;   if this is on the top level then quit 
 .I FBXX=-2,FBLEVEL'>1 S FBRETV=-1 S FBEXIT=1 Q
 .;   if lower level then go one level up
 .I FBXX=-2,FBLEVEL>1 S:FBLEVEL>1 FBLEVEL=FBLEVEL-1 Q
 .; If timeout, or not selected, or ^^ then quit
 .I FBXX=-1 S FBRETV=-1 S FBEXIT=1 Q
 .; if Code Found and Selected by the user save selection in FBRETV and quit
 .I $P(FBXX,";")'="99:CAT" S FBRETV=FBXX S FBEXIT=1 Q
 .; If Category Found and Selected by the user:  
 .;  go to the next inner level
 .;  change level number 
 .S FBLEVEL=FBLEVEL+1
 .;  set the new level with the new search string
 .;  and repeat 
 .S FBLVTXT(FBLEVEL)=$P($P($G(FBXX),"^"),";",2)
 Q FBRETV
 ;
 ; Look-up help for ?
INPHLP ;
 I $G(X)["???" D INPHLP3 Q
 I $G(X)["??" D INPHLP2 Q
 W !," Enter code or ""text"" for more information." Q
 Q
 ;-----------
 ; Look-up help for ??
INPHLP2 ;
 W !," Enter a ""free text"" term or part of a term such as ""femur fracture""."
 W !!," or "
 W !!," Enter a ""classification code"" (ICD/CPT etc) to find the single term"
 W !," associated with the code."
 W !!," or "
 W !!," Enter a ""partial code"". Include the decimal when a search criterion"
 W !," includes 3 characters or more for code searches."
 Q
 ;--------
 ; Look-up help for ???
INPHLP3 ;
 W !," Number of Code Matches"
 W !," ----------------------"
 W !!," The ICD-10 Diagnosis Code search will show the user the number of matches"
 W !," found, indicate if additional characters in ICD code exist, and the number"
 W !," of codes within the category or subcategory that are available for selection."
 W !," For example:"
 W !!," 14 matches found"
 W !!," M91. - Juvenile osteochondrosis of hip and pelvis (19)"
 W !!," This indicates that 14 unique matches or matching groups have been found"
 W !," and will be displayed."
 W !!," M91. - the ""-"" indicates that there are additional characters that specify"
 W !," unique ICD-10 codes available."
 W !!," (19) Indicates that there are 19 additional ICD-10 codes in the M91 ""family"""
 W !," that are possible selections."
 Q
 ;--------
 ;ask YES/NO questions
 ;input parameters :
 ; FBDFLT- 0/null- not default, 1- yes, 2 -no
 ; FBPROM - prompt string
 ; FBHELP - help text
 ;returns 
 ; 2 - no,
 ; 1 -yes,
 ; 0 - no answer (time out)
 ; -3 - ^ or ^^
 ; 0 - no answer
QUESTION(FBDFLT,FBPROM,FBHELP) ;
 N DIR
 S %=$G(FBDFLT,2)
 S DIR(0)="Y",DIR("A")=FBPROM,DIR("B")=$S(%=1:"Yes",%=2:"No",1:"")
 S:$L($G(FBHELP)) DIR("?")=FBHELP
 D ^DIR
 Q:Y["^" -3
 Q:Y=1 1
 Q:Y=0 2
 Q 0
 ;
 ;------------
 ;get search string
 ;input parameters :
 ; FBPRMT prompt text
 ; FBHLP1 "?" help text
 ; FBHLP2 "??" help text
 ; FBDFLT- default response
 ;returns piece1 ^ piece 2
 ; piece1:
 ; 0 if normal input
 ; or -1 if invalid data
 ; or -2 if time out
 ; or -3 if ^
 ; or -5 if @
 ; piece2: string entered by the user
SRCHSTR(FBPRMT,FBHLP1,FBHLP2,FBDFLT) ;
 N DIR
 S DIR("A")=FBPRMT
 S DIR("?")=FBHLP1
 S DIR("??")=FBHLP2
 I $L($G(FBDFLT)) S DIR("B")=FBDFLT
 S DIR(0)="FAOr^0:245"
 D ^DIR
 Q:$D(DTOUT) -2
 Q:$D(DUOUT) -3
 Q:X="@" -5
 Q:Y["^" -3
 Q:Y="" -1
 Q 0_U_Y
 ;
 ;set parameters
 ;input parameters 
 ; FBPAR - local array to sets and store string constants for your messages and prompts 
SETPARAM(FBPAR) ;
 S FBPAR("ASKDATE")="Date of interest? "
 I FBPRMPT'[":" S FBPRMPT=FBPRMPT_": "
 S FBPAR("SEARCH_PROMPT")=FBPRMPT
 S FBPAR("HELP ?")="^D INPHLP^FBASF"
 S FBPAR("HELP ??")="^D INPHLP2^FBASF"
 S FBPAR("NO DATA FOUND")="  No data found"
 S FBPAR("EXITING")="  Exiting"
 S FBPAR("TRY LATER")="  Try again later"
 S FBPAR("NO DATA SELECTED")="  No data selected"
 S FBPAR("TRY ANOTHER")="Try another"
 S FBPAR("WISH CONTINUE")="Do you wish to continue(Y/N)"
 S FBPAR("EXCEEDS MESSAGE1")="Searching for """
 S FBPAR("EXCEEDS MESSAGE2")=""" requires inspecting "
 S FBPAR("EXCEEDS MESSAGE3")=" records to determine if they match the search criteria.  This could take quite some time.  Suggest refining the search by further specifying """
 S FBPAR("NO MATCHES FOUND")="  No matches found??"
 S FBPAR("ENTER ADM DIAG")="  Enter the admitting diagnosis for this claim."
 S FBPAR("EXIT NOT ALLOWED")="   Exit not allowed??"
 S FBPAR("ENTER MORE")=" Please enter at least the first two characters of the ICD-10 code or code"
 S FBPAR("ENTER MORE2")=" description to start the search."
 S FBPAR("YES OR NO")="Answer 'Y' for 'Yes' or 'N' for 'No'"
 S FBPAR("NOTHING DELETED")="  <NOTHING DELETED>"
 S FBPAR("REQUIRED")="??  Required"
 N FBX S FBX=FBPRMPT
 F  Q:(($E(FBX)'=" ")&($E(FBX)'?1C))  S FBX=$E(FBX,2,99) ; remove leading space or control chars.
 S FBPAR("FIELD_NAME")=$P(FBX,":")
 Q
 ;
 ;
 ;a wrapper for ^DIWP
 ;accumulates a text and then writes it to the device
 ;input parameters :
 ; X - text
 ; FBMODE:
 ;  0 - start
 ;  1 - accumulate 
 ;  2 - write
 ;example:
 ;D FORMWRIT^FBASF("this API is a wrapper for ^DIWP, it accumulates a text and then writes it to the device, you can use it in your application code",0)
 ;D FORMWRIT^FBASF("some more text ",1)
 ;D FORMWRIT^FBASF("",2)
FORMWRIT(X,FBMODE) ;
 N FBLI1
 ;if "start" mode
 I FBMODE=0 K ^UTILITY($J,"W")
 S DIWL=1,DIWR=79
 I $L(X)>0 D ^DIWP
 ;if "write" mode
 I FBMODE=2 D
 . S FBLI1=0 F  S FBLI1=$O(^UTILITY($J,"W",1,FBLI1)) Q:+FBLI1=0  W !,$G(^UTILITY($J,"W",1,FBLI1,0))
 . K ^UTILITY($J,"W")
 Q
 ;
 ; Clean up environment and quit
EXIT ;
 K %,DIC,DIR,DIROUT,DIRUT,DTOUT,DUOUT,X,Y,%Y,FBDT,FBFILE,FBIEN,FBDFLT,FBOUT,FBREQFLD,DXPRMPT,LNNUM,DIWL,DIWR
 Q
 ;
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HFBASF   12115     printed  Sep 23, 2025@19:33:26                                                                                                                                                                                                      Page 2
FBASF     ;AISC/JLG - ICD10 DIAGNOSIS CODE ASF (Advanced Search Functionality) ;3/26/2012
 +1       ;;3.5;FEE BASIS;**139**;JAN 30, 1995;Build 127
 +2       ;;Per VA Directive 6402, this routine should not be modified.
 +3       ;
 +4       ; Reference to API $$CODEN^ICDEX supported by ICR #5747
 +5       ;
 +6       ;Prompt for ICD10 diagnosis entries  
 +7       ; params, 1-Diagnosis prompt
 +8       ;         2-prompt line number (null if no number)
 +9       ;         3-allow up arrow (^) flag (optional) -if this is set to "Y" then the up arrow will be accepted for early exit
 +10      ;         4-allow deletion of DX field? (optional)  -if this is set to "Y", @ is an acceptable entry
 +11      ;         5-allow forcing a field to be required (optional)  -if this is set to "Y", the field will be forced to be required
ASKICD10(DXPRMPT,LNNUM,ALWUPA,ALDEL,ALFREQ) ;
 +1        NEW FBOUT,FBDC,ICDRET,FBTMP,FBPRMPT
           SET FBDC=""
 +2        SET FBPRMPT=DXPRMPT_LNNUM
 +3       ; EDATE must be assigned prior to calling this s/r. It represents 'date of interest'
           SET ICDRET=$$EN(EDATE,FBDC)
 +4        DO EXIT
 +5       ;returns the value of ien or -1
           QUIT ICDRET
 +6       ; 
EN(EFFDATE,X) ; -- params 1-date of interest 2-diagnosis code
 +1        NEW FBQUIT,FBRETV,FBPARAM,FBCSYS,FBOUT,FBDFN
 +2       ; set screen messages
           DO SETPARAM(.FBPARAM)
 +3        SET FBDT=EFFDATE
           SET FBFILE=DP
           SET FBIEN=DA
           SET FBDFLT=""
           SET FBRETV=0
           SET FBOUT=""
 +4        if $DATA(DFN)
               SET FBDFN=DFN
 +5       ; 161.01 is the sub-field authorization in fee basis patient file
 +6        if FBFILE="161.01"
               SET FBDFLT=$$GETDC^FBASFU(FBFILE,FBDFN,FBIEN)
 +7       ; 162.7 is the unauthorized claim funds file
 +8        if FBFILE="162.7"
               SET FBDFLT=$$GETDCUC^FBASFU(FBFILE,FBIEN)
 +9       ; set default value if applicable
           if FBDFLT']""
               SET FBDFLT=$$GETVAL^FBASFU(FBFILE,FBIEN,FBPARAM("FIELD_NAME"))
 +10      ;
EN1       ; 
 +1        SET FBRETV=$$DIAG10(FBDT,FBDFLT,.FBPARAM)
 +2        IF (FBRETV']"")!(FBRETV<0)
               QUIT FBRETV
 +3       ; don't print labels for deletions
           IF FBRETV="@"
               QUIT FBRETV
 +4       ; prints ICD code and description to the screen
           SET FBRETV=$$PRTICD10^FBASFU(FBRETV)
 +5        SET FBRETV=$PIECE($PIECE(FBRETV,"^"),";")
 +6        if FBRETV=-1
               GOTO EN1
 +7       ; returns IEN file #80 or -1
           QUIT FBRETV
 +8       ;//---------
 +9       ;The entry point for ICD-10 diagnosis search functionality
 +10      ;input parameters :
 +11      ; FBDT - date of interest
 +12      ; FBDFLT - default values for the search string (can be a code by default)
 +13      ; FBPARAM - parameters/string constants (see SETPARAM for details)
 +14      ;returns ICD-10 code selected by the user:
 +15      ;  IEN file #80;ICD code value^description
 +16      ; results
 +17      ; or -1 if invalid data(press enter)
 +18      ; "" if not found 
 +19      ; or -2 if time out
 +20      ; or -3 if ^ or ^^
 +21      ; or -4 in ICD10 if the usre answered NO for the question "Do you wish to continue(Y/N)?"
 +22      ; or -5 if deletion of DX field is attempted
 +23      ; 
DIAG10(FBDT,FBDFLT,FBPARAM) ;
 +1        NEW FBINP,FBTMP,FBREQFLDMP
 +2       ; up arrow allow flag
           if '$DATA(ALWUPA)
               SET ALWUPA="N"
 +3       ; delete allow flag
           if '$DATA(ALDEL)
               SET ALDEL="N"
 +4       ; force required allow flag
           if '$DATA(ALFREQ)
               SET ALFREQ="N"
ASKAGAIN  ;
 +1        SET FBINP=$$SRCHSTR(FBPARAM("SEARCH_PROMPT"),FBPARAM("HELP ?"),FBPARAM("HELP ??"),FBDFLT)
 +2       ;user should enter at least 2 characters
 +3        IF FBINP'<0
               IF $LENGTH($PIECE(FBINP,U,2))'>1
                   WRITE !!,FBPARAM("ENTER MORE")
                   if $LENGTH(FBPARAM("ENTER MORE2"))>0
                       WRITE !,FBPARAM("ENTER MORE2")
                   WRITE !
                   GOTO ASKAGAIN
 +4       ; return values from SRCHSTR function ... $D(DTOUT) -2, $D(DUOUT) -3, Y["^" -3, Y="" -1, otherwise 0_U_Y
 +5       ; timed out
           if FBINP=-2
               QUIT FBINP
 +6       ; "^" entered
           if (ALWUPA="Y")&(FBINP=-3)
               QUIT FBINP
 +7        SET FBREQFLD=$$REQFLD^FBASFU(FBFILE,FBPARAM("FIELD_NAME"))
 +8        IF ((ALFREQ="Y")&(FBINP=-5))
               SET FBREQFLD=0
 +9        IF ((FBINP=-5)&('FBREQFLD))
               WRITE FBPARAM("REQUIRED")
               GOTO ASKAGAIN
 +10       IF ALDEL="Y"
               IF FBINP=-5
                   IF $GET(FBDFLT)=""
                       SET ALDEL="N"
 +11       IF ALDEL="Y"
               IF FBINP=-5
                   NEW FBYN
                   Begin DoDot:1
 +12                   SET FBYN=$$QUESTION^FBASF(2,"SURE YOU WANT TO DELETE")
 +13                   IF FBYN'=1
                           WRITE FBPARAM("NOTHING DELETED")
                   End DoDot:1
                   if FBYN=1
                       QUIT "@"
                   GOTO ASKAGAIN
 +14       IF FBINP=-5
               WRITE "??"
               GOTO ASKAGAIN
 +15       IF ((FBREQFLD=-1)&(FBINP=-3))
               WRITE !,FBPARAM("EXIT NOT ALLOWED")
               GOTO ASKAGAIN
 +16      ; if not a required field and NOT a valid search string for icd code
           if ((FBREQFLD=-1)&(FBINP'[U))
               QUIT FBINP
 +17      ; if a space is entered for a required field
           IF FBINP=-1
               Begin DoDot:1
 +18               WRITE "??"
 +19               IF FBPARAM("SEARCH_PROMPT")["ADMITTING DIAGNOSIS"
                       WRITE !,FBPARAM("ENTER ADM DIAG")
               End DoDot:1
 +20      ;space entered for required field
           IF ((FBREQFLD=0)&(FBINP=-1))
               GOTO ASKAGAIN
 +21      ;^ entered for all ICD fields
           IF FBINP=-3
               WRITE !,FBPARAM("EXIT NOT ALLOWED")
               GOTO ASKAGAIN
 +22      ; check if icd code is inactive
           SET FBTMP=$$STATCHK^FBASFU($PIECE(FBINP,U,2),FBDT)
 +23      ; If icd code is inactive
           if FBTMP=-1
               GOTO ASKAGAIN
 +24       NEW FBMATCH
           SET FBMATCH=$$ISMATCH($PIECE(FBINP,U,2))
 +25       SET FBINP=$$LEXICD10($PIECE(FBINP,U,2),FBDT,.FBPARAM)
 +26      ; if the threshold for the results is reached and user wants to refine search criteria
           if FBINP=-4
               GOTO ASKAGAIN
 +27       IF FBINP']""
               WRITE !,FBPARAM("NO MATCHES FOUND")
               IF FBPARAM("SEARCH_PROMPT")["ADMITTING DIAGNOSIS"
                   WRITE !,"  ",FBPARAM("ENTER ADM DIAG")
 +28       if FBINP']""
               GOTO ASKAGAIN
 +29       if FBINP=-1
               GOTO ASKAGAIN
 +30       QUIT FBINP_"^"_FBMATCH
 +31      ;
 +32      ;input parameter - diagnosis code
 +33      ;Returns 0 (zero) if diagnosis code is an exact match, otherwise return -1
ISMATCH(FBDCDE) ;
 +1       ;set default to -1
           NEW FBMFLG
           SET FBMFLG=-1
 +2        if $$CODEN^ICDEX(FBDCDE,80)>0
               SET FBMFLG=0
 +3        QUIT FBMFLG
 +4       ;--------------
 +5       ;The entry point for ICD-10 diagnosis search functionality
 +6       ;can be called from applications directly
 +7       ; Supported ICR 5681 ($$DIAGSRCH^LEX10CS)
 +8       ;input parameters :
 +9       ; FBTXT - search string
 +10      ; FBDATE - date of interest
 +11      ; FBPAR - array with text messages and other string constants
 +12      ;returns ICD-10 code selected by the user:
 +13      ;  IEN file #80;ICD code value^description
 +14      ; or 
 +15      ; "" if not found 
 +16      ; -1 if exit : ^ or ^^
 +17      ; -2 if continue searching
 +18      ;
LEXICD10(FBTXT,FBDATE,FBPAR) ; ICD-10 Search
 +1        NEW FBLVTXT
 +2       ;parameters check
 +3        SET FBDATE=+$GET(FBDATE)
 +4        IF FBDATE'?7N
               QUIT -1
 +5        SET FBTXT=$GET(FBTXT)
 +6        if '$LENGTH(FBTXT)
               QUIT -1
 +7        NEW FBNUMB
 +8        SET FBNUMB=$$FREQ^LEXU(FBTXT)
 +9        IF FBNUMB>$$MAX^LEXU(30)
               Begin DoDot:1
 +10               DO FORMWRIT(FBPAR("EXCEEDS MESSAGE1")_FBTXT_FBPAR("EXCEEDS MESSAGE2")_FBNUMB_FBPAR("EXCEEDS MESSAGE3")_FBTXT_""".",0)
 +11               DO FORMWRIT("",2)
               End DoDot:1
               IF $$QUESTION(2,FBPARAM("WISH CONTINUE"),FBPARAM("YES OR NO"))'=1
                   QUIT -4
 +12      ;new and set variables
 +13       NEW DIROUT,DUOUT,DTOUT,FBEXIT,FBICDNT
 +14       NEW FBRETV,FBXX,FBLEVEL
 +15       SET FBRETV=""
 +16       SET FBEXIT=0
 +17      ;level 1 stores the original search string
           SET FBLEVEL=1
           SET FBLVTXT(FBLEVEL)=FBTXT
 +18      ; main loop
 +19       FOR 
               if FBEXIT>0
                   QUIT 
               Begin DoDot:1
 +20               KILL FBICDY
 +21      ;get the search string from the current level and call LEX API
 +22               SET FBICDY=$$DIAGSRCH^LEX10CS(FBLVTXT(FBLEVEL),.FBICDY,FBDATE,30)
 +23               if $ORDER(FBICDY(" "),-1)>0
                       SET FBICDY=+FBICDY
 +24      ; Nothing found
 +25               IF +FBICDY'>0
                       SET FBEXIT=1
                       SET FBXX=-1
                       QUIT 
 +26      ; Single match found for partial text search
 +27               IF FBMATCH<0
                       IF FBLEVEL=1
                           IF FBICDY=1
                               SET FBMATCH=0
 +28      ; display the list of items and ask the user to select the item from the list
 +29               SET FBXX=$$SEL^FBASFL(.FBICDY,8)
 +30      ; if ^ was entered 
 +31      ;   if this is on the top level then quit 
 +32               IF FBXX=-2
                       IF FBLEVEL'>1
                           SET FBRETV=-1
                           SET FBEXIT=1
                           QUIT 
 +33      ;   if lower level then go one level up
 +34               IF FBXX=-2
                       IF FBLEVEL>1
                           if FBLEVEL>1
                               SET FBLEVEL=FBLEVEL-1
                           QUIT 
 +35      ; If timeout, or not selected, or ^^ then quit
 +36               IF FBXX=-1
                       SET FBRETV=-1
                       SET FBEXIT=1
                       QUIT 
 +37      ; if Code Found and Selected by the user save selection in FBRETV and quit
 +38               IF $PIECE(FBXX,";")'="99:CAT"
                       SET FBRETV=FBXX
                       SET FBEXIT=1
                       QUIT 
 +39      ; If Category Found and Selected by the user:  
 +40      ;  go to the next inner level
 +41      ;  change level number 
 +42               SET FBLEVEL=FBLEVEL+1
 +43      ;  set the new level with the new search string
 +44      ;  and repeat 
 +45               SET FBLVTXT(FBLEVEL)=$PIECE($PIECE($GET(FBXX),"^"),";",2)
               End DoDot:1
 +46       QUIT FBRETV
 +47      ;
 +48      ; Look-up help for ?
INPHLP    ;
 +1        IF $GET(X)["???"
               DO INPHLP3
               QUIT 
 +2        IF $GET(X)["??"
               DO INPHLP2
               QUIT 
 +3        WRITE !," Enter code or ""text"" for more information."
           QUIT 
 +4        QUIT 
 +5       ;-----------
 +6       ; Look-up help for ??
INPHLP2   ;
 +1        WRITE !," Enter a ""free text"" term or part of a term such as ""femur fracture""."
 +2        WRITE !!," or "
 +3        WRITE !!," Enter a ""classification code"" (ICD/CPT etc) to find the single term"
 +4        WRITE !," associated with the code."
 +5        WRITE !!," or "
 +6        WRITE !!," Enter a ""partial code"". Include the decimal when a search criterion"
 +7        WRITE !," includes 3 characters or more for code searches."
 +8        QUIT 
 +9       ;--------
 +10      ; Look-up help for ???
INPHLP3   ;
 +1        WRITE !," Number of Code Matches"
 +2        WRITE !," ----------------------"
 +3        WRITE !!," The ICD-10 Diagnosis Code search will show the user the number of matches"
 +4        WRITE !," found, indicate if additional characters in ICD code exist, and the number"
 +5        WRITE !," of codes within the category or subcategory that are available for selection."
 +6        WRITE !," For example:"
 +7        WRITE !!," 14 matches found"
 +8        WRITE !!," M91. - Juvenile osteochondrosis of hip and pelvis (19)"
 +9        WRITE !!," This indicates that 14 unique matches or matching groups have been found"
 +10       WRITE !," and will be displayed."
 +11       WRITE !!," M91. - the ""-"" indicates that there are additional characters that specify"
 +12       WRITE !," unique ICD-10 codes available."
 +13       WRITE !!," (19) Indicates that there are 19 additional ICD-10 codes in the M91 ""family"""
 +14       WRITE !," that are possible selections."
 +15       QUIT 
 +16      ;--------
 +17      ;ask YES/NO questions
 +18      ;input parameters :
 +19      ; FBDFLT- 0/null- not default, 1- yes, 2 -no
 +20      ; FBPROM - prompt string
 +21      ; FBHELP - help text
 +22      ;returns 
 +23      ; 2 - no,
 +24      ; 1 -yes,
 +25      ; 0 - no answer (time out)
 +26      ; -3 - ^ or ^^
 +27      ; 0 - no answer
QUESTION(FBDFLT,FBPROM,FBHELP) ;
 +1        NEW DIR
 +2        SET %=$GET(FBDFLT,2)
 +3        SET DIR(0)="Y"
           SET DIR("A")=FBPROM
           SET DIR("B")=$SELECT(%=1:"Yes",%=2:"No",1:"")
 +4        if $LENGTH($GET(FBHELP))
               SET DIR("?")=FBHELP
 +5        DO ^DIR
 +6        if Y["^"
               QUIT -3
 +7        if Y=1
               QUIT 1
 +8        if Y=0
               QUIT 2
 +9        QUIT 0
 +10      ;
 +11      ;------------
 +12      ;get search string
 +13      ;input parameters :
 +14      ; FBPRMT prompt text
 +15      ; FBHLP1 "?" help text
 +16      ; FBHLP2 "??" help text
 +17      ; FBDFLT- default response
 +18      ;returns piece1 ^ piece 2
 +19      ; piece1:
 +20      ; 0 if normal input
 +21      ; or -1 if invalid data
 +22      ; or -2 if time out
 +23      ; or -3 if ^
 +24      ; or -5 if @
 +25      ; piece2: string entered by the user
SRCHSTR(FBPRMT,FBHLP1,FBHLP2,FBDFLT) ;
 +1        NEW DIR
 +2        SET DIR("A")=FBPRMT
 +3        SET DIR("?")=FBHLP1
 +4        SET DIR("??")=FBHLP2
 +5        IF $LENGTH($GET(FBDFLT))
               SET DIR("B")=FBDFLT
 +6        SET DIR(0)="FAOr^0:245"
 +7        DO ^DIR
 +8        if $DATA(DTOUT)
               QUIT -2
 +9        if $DATA(DUOUT)
               QUIT -3
 +10       if X="@"
               QUIT -5
 +11       if Y["^"
               QUIT -3
 +12       if Y=""
               QUIT -1
 +13       QUIT 0_U_Y
 +14      ;
 +15      ;set parameters
 +16      ;input parameters 
 +17      ; FBPAR - local array to sets and store string constants for your messages and prompts 
SETPARAM(FBPAR) ;
 +1        SET FBPAR("ASKDATE")="Date of interest? "
 +2        IF FBPRMPT'[":"
               SET FBPRMPT=FBPRMPT_": "
 +3        SET FBPAR("SEARCH_PROMPT")=FBPRMPT
 +4        SET FBPAR("HELP ?")="^D INPHLP^FBASF"
 +5        SET FBPAR("HELP ??")="^D INPHLP2^FBASF"
 +6        SET FBPAR("NO DATA FOUND")="  No data found"
 +7        SET FBPAR("EXITING")="  Exiting"
 +8        SET FBPAR("TRY LATER")="  Try again later"
 +9        SET FBPAR("NO DATA SELECTED")="  No data selected"
 +10       SET FBPAR("TRY ANOTHER")="Try another"
 +11       SET FBPAR("WISH CONTINUE")="Do you wish to continue(Y/N)"
 +12       SET FBPAR("EXCEEDS MESSAGE1")="Searching for """
 +13       SET FBPAR("EXCEEDS MESSAGE2")=""" requires inspecting "
 +14       SET FBPAR("EXCEEDS MESSAGE3")=" records to determine if they match the search criteria.  This could take quite some time.  Suggest refining the search by further specifying """
 +15       SET FBPAR("NO MATCHES FOUND")="  No matches found??"
 +16       SET FBPAR("ENTER ADM DIAG")="  Enter the admitting diagnosis for this claim."
 +17       SET FBPAR("EXIT NOT ALLOWED")="   Exit not allowed??"
 +18       SET FBPAR("ENTER MORE")=" Please enter at least the first two characters of the ICD-10 code or code"
 +19       SET FBPAR("ENTER MORE2")=" description to start the search."
 +20       SET FBPAR("YES OR NO")="Answer 'Y' for 'Yes' or 'N' for 'No'"
 +21       SET FBPAR("NOTHING DELETED")="  <NOTHING DELETED>"
 +22       SET FBPAR("REQUIRED")="??  Required"
 +23       NEW FBX
           SET FBX=FBPRMPT
 +24      ; remove leading space or control chars.
           FOR 
               if (($EXTRACT(FBX)'=" ")&($EXTRACT(FBX)'?1C))
                   QUIT 
               SET FBX=$EXTRACT(FBX,2,99)
 +25       SET FBPAR("FIELD_NAME")=$PIECE(FBX,":")
 +26       QUIT 
 +27      ;
 +28      ;
 +29      ;a wrapper for ^DIWP
 +30      ;accumulates a text and then writes it to the device
 +31      ;input parameters :
 +32      ; X - text
 +33      ; FBMODE:
 +34      ;  0 - start
 +35      ;  1 - accumulate 
 +36      ;  2 - write
 +37      ;example:
 +38      ;D FORMWRIT^FBASF("this API is a wrapper for ^DIWP, it accumulates a text and then writes it to the device, you can use it in your application code",0)
 +39      ;D FORMWRIT^FBASF("some more text ",1)
 +40      ;D FORMWRIT^FBASF("",2)
FORMWRIT(X,FBMODE) ;
 +1        NEW FBLI1
 +2       ;if "start" mode
 +3        IF FBMODE=0
               KILL ^UTILITY($JOB,"W")
 +4        SET DIWL=1
           SET DIWR=79
 +5        IF $LENGTH(X)>0
               DO ^DIWP
 +6       ;if "write" mode
 +7        IF FBMODE=2
               Begin DoDot:1
 +8                SET FBLI1=0
                   FOR 
                       SET FBLI1=$ORDER(^UTILITY($JOB,"W",1,FBLI1))
                       if +FBLI1=0
                           QUIT 
                       WRITE !,$GET(^UTILITY($JOB,"W",1,FBLI1,0))
 +9                KILL ^UTILITY($JOB,"W")
               End DoDot:1
 +10       QUIT 
 +11      ;
 +12      ; Clean up environment and quit
EXIT      ;
 +1        KILL %,DIC,DIR,DIROUT,DIRUT,DTOUT,DUOUT,X,Y,%Y,FBDT,FBFILE,FBIEN,FBDFLT,FBOUT,FBREQFLD,DXPRMPT,LNNUM,DIWL,DIWR
 +2        QUIT 
 +3       ;