<?php			
	unset($totalCondition);
	foreach ($colTypesStructure[$key] as $key_code => $keyVar) {
				
		$sql = "SELECT count(*) as n, ".$keyVar." as varName  FROM ".$proj."_structure WHERE ".$keyVar." > 0 ";
			if ($filter) $sql .=" AND ( ".$filter." )";	
		$sql .=" GROUP BY ".$keyVar;
					
		//echo $sql;
		$res = $db->SQLexecute($sql);
		while ($zad = $db->fetchAssoc($res)) {
			$freq[$keyVar][$zad['varName']] = $zad['n'];		
		}
		$totalCondition[] = $keyVar." > 0 ";						
	}
		
	$sql = "SELECT count(*) as n  FROM ".$proj."_structure WHERE ( ";
	$sql .= implode(" OR ",$totalCondition);
	$sql .= " ) ";
		if ($filter) $sql .=" AND ( ".$filter." )";	
				
	//echo $sql;
	$res = $db->SQLexecute($sql);
	$zad = $db->fetchAssoc($res);
	$total = $zad['n'];	
			
			
	$numberOfLegends = count($colTypesLegend[$key]);
	
	foreach ($colAns[$key] as $colKey=>$labVal) {
		$Graph['category'][] = controlGraphString($labVal[1]);
		
		foreach ($colTypesLegend[$key] as $legendLabs) {
			$Graph['labels'][$legendLabs[0]] = $legendLabs[1];
			$Graph['values'][$legendLabs[0]][] = round( ($freq[$key."_".$labVal[0]][$legendLabs[0]]/$total) * 100,2);
		}
	}
	
	if ($_GET['showTable']) {
			echo "<table class=\"reportTable\" cellpadding=\"5\" cellspacing=\"0\" width=\"600\" align=\"center\">";
					echo "<tr>
								<td class=\"TableHeader\">".$val."</td> ";
					
					foreach ($colTypesLegend[$key] as $legendLabs) echo " <td class=\"TableHeader\" colspan=\"2\">".$legendLabs[1]."</td> ";		
								 		
					echo " </tr>";
					echo "<tr>
								<td class=\"TableSubHeader\">&nbsp;</td> ";
					
					foreach ($colTypesLegend[$key] as $legendLabs) echo " <td class=\"TableSubHeader\">N</td><td class=\"TableSubHeader\">%</td>";		
								 		
					echo " </tr>";
					
					foreach ($colAns[$key] as $colKey=>$labVal) {
						echo "<tr>";
							echo "<td>".$labVal[1]."</td>";
							
							
							
							foreach ($colTypesLegend[$key] as $legendLabs) {
								echo "<td align=\"center\">";
									if (!$freq[$key."_".$labVal[0]][$legendLabs[0]]) echo "0";
										else echo $freq[$key."_".$labVal[0]][$legendLabs[0]];
								echo "</td>";
								echo "<td align=\"center\">";
								if (!$freq[$key."_".$labVal[0]][$legendLabs[0]]) {
									echo "0";
									//$Graph['labels'][$legendLabs[0]] = $legendLabs[1];
									//$Graph['values'][$legendLabs[0]][] = 0;
								}
									else {
										//$Graph['labels'][$legendLabs[0]] = $legendLabs[1];
										//$Graph['values'][$legendLabs[0]][] = round( ($freq[$key."_".$labVal[0]][$legendLabs[0]]/$total) * 100,2);
																	
										echo number_format(( round( ($freq[$key."_".$labVal[0]][$legendLabs[0]]/$total) * 100,2)),1);
									}
								
								echo "</td>";
							}
							
							
						echo "</tr>";
					}
					echo "<tr>";
						echo "<td class=\"TableHeader\" style=\"text-align:right\">TOTAL</td>";
						foreach ($colTypesLegend[$key] as $legendLabs) {
							echo "<td class=\"TableHeader\">".$total."</td>";
							echo "<td class=\"TableHeader\" >100%</td>";
						}
					echo "</tr>";
			echo "</table>";
			}
				
			if ($_GET['showGraph']) {
					foreach ($Graph['labels'] as $grKey => $grVal) {
						$Graph['syntax'][] = 
						"	{
								name: '".controlGraphString($grVal)."',
						        data: [".implode(",",$Graph['values'][$grKey])."]
							}
						";
					}
					
					$graphHeight = 300 + count($Graph['category']) * 30;
					
					echo  "<script type=\"text/javascript\">
						$(function () {
						   
						    $(document).ready(function() {
						         new Highcharts.Chart({
						            chart: {
						                renderTo: 'container".$key."',
						                type: 'bar'
						                
						            },
						            
						            title: {
						                text: '".controlGraphString(wordwrap($val,70,"<br />"))."'
						                
						            },
						            xAxis: {
						                categories: ['".implode("','",$Graph['category'])."']
						            },
						            yAxis: {
						                min: 0,
						                max: 100,
						                title: {
						                	text: '%'
						           		}
						            },
						            legend: {
						                backgroundColor: '#FFFFFF',
						                reversed: true
						            },
						            tooltip: {
						                formatter: function() {
						                    return ''+
						                        this.series.name +': '+ this.y +'';
						                }
						            },
						            plotOptions: {
						                series: {
						                    stacking: 'normal',
						                
						                   dataLabels: {
								                        enabled: true,
								                        align: 'center',
								                        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
								                        formatter: function() {
								                        	return  this.y +'%';
								                		}
								                    }
										}
						            },
						                credits: {
										    enabled: false
										},
						                series: [".implode(",",$Graph['syntax'])."]
						        });
						    });
						    
						});
						</script>
						<div id=\"container".$key."\" style=\"width: 600px;  height: ".$graphHeight."px; padding-top:20px; margin: 0 auto\"></div>";
				} // end if  ($_GET['showGraph']) 
				else echo "<div  style=\"width: 600px;  height: 50px; \"></div>";
?>