<cfif Not isDefined('session.AgencyId') or session.AgencyId eq ''>

 <div style='margin:20px;'>
  Your session has Timed Out.
   <br/><br/>
  Please <a href='/'>Sign In</a> again to continue.
 </div>
 <cfexit>
</cfif>


<cfif IsDefined("form.pid")>
	<cfif #form.pid# EQ 0>
		<h1>The information you provided in the form was incomplete or incorrect.  Please take a moment to review the following list:<br /></h1>  Patient Id Is Missing. 
 <br />
	 <FORM> 
	  <INPUT type="button" value="Go Back" onClick="history.back()"> 
	 </FORM>  
	 <cfabort>
	</cfif>
</cfif>
<!--- Calculate EClaim and QB Totals --->

<cfset form.Total_EPay = 00.00 />
<cfset form.Total_Units = 0 />
<cfset form.Total_EClaim = 0 />
<cfset form.Total_QB = 0 />

<cfparam name="form.EmpPay_ID" default="" />
<cfparam name="url.close" default=-1 />

<cfparam name="url.thedate" default='' />
<cfparam name="form.thedate" default='' />



<cfif NOT isDefined("form.Rev_CodeID")>
 <h2>Your Session has timed out. Please Login again.<br /></h2> 
 <FORM> 
  <INPUT type="button" value="Go Back" onClick="history.back()"> 
 </FORM> 
 <cfelseif form.Rev_CodeID eq 0>
 <h1>The information you provided in the form was incomplete or incorrect.  Please take a moment to review the following list:<br /></h1> 
 Revenue Code is required. 
 <br />
 <FORM> 
  <INPUT type="button" value="Go Back" onClick="history.back()"> 
 </FORM> 
 <cfabort>
<cfelse>

<cfset Session.CS.AssessmentTrackerID=#form.Assessment_Tracker_ID#  />
<cfif Session.CS.AssessmentTrackerID eq "0" or Session.CS.AssessmentTrackerID eq "">
	<cfquery name="GetAST" datasource="#Application.DataSrc#">
		 select Assessment_Tracker_ID from Assessment_New where Assessment_ID='#form.Assessment_ID#'
	</cfquery>
	<cfset Session.CS.AssessmentTrackerID=#GetAST.Assessment_Tracker_ID# /> 
</cfif>

<cfset Session.CS.AssessmentID=#form.Assessment_ID#  />



<cfquery name="GetRev" datasource="#Application.DataSrc#">
	SELECT Rev_CodeId, EClaim_Price, Quick_Books_Price, HCPCS, Rev_Code, Visit_Type, Measure, Description
	FROM   #Request.prefix_db_agency#.Revenue_Code 
	WHERE  Rev_CodeId = '#form.Rev_CodeId#'
	AND    Agency_Id = '#session.AgencyId#'
</cfquery>

<!--- JDB: Always check for GetRev, not just in ADD Mode...
<cfif form.scheduleitems_id eq 0>
--->
<cfif GetRev.RecordCount eq 1>
 <cfif isdefined("session.rollover") and  session.rollover eq 1 and isdefined("session.Visit_Type")>
  <cfset form.visit_type = #session.Visit_Type# />
 <cfelse>
  <cfset form.visit_type = #getrev.visit_type# />
 </cfif>
<cfelse>
 <cfset form.visit_type = #form.visit_type# />
</cfif>

<cfif len(trim(form.visit_type)) eq 0>
	<cfquery name="GetVT" datasource="#Application.DataSrc#">
		SELECT Assessment_Reason_M0100 as vt
		FROM   #Request.prefix_db_agency#.Assessment_New 
		WHERE  assessment_tracker_id = '#Session.CS.AssessmentTrackerID#'
		AND    assessment_id = '#Session.CS.AssessmentID#'
		AND   (Agency_ID = '#session.AgencyID#' or Agency_ID = 1)
	</cfquery>
	<cfif GetVT.recordcount><cfset form.visit_type=#getvt.vt# /></cfif>
</cfif>

<cfquery name="GetEPay" datasource="#Application.DataSrc#">
	SELECT Pay_Rate, Measure 
	FROM   #Request.prefix_db_agency#.Employee_Payroll 
	WHERE  Agency_ID = '#session.AgencyID#' 
	AND    EmpPay_ID = '#form.EmpPay_ID#'
</cfquery>

<cfquery name="GetOutput" datasource="#Application.DataSrc#">
	SELECT Output_Type FROM #Request.prefix_db_agency#.Payer WHERE Agency_ID = #session.AgencyID# AND Payer_ID = '#form.Payer_ID#'
</cfquery>

<cfif #form.StartTime# IS NOT "" AND #form.EndTime# IS NOT "">

 <cfif form.StartTimeAMPM eq 'AM' and LEFT(form.StartTime, 3) eq '12:'>
  <cfset form.StartTime = '00:' & Right(form.StartTime, 2) />
 </cfif>
 <cfif form.EndTimeAMPM eq 'AM' and LEFT(form.EndTime, 3) eq '12:'>
  <cfset form.EndTime = '00:' & Right(form.EndTime, 2) />
 </cfif>

 <!--- Determine number of minutes (n) apart --->
 <cfset diff = #DateDiff("n", form.StartTime, form.EndTime)# />
 <!--- Calculate number of units (4 per hour) --->
 <cfset form.Total_Units = (#diff#/60)*100/25 />
 <cfif Form.Total_Units lt 0>
  <cfset Form.Total_Units=Form.Total_Units+96 />
 </cfif>
 <cfset form.Total_Hours = (#diff#/15)*25/100 />
  <cfif form.Total_Hours lt 0>
   <cfset form.Total_Hours = form.Total_Hours + 24 />
  </cfif>
 <cfif form.Rev_CodeID NEQ '' or form.Rev_CodeID NEQ 0>
  <cfif #GetRev.Measure# IS "1">
   <cfset form.Total_EClaim = #Total_Hours# * #GetRev.EClaim_Price# />
   <cfset form.Total_QB = #Total_Hours# * #GetRev.Quick_Books_Price# />
  </cfif>
  <cfif #GetRev.Measure# IS ".25">
   <cfset form.Total_EClaim = #Total_Units# * #GetRev.EClaim_Price# />
   <cfset form.Total_QB = #Total_Units# * #GetRev.Quick_Books_Price# />
  </cfif>
  <cfif #GetRev.Measure# IS "visit">
   <cfset form.Total_EClaim = 1 * #GetRev.EClaim_Price# />
   <cfset form.Total_QB = 1 * #GetRev.Quick_Books_Price# />
  </cfif>
 </cfif>
 <cfif #form.EmpPay_ID# IS NOT 0>
  <cfif #GetEPay.Measure# IS "visit">
   <cfset form.Total_EPay = 1 * #GetEPay.Pay_Rate# />
  </cfif>
  <cfif #GetEPay.Measure# IS "hourly">
   <cfset form.Total_EPay = #Total_Hours# * #GetEPay.Pay_Rate# />
  </cfif>
  <cfif #GetEPay.Measure# IS "salary">
   <cfset form.Total_EPay = 00.00 />
  </cfif>
 </cfif>
<cfelse>
	<cfset form.Total_EPay = 00.00 />
	<cfset form.Total_Units = 0 />
	<cfset form.Total_EClaim = 0 />
	<cfset form.Total_QB = 0 />
</cfif>
<cfif #GetOutput.Output_Type# IS "Medicare">
  <cfif #form.Visit_Type# IS '1 - Start of care - further visits planned' OR #form.Visit_Type# IS '4 - Recertification (follow-up) reassessment'  OR #form.Visit_Type# IS 'Assessment' >
    <cfset form.Output_Type = 'RAP' />
  <cfelse>
    <cfset form.Output_Type = #GetOutput.Output_Type# />
  </cfif>
<cfelse>
 <cfset form.Output_Type = #GetOutput.Output_Type# />
</cfif>
<cfif #GetRev.Rev_CodeID# IS NOT "">
  <cfset form.Rev_Code = #GetRev.Rev_Code# />
  <cfset form.HCPCS = #GetRev.HCPCS# />
  <cfset form.Bill_Type = #GetRev.Description# />
</cfif>

<cfif #form.Paperwork# IS NOT "">
  <cfset Paperwork = (#form.Paperwork#/15)*25/100 />
  <cfset form.Paperwork = Paperwork />
<cfelse>
  <cfset Paperwork = 0 />
  <cfset form.Paperwork = Paperwork />
</cfif>
<cfif #form.Travel# IS NOT "">
   <cfset Travel = (#form.Travel#/15)*25/100 />
   <cfset form.Travel = Travel />
<cfelse>
  <cfset Travel = 0 />
   <cfset form.Travel = Travel />
</cfif>

<cfif #parameterexists(Form.PayMileage)# >
	<cfset form.PayMileage = #form.paymileage# />
<cfelse>
	<cfset form.PayMileage = 0 />
</cfif>

<cfif form.scheduleitems_id eq 0>
 <cfset form.Record_Created_By = #Session.EmployeeID# />
 <cfset form.Record_Creation_Date = #Now()# />
</cfif>

<cfset form.Record_Mod_By = #Session.EmployeeID# />
<cfset form.Record_Mod_Date = #Now()# />
<cfset Payer_ID = #form.Payer_ID# />

<!--- format the start and end times --->

<cfset thehour = GetToken(form.StartTime,1,':') />
<cfset theminute = GetToken(form.StartTime,2,':') />
<cfset form.starttime = "#thehour#:#theminute#" />
<cfset thehour = GetToken(form.EndTime,1,':') />
<cfset theminute = GetToken(form.EndTime,2,':') />
<cfset form.endtime = "#thehour#:#theminute#" />


		<cfif form.scheduleitems_id eq 0>
		<cfquery name="InsertSchedule" datasource="#Application.DataSrc#">
			<!--- <cftransaction> --->
			INSERT INTO #Request.prefix_db_agency#.ScheduleItems (
			Patient_ID, Visit_Date, Employee_ID, <cfif isdefined('form.EmpPay_ID') and form.EmpPay_ID neq ''>EmpPay_ID,</cfif> Record_creation_Date, Record_created_By, Record_Mod_Date, Record_Mod_By
			,Agency_ID, Visit_Type, Assessment_Tracker_ID,Assessment_ID, Progress_Note,Payer_ID, StartTime, EndTime, Rev_CodeID,
			Paperwork,Travel,Mileage,Telephony_Yes,Total_Units,Total_EClaim,Total_QB,Visit_Notes,Missed,PayMileage,
			<cfif isdefined('Form.Bill_Type')>
			Bill_Type,
			</cfif>
			Rev_Code,HCPCS,Output_Type,Instructions
			) 
			VALUES 
			(
			'#Session.CS.PatientID#', #CreateODBCDateTime(form.Visit_Date)#,'#form.Employee_ID#', <cfif isdefined('form.EmpPay_ID') and form.EmpPay_ID neq ''>'#form.EmpPay_ID#',</cfif>#Now()#, 
			'#form.Employee_ID#', #Now()#,'#form.Employee_ID#','#Session.AgencyID#','#Form.Visit_Type#',
			'#Session.CS.AssessmentTrackerID#','#Session.CS.AssessmentID#','#Form.Progress_Note#','#Form.Payer_ID#', 
			#CreateODBCTime(Form.StartTime)#, #CreateODBCTime(Form.EndTime)#, '#Form.Rev_CodeID#','#Form.Paperwork#', 
			'#Form.Travel#','#Form.Mileage#','#Form.Telephony_Yes#','#form.Total_Units#','#form.Total_EClaim#','#form.Total_QB#',
		    '#Form.Visit_Notes#','#Form.Missed#','#Form.PayMileage#',<cfif isdefined('Form.Bill_Type')>'#Form.Bill_Type#',</cfif>'#Form.Rev_Code#','#form.HCPCS#',
			'#form.Output_Type#','#Form.Visit_Instructions#')
			</cfquery>
			<cfquery datasource="#Application.DataSrc#" name="upd">
		select MAX(scheduleitems_id) as maxid from #Request.prefix_db_agency#.ScheduleItems where agency_id = '#session.agencyid#' and employee_id = '#form.employee_id#'
		</cfquery>

	    <cfset form.scheduleitems_id = #upd.maxid# />

			<cfelse>
			
			<cfquery name="UpdateSchedule" datasource="#Application.DataSrc#">
			  Update #Request.prefix_db_agency#.ScheduleItems set 
<!---			  Patient_ID=#Session.CS.PatientID#,
 --->			  Visit_Date=#CreateODBCDateTime(form.Visit_Date)#,
			  Employee_ID='#form.Employee_ID#',
			  Payer_ID='#Form.Payer_ID#',
			  EmpPay_ID='#Form.EmpPay_ID#',
			  Record_creation_Date=#Now()#,
			  Record_created_By='#form.Employee_ID#',
			  Record_Mod_Date=#Now()#,
			  Record_Mod_By='#form.Employee_ID#',
			  Agency_ID='#Session.AgencyID#',
			  Visit_Type='#Form.Visit_Type#',
			  Output_Type='#form.Output_Type#',
			  Instructions='#Form.Visit_Instructions#',
<!---			  Assessment_Tracker_ID='#Session.CS.AssessmentTrackerID#',
			  Assessment_ID='#Session.CS.AssessmentID#',			  
 --->			
			  Assessment_Tracker_Id = <cfif Session.CS.AssessmentTrackerID eq ''>0<cfelse>'#Session.CS.AssessmentTrackerID#'</cfif>,
			  Assessment_Id = '#Form.Assessment_Id#',
 Progress_Note='#Form.Progress_Note#',
			  StartTime=#CreateODBCTime(Form.StartTime)#,
			  EndTime=#CreateODBCTime(Form.EndTime)#,
			  Rev_CodeID='#Form.Rev_CodeID#',
			  Paperwork='#Form.Paperwork#',
			  Travel='#Form.Travel#'
			  ,Mileage='#Form.Mileage#'
               <cfif isdefined("form.Telephony_Yes")>
			  ,Telephony_Yes='#Form.Telephony_Yes#'
              </cfif>
			  ,Visit_Notes='#Form.Visit_Notes#'
			  ,Total_Units='#form.Total_Units#'
			  ,Total_EClaim='#form.Total_EClaim#'
			  ,Total_QB='#form.Total_QB#'
              <cfif isdefined("form.Missed")>
			  ,Missed='#form.Missed#'
               </cfif>
			  ,PayMileage='#Form.PayMileage#'
			  <cfif isdefined("form.Bill_Type")>
			  ,Bill_Type='#form.Bill_Type#'
			  </cfif>			  
			  ,Rev_Code='#Rev_Code#'
			  ,HCPCS='#form.HCPCS#'
			  where ScheduleItems_Id = '#Form.ScheduleItems_Id#'
			</cfquery>
			
		</cfif>

<cf_telephonyid result="itemfinal" action="updateitem" employeeid="#form.employee_id#" visitdate=#form.visit_date# id=#form.scheduleitems_id#> 
<cf_telephonyid result="encodefinal" action="encode" employeeid="#form.employee_id#" visitdate=#form.visit_date# item=#itemfinal#> 

<cfset siti = form.scheduleitems_id />
 <cfif LEN(siti) eq 7>
  <cfset siti = '000' & siti />
 <cfelseif LEN(siti) eq 8>
  <cfset siti = '00' & siti />
 <cfelseif LEN(siti) eq 9>
  <cfset siti = '0' & siti />
 </cfif>
<cfquery datasource="#Application.DataSrc#" name="upd">
 update #Request.prefix_db_agency#.ScheduleItems set telephony_id = '#siti#' where scheduleitems_id = '#form.scheduleitems_id#'
</cfquery>

<cfif url.close eq 1>
<cfif isdefined('form.frm') and form.frm eq 'schedules' and isdefined('form.view') and form.view eq 'day'>
<cflocation url="/schedules/index.cfm?view=day&mmid=3" addtoken="no">
<cfelseif isdefined('form.frm') and form.frm eq 'schedules' and isdefined('form.view') and form.view eq 'week'>
<cflocation url="/schedules/index.cfm?page=schedules&cat=&rpt=&view=week" addtoken="no">
<cfelseif isdefined('form.frm') and form.frm eq 'schedules' and isdefined('form.view') and form.view eq 'month'>
<cflocation url="/schedules/index.cfm?page=schedules&cat=&rpt=&view=month" addtoken="no">
<cfelse>
<cflocation url="/patientadmin_new/index.cfm?page=patient&ID=#form.pid#&cat=Patient" addtoken="no">
<!---<cflocation url="/managementreports/index.cfm?page=reports&cat=patients&rpt=Payroll_Codes&Action=Res" addtoken="no">--->
</cfif>

<cfelseif url.close eq 0>
	<script>
	//var agt=navigator.userAgent.toLowerCase();
	//if (agt.indexOf("msie") != -1) {
		
		<cfif findnocase('QB_compliancebyweek',form.previous)>
		
		  <!--- 1.1 - Parse the querystring elements (if any) from the FORM.previous URL to see if the "d" URL variable
			    is already in there. --->		  
		  <cfif FindNoCase('?', FORM.previous) GT 0>
		  
		    <!--- 1.1 - Set some variables with the URL & querystring --->
		    <cfset VARIABLES.DestinationURL = GetToken(FORM.previous, 1, '?') & '?' />
		    <cfset VARIABLES.URLQuerystring = GetToken(FORM.previous, 2, '?') />
		    
		    <!--- 1.1 - Loop through the querystring elements... --->
		    <cfloop list="#VARIABLES.URLQuerystring#" index="thisQuerystringItem" delimiters="&">
		    
		      <!--- 1.1 - Parse the values at the equal sign to separate query variable name from value. --->
		      <cfset VARIABLES.thisVarName = GetToken(thisQuerystringItem, 1, '=') />
		      <cfset VARIABLES.thisVarValue = GetToken(thisQuerystringItem, 2, '=') />
		      
		      <!--- 1.1 - If this is the "d" (ie. date) value, we'll replace it with the date that was submitted in the form. --->
		      <cfif VARIABLES.thisVarName IS "d">

<!--- JDB: Don't do this here b/c it does not appear to work... do it "my way" instead...
			   <cfset VARIABLES.DestinationURL = VARIABLES.DestinationURL & '&d=' & FORM.vd />
--->

		      <!--- 1.1 - If this isn't the "d" variable, we'll just ignore it (and append it to our destination URL) --->
		      <cfelse>
			   <cfset VARIABLES.DestinationURL = VARIABLES.DestinationURL & '&' & thisQuerystringItem />
		      </cfif>
		    
		    </cfloop>
		    
		  <!--- 1.1 - If there's no question mark, then we don't have any querystring variables to deal with. --->
		  <cfelse>
		    <cfset VARIABLES.DestinationURL = FORM.previous />		    
		  </cfif>
		  
		  <!--- append the date if it is not empty... --->

		  <cfif url.thedate neq ''>
			<cfset VARIABLES.DestinationURL = VARIABLES.DestinationURL & '&d=' & url.thedate />
		  <cfelseif form.thedate neq ''>
			<cfset VARIABLES.DestinationURL = VARIABLES.DestinationURL & '&d=' & form.thedate />
		  </cfif>
		  
		  <!--- <cfoutput>window.location='#form.previous#&d=#form.vd#';</cfoutput> --->
		  <cfoutput>window.location='#VARIABLES.DestinationURL#';</cfoutput>

		<cfelse>
		  <cfoutput>window.location='#form.previous#';</cfoutput>
		</cfif>
	/*} else {
		history.back(0);
	}*/
	</script>
<cfelse>
<!---<cfwindow 
    name="VisitUpdated"   
    title="New Visit - Added"
    height="500"     
    width="800"  
    resizable="false"     
    center="true" 
    closable="false" 
    draggable="true"     
    modal="true"   
    bodystyle="background-color:white;"	
	initshow="true">
	<table border="0" cellspacing="0" cellpadding="0" width="98%" align="center">
	<tr><td colspan="3" style="padding-top:5px; padding-right:2px" align="right" >
	<img src="../../images/vizard_close.png" style="cursor:pointer;" onclick="secureHREF('/index.cfm?view=day&mmid=3');" name="imgclose" id="imgclose" />
	</td></tr>
	<tr>
		 <td style="height:5px">
		 </td>
	</tr>
	<tr>
		<td colspan="3" width="98%" style=" background-image:url(../../images/vizard_title_bg.gif); height:13px;" >
		
		</td>
	</tr>
	<tr>
		 <td style="height:5px">
		 </td>
	</tr>
	</table>
	<table border="0" cellpadding="0" cellspacing="0" width="98%">
	<tr>
	<td style="padding-left:5px">
	<h1>Visit Updated</h1> 
	<br> </td>
	</tr>
	<tr>
	<td style="padding-left:5px">
	<cfoutput>
	<a href="javascript:void(0);" onclick="javascript:secureHREF('index.cfm?page=patient&cat=Patient')">
	Edit Visits in Patient Chart</a></cfoutput>
	 <br><br> 
	 </td>
	 </tr>
	 <tr>
	 <td style="padding-left:5px">
	<cfoutput><a href="index.cfm?view=day&mmid=3">Go to Schedules</a></cfoutput>
	 <br><br>
	 </td>
	 </tr>
	 <tr>
	 <td style="padding-left:5px">
	<cfoutput><a href="../dashboard/index.cfm?page=dashboard&cat=&rpt=visit_compliance">Compliance for Unbilled Visits</a></cfoutput><br></td>
	</tr>
	</table>
</cfwindow>--->
</cfif>
</cfif>
