<!---<cfoutput>
Welcome <br>
#form.chkauth#<br>
</cfoutput>--->

<cfif isdefined('Form.chkauth')>
	<cfloop index="x" list="#Form.chkauth#" delimiters=",">
		<cfset increment="0">
		<cfset Scheduleitem_id = "">
		<cfset Visitdate = "">
		<cfset Authid = "">
			<cfloop index="y" list="#x#" delimiters="|">
			
				<cfset increment= increment + 1>
				<cfif increment eq 1>
					<cfset Scheduleitem_id =#Y#>
				<cfelseif increment eq 2>
					<cfset Visitdate= #Y#>
				<cfelseif increment eq 3>
					<cfset Authid= #Y#>
				</cfif>	
			</cfloop>
		
			<cfquery  name="checkauthid" datasource="#Application.DataSrc#">
				SELECT Auth_ID FROM #Request.prefix_db_agency#.ScheduleItems where ScheduleItems_ID='#Scheduleitem_id#'
			</cfquery>
					
			<cfif Trim(checkauthid.Auth_ID) eq 0>
			
				<cfquery  name="Updateauthid" datasource="#Application.DataSrc#">
				update #Request.prefix_db_agency#.ScheduleItems SET Auth_ID='#Authid#' where ScheduleItems_ID='#Scheduleitem_id#'
				</cfquery>
				
				<cfquery  name="GetAuth_Details_ID" datasource="#Application.DataSrc#">
				select Auth_Details_ID  from #Request.prefix_db_agency#.authorizations_details where Auth_ID='#Authid#' and ScheduleItems_ID is null order by Auth_Details_ID asc limit 1 
				</cfquery>
				
				<cfif GetAuth_Details_ID.recordcount gt 0>
					<cfquery  name="Updateschid" datasource="#Application.DataSrc#">
					update #Request.prefix_db_agency#.authorizations_details SET ScheduleItems_ID= #Scheduleitem_id# where Auth_Details_ID=#GetAuth_Details_ID.Auth_Details_ID# and Auth_ID='#Authid#' and ScheduleItems_ID is null
					</cfquery>
				<cfelse>
					<script language="javascript" type="text/javascript">				
						window.onload = function()
						{
						alert('you cannot add authorization. because it is exceeded');
						window.location.href="../schedules/index.cfm?page=schedules&cat=&rpt=&view=month&month=#month#&year=#year#";
						}
					</script>
				</cfif>
				<script language="javascript" type="text/javascript">				
					window.onload = function()
					{
					alert('Successfully updated..');
					window.location.href="../schedules/index.cfm?page=schedules&cat=&rpt=&view=month&month=#month#&year=#year#";
					}
				</script>
				
				
			<cfelse>			
				<script language="javascript" type="text/javascript">				
					window.onload = function()
					{
					 doyou = confirm("This scheduleitem has already scheduled with autorization..Do you want to continue? (OK = Yes   Cancel = No)");
						 if(doyou == false)
						 {
						  window.location.href="../schedules/index.cfm?page=schedules&cat=&rpt=&view=month&month=#month#&year=#year#";
						 }		
						 else
						 {			
						  window.location.href="../schedules/index.cfm?page=schedules&cat=&rpt=&view=month&month=#month#&year=#year#";
						 }				
					}
				</script>
				
			</cfif>
				
	</cfloop>
	
<cfelse>
	<script language="javascript" type="text/javascript">
		window.onload = function()
		{
		alert('Please select the authorization checkbox');		
	    window.location.href="../schedules/index.cfm?page=schedules&cat=&rpt=&view=month&month=#month#&year=#year#";
		}
	</script>
	<cflocation url="../schedules/index.cfm?page=schedules&cat=&rpt=&view=month&month=#month#&year=#year#" addtoken="no">
</cfif>

