<?php

				
	 			$sql = " SELECT id_answer, sc_value, q_name 
					 		FROM ".DB_WARPIT_WEBCATI.".".$proj."_mystery AS a
							LEFT JOIN ".DB_WARPIT_WEBCATI.".".$proj." AS b ON a.id_question = b.id WHERE q_name = '".$key."'";
	 			//echo $sql;
	 			$res = $db->SQLexecute($sql);
	 			while ($zad = $db->fetchAssoc($res)) $scoreFactors[$zad['q_name']][$zad['id_answer']] = abs($zad['sc_value']);	
							
				
					
				$sql = "SELECT count(*) as n, ".$key." as varName FROM ".$proj."_structure WHERE ".$key." > 0 ";
					if ($filter) $sql .=" AND ".$filter;	
				$sql .= " GROUP BY ".$key;
				//echo $sql;
				$res = $db->SQLexecute($sql);
				
				while ($zad = $db->fetchAssoc($res)) {
					$freq[$zad['varName']] = $zad['n'];	
					$total +=  $zad['n'];	
					
					// ponders
					$freqPonder[$zad['varName']] = $zad['n'] * $scoreFactors[$key][$zad['varName']] ;	
					$totalPonder +=  $zad['n'] * $scoreFactors[$key][$zad['varName']];	
				}
				foreach ($colAns[$key] as $labVal) {
					$Graph['labels'][] = $labVal[1];
					$Graph['values'][] =  round( ($freq[$labVal[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>
							 		<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 ($colAns[$key] as $labVal) {
							echo "<tr>";
								echo "<td>".$labVal[1]."</td>";
								echo "<td align=\"center\">";
									if (!$freq[$labVal[0]]) echo "0";
										else echo $freq[$labVal[0]];
								echo "</td>";
								echo "<td align=\"center\">";
									if (!$freq[$labVal[0]]) echo "0";
										else {					
											echo number_format(( round( ($freq[$labVal[0]]/$total) * 100,2)),1);
										}
									
								echo "</td>";
						if ($_GET['showPonders']) {
								echo "<td align=\"center\">";
									if (!$freqPonder[$labVal[0]]) echo "0";
										else echo$freqPonder[$labVal[0]];
								echo "</td>";
								echo "<td align=\"center\">";
									if (!$freqPonder[$labVal[0]]) echo "0";
										else {							
											echo number_format(( round( ($freqPonder[$labVal[0]]/$totalPonder) * 100,2)),1);
										}
									
								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>";
				}
				if ($_GET['showGraph']) {
					if (count($Graph['labels']) > 3) { 
							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 />"))."',
								                margin: 50
								            },
								            tooltip: {
								                formatter: function() {
								               // console.log(this);
								                    return this.x +': '+ this.y + '%';
								                }
							            	},
								            credits: {
											    enabled: false
											},
								            xAxis: {
							                	categories: ['".implode("','",$Graph['labels'])."']
								            },
								            yAxis: {
							              		min: 0,
								          	    
								                title: {
								                	text: '%'
								           		}
							            	},
							            	legend: {
							            		enabled: false
							            	},
							            	  plotOptions: {
									                bar: {
									                    dataLabels: {
									                        enabled: true,
									                         formatter: function(a,val) {									                    
										                       return this.y +' %';
										               		 }
									                    }
									                   
									                }
									            },
				
									         series: [{
								               
								               
								                data: [
								         ";
							       
								                 	unset($tmp);
													foreach ($Graph['labels'] as $key1=>$val) {
															
															$tmp[] = "['".$val."',".round($Graph['values'][$key1],1)."]";
												        
														} 
													echo implode(",",$tmp);
												
								echo "             
								                ]
								            }]
								        });
								    });
								    
								});
								</script>	
						
									<div id=\"container".$key."\" style=\"width: 600px;  height: 400px; padding-top:20px; margin: 0 auto\"></div>";
					
					} else {
					
					echo  "<script type=\"text/javascript\">
					$(function () {
						    
						    $(document).ready(function() {
						         new Highcharts.Chart({
						            chart: {
						                renderTo: 'container".$key."',
						                plotBackgroundColor: null,
						                plotBorderWidth: null,
						                plotShadow: false
						            },
						            title: {
						                text: '".controlGraphString(wordwrap($val,60,"<br />"))."'
						            },
						            tooltip: {
						        	    pointFormat: '<b>{point.percentage}%</b>',
						            	percentageDecimals: 1
						            },
						            credits: {
									    enabled: false
									},
						            plotOptions: {
						                pie: {
						                    allowPointSelect: true,
						                    cursor: 'pointer',
						                    dataLabels: {
						                        enabled: true,
						                        
						                        color: '#000000',
						                        connectorColor: '#000000',
						                        formatter: function() {
						                      //  console.log(this);
						                            return '<b>'+ this.point.name +'</b>: '+ this.point.y +' %';
						                        }
						                    }
						                }
						            },
						            series: [{
						                type: 'pie',
						                
						                data: [
						         ";
					       
						                 	unset($tmp);
											foreach ($Graph['labels'] as $key1=>$val) {
													
													$tmp[] = "['".$val."',".round($Graph['values'][$key1],1)."]";
										        
												} 
											echo implode(",",$tmp);
										
						echo "             
						                ]
						            }]
						        });
						    });
						    
						});
						</script>	
				
							<div id=\"container".$key."\" style=\"width: 600px;  height: 400px; padding-top:20px; margin: 0 auto\"></div>";
					}
				} // end if  ($_GET['showGraph']) 
				else echo "<div  style=\"width: 600px;  height: 50px; \"></div>";
?>