<?php
include('config/config.php');
include('includes/classes/class.Vote.php');

$objVote=new Vote();

if(isset($_SESSION['QuestionAttempt']))
{
	$questionIds=array();
	$voterQuestions=array();
	$questionIds=$_SESSION['QuestionAttempt'];
	
	$traceParty=array();
	$tracePoints=array();
	
	foreach($questionIds as $quest=>$key)
	{
		if(!$objVote->checkSkipAnswer($key))
		{
			array_push($voterQuestions,$key);
		}
	}
	
	$partyRecord=$objVote->getAllParties();

	foreach($voterQuestions as $key=>$value)
	{
	  $voterData=$objVote->getVoterResponses($value);
	  
	  $allParties=$objVote->getAllParties();
	    
	  foreach($allParties as $party)
	  {
		  $partyData=$objVote->getPartyRespones($party->Id,$voterData[0]->QuestionId);
		  
		  if($partyData[0]->Answer==$voterData[0]->Answer)
		  {
			  if($partyData[0]->PriorityBit=='1' && $voterData[0]->PriorityBit=='1')
			  {
				 $points=$party->Points;
				 $points+=4;
				 $objVote->updatePoints($party->Id,$points); 
			  }
			  else if($partyData[0]->PriorityBit=='1' && $voterData[0]->PriorityBit!='1')
			  {
				$points=$party->Points;
				$points+=2;
				$objVote->updatePoints($party->Id,$points);  
			  }
			  else if($partyData[0]->PriorityBit!='1' && $voterData[0]->PriorityBit=='1')
			  {
				$points=$party->Points;
				$points+=2;
				$objVote->updatePoints($party->Id,$points);  
			  }
			  else
			  {
				$points=$party->Points;
				 $points+=1;
				 $objVote->updatePoints($party->Id,$points);   
			  }
		  }
		  else
		  {
			 if($voterData[0]->Answer=='A' && $partyData[0]->Answer=='D')
			 {
				if($partyData[0]->PriorityBit=='1' && $voterData[0]->PriorityBit=='1')
			    {
					$points=$party->Points;
				 	$points-=4;
				 	$objVote->updatePoints($party->Id,$points);
				}
				if($partyData[0]->PriorityBit=='1' && $voterData[0]->PriorityBit!='1')
			    {
					$points=$party->Points;
				 	$points-=2;
				 	$objVote->updatePoints($party->Id,$points);
				}
				if($partyData[0]->PriorityBit!='1' && $voterData[0]->PriorityBit=='1')
			    {
					$points=$party->Points;
				 	$points-=2;
				 	$objVote->updatePoints($party->Id,$points);
				}
				else
				{
					$points=$party->Points;
				 	$points-=1;
				 	$objVote->updatePoints($party->Id,$points);
				}
			 }
			 else if($voterData[0]->Answer=='D' && $partyData[0]->Answer=='A')
			 {
				if($partyData[0]->PriorityBit=='1' && $voterData[0]->PriorityBit=='1')
			    {
					$points=$party->Points;
				 	$points-=4;
				 	$objVote->updatePoints($party->Id,$points);
				}
				if($partyData[0]->PriorityBit=='1' && $voterData[0]->PriorityBit!='1')
			    {
					$points=$party->Points;
				 	$points-=2;
				 	$objVote->updatePoints($party->Id,$points);
				}
				if($partyData[0]->PriorityBit!='1' && $voterData[0]->PriorityBit=='1')
			    {
					$points=$party->Points;
				 	$points-=2;
				 	$objVote->updatePoints($party->Id,$points);
				}
				else
				{
					$points=$party->Points;
				 	$points-=1;
				 	$objVote->updatePoints($party->Id,$points);
				}
			 }
			 else if($voterData[0]->Answer=='A' && $partyData[0]->Answer=='N')
			 {
				if($partyData[0]->PriorityBit=='1' && $voterData[0]->PriorityBit=='1')
			    {
					$points=$party->Points;
				 	$points-=1;
				 	$objVote->updatePoints($party->Id,$points);
				}
				if($partyData[0]->PriorityBit=='1' && $voterData[0]->PriorityBit!='1')
			    {
					$points=$party->Points;
				 	$points-=1;
				 	$objVote->updatePoints($party->Id,$points);
				}
				if($partyData[0]->PriorityBit!='1' && $voterData[0]->PriorityBit=='1')
			    {
					$points=$party->Points;
				 	$points-=1;
				 	$objVote->updatePoints($party->Id,$points);
				}
				else
				{
					$points=$party->Points;
				 	$points+=0;
				 	$objVote->updatePoints($party->Id,$points);
				} 
			 }
			 else if($voterData[0]->Answer=='N' && $partyData[0]->Answer=='A')
			 {
				if($partyData[0]->PriorityBit=='1' && $voterData[0]->PriorityBit=='1')
			    {
					$points=$party->Points;
				 	$points-=2;
				 	$objVote->updatePoints($party->Id,$points);
				}
				if($partyData[0]->PriorityBit=='1' && $voterData[0]->PriorityBit!='1')
			    {
					$points=$party->Points;
				 	$points-=1;
				 	$objVote->updatePoints($party->Id,$points);
				}
				if($partyData[0]->PriorityBit!='1' && $voterData[0]->PriorityBit=='1')
			    {
					$points=$party->Points;
				 	$points-=1;
				 	$objVote->updatePoints($party->Id,$points);
				}
				else
				{
					$points=$party->Points;
				 	$points+=0;
				 	$objVote->updatePoints($party->Id,$points);
				}
			 }			 
			 
		  }
		  
	  }
	  	
	}
	
	$newPartyRecord=$objVote->getAllParties();
	
	for($i=0;$i<sizeof($newPartyRecord);$i++)
	{
		$traceParty[$newPartyRecord[$i]->Id]=($newPartyRecord[$i]->Points)-($partyRecord[$i]->Points);
	}
	arsort($traceParty);
	unset($_SESSION['QuestionAttempt']);
}
else
{
header('location:index.php');
exit;
}
?>
<!doctype html>  
<html dir="ltr" lang="en-US">
	
	<head>
		<title>Voting Advice Application</title>
		<!-- generic demo style -->
		<link rel="stylesheet" href="./css/reset.css" />
		<link rel="stylesheet" href="./css/demo.css" />
		<!-- scripts -->
		<link rel="stylesheet" href="css/jquery-ui.css">
		<script src="js/jquery-1.10.2.js"></script>
        <script src="js/jquery-ui.js"></script>
        
        <script>
		  $(function() {
			$("#accordion").accordion({
			  collapsible: true,
    		  active: false,
    		  heightStyle: 'content'
			});
		  });
		 </script>
		<!-- shiv me up baby! -->
		<!--[if lt IE 9]>
			<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
		<![endif]-->
		  <style>
			html{
				width:100%;
				height:100%;
			}
			
			body{
				width:100%;
				height:100%;
			}
			
			#header{
				width:100%;
			}
			
			#container
			{
			  width:100%;	
			}
			
			#footer{
				width:100%;
			}
        </style>
         
	</head>	
    <body>
		<div id="header">
			<div id="header-wrapper">
				<h1>Voting Advice Application</h1>
				<h2>An Advice Application</h2>
				<p><a href="index.php">Home</a> | <a href="statistics/index.php">Statistics</a></p>
			</div>
			<div class="gradient">&nbsp;</div>
		</div>
		
		<div id="container">
		 <div id="wrapper">
			<h2 id="introduction">Your Results</h2>
			
			<table style="border:1px solid #333 !important; width:100%;">
			 
			 <thead style="background: #D9D9D9;">
			  <th style="border-bottom:1px solid #000000 !important;border-right:1px solid #000000 !important;text-align:center; height:30px;" width="8%"></th>
			  <th style="border-bottom:1px solid #000000 !important;border-right:1px solid #000000 !important;text-align:center; height:30px;" width="10%">Logo</th>
			  <th style="border-bottom:1px solid #000000 !important;border-right:1px solid #000000 !important;text-align:center; height:30px;" width="25%">Candidate/Party</th>
			  <th style="border-bottom:1px solid #000000 !important;border-right:1px solid #000000 !important;text-align:center; height:30px;" width="45%">Overview</th>
			  <th style="border-bottom:1px solid #000000 !important;border-right:1px solid #000000 !important;text-align:center; height:30px;" width="20%">Points</th>
			 </thead>
			 
			 <tbody>
			 <?php
			 $i=0;
			 foreach($traceParty as $key=>$value)
			 {
				 $i++;
				 $partyData=$objVote->getPartyData($key);
			 ?>
			 <tr>
			   <td style="border-bottom:1px solid #000000 !important;border-right:1px solid #000000 !important; height:10px;" align="center"><?php echo $i;?></td>
			   <td style="border-bottom:1px solid #000000 !important;border-right:1px solid #000000 !important; height:10px;" align="center"><img src="uploads/partylogo/thumbs/<?php echo "thumb_".$partyData[0]->Logo;?>"/></td>
			   <td style="border-bottom:1px solid #000000 !important;border-right:1px solid #000000 !important; height:10px;" align="center"><?php echo $partyData[0]->Name." (".$partyData[0]->Party.")";?></td>
			   <td style="border-bottom:1px solid #000000 !important;border-right:1px solid #000000 !important; height:10px;">&nbsp;<?php echo " ".$partyData[0]->Description;?></td>
			   <td style="border-bottom:1px solid #000000 !important;border-right:1px solid #000000 !important; height:10px;" align="center"><?php echo $value;?></td>
			  <tr/>
			  <?php
			 }
			  ?>
			 </tbody>
			 
			</table>
						
		 </div>
	    </div> 	
		
		<div id="footer">
			<div class="gradient">&nbsp;</div>
			<div id="footer-wrapper">
			<p>&copy; <a href="https://klaus-uwe.me">Klaus-Uwe Mitterer</a> 2014</p>
			</div>
		</div>

	</body>
</html>
