 <!--- Perform validity checks ---> 
   <!--- If no dates were selected, display error message ---> 
   <cfif Form.Sched_Dates IS ""> 
    <cfset ErrorFlag = "No Dates"> 
   <cfelse> 
    <!--- Check all dates to make sure they are within the Certification Period, if not display error ---> 
	
    <cfloop list="#Form.Sched_Dates#" index="CheckDate"> 
       <cfif DateCompare(CheckDate, Form.Certification_Start) IS -1 OR DateCompare(CheckDate, Form.Certification_End) IS 1> 
        <cfset ErrorFlag = "Out of Range"> 
      </cfif> 
    </cfloop> 
	<cfparam name="ErrorFlag" default="">
	
    <cfif ErrorFlag neq "Out of Range"> <!--- JDB: Do not insert ScheduleItems if Dates are Out of Range --->
      <!--- Get Rev_Code information ---> 
	  <cfsetting requesttimeout="5000">
      <cfquery  datasource="#Application.DataSrc#" name="GetRev" timeout="500">
       SELECT Revenue_Code.Rev_CodeID, Revenue_Code.Visit_Type, Revenue_Code.Rev_Code, Revenue_Code.HCPCS, Revenue_Code.EClaim_Price, Revenue_Code.Quick_Books_Price, Revenue_Code.Measure, Revenue_Code.Description, Revenue_Code.Payer_ID, Payer.Payer_ID, Payer.Output_Type FROM #Request.prefix_db_agency#.Revenue_Code, #Request.prefix_db_agency#.Payer WHERE Revenue_Code.Rev_CodeID =
       <cfqueryparam cfsqltype="cf_sql_integer" value="#Form.Rev_CodeID#" maxlength="10"> 
       And Payer.Payer_ID = Revenue_Code.Payer_ID Order by Revenue_Code.Visit_Type ASC
      </cfquery> 
      <!--- Assign Rev_Code info to Form variables ---> 
      <cfset Form.Visit_Type = GetRev.Visit_Type> 
      <cfset Form.Rev_Code = GetRev.Rev_Code> 
      <cfset Form.HCPCS = GetRev.HCPCS> 
      <cfset Form.Bill_Type = GetRev.Description> 
      <cfset Temp_EClaim_Price = GetRev.EClaim_Price> 
      <cfset Temp_Quick_Books_Price = GetRev.Quick_Books_Price> 
      <cfset Temp_Measure = GetRev.Measure> 
      <cfset Form.EClaimed = 0> 
      <cfif Form.Visit_Type IS 'Assessment' AND GetRev.Output_Type IS 'Medicare'> 
       <cfset Form.Output_Type = 'RAP'> 
       <cfelse> 
       <cfset Form.Output_Type = GetRev.Output_Type> 
      </cfif> 
      <!--- Calculate the Units, Total_EClaim and Total_QB values based on items selected ---> 
      <!--- Determine number of minutes (n) apart ---> 
      <cfset diff = #DateDiff("n",Form.StartTime,Form.EndTime)#> 
      <!--- Calculate number of units (4 per hour) ---> 
      <cfswitch expression="#Temp_Measure#"> 
       <cfcase value=".25"> 
        <cfset Form.Total_Units = (#diff#/60)*100/25> 
       </cfcase> 
       <cfcase value="1"> 
        <cfset Form.Total_Units = (#diff#/60)*100/100> 
       </cfcase> 
       <cfcase value="visit"> 
        <cfset Form.Total_Units = 1> 
       </cfcase> 
       <cfdefaultcase> 
        <cfset Form.Total_Units = 0> 
       </cfdefaultcase> 
      </cfswitch> 
      <cfif Form.Total_Units lt 0> 
       <cfset Form.Total_Units=Form.Total_Units+96> 
     </cfif> 
     <cfif Form.Visit_Type IS NOT 'Assessment' AND Temp_EClaim_Price IS NOT ""> 
       <cfset Form.Total_EClaim = Temp_EClaim_Price * Form.Total_Units> 
       <cfset Form.Total_QB = Temp_Quick_Books_Price> 
     <cfelse> 
      <cfif Session.Cert.Homebound_Status IS NOT ""> 
        <cfset Form.Total_EClaim = Session.Cert.Homebound_Status> 
        <cfset Form.Total_QB = Session.Cert.Homebound_Status> 
        <cfelse> 
        <cfset Form.Total_EClaim = 0> 
        <cfset Form.Total_QB = 0> 
      </cfif> 
     </cfif> 
     <cfloop list="#Form.Sched_Dates#" index="ThisDate"> 
       <cfset TempPayerID = GetToken(Form.Payer_ID,1,"|")> 
       <cfset TempPayer = GetToken(Form.Payer_ID, 2,"|")> 
       <!--- 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#"> 
	     <cfsetting requesttimeout="5000">
        <cfquery  datasource="#Application.DataSrc#" name="AllVisits" timeout="500">
          INSERT INTO #Request.prefix_db_agency#.ScheduleItems (
					   Patient_ID, Visit_Date, Employee_ID, EmpPay_ID, Record_creation_Date, Record_created_By, Record_Mod_Date, Record_Mod_By
						,Agency_ID, Visit_Type, Assessment_Tracker_ID, Assessment_ID, Progress_Note, EClaimed, Rev_Code, HCPCS
						,Payer_ID, StartTime, EndTime, Rev_CodeID, Total_Units, Total_EClaim, Total_QB
						,Bill_Type, Paperwork, Travel, Mileage, Output_Type, Telephony_Yes
						,Visit_Notes
				  ) VALUES (
					  #Form.Patient_ID#, #CreateODBCDateTime(ThisDate)#, '#Form.Employee_ID#', '#Form.EmpPay_ID#', #Now()#, #Session.EmployeeID#, #Now()#, #Session.EmployeeID#
					 ,#Session.AgencyID#, '#Form.Visit_Type#', #Form.ATID#, #Form.AID#, 'No', '0', '#Form.Rev_Code#','#Form.HCPCS#'
					 ,'#temppayerid#', #CreateODBCTime(Form.StartTime)#, #CreateODBCTime(Form.EndTime)#, #Form.Rev_CodeID#, #Form.Total_Units#, #Form.Total_EClaim#, #Form.Total_QB#
					 ,'#Form.Bill_Type#', '0.00', '0.00', '0', '#Form.Output_Type#', '#Form.Telephony_Yes#'
					 ,'#Form.Visit_Notes#'
					)
         </cfquery> 
		   <cfsetting requesttimeout="5000">
         <cfquery  name="NewVisit" datasource="#Application.DataSrc#" timeout="500">
          <!--- SELECT MAX(ScheduleItems_ID) as maxid from #Request.prefix_db_agency#.ScheduleItems --->
          SELECT Last_Insert_Id() AS maxId
         </cfquery>
       <cfif form.employee_id neq 0>
        <cf_telephonyid result="theitem" action="updateitem" employeeid="#Form.Employee_ID#" visitdate=#thisdate# id=#NewVisit.maxid#> <cf_telephonyid result="encodefinal" action="encode" employeeid="#Form.Employee_ID#" visitdate=#thisdate# item=#theitem#> 
		  <cfsetting requesttimeout="5000">
        <cfquery  datasource="#Application.DataSrc#" name="upd" timeout="500">
		
          update #Request.prefix_db_agency#.ScheduleItems set telephony_id = '#encodefinal#' where scheduleitems_id = #NewVisit.maxid#
         </cfquery> 
      </cfif> 
     </cfloop> 
     <!--- Clear this page's Session vars ---> 
    </cfif> <!--- JDB: End "Out of Range" error check --->
   </cfif> 
  
    <!--- If ErrorFlag has not been set, display success message, otherwise display error message ---> 
  <cfif ErrorFlag IS ""> 
    <cflocation url="index.cfm?view=day"> 
    <cfelse> 
    <cfswitch expression="#ErrorFlag#"> 
      <cfcase value="No Dates"> 
      <h3>Scheduling Problem</h3> 
      <p><strong>You must select at least one date to schedule.</strong></p> 
      <p><strong>Please click the Back button in your browser to correct the problem.</strong></p> 
      </cfcase> 
      <cfcase value="Too Many"> 
      <h3>Scheduling Problem</h3> 
      <p><strong>For an Admission Visit, you can only specify one visit date.</strong></p> 
      <p><strong>Please click the Back button in your browser to correct the problem.</strong></p> 
      </cfcase> 
      <cfcase value="Out of Range"> 
      <h3>Scheduling Problem</h3> 
      <p><strong>One of the dates you specified is outside the Certification Period.</strong></p> 
      <p><strong>Please click the Back button in your browser to correct the problem.</strong></p> 
      </cfcase> 
	 </cfswitch> 
  </cfif> 
   