<cfparam name="client.Loc_ID" default ="0">
<cfparam name="form.Pt_SSN_UK" default ="0">
<cfparam name="form.Status" default ="0">
<cfparam name="form.Pt_Longitude" default ="0">
<cfparam name="form.Pt_Latitude" default ="0">
<cfparam name="form.FIPS_Code" default ="0">
<cfparam name="form.CBSA_Code" default ="0">
<cfparam name="form.UrbanRural" default ="0">
<cfparam name="form.WageIndex" default ="0">
<cfparam name="form.Pt_language" default ="">
<cfparam name="form.Interpreter" default ="">
<cfparam name="form.pat_status" default ="0">
<cfparam name="form.unique_Pt_Agy_ID" default ="0">
<cfparam name="form.Pt_Race" default ="">
<cfparam name="form.M0140_ETHNIC" default ="">
<cfparam name="form.otherallergies" default ="">
<cfparam name="form.Pt_SSN" default ="">
<cfparam name="form.Pt_SSN_UK" default ="0">
<cfparam name="form.Home_Directions" default ="">
<cfparam name="form.other_concerns" default ="">
<cfparam name="form.Email" default ="">
<cfparam name="form.Comments" default ="">
<cfparam name="form.Referral_ID" default ="0">
<cfparam name="form.Payer_ID" default ="0">
 
<cfset form.Record_Creation_Date = "#Dateformat(Now(),'yyyy-mm-dd')#" />
<cfset form.Record_Created_By = 0 />
<cfset form.Record_Mod_Date = #Now()# />
<cfset form.Record_Mod_By = 0 />
<cfset DoCommit = 1>
<cfset request.general = CreateObject("component","components.general.general").init()>

<!--- #10449: Never persist PLACEHOLDER unless the user explicitly selected the PLACEHOLDER option --->
<cffunction name="patientFormPlaceholderSelected" output="false" returntype="boolean">
	<cfargument name="fieldName" type="string" required="true">
	<cfif NOT structKeyExists(form, arguments.fieldName)>
		<cfreturn false>
	</cfif>
	<cfset var fieldValue = form[arguments.fieldName]>
	<cfif isSimpleValue(fieldValue)>
		<cfreturn ListFindNoCase(fieldValue, "PLACEHOLDER", ",!") GT 0 OR trim(fieldValue) EQ "PLACEHOLDER">
	</cfif>
	<cfreturn false>
</cffunction>

<cffunction name="stripUnselectedPlaceholderValue" output="false" returntype="string">
	<cfargument name="value" type="string" required="true">
	<cfargument name="placeholderSelected" type="boolean" required="true">
	<cfset var result = trim(arguments.value)>
	<cfif len(result) EQ 0 OR arguments.placeholderSelected>
		<cfreturn result>
	</cfif>
	<cfif result EQ "PLACEHOLDER">
		<cfreturn "">
	</cfif>
	<cfset result = reReplace(result, "(^|[!,])\s*PLACEHOLDER\s*([!,]|$)", "\1\2", "all")>
	<cfset result = reReplace(result, "(^|[!,]){2,}", "\1", "all")>
	<cfset result = reReplace(result, "^[,!\s]+|[,!\s]+$", "", "all")>
	<cfreturn trim(result)>
</cffunction>

<cfif NOT patientFormPlaceholderSelected("Pt_Race")>
	<cfset form.Pt_Race = stripUnselectedPlaceholderValue(form.Pt_Race, false)>
</cfif>
<cfif NOT patientFormPlaceholderSelected("M0140_ETHNIC")>
	<cfset form.M0140_ETHNIC = stripUnselectedPlaceholderValue(form.M0140_ETHNIC, false)>
</cfif>
<cfif NOT patientFormPlaceholderSelected("Allergies")>
	<cfset form.otherallergies = stripUnselectedPlaceholderValue(form.otherallergies, false)>
</cfif>

<cfset manualPatientTextFields = "Pt_First,Pt_Middle,Pt_Last,Pt_Suffix,Pt_Street,Pt_City,Pt_State,Pt_Phone,Pt_Zip,Pt_Gender,Pt_language,Interpreter,Home_Directions,Email,Comments,Pt_SSN,other_concerns">
<cfloop list="#manualPatientTextFields#" index="placeholderField">
	<cfif structKeyExists(form, placeholderField) AND trim(form[placeholderField]) EQ "PLACEHOLDER">
		<cfset form[placeholderField] = "">
	</cfif>
</cfloop>

<cfif form.pat_status EQ 4 >
    <cfset form.status  = form.pat_status />
</cfif>


<cfif form.patid EQ 0 OR form.patid EQ""> 
    <cfset  form.Pt_Zip = form.Pt_Zip1 />
<cfelse>
    <cfset  form.Pt_Zip = form.Pt_Zip1 &'-'&form.Pt_Zip2 />
</cfif>

<cfquery name="getzip" datasource="#Application.DataSrc#">
    select * from zip_cbsa WHERE ZIPCODE = <cfqueryparam VALUE="#Left(form.Pt_Zip,5)#"  cfsqltype="cf_sql_varchar">
</cfquery>
<cfset ZIP_LATITUDE = 0 />
<cfset ZIP_LONGITUDE = 0 />
<cfif getzip.recordcount gt 0 >
    <!---<cfif (form.patid EQ 0 OR form.patid EQ"") AND form.Pt_Latitude EQ '0'> 
        <cfset form.Pt_Latitude = #getzip.LATITUDE# />
        <cfset form.Pt_Longitude = #getzip.LONGITUDE# />
    </cfif>--->
    <cfset ZIP_LATITUDE = #getzip.LATITUDE# />
    <cfset ZIP_LONGITUDE = #getzip.LONGITUDE# />
    <cfset form.FIPS_Code = #getzip.STATEFIPS# & #getzip.COUNTYANSI# />
    <cfset form.CBSA_Code = #getzip.CBSA# />
    <cfset form.UrbanRural = #getzip.UrbanRural# />
    <cfset form.WageIndex = #getzip.WageIndex# />
</cfif>
<cfset id = "agency_"&#session.AgencyID#&"/" />
<cfset filepath = "/var/www/datafiles/Patient_images/"&#id# />

<cfif form.patid EQ 0 OR form.patid EQ""> 
    <cftransaction>
        <cftry> 
            <cfquery  name="getp" datasource="#Application.DataSrc#">
                SELECT IFNULL(MAX(Patient_ID),0)+1 As pPatient_ID FROM #Request.prefix_db_agency#.pPatients 
            </cfquery>
            <cfquery  name="getautop" datasource="#Application.DataSrc#">
                SELECT IFNULL(MAX(AutoPatient_ID),0)+1 As pAutoPatient_ID FROM #Request.prefix_db_agency#.pPatients 
            </cfquery>

            <cfset pPatient_ID =  #getp.pPatient_ID# />
            <cfset pAutoPatient_ID =  #getautop.pAutoPatient_ID# />

            <cfquery name="Insert_patients" datasource="#Application.DataSrc#"  result="result">
               INSERT INTO #Request.prefix_db_agency#.pPatients (Patient_ID,Loc_ID, Status,<cfif form.status EQ 4 >Referral_ID,Payer_ID,</cfif> Pt_Agy_ID, Pt_First, Pt_Middle, Pt_Last, Pt_Suffix, Pt_Street, Pt_City, 
                Pt_State, Pt_Phone, Pt_Zip, Pt_DOB,Pt_Race, M0140_ETHNIC, Pt_Gender, Pt_language,Interpreter, Allergies, 
                Pt_SSN, Pt_SSN_UK, Home_Directions, Specific_Concerns, Email, Comments, Zip_Longitude, Zip_Latitude, Pt_Longitude, Pt_Latitude ,
                FIPS_Code, CBSA_Code, UrbanRural, WageIndex, Record_Creation_Date, Record_Created_By, Record_Mod_Date, 
                Record_Mod_By)
                VALUES
                (   <cfqueryparam VALUE="#pPatient_ID#"  cfsqltype="cf_sql_integer">,
                    <cfqueryparam VALUE="#session.agencyid#"  cfsqltype="cf_sql_integer">,
                    <cfqueryparam VALUE="#form.Status#"  cfsqltype="cf_sql_integer">,
                    <cfif form.status EQ 4 >
                            <cfqueryparam VALUE="#form.Referral_ID#"  cfsqltype="cf_sql_integer">,
                            <cfqueryparam VALUE="#form.Payer_ID#"  cfsqltype="cf_sql_integer">,
                    </cfif>
                    <cfif form.unique_Pt_Agy_ID EQ 1>
                        <cfqueryparam VALUE="#Trim(pAutoPatient_ID)#"  cfsqltype="cf_sql_varchar">,
                    <cfelse>
                        <cfqueryparam VALUE="#Trim(form.Pt_Agy_ID)#"  cfsqltype="cf_sql_varchar">,
                    </cfif>
                    <cfqueryparam VALUE="#Trim(form.Pt_First)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Middle)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Last)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Suffix)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Street)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_City)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_State)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Phone)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Zip)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.Pt_DOB#"  cfsqltype="cf_sql_date">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Race)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.M0140_ETHNIC)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Gender)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_language)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Interpreter)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.otherallergies)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_SSN)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.Pt_SSN_UK#"  cfsqltype="cf_sql_integer">,
                    <cfqueryparam VALUE="#Trim(form.Home_Directions)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.other_concerns)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Email)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Comments)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#ZIP_LONGITUDE#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#ZIP_LATITUDE#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Longitude)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Latitude)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.FIPS_Code)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.CBSA_Code)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.UrbanRural)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.WageIndex)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp">,
                    <cfqueryparam value = "#session.employeeid#" CFSQLType ="CF_SQL_INTEGER">,
                    <cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp">,
                    <cfqueryparam value = "#session.employeeid#" CFSQLType ="CF_SQL_INTEGER">
                )
             </cfquery>
            <cfset enc_patid  = encrypt(#pPatient_ID#,#application.enckey#,"AES","Hex") />

            <cfquery  name="Insert_Coding_request" datasource="#Application.DataSrc#">
                    INSERT INTO #Request.prefix_db_lookup#.Coding_Request  (Agency_ID,Request_Date,Last_Name,First_Name,Assessment_Type,
                     Assessment_Date,Assigned_to,Coding_status,Assigned_by,Request_Time,MHCB_Entered,
                     time_requested,Updated_by) 
                    VALUES ('#session.agencyid#','#DateFormat(now(),"yyyy-mm-dd")#',
                        '#Trim(form.Pt_Last)#','#Trim(form.Pt_First)#',NULL,NULL,'Melissa','eligibility', '#session.employeeid#',
                        '#TimeFormat(now(), "short")#',#now()#,#now()#,'#Session.employeeid#')
            </cfquery>

            <cfif  session.agencyid NEQ '179'>
                <cfsavecontent variable="body_informations">
                    <cfoutput>
                        Hello Team,<br><br>
                        We got a new <b>eligibility request</b> with below information<br>
                        Agency Name   : <b>#getTimeZone.Agency_Name# (#getTimeZone.Agency_Login#)</b><br>
                        Patient Name  : <b>#form.Pt_First#,#form.Pt_Last#</b><br>
                        Requested Date: <b>#DateFormat(now(),"yyyy-mm-dd")# - #TimeFormat(now(), "short")#</b><br><br><br><br>
                        <i>
                            This is an automated system notification from <a href='https://secure.myhomecarebiz.com/index.cfm'>MyHomeCareBiz.com</a>. Therefore, there is no Unsubscribe option.
                        </i>
                        <br><br><br><br><br><br>
                    </cfoutput>
                </cfsavecontent>
                <cfscript>
                    request.general = CreateObject("component","components.general.general").init();
                    getresult = request.general.SendMail(To="billing@myhomecarebiz.com,coding@myhomecarebiz.com,denise.l@myhomecarebiz.com,charmaine.v@myhomecarebiz.com,jordan.d@myhomecarebiz.com,ann.r@myhomecarebiz.com,hedelyn.f@myhomecarebiz.com,jayar.p@myhomecarebiz.com",Subject="Notification for New eligibility Request",Body="'#body_informations#'");
                </cfscript>
            </cfif>

            <cfif getTimeZone.svc_intake_dataentry EQ 1>
                <cfquery  name="Insert_Coding_request" datasource="#Application.DataSrc#">
                        INSERT INTO #Request.prefix_db_lookup#.Coding_Request  (Agency_ID,Request_Date,Last_Name,First_Name,Assessment_Type,
                         Assessment_Date,Assigned_to,Coding_status,Assigned_by,Request_Time,MHCB_Entered,
                         time_requested,Updated_by) 
                        VALUES ('#session.agencyid#','#DateFormat(now(),"yyyy-mm-dd")#',
                            '#Trim(form.Pt_Last)#','#Trim(form.Pt_First)#',NULL,NULL,'Melissa','intake_dataentry', '#session.employeeid#',
                            '#TimeFormat(now(), "short")#',#now()#,#now()#,'#Session.employeeid#')
                </cfquery>
            </cfif>

            <cfcatch>
                <cfset DoCommit = 0>
                <cftransaction action="rollback" />
                <cfdump var="#cfcatch#"><cfabort>
            </cfcatch>
        </cftry>
        <cfif DoCommit EQ 1>
            <cftransaction action="commit" />
        </cfif>            
    </cftransaction>  
<cfelse>
     
    <cftransaction>
     <cftry>

        <cfquery name="getpatients" datasource="#Application.DataSrc#"  result="result">
           SELECT * FROM #Request.prefix_db_agency#.pPatients  
           WHERE Status  IN (0,1,4)
           AND  Patient_ID = <cfqueryparam VALUE="#form.patid#"  cfsqltype="cf_sql_integer">
        </cfquery>
         <cfset form.Zip_Longitude = getpatients.Zip_Longitude />
        <cfset form.Zip_Latitude = getpatients.Zip_Latitude /> 
       <!---  <cfset form.Pt_Longitude = getpatients.Pt_Longitude />
        <cfset form.Pt_Latitude = getpatients.Pt_Latitude /> --->
        <!--- <cfquery name="getzip" datasource="#Application.DataSrc#">
            select * from zip_cbsa WHERE ZIPCODE = <cfqueryparam VALUE="#Left(form.Pt_Zip,5)#"  cfsqltype="cf_sql_varchar">
        </cfquery>
        <cfif getzip.recordcount gt 0 >
            <cfset form.Pt_Latitude = #getzip.LATITUDE# />
            <cfset form.Pt_Longitude = #getzip.LONGITUDE# />
            <cfset form.FIPS_Code = #getzip.STATEFIPS# & #getzip.COUNTYANSI# />
            <cfset form.CBSA_Code = #getzip.CBSA# />
            <cfset form.UrbanRural = #getzip.UrbanRural# />
            <cfset form.WageIndex = #getzip.WageIndex# />
        </cfif> --->

        <cfquery name="update_patients" datasource="#Application.DataSrc#"  result="result">
           UPDATE #Request.prefix_db_agency#.pPatients SET 
           Status   = <cfqueryparam VALUE="3"  cfsqltype="cf_sql_integer">,
           Record_Mod_Date = <cfqueryparam VALUE="#now()#"  cfsqltype="cf_sql_timestamp">,
           Record_Mod_By = <cfqueryparam VALUE="#form.Record_Mod_By#"  cfsqltype="cf_sql_integer">
           WHERE  Loc_ID  = <cfqueryparam VALUE="#session.agencyid#"  cfsqltype="cf_sql_integer">
           AND  Patient_ID = <cfqueryparam VALUE="#form.patid#"  cfsqltype="cf_sql_integer">
        </cfquery>
        <cfset pPatient_ID = form.patid />
        <cfset imagename = #form.patid# & ".jpg" />
        <cfquery name="Insert_patients" datasource="#Application.DataSrc#"  result="result">
               INSERT INTO #Request.prefix_db_agency#.pPatients (Patient_ID,Loc_ID, Status, Pt_Agy_ID,
                Pt_First, Pt_Middle, Pt_Last,
                Pt_Suffix, Pt_Street, Pt_City, Pt_State, Pt_Phone, Pt_Zip, Pt_DOB,Pt_Race, M0140_ETHNIC, Pt_Gender,
                 Pt_language, Interpreter, Allergies, Pt_SSN, Pt_SSN_UK, Home_Directions, Specific_Concerns, Email,
                  Comments, Zip_Longitude, Zip_Latitude, Pt_Longitude, Pt_Latitude,
                   FIPS_Code, CBSA_Code, UrbanRural, WageIndex,FileName, Record_Creation_Date, 
                   Record_Created_By, Record_Mod_Date, Record_Mod_By)
                VALUES
                (   <cfqueryparam VALUE="#pPatient_ID#"  cfsqltype="cf_sql_integer">,
                    <cfqueryparam VALUE="#session.agencyid#"  cfsqltype="cf_sql_integer">,
                    <cfqueryparam VALUE="#form.Status#"  cfsqltype="cf_sql_integer">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Agy_ID)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_First)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Middle)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Last)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Suffix)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Street)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_City)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_State)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Phone)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Zip)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.Pt_DOB#"  cfsqltype="cf_sql_date">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Race)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.M0140_ETHNIC)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_Gender)#"  cfsqltype="cf_sql_varchar">,
                     <cfqueryparam VALUE="#Trim(form.Pt_language)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Interpreter)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.otherallergies)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Pt_SSN)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.Pt_SSN_UK#"  cfsqltype="cf_sql_integer">,
                    <cfqueryparam VALUE="#Trim(form.Home_Directions)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.other_concerns)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#Trim(form.Email)#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.Comments#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.Zip_Longitude#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.Zip_Latitude#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.Pt_Longitude#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.Pt_Latitude#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.FIPS_Code#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.CBSA_Code#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.UrbanRural#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#form.WageIndex#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam VALUE="#imagename#"  cfsqltype="cf_sql_varchar">,
                    <cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp">,
                    <cfqueryparam value = "#session.employeeid#" CFSQLType ="CF_SQL_INTEGER">,
                    <cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp">,
                    <cfqueryparam value = "#session.employeeid#" CFSQLType ="CF_SQL_INTEGER">
                )
        </cfquery>
        
        <!--- Mark EVV records for retransmission if ANY field from General Information page changed --->
        <!--- Compare with previous data from getpatients query (before status was changed to 3) --->
        <!--- Check ALL fields that can be edited on the General Information page --->
        <cfset patientDataChanged = false>
        <cfif getpatients.recordcount GT 0>
            <!--- Normalize values for comparison (handle nulls and empty strings) --->
            <!--- Basic Information --->
            <cfset oldStatus = getpatients.Status>
            <cfset newStatus = form.Status>
            <cfset oldPt_Agy_ID = len(trim(getpatients.Pt_Agy_ID)) ? trim(getpatients.Pt_Agy_ID) : "">
            <cfset newPt_Agy_ID = len(trim(form.Pt_Agy_ID)) ? trim(form.Pt_Agy_ID) : "">
            <cfset oldPt_First = len(trim(getpatients.Pt_First)) ? trim(getpatients.Pt_First) : "">
            <cfset newPt_First = len(trim(form.Pt_First)) ? trim(form.Pt_First) : "">
            <cfset oldPt_Middle = len(trim(getpatients.Pt_Middle)) ? trim(getpatients.Pt_Middle) : "">
            <cfset newPt_Middle = len(trim(form.Pt_Middle)) ? trim(form.Pt_Middle) : "">
            <cfset oldPt_Last = len(trim(getpatients.Pt_Last)) ? trim(getpatients.Pt_Last) : "">
            <cfset newPt_Last = len(trim(form.Pt_Last)) ? trim(form.Pt_Last) : "">
            <cfset oldPt_Suffix = len(trim(getpatients.Pt_Suffix)) ? trim(getpatients.Pt_Suffix) : "">
            <cfset newPt_Suffix = len(trim(form.Pt_Suffix)) ? trim(form.Pt_Suffix) : "">
            
            <!--- Address Information --->
            <cfset oldPt_Street = len(trim(getpatients.Pt_Street)) ? trim(getpatients.Pt_Street) : "">
            <cfset newPt_Street = len(trim(form.Pt_Street)) ? trim(form.Pt_Street) : "">
            <cfset oldPt_City = len(trim(getpatients.Pt_City)) ? trim(getpatients.Pt_City) : "">
            <cfset newPt_City = len(trim(form.Pt_City)) ? trim(form.Pt_City) : "">
            <cfset oldPt_State = len(trim(getpatients.Pt_State)) ? trim(getpatients.Pt_State) : "">
            <cfset newPt_State = len(trim(form.Pt_State)) ? trim(form.Pt_State) : "">
            <cfset oldPt_Zip = len(trim(getpatients.Pt_Zip)) ? trim(getpatients.Pt_Zip) : "">
            <cfset newPt_Zip = len(trim(form.Pt_Zip)) ? trim(form.Pt_Zip) : "">
            <cfset oldPt_Longitude = len(trim(getpatients.Pt_Longitude)) ? trim(getpatients.Pt_Longitude) : "">
            <cfset newPt_Longitude = len(trim(form.Pt_Longitude)) ? trim(form.Pt_Longitude) : "">
            <cfset oldPt_Latitude = len(trim(getpatients.Pt_Latitude)) ? trim(getpatients.Pt_Latitude) : "">
            <cfset newPt_Latitude = len(trim(form.Pt_Latitude)) ? trim(form.Pt_Latitude) : "">
            
            <!--- Contact Information --->
            <cfset oldPt_Phone = len(trim(getpatients.Pt_Phone)) ? trim(getpatients.Pt_Phone) : "">
            <cfset newPt_Phone = len(trim(form.Pt_Phone)) ? trim(form.Pt_Phone) : "">
            <cfset oldEmail = len(trim(getpatients.Email)) ? trim(getpatients.Email) : "">
            <cfset newEmail = len(trim(form.Email)) ? trim(form.Email) : "">
            
            <!--- Personal Information --->
            <cfset oldPt_DOB = getpatients.Pt_DOB>
            <cfset newPt_DOB = form.Pt_DOB>
            <cfset oldPt_Gender = len(trim(getpatients.Pt_Gender)) ? trim(getpatients.Pt_Gender) : "">
            <cfset newPt_Gender = len(trim(form.Pt_Gender)) ? trim(form.Pt_Gender) : "">
            <cfset oldPt_SSN = len(trim(getpatients.Pt_SSN)) ? trim(getpatients.Pt_SSN) : "">
            <cfset newPt_SSN = len(trim(form.Pt_SSN)) ? trim(form.Pt_SSN) : "">
            <cfset oldPt_SSN_UK = getpatients.Pt_SSN_UK>
            <cfset newPt_SSN_UK = form.Pt_SSN_UK>
            
            <!--- Medical/Demographic Information --->
            <cfset oldPt_Race = len(trim(getpatients.Pt_Race)) ? trim(getpatients.Pt_Race) : "">
            <cfset newPt_Race = len(trim(form.Pt_Race)) ? trim(form.Pt_Race) : "">
            <cfset oldM0140_ETHNIC = len(trim(getpatients.M0140_ETHNIC)) ? trim(getpatients.M0140_ETHNIC) : "">
            <cfset newM0140_ETHNIC = len(trim(form.M0140_ETHNIC)) ? trim(form.M0140_ETHNIC) : "">
            <cfset oldPt_language = len(trim(getpatients.Pt_language)) ? trim(getpatients.Pt_language) : "">
            <cfset newPt_language = len(trim(form.Pt_language)) ? trim(form.Pt_language) : "">
            <cfset oldInterpreter = len(trim(getpatients.Interpreter)) ? trim(getpatients.Interpreter) : "">
            <cfset newInterpreter = len(trim(form.Interpreter)) ? trim(form.Interpreter) : "">
            <cfset oldAllergies = len(trim(getpatients.Allergies)) ? trim(getpatients.Allergies) : "">
            <cfset newAllergies = len(trim(form.otherallergies)) ? trim(form.otherallergies) : "">
            <cfset oldSpecific_Concerns = len(trim(getpatients.Specific_Concerns)) ? trim(getpatients.Specific_Concerns) : "">
            <cfset newSpecific_Concerns = len(trim(form.other_concerns)) ? trim(form.other_concerns) : "">
            
            <!--- Other Information --->
            <cfset oldHome_Directions = len(trim(getpatients.Home_Directions)) ? trim(getpatients.Home_Directions) : "">
            <cfset newHome_Directions = len(trim(form.Home_Directions)) ? trim(form.Home_Directions) : "">
            <cfset oldComments = len(trim(getpatients.Comments)) ? trim(getpatients.Comments) : "">
            <cfset newComments = len(trim(form.Comments)) ? trim(form.Comments) : "">
            
            <!--- Compare ALL fields from General Information page --->
            <!--- If ANY field changed, set patientDataChanged to true --->
            <cfif 
                newStatus NEQ oldStatus OR
                newPt_Agy_ID NEQ oldPt_Agy_ID OR
                newPt_First NEQ oldPt_First OR
                newPt_Middle NEQ oldPt_Middle OR
                newPt_Last NEQ oldPt_Last OR
                newPt_Suffix NEQ oldPt_Suffix OR
                newPt_Street NEQ oldPt_Street OR
                newPt_City NEQ oldPt_City OR
                newPt_State NEQ oldPt_State OR
                newPt_Zip NEQ oldPt_Zip OR
                newPt_Longitude NEQ oldPt_Longitude OR
                newPt_Latitude NEQ oldPt_Latitude OR
                newPt_Phone NEQ oldPt_Phone OR
                newEmail NEQ oldEmail OR
                (isDefined("newPt_DOB") AND isDefined("oldPt_DOB") AND DateCompare(newPt_DOB, oldPt_DOB) NEQ 0) OR
                newPt_Gender NEQ oldPt_Gender OR
                newPt_SSN NEQ oldPt_SSN OR
                newPt_SSN_UK NEQ oldPt_SSN_UK OR
                newPt_Race NEQ oldPt_Race OR
                newM0140_ETHNIC NEQ oldM0140_ETHNIC OR
                newPt_language NEQ oldPt_language OR
                newInterpreter NEQ oldInterpreter OR
                newAllergies NEQ oldAllergies OR
                newSpecific_Concerns NEQ oldSpecific_Concerns OR
                newHome_Directions NEQ oldHome_Directions OR
                newComments NEQ oldComments
            >
                <cfset patientDataChanged = true>
            </cfif>
        </cfif>
        
        <!--- Mark EVV for retransmission and re-queue automation when patient data changed (#10425) --->
        <cfset evvManager = createObject("component", "components.evv.EVVStatusManager").init()>
        <cfif patientDataChanged AND evvManager.agencyUsesEVVAutomation(val(session.agencyid))>
            <cftry>
                <!--- #10425: Re-queue automated EVV transmit + status polling on patient update --->
                <cfset evvPatientAuto = createObject("component", "components.evv.EVVPatientAutomation").init()>
                <cfset evvQueueResult = evvPatientAuto.requeuePatientClientOnDataChange(
                    patientID = pPatient_ID,
                    agencyID = val(session.agencyid),
                    triggerSource = "patient_update",
                    createdBy = val(session.employeeid)
                )>
                <cflog file="patient_action" text="Patient #pPatient_ID# data changed - EVV requeue queued=#evvQueueResult.queued# message=#evvQueueResult.message#">
                <cfcatch>
                    <!--- Log error but don't fail the patient update --->
                    <cflog file="patient_action" text="Error re-queueing EVV for patient #pPatient_ID#: #cfcatch.message# - #cfcatch.detail#">
                </cfcatch>
            </cftry>
        <cfelse>
            <cflog file="patient_action" text="Patient #pPatient_ID# saved but no EVV-related changes detected">
        </cfif>
        
        <cfif isdefined('Form.ufFile') and Form.ufFile neq ''>
            <cfparam name="cffile.serverFile" default="" />
              

                <cffile action="upload" filefield="Form.ufFile"  destination="#filepath##form.patid#.jpg" nameconflict="overwrite"  >
                <cffile action="readbinary" variable="fileContent" file="#filepath##File.ServerFile#">
            <!---   <cfif File.FileSize gt 50600>   
                    <cfset Form.ufFile="" />
                    <script language='javascript' type='text/javascript'>
                        bootbox.alert('File size exceeds 40kb');
                        window.location.href ="/patientadmin_new/index.cfm?page=patient&ID=<cfoutput>#session.cs.enc_patientid#</cfoutput>&cat=GeneralInformation";
                    </script>
                <cfelse>--->
                    
                         <cfquery name='file_upload' datasource='#Application.DataSrc#'>
                            UPDATE IGNORE  #Request.prefix_db_agency#.pPatients SET
                                FileName = '#form.patid#.jpg'
                            WHERE Patient_ID = <cfqueryparam VALUE="#form.patid#"  cfsqltype="cf_sql_integer">
                            AND Status   != <cfqueryparam VALUE="3"  cfsqltype="cf_sql_integer">
                        </cfquery>
            <!---</cfif>--->
            </cfif>
        <cfcatch>
            <cfset DoCommit = 0>
            <cftransaction action="rollback" />
        </cfcatch>
    </cftry>
    <cfif DoCommit EQ 1>
        <cftransaction action="commit" />
    </cfif>            
</cftransaction>
 <cfset enc_patid  = encrypt(#form.patid#,#application.enckey#,"AES","Hex") />
</cfif>
  <!--- <cfif CGI.REMOTE_ADDR EQ '103.143.7.161' AND form.patid NEQ 0 AND isDefined("getpatients") AND getpatients.recordcount GT 0>
    <cfoutput>
      <h2>Patient Data Comparison - Patient ID: #form.patid#</h2>
      <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse; font-family: Arial, sans-serif; font-size: 12px;">
        <tr style="background-color: ##f0f0f0; font-weight: bold;">
          <th>Field</th>
          <th>Old Value</th>
          <th>New Value</th>
          <th>Changed</th>
        </tr>
        <tr>
          <td>Status</td>
          <td>#oldStatus#</td>
          <td>#newStatus#</td>
          <td><cfif newStatus NEQ oldStatus>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_Agy_ID</td>
          <td>#oldPt_Agy_ID#</td>
          <td>#newPt_Agy_ID#</td>
          <td><cfif newPt_Agy_ID NEQ oldPt_Agy_ID>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_First</td>
          <td>#oldPt_First#</td>
          <td>#newPt_First#</td>
          <td><cfif newPt_First NEQ oldPt_First>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_Middle</td>
          <td>#oldPt_Middle#</td>
          <td>#newPt_Middle#</td>
          <td><cfif newPt_Middle NEQ oldPt_Middle>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_Last</td>
          <td>#oldPt_Last#</td>
          <td>#newPt_Last#</td>
          <td><cfif newPt_Last NEQ oldPt_Last>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_Suffix</td>
          <td>#oldPt_Suffix#</td>
          <td>#newPt_Suffix#</td>
          <td><cfif newPt_Suffix NEQ oldPt_Suffix>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_Street</td>
          <td>#oldPt_Street#</td>
          <td>#newPt_Street#</td>
          <td><cfif newPt_Street NEQ oldPt_Street>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_City</td>
          <td>#oldPt_City#</td>
          <td>#newPt_City#</td>
          <td><cfif newPt_City NEQ oldPt_City>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_State</td>
          <td>#oldPt_State#</td>
          <td>#newPt_State#</td>
          <td><cfif newPt_State NEQ oldPt_State>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_Zip</td>
          <td>#oldPt_Zip#</td>
          <td>#newPt_Zip#</td>
          <td><cfif newPt_Zip NEQ oldPt_Zip>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_Longitude</td>
          <td>#oldPt_Longitude#</td>
          <td>#newPt_Longitude#</td>
          <td><cfif newPt_Longitude NEQ oldPt_Longitude>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_Latitude</td>
          <td>#oldPt_Latitude#</td>
          <td>#newPt_Latitude#</td>
          <td><cfif newPt_Latitude NEQ oldPt_Latitude>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_Phone</td>
          <td>#oldPt_Phone#</td>
          <td>#newPt_Phone#</td>
          <td><cfif newPt_Phone NEQ oldPt_Phone>YES</cfif></td>
        </tr>
        <tr>
          <td>Email</td>
          <td>#oldEmail#</td>
          <td>#newEmail#</td>
          <td><cfif newEmail NEQ oldEmail>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_DOB</td>
          <td>#oldPt_DOB#</td>
          <td>#newPt_DOB#</td>
          <td><cfif isDefined("newPt_DOB") AND isDefined("oldPt_DOB") AND DateCompare(newPt_DOB, oldPt_DOB) NEQ 0>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_Gender</td>
          <td>#oldPt_Gender#</td>
          <td>#newPt_Gender#</td>
          <td><cfif newPt_Gender NEQ oldPt_Gender>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_SSN</td>
          <td>#oldPt_SSN#</td>
          <td>#newPt_SSN#</td>
          <td><cfif newPt_SSN NEQ oldPt_SSN>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_SSN_UK</td>
          <td>#oldPt_SSN_UK#</td>
          <td>#newPt_SSN_UK#</td>
          <td><cfif newPt_SSN_UK NEQ oldPt_SSN_UK>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_Race</td>
          <td>#oldPt_Race#</td>
          <td>#newPt_Race#</td>
          <td><cfif newPt_Race NEQ oldPt_Race>YES</cfif></td>
        </tr>
        <tr>
          <td>M0140_ETHNIC</td>
          <td>#oldM0140_ETHNIC#</td>
          <td>#newM0140_ETHNIC#</td>
          <td><cfif newM0140_ETHNIC NEQ oldM0140_ETHNIC>YES</cfif></td>
        </tr>
        <tr>
          <td>Pt_language</td>
          <td>#oldPt_language#</td>
          <td>#newPt_language#</td>
          <td><cfif newPt_language NEQ oldPt_language>YES</cfif></td>
        </tr>
        <tr>
          <td>Interpreter</td>
          <td>#oldInterpreter#</td>
          <td>#newInterpreter#</td>
          <td><cfif newInterpreter NEQ oldInterpreter>YES</cfif></td>
        </tr>
        <tr>
          <td>Allergies</td>
          <td>#oldAllergies#</td>
          <td>#newAllergies#</td>
          <td><cfif newAllergies NEQ oldAllergies>YES</cfif></td>
        </tr>
        <tr>
          <td>Specific_Concerns</td>
          <td>#oldSpecific_Concerns#</td>
          <td>#newSpecific_Concerns#</td>
          <td><cfif newSpecific_Concerns NEQ oldSpecific_Concerns>YES</cfif></td>
        </tr>
        <tr>
          <td>Home_Directions</td>
          <td>#oldHome_Directions#</td>
          <td>#newHome_Directions#</td>
          <td><cfif newHome_Directions NEQ oldHome_Directions>YES</cfif></td>
        </tr>
        <tr>
          <td>Comments</td>
          <td>#oldComments#</td>
          <td>#newComments#</td>
          <td><cfif newComments NEQ oldComments>YES</cfif></td>
        </tr>
        <tr style="background-color: ##e0e0e0; font-weight: bold;">
          <td colspan="3">Patient Data Changed</td>
          <td>#patientDataChanged#</td>
        </tr>
      </table>
    </cfoutput>
    <cfabort>
</cfif> --->
<cfif CGI.REMOTE_ADDR EQ '103.143.7.161'>
<!--- <cfabort> --->
</cfif>
<cfif form.Status EQ '2'>
    <cflocation url="#application.siteurl#/patientadmin_new/index.cfm?aMenu=clients&page=search&mmid=2" addtoken="no">   
</cfif>
<cfquery name="getcontact" datasource="#Application.DataSrc#">
      SELECT * FROM #Request.prefix_db_agency#.pPtContacts 
      WHERE Patient_ID =  <cfqueryparam VALUE="#pPatient_ID#"  cfsqltype="cf_sql_integer">
</cfquery>

<cfif getcontact.recordcount EQ 0 AND  form.Status LT 2>
    <cflocation url="#application.siteurl#/patientadmin_new/index.cfm?page=patient&ID=#enc_patid#&cat=contactaction&CID=0&message=At least one Emergency Contact is required. Please complete below." addtoken="no">
<cfelse>  
    <cflocation url="#application.siteurl#/patientadmin_new/index.cfm?page=patient&ID=#enc_patid#&cat=GeneralInformation" addtoken="no">   
</cfif>


 