sub production_time_read_db_to_match_events_and_systems {
		
	&common_date_array($last_start_ccyymmdd,$last_end_ccyymmdd);
	
	&db_min_ro('event_systems','id,name,description,system_type','','','') ;
	foreach my $id (keys %{$db{event_systems}}) { 
		$event_system_description{$id} = $db{event_systems}{$id}{description} ; 
		$event_system_name{$id}        = $db{event_systems}{$id}{name} ; 
	}
	
	$start_sql = substr($start_sql,0,11) . '00:00:00' ;
	$end_sql = substr($end_sql,0,11) . '23:59:59' ;
	
	# &db_min_ro('event_quotes','id,ref,event_system_id_multiple,format_of_title,date_from,date_to',"(date_from BETWEEN '$start_sql' AND '$end_sql') OR (date_to BETWEEN '$start_sql' AND '$end_sql')",'','') ;
	
	&db_min_ro('event_quotes','id,ref,event_system_id_multiple,format_of_title,date_from,date_to,sport_type_ids,club_ids',"((date_from BETWEEN '$start_sql' AND '$end_sql') OR (date_to BETWEEN '$start_sql' AND '$end_sql')) AND event_system_id_multiple IS NOT NULL AND event_system_id_multiple <> ''",'','')  ;
	
	our %seen_event_system_in_event_quotes = () ; 
	
	foreach my $event_id (keys %{$db{event_quotes}}) {
		
		$db{event_quotes}{$event_id}{ref} =~ s/\'//g ; 
		
		$db{event_quotes}{$event_id}{event_system_id_multiple} =~ s/;+/;/g; # Replace multiple semicolons with one
		$db{event_quotes}{$event_id}{event_system_id_multiple} =~ s/^;+|;+$//g; # Remove leading or trailing semicolons
		
		# next unless $db{event_quotes}{$event_id}{event_system_id_multiple} ;
		
		$opts{matched_event} .= qq~<option value="$event_id">[$event_id] $db{event_quotes}{$event_id}{ref}</option>~ ;  
		
		foreach (split(";",$db{event_quotes}{$event_id}{event_system_id_multiple})) {
			
			# $opts{matched_system} .= $seen_event_system_in_event_quotes{$_}
			$seen_event_system_in_event_quotes{$_} = 1 ;
			
		}
	}
	
	foreach (sort {$seen_event_system_in_event_quotes{$a} cmp $seen_event_system_in_event_quotes{$b}} keys %seen_event_system_in_event_quotes) {
		$opts{matched_system} .= qq~<option value="$_">$db{event_systems}{$_}{name} [$db{event_systems}{$_}{description}]</option>~ ; 
	}
	
	$opts{matched_system_none} .= qq~<option value="-1">$db{event_systems}{-1}{name} [$db{event_systems}{-1}{description}]</option>~ ; 
	
	&db_min_ro('sport_types','*','','','') ; 
	
	our %sport_name_to_id = () ;
	
	foreach (keys %{$db{sport_types}}) {
		$db{sport_types}{$_}{name} = lc $db{sport_types}{$_}{name} ;
		$db{sport_types}{$_}{name} =~ s/ //g ;
		$sport_name_to_id{$db{sport_types}{$_}{name}} = $_ ; 
	}
	
	&db_min_ro('clubs','id,name','','','') ; 
	
	foreach (keys %{$db{clubs}}) {
		my $c_name = lc $db{clubs}{$_}{name} ;
		$c_name =~ s/\'//g ;
		$club_name_to_id{$c_name} = $_ ; 
	}
	
	# foreach my $id (keys %{$db{event_quotes}}) { 
		# # $match_bank_trans{$db{event_quotes}{$id}{trans_date}}{$db{event_quotes}{$id}{description}} = 1 ; 
		
		# foreach my $event_id (split(";",$db{$tables}{$id}{event_system_id_multiple})) {			
			# $val .= qq~$db{event_systems}{$event_id}{name} [$db{event_systems}{$event_id}{description}], ~ ;		
		# }
	# }
	
} #------------------------------------------------------------------------------------------ 

sub production_time_process_matched_events_and_systems {
	
	my %system_match = () ; my %matched_event_name = () ; my %csv_line_possible_match = () ; my %matched_events = () ;
	
	$system_match{"None"}{"None"} = -1 ;

	foreach my $event_code (keys %csv_line) {
 
		next if $matched_event{$event_code} ;

		my ($club_name,$system_desc_clue) = split("-",$csv_line{$event_code}{club_name}) ; 
		$club_name =~ s/ $//g ; $system_desc_clue =~ s/^ //g ; # remove whitespace
		$club_name =~ s/\'//g ;
		
		$system_desc_clue = "None" unless $system_desc_clue ; 
		
		my @eventdetails = split(/ /,$club_name) ; 
		
		my $system_name_clue = $eventdetails[-1] ; $system_name_clue = "None" if $system_name_clue !~ /\d+/ ;
		
		if ($system_desc_clue =~ /\d+/ && length($system_desc_clue) == 5) {
			$system_name_clue = $system_desc_clue ; 
			$system_desc_clue = 'None' ;
		}
		
		if ($system_desc_clue =~ /AISA/ && $system_desc_clue =~ /SOLO/) {
			my @sysparts = split(/\_/,$system_desc_clue) ;
			$system_name_clue = $sysparts[2] ;
			$system_desc_clue =~ s/\_/ /g ;
		}
		
		$csv_line{$event_code}{system_id} = -1 if $system_name_clue eq 'None' && $system_desc_clue eq 'None' ;
		
		$system_desc_clue = lc $system_desc_clue ;
		
		$csv_line{$event_code}{system_id} = $system_match{$system_name_clue}{$system_desc_clue} if $system_match{$system_name_clue}{$system_desc_clue} ;
		
		if (!$csv_line{$event_code}{system_id}) {
			
			foreach my $system_id (keys %{$db{event_systems}}) {
			# foreach my $system_id (keys %seen_event_system_in_event_quotes) {	
				
				my $sys_desc  = lc $db{event_systems}{$system_id}{description} ;
				my $sys_name  = $db{event_systems}{$system_id}{name} ;				
				
				if (($sys_name =~ /\b$system_name_clue\b/ || $sys_name =~ /_$system_name_clue\b/) && $sys_desc =~ /\b$system_desc_clue\b/ && $system_name_clue ne 'None' && $system_desc_clue ne 'none') {
					$csv_line{$event_code}{system_id} = $system_id ;
					$system_match{$system_name_clue}{$system_desc_clue} = $system_id  ;
					last ;
				} elsif (($sys_name =~ /\b$system_name_clue\b/ || $sys_name =~ /_$system_name_clue\b/) && $system_name_clue ne 'None' && $system_desc_clue eq 'none') {
					$csv_line{$event_code}{system_id} = $system_id ;
					$system_match{$system_name_clue}{$system_desc_clue} = $system_id  ;
					last ;
				} elsif ($sys_desc =~ /\b$system_desc_clue\b/ && $system_name_clue eq 'None' && $system_desc_clue ne 'none') {
					$csv_line{$event_code}{system_id} = $system_id ;
					$system_match{$system_name_clue}{$system_desc_clue} = $system_id  ;	
					last ;					
				}
			}
		}

		my @sport_split = split(":",$csv_line{$event_code}{event_name}) ;
		
		$sport_split[0] =~ s/ //g ;
		$sport_split[0] = lc $sport_split[0] ; 
		
		my $sport_id = ($csv_line{$event_code}{event_name} =~ /:/) ? $sport_name_to_id{$sport_split[0]} : '' ;

		my @event_name_details = split(/-/,$csv_line{$event_code}{event_name}) ; 
		
		my $to_match_event_name = $event_name_details[-1] ;

		$to_match_event_name =~ s/^ //g ;
		$to_match_event_name =~ s/ $//g ;

		$to_match_event_name = ($csv_line{$event_code}{event_name} =~ /:/ && $csv_line{$event_code}{event_name} !~ /-/) ? $sport_split[1] : $to_match_event_name ;
		
		$to_match_event_name =~ s/\'//g ;
		
		my $club_id = $club_name_to_id{$club_name} ; 
		
		$club_name = ($csv_line{$event_code}{club_name} =~ /AISA/ && $csv_line{$event_code}{club_name} =~ /SOLO/) ? lc $club_name : lc $csv_line{$event_code}{club_name} ; 
		
		if (!$csv_line{$event_code}{system_id}) {
		
			foreach my $id (keys %{$db{event_quotes}}) {
				
				next unless $id ;
				
				next if $sport_id && $db{event_quotes}{$id}{sport_type_ids} !~ /\b$sport_id\b/ ;
				
				next if $club_id && $db{event_quotes}{$id}{club_ids} !~ /\b$club_id\b/ ;
				
				next if !$db{event_quotes}{$id}{event_system_id_multiple} || $db{event_quotes}{$id}{event_system_id_multiple} !~ /\d+/ ;
				
				my $event_end = substr($db{event_quotes}{$id}{date_to},0,11) . '23:59:59' ;
				
				my $event_start = substr($db{event_quotes}{$id}{date_from},0,11) . '00:00:00' ;

				my $after_event_end = &greater_date_time($event_end,$csv_line{$event_code}{start_time}) ; 
				next if $after_event_end  ;
				
				my $before_event_start = &greater_date_time($csv_line{$event_code}{end_time},$event_start) ; 
				next if $before_event_start  ;

				if ($to_match_event_name && ($db{event_quotes}{$id}{ref} eq $to_match_event_name || $db{event_quotes}{$id}{ref} =~ /$to_match_event_name/)) {
					$csv_line{$event_code}{event_id} = $id ;
					$matched_events{$event_code} .= qq~$id;~ ; 
				}  		
				
				my $found_match = 0 ; 
				
				foreach my $system_id (split(";",$db{event_quotes}{$id}{event_system_id_multiple})) {			

					my $sys_desc  = lc $db{event_systems}{$system_id}{description} ;
					my $sys_name  = $db{event_systems}{$system_id}{name} ;				
					
					if ($sys_name =~ /\b$system_name_clue\b/ && $sys_desc =~ /\b$system_desc_clue\b/ && $system_name_clue ne 'None' && $system_desc_clue ne 'none') {
						$csv_line{$event_code}{system_id} = $system_id ;
						$system_match{$system_name_clue}{$system_desc_clue} = $system_id  ;
					} elsif ($sys_name =~ /\b$system_name_clue\b/ && $system_name_clue ne 'None' && $system_desc_clue eq 'none') {
						$csv_line{$event_code}{system_id} = $system_id ;
						$system_match{$system_name_clue}{$system_desc_clue} = $system_id  ;
					} elsif ($sys_desc =~ /\b$system_desc_clue\b/ && $system_name_clue eq 'None' && $system_desc_clue ne 'none') {
						$csv_line{$event_code}{system_id} = $system_id ;
						$system_match{$system_name_clue}{$system_desc_clue} = $system_id  ;					
					}
				
					# my $sys_desc  = $db{event_systems}{$system_id}{description} ;
					# my $sys_name  = $db{event_systems}{$system_id}{name} ;

					# # my @sysparts = split(/\_/,$system) ;
					
					# if ($sys_name =~ /\b$sysno\b/ && $sys_desc =~ /\b$system\b/iog) {
						# $csv_line{$event_code}{system_id} = $system_id ;
						# $system_match{$system}{$sysno} = $system_id  ;
						# $found_match = 1 ;
					# } elsif ($system =~ /\d+/ && $sys_name =~ /\b$system\b/) {
						# $csv_line{$event_code}{system_id} = $system_id ;
						# $system_match{$system}{$sysno} = $system_id  ;
						# $found_match = 1 ;
					# } elsif ($system =~ /AISA/ && $system =~ /SOLO/ && $sysparts[2] && $sys_name =~ /\b$sysparts[2]\b/) {
						# $csv_line{$event_code}{system_id} = $system_id ;
						# $system_match{$system}{$sysno} = $system_id  ;
						# $found_match = 1 ;
					# } elsif ($sys_desc eq 'St Stithians Hockey Red' && $clubname =~ /Red/i && $clubname =~ /Stithians/i) {
						# $csv_line{$event_code}{system_id} = $event_id  ;
						# $system_match{$system}{$sysno} = $system_id  ;
						# $found_match = 1 ;
					# }			
				}
				
				if ($found_match && !$csv_line{$event_code}{event_id}) {
					$csv_line_possible_match{$event_code}{event_id} .= qq~$id;~ ; 
				}
			}				
		} 
		
		if ($csv_line{$event_code}{system_id} && !$csv_line{$event_code}{event_id}) {
			
			foreach my $id (keys %{$db{event_quotes}}) {
				
				next if $sport_id && $db{event_quotes}{$id}{sport_type_ids} && $db{event_quotes}{$id}{sport_type_ids} !~ /\b$sport_id\b/ ;
				
				next if $club_id && $db{event_quotes}{$id}{club_ids} !~ /\b$club_id\b/ ;
				
				my $event_end = substr($db{event_quotes}{$id}{date_to},0,11) . '23:59:59' ;
				
				my $event_start = substr($db{event_quotes}{$id}{date_from},0,11) . '00:00:00' ;
				
				my $after_event_end = &greater_date_time($event_end,$csv_line{$event_code}{start_time}) ; 
				next if $after_event_end  ;
				
				my $before_event_start = &greater_date_time($csv_line{$event_code}{end_time},$event_start) ; 
				next if $before_event_start  ;
				
				if (!$db{event_quotes}{$id}{event_system_id_multiple} || $db{event_quotes}{$id}{event_system_id_multiple} !~ /\d+/ || $db{event_quotes}{$id}{event_system_id_multiple} !~ /\b$csv_line{$event_code}{system_id}\b/) {
					if ($csv_line{$event_code}{system_id} && !$seen_event_system_in_event_quotes{$csv_line{$event_code}{system_id}}) {
						$csv_line_possible_match{$event_code}{event_id} .= qq~$id;~ ; 
					}
					next ;
				}
				
				# if ($to_match_event_name && ($db{event_quotes}{$id}{ref} eq $to_match_event_name || $db{event_quotes}{$id}{ref} =~ /$to_match_event_name/)) {
				if ($db{event_quotes}{$id}{sport_type_ids} && $to_match_event_name && ($db{event_quotes}{$id}{ref} eq $to_match_event_name || $db{event_quotes}{$id}{ref} =~ /$to_match_event_name/)) {
					$csv_line{$event_code}{event_id} = $id ;
					last ;
				} elsif ($db{event_quotes}{$id}{sport_type_ids}) {
					$csv_line{$event_code}{event_id} = $id ;
					last ;
				}  else {
					$csv_line_possible_match{$event_code}{event_id} .= qq~$id;~ ; 
				}
			}
		}
		
		if ((!$csv_line{$event_code}{system_id}) && !$csv_line{$event_code}{event_id}) {
			
			foreach my $id (keys %{$db{event_quotes}}) {
				
				next if $sport_id && $db{event_quotes}{$id}{sport_type_ids} && $db{event_quotes}{$id}{sport_type_ids} !~ /\b$sport_id\b/ ;
				
				next if $club_id && $db{event_quotes}{$id}{club_ids} !~ /\b$club_id\b/ ;
				
				next if !$db{event_quotes}{$id}{event_system_id_multiple} || $db{event_quotes}{$id}{event_system_id_multiple} !~ /\d+/ ;
				
				my $event_end = substr($db{event_quotes}{$id}{date_to},0,11) . '23:59:59' ;
				
				my $event_start = substr($db{event_quotes}{$id}{date_from},0,11) . '00:00:00' ;
				
				my $after_event_end = &greater_date_time($event_end,$csv_line{$event_code}{start_time}) ; 
				next if $after_event_end  ;
				
				my $before_event_start = &greater_date_time($csv_line{$event_code}{end_time},$event_start) ; 
				next if $before_event_start  ;

				# if (!$db{event_quotes}{$id}{event_system_id_multiple} || $db{event_quotes}{$id}{event_system_id_multiple} !~ /\d+/ || $db{event_quotes}{$id}{event_system_id_multiple} !~ /\b$csv_line{$event_code}{system_id}\b/) {
					# if ($csv_line{$event_code}{system_id} && !$seen_event_system_in_event_quotes{$csv_line{$event_code}{system_id}}) {
						# $csv_line_possible_match{$event_code}{event_id} .= qq~$id;~ ; 
					# }
					# next ;
				# }

				if ($db{event_quotes}{$id}{sport_type_ids} && $to_match_event_name && ($db{event_quotes}{$id}{ref} eq $to_match_event_name || $db{event_quotes}{$id}{ref} =~ /$to_match_event_name/)) {
					$csv_line{$event_code}{event_id} = $id ;
					last ;
				} elsif ($db{event_quotes}{$id}{sport_type_ids}) {
					$csv_line{$event_code}{event_id} = $id ;
					last ;
				} else {
					$csv_line_possible_match{$event_code}{event_id} .= qq~$id;~ ; 
				}
			}
		}
		
		chop $csv_line_possible_match{$event_code}{event_id} if $csv_line_possible_match{$event_code}{event_id} ;
		
	}

	$print_box_content_rows .= &common_min_forms_start("$lcpage") ;
	
	our @sql_col_display = ("club_system","system_nr","matched_system","production_hours","production_seconds","cloud_recording_seconds","club_name","start","end","event_name","matched_event","match") ;
	
	my $row_cnt = 0 ; my %no_system_or_event = () ; 
	
	foreach my $event_code (sort {$csv_line{$a}{line_cnt} cmp $csv_line{$b}{line_cnt}} keys %csv_line) {
	
		next if $matched_event{$event_code} ;
		
		$row_cnt++ ;
		
		$print_tbody .= qq~<tr id="$row_cnt">~ ;	
		
		my $cell_id = qq~~ ;
		
		my ($system,$sysno) = split(" - ",$csv_line{$event_code}{club_name}) ; 
		
		my @get_sport = (!$csv_line{$event_code}{system_id} || (!$csv_line{$event_code}{event_id} && !$csv_line_possible_match{$event_code}{event_id}) || (!$csv_line{$event_code}{event_id} && $csv_line_possible_match{$event_code}{event_id} =~ /;/)) ? split(/\:/,$csv_line{$event_code}{event_name}) : () ;  
		
		$extra_form_fields .= qq~<input type="text" name='$event_code' value="$event_code_to_line{$event_code}" style="display:none;">~ if !$csv_line{$event_code}{system_id} || !$csv_line{$event_code}{event_id} ;
		
		foreach my $col (@sql_col_display) {
			
			my $val = '' ; $nowrap = '' ;
			
			if ($col eq 'club_name') {
				$val = $csv_line{$event_code}{club_name} ;
			} elsif ($col eq 'system_nr') {
				$val = $sysno ;
			} elsif ($col eq 'start') {
				$val = $csv_line{$event_code}{start_time} ;
				$nowrap = 'nowrap' ;
			} elsif ($col eq 'end') {
				$val = $csv_line{$event_code}{end_time} ;
				$nowrap = 'nowrap' ;
			} elsif ($col eq 'club_system') {
				$val = $system ;
			} elsif ($col eq 'matched_system') {

				$preferred_placeholder{"system_$row_cnt"} = "Select System" ; 
				
				$found_system_drop_down = 1 if !$csv_line{$event_code}{system_id} || $csv_line{$event_code}{system_id} eq '-1' ;
				
				# $allow_deselect{"system_$row_cnt"} = 1 ;
				
				$val = ($csv_line{$event_code}{system_id} && $csv_line{$event_code}{system_id} ne '-1') ? "$db{event_systems}{$csv_line{$event_code}{system_id}}{name} [$db{event_systems}{$csv_line{$event_code}{system_id}}{description}]" : ($csv_line{$event_code}{system_id} eq '-1') ? &common_min_table_select("system_$row_cnt","",'','','','','',"matched_system_none") : &common_min_table_select("system_$row_cnt","",'','','','','',"matched_system") ;
				# $val = ($csv_line{$event_code}{system_id} && $csv_line{$event_code}{system_id} ne '-1') ? "$db{event_systems}{$csv_line{$event_code}{system_id}}{name} [$db{event_systems}{$csv_line{$event_code}{system_id}}{description}]" : &common_min_table_select("system_$row_cnt","",'','','','','',"matched_system") ;
				
				if (!$csv_line{$event_code}{system_id} || $csv_line{$event_code}{system_id} eq '-1') {
					
					my $sport_name = ($csv_line{$event_code}{event_name} =~ /:/) ? $get_sport[0] : $csv_line{$event_code}{event_name} ;
					$no_system_or_event{$csv_line{$event_code}{club_name}}{substr($csv_line{$event_code}{start_time},0,10)}{$sport_name}{system} .= qq~"#selectSystem_$row_cnt",~ ;
					
				}
				
				$cell_id = qq~id="matched_system_$row_cnt"~ ;
				
			} elsif ($col eq 'production_hours') {
				$val = $csv_line{$event_code}{production_hours} ;
				$val =~ s/\,/\./g ;
			} elsif ($col eq 'production_seconds') {
				$val = &common_commify($csv_line{$event_code}{production_seconds}) ;
			} elsif ($col eq 'cloud_recording_seconds') {
				$val = &common_commify($csv_line{$event_code}{cloud_seconds}) ;
			} elsif ($col eq 'event_name') {
				$val = $csv_line{$event_code}{event_name} ;
			} elsif ($col eq 'matched_event') {

				$csv_line_possible_match{$event_code}{event_id} =~ s/\;/\_/g ;
				
				my $col_name = (!$csv_line{$event_code}{event_id} && $csv_line_possible_match{$event_code}{event_id}) ? "matched_event_$csv_line_possible_match{$event_code}{event_id}" : "matched_event" ;

				$opts{$col_name} =  
				($csv_line_possible_match{$event_code}{event_id} && !$csv_line{$event_code}{event_id}) ? join "</option><option ", map { qq~value="$_"><strong>Possible : </strong>$db{event_quotes}{$_}{ref} [$_]~ } split "_" , $csv_line_possible_match{$event_code}{event_id} : 
				$opts{matched_event} ; 
				
				$opts{$col_name} = qq~<option ~ . $opts{$col_name} . qq~</option>~ if $col_name ne 'matched_event' ;
			
				$preferred_placeholder{"event_$row_cnt"} = "Select Event" ; 
				
				$val = ($csv_line{$event_code}{event_id}) ? "$db{event_quotes}{$csv_line{$event_code}{event_id}}{ref} [$csv_line{$event_code}{event_id}]" : 
				
				# (!$csv_line{$event_code}{event_id} && $csv_line_possible_match{$event_code}{event_id} && $csv_line_possible_match{$event_code}{event_id} !~ /_/) ? "<strong>POSSIBLE : </strong>$db{event_quotes}{$csv_line_possible_match{$event_code}{event_id}}{ref} [$csv_line_possible_match{$event_code}{event_id}]" : 
				
				(!$csv_line{$event_code}{event_id} && $csv_line_possible_match{$event_code}{event_id}) ? &common_min_table_select("event_$row_cnt",'','','','','','',$col_name) : 
				&common_min_table_select("event_$row_cnt",'','','','','','','matched_event') ;
				
				$found_event_drop_down = 1 if !$csv_line{$event_code}{event_id} ;
				# $found_event_drop_down = 1 if !$csv_line{$event_code}{event_id} && (!$csv_line_possible_match{$event_code}{event_id} || $csv_line_possible_match{$event_code}{event_id} =~ /_/) ;
					
				# if ((!$csv_line{$event_code}{event_id} && !$csv_line_possible_match{$event_code}{event_id}) || (!$csv_line{$event_code}{event_id} && $csv_line_possible_match{$event_code}{event_id} =~ /_/)) {
				if (!$csv_line{$event_code}{event_id}) {
					
					my $sport_name = ($csv_line{$event_code}{event_name} =~ /:/) ? $get_sport[0] : $csv_line{$event_code}{event_name} ;
					
					$no_system_or_event{$csv_line{$event_code}{club_name}}{substr($csv_line{$event_code}{start_time},0,10)}{$sport_name}{event} .= qq~"#selectEvent_$row_cnt",~ ;
					
				}
				$cell_id = qq~id="matched_event_$row_cnt"~ ;
				
			} elsif ($col eq 'match') {
				
				$nowrap = 'nowrap' ;
				
				$cell_id = qq~id="final_col_$row_cnt"~ ;
				
				my $tool = $pixellot_system{$ccyymmdd}{$period} ; 

				$start_sql =~ s/ /T/g ;
				$end_sql =~ s/ /T/g ;
				
				# my $system_input = ($csv_line{$event_code}{system_id}) ? qq~<input style='display:none;' value='$csv_line{$event_code}{system_id}' id='selectSystem_$row_cnt' name="system_$row_cnt">~ : qq~~ ;
				my $system_input = ($csv_line{$event_code}{system_id} && $csv_line{$event_code}{system_id} ne '-1') ? qq~<input style='display:none;' value='$csv_line{$event_code}{system_id}' id='selectSystem_$row_cnt' name="system_$row_cnt">~ : qq~~ ;
				
				# my $matched_event_id = ($csv_line{$event_code}{event_id}) ? $csv_line{$event_code}{event_id} : ($csv_line_possible_match{$event_code}{event_id} && $csv_line_possible_match{$event_code}{event_id} !~ /_/) ? $csv_line_possible_match{$event_code}{event_id} : '' ; 
				
				my $matched_event_id = ($csv_line{$event_code}{event_id}) ? $csv_line{$event_code}{event_id} : '' ; 
				
				my $event_input = ($matched_event_id) ? qq~<input style='display:none;' value='$matched_event_id' id='selectEvent_$row_cnt' name="event_$row_cnt">~ : qq~~ ;
				
				my $match_icon = ($system_input && $event_input) ? "ok" : "remove" ; 
				my $match_class = ($system_input && $event_input) ? "success" : "danger" ; 
				
				push @unmatched_rows, "#selectEvent_$row_cnt" if !$event_input ;
				push @unmatched_rows, "#selectSystem_$row_cnt" if !$system_input ;
				
				my $not_matched = (!$event_input || !$system_input) ? qq~<input style='display:none;' value='1' id='not_matched_$row_cnt'>~ : qq~~ ; 
				
				# my $no_similar_rows = ($csv_line{$event_code}{system_id} && $csv_line{$event_code}{system_id} ne '-1' && $csv_line{$event_code}{event_id}) ? '-1' : '' ;
				
				my $similar_rows = (!$event_input || !$system_input) ? qq~<input value='' name='similar_rows_$row_cnt' style='display:none;'>~ : qq~~ ;
				
				$val = "
					$system_input 
					$event_input
					$not_matched
					$similar_rows
					<input style='display:none;' value='$event_code' id='event_code_$row_cnt' name='event_code_$row_cnt'>
					<input style='display:none;' value='$csv_line{$event_code}{production_seconds}' id='time_$row_cnt' name='time_$row_cnt'>
					<input style='display:none;' value='$csv_line{$event_code}{cloud_seconds}' id='cloud_time_$row_cnt' name='cloud_time_$row_cnt'>
					<a class='btn btn-$match_class btn-sm' id='matched_row_$row_cnt' href=javascript:match_event_system('$row_cnt','$event_code','$csv_line{$event_code}{production_seconds}','$csv_line{$event_code}{cloud_seconds}'); data-title='MATCH' data-toggle='tooltip' data-placement='left' data-placement='left' data-original-title title><i class='glyphicon glyphicon-$match_icon'></i></a>
					&nbsp;&nbsp;
					<a class='btn btn-success btn-sm' href=javascript:dlgMdl('$useropts{'scripts'}/dialog/manually_match_csv_row.pl?$matched_event_id&$csv_line{$event_code}{system_id}&$csv_line{$event_code}{production_seconds}&$csv_line{$event_code}{cloud_seconds}&$event_code&$start_sql&$end_sql&$row_cnt&add','Manually&nbsp;Match','','medium-dialog'); data-title='MATCH MANUALLY' data-toggle='tooltip' data-placement='left' data-placement='left' data-original-title title>
						<i class='glyphicon glyphicon-question-sign'></i>
					</a> 
				" ;

				$start_sql =~ s/T/ /g ;
				$end_sql =~ s/T/ /g ;

			}
			$print_tbody .= qq~<td $cell_id $nowrap>$val</td>~ ;	
		}
		$print_tbody .= qq~</tr>~ ;	
	}
	
	$trigger_jquery_raw .= qq~
		
		// let inputString = '1,21,32,4';  // Your input string
		// let outputString = inputString.split(',').map(num => 'abc_' + num).join(',');  
		
		function auto_select_similar_drop_downs (ids,changed_val) {
			for (let id of ids) {
				if (\$(id).val()) {
					continue ;
				}
				\$(id).val(changed_val) ;
				\$(id).trigger("chosen:updated") ;
				fix_match_icon(id) ;
			} 	
		}
		
	~ ;
	
	my %seen_rows_set = () ; 
	
	foreach my $club_name (keys %no_system_or_event) {
		foreach my $start_time (keys %{$no_system_or_event{$club_name}}) {
			foreach my $sport_name (keys %{$no_system_or_event{$club_name}{$start_time}}) {
				foreach my $sys_evt (keys %{$no_system_or_event{$club_name}{$start_time}{$sport_name}}) {
					
					chop $no_system_or_event{$club_name}{$start_time}{$sport_name}{$sys_evt} if $no_system_or_event{$club_name}{$start_time}{$sport_name}{$sys_evt} ;
					my $ids_change = $no_system_or_event{$club_name}{$start_time}{$sport_name}{$sys_evt} ; 
					$ids_change =~ s/\"//g ;
					
					my $ids_change2 = $ids_change ; 
					
					$ids_change2 =~ s/#selectEvent_(\d+)/input\[name='similar_rows_$1'\]/g;
					$ids_change2 =~ s/#selectSystem_(\d+)/input\[name='similar_rows_$1'\]/g;
 
					my $ids_change_row_nrs = $ids_change ;
					$ids_change_row_nrs =~ s/#selectEvent_//g ;
					$ids_change_row_nrs =~ s/#selectSystem_//g ;
					
					my @first_nr = split(/\,/,$ids_change_row_nrs) ;
					
					my $event_or_system = ($no_system_or_event{$club_name}{$start_time}{$sport_name}{$sys_evt} =~ /Event/) ? 'event' : 'system' ;
					
					next if $seen_rows_set{$ids_change_row_nrs}{$event_or_system} ; 
					$seen_rows_set{$ids_change_row_nrs}{$event_or_system} = 1 ; 

					$trigger_jquery_raw .= qq~

						if (\$("#not_matched_$first_nr[0]").val() && !\$("input[name='similar_rows_$first_nr[0]']").val()) {
							\$("$ids_change2").val("$ids_change_row_nrs") ;							
						} 
						
						\$("$ids_change").change( function () {
							
							fix_match_icon(this.id) ;
							if (!\$(this).val()) {
								return ; 
							}
							auto_select_similar_drop_downs([$no_system_or_event{$club_name}{$start_time}{$sport_name}{$sys_evt}],\$(this).val()) ; 
						
						}) ;
						
					~ ; 
				}
			}
		}
	}
	
	my $unmatched_rows_str = join(",",@unmatched_rows) ;

	$trigger_jquery_raw .= qq~
	
		function get_row_nr (id) {
				
			let lastIndex = id.lastIndexOf('_');
			lastIndex = parseInt(lastIndex) ;
			let row_nr_1 = id.substring(1+lastIndex); 
			return row_nr_1 ; 
			
		}
		
		function fix_match_icon (id) {
			
			let row_nr = get_row_nr(id) ;
			
			if (\$("#selectEvent_"+row_nr).val() && \$("#selectSystem_"+row_nr).val() && \$("#not_matched_"+row_nr).val()) {
				\$("#matched_row_"+row_nr).removeClass('btn btn-danger btn-sm').addClass('btn btn-success btn-sm');	 			
				\$("#matched_row_"+row_nr).html("<i class='glyphicon glyphicon-ok'></i>") ; 	 			
				\$("#not_matched_"+row_nr).val("") ;
			} else if ((!\$("#selectEvent_"+row_nr).val() || !\$("#selectSystem_"+row_nr).val()) && !\$("#not_matched_"+row_nr).val()) {
				\$("#matched_row_"+row_nr).removeClass('btn btn-success btn-sm').addClass('btn btn-danger btn-sm');	 			
				\$("#matched_row_"+row_nr).html("<i class='glyphicon glyphicon-remove'></i>") ;
				\$("#not_matched_"+row_nr).val("1") ;
			}		
		}
		
	// 	\$("$unmatched_rows_str").change( function () {
	//			fix_match_icon(this.id) ; 
	//	}) ;
	
	~ if $unmatched_rows_str ;
	
	$trigger_jquery_raw .= qq~\$("#itv-table tr th:nth-last-child(2)").css("width","20%");~ if $found_event_drop_down ; ## MATCHED EVENT COLUMN
	
	$trigger_jquery_raw .= qq~\$("#itv-table tr th:nth-last-child(10)").css("width","20%");~ if $found_system_drop_down ;  ## MATCHED SYSTEM COLUMN
	
	$trigger_jquery_raw .= qq~
  	
		function match_event_system (row_nr,event_code,production_seconds,cloud_seconds) {
			
			let event_id = \$("#selectEvent_"+row_nr).val() ;
			let system_id = \$("#selectSystem_"+row_nr).val() ;
			
			if (!event_id || !system_id) {
				noty({text:'Please select a System and Event!!!',layout:"center",type:"error",timeout:3000}) ;
				return ; 
			}
			
			\$("#"+row_nr).css("display","none") ;
			
			var url = "$useropts{scripts}/get/add_event_system_hours.pl?"+event_id+"&"+system_id+"&"+production_seconds+"&"+cloud_seconds+"&"+event_code ;

			\$.get(url) ;
			\$("#final_col_"+row_nr).html('') ;
			
		}
		
		// \$("#itv-table tr:eq(0) th:nth-last-child(1)").html("<span>Match All</span>&nbsp;&nbsp;<a class='btn btn-success btn-sm' href=javascript:match_all(1); data-title='MATCH ALL (inc. Possible Events)' data-toggle='tooltip' data-placement='left' data-placement='left' data-original-title title><i class='glyphicon glyphicon-ok'></i></a>&nbsp;&nbsp;<a class='btn btn-warning btn-sm' href=javascript:match_all(0); data-title='MATCH ALL (excl. Possible Events)' data-toggle='tooltip' data-placement='left' data-placement='left' data-original-title title><i class='glyphicon glyphicon-ok'></i></a>") ; 
		
		\$("#itv-table thead tr:eq(0) th:nth-last-child(1)").css('white-space','nowrap') ;
		
	~ ;
		
} #------------------------------------------------------------------------------------------
 
1 ;