<?php
			$sqlUnion = array();
		
			foreach ($grp[$grKey] as $proj1) {
				$sql = "SELECT count(*) as n, ".$colTypesStructure[$key][0]." as varName FROM ".$proj1."_structure WHERE flag_bad = 101 AND ".$colTypesStructure[$key][0]." != ''";
					if ($filter) $sql .=" AND ".$filter;	
				$sql .= " GROUP BY ".$colTypesStructure[$key][0];
				//echo $sql;
				$sqlUnion[] = $sql;	
			}
				$sql = "(".implode(") UNION ALL (",$sqlUnion)." ) ";
				$res = $db->SQLexecute($sql);
				
				while ($zad = $db->fetchAssoc($res)) {
					$freq[$zad['varName']] += $zad['n'];	
					$total +=  $zad['n'];	
					if ($_GET['showPonders']) {
						// ponders
						$freqPonder[$zad['varName']] += $zad['n'] * abs($scoreFactors[$key][$zad['varName']]) ;	
						$totalPonder +=  $zad['n'] * abs($scoreFactors[$key][$zad['varName']]);	
					}
				}
								
				$Graph['titel'] = $val;
				foreach ($colAns[$key] as $labVal) {				
					$Graph['labels'][] = $labVal[1];
					$Graph['values'][] =  round( ($freq[$labVal[0]]/$total) * 100,2);	
				}
				
				if ($_GET['showPonders']) {
					$GraphPonder['titel'] = $val."<br />* \"weighted\"";
					foreach ($colAns[$key] as $labVal) {
						$GraphPonder['labels'][] = $labVal[1];
						$GraphPonder['values'][] =  round( ($freqPonder[$labVal[0]]/$totalPonder) * 100,2);
					}	
				}			
				
				if ($_GET['showTable']) {
					echo "<table class=\"reportTable\" cellpadding=\"5\" cellspacing=\"0\" width=\"600\" align=\"center\">";
						echo "<tr>
									<td class=\"TableHeader\">".($_GET['showVarName'] ? $key." " : "" ).$val."</td>
							 		<td class=\"TableHeader\" width=20>N</td>
							 		<td class=\"TableHeader\" width=20>%</td>";
						if ($_GET['showPonders']) {
							echo 	   "<td class=\"TableHeader\" width=20>N<br><small>(ponder)</small></td>
							 			<td class=\"TableHeader\" width=20>%</td>";
						}
						echo	"</tr>";
						foreach ($freq as $keyFreq => $nFreq) {
							echo "<tr>";
								echo "<td>".$keyFreq."</td>";
								echo "<td align=\"center\">";
									echo $nFreq;
								echo "</td>";
								echo "<td align=\"center\">";
											
											echo number_format(( round( ($nFreq / $total) * 100,2)),1);
																		
								echo "</td>";
						if ($_GET['showPonders']) {
								echo "<td align=\"center\">";
									echo " / ";
								echo "</td>";
								echo "<td align=\"center\">";
									echo " / ";
									
								echo "</td>";
						}
							echo "</tr>";
						}
						echo "<tr>";
							echo "<td class=\"TableHeader\" style=\"text-align:right\">TOTAL</td>";
							echo "<td class=\"TableHeader\">".$total."</td>";
							echo "<td class=\"TableHeader\" >100%</td>";
						if ($_GET['showPonders']) {
							echo "<td class=\"TableHeader\">".$totalPonder."</td>";
							echo "<td class=\"TableHeader\" >100%</td>";
						}
						echo "</tr>";
					echo "</table>";
				}
			
?>