<?php
  ///////////////////////////////////////////////////
error_reporting(E_ALL ^ E_NOTICE);
session_start();
include_once("session_class.php");
$session = new Sessions;
if (!$session->is_session(session_id())) { die('Сначала зайдите в игру <a href=/chat.htm>enter</a>') ; }
  ///////////////////////////////////////////////////
?>
<HTML><HEAD>
<link rel=stylesheet type="text/css" href="../i/main.css">
<meta content="text/html; charset=windows-1251" http-equiv=Content-type>
<META Http-Equiv=Cache-Control Content=no-cache>
<meta http-equiv=PRAGMA content=NO-CACHE>
<META Http-Equiv=Expires Content=0>
</HEAD>
<body bgcolor=e2e0e0><p align=right><INPUT TYPE=button value="Вернуться" onClick="top.returned()"></p>
<?php
  //foreach($_POST as $key => $value)
  //  Print $key." - ".$value."<br>";
  include_once('poll/pollconfig.inc');
  include_once("poll/lng/rus_lng.inc");
	include_once('poll/functions.inc');
	include_once('poll/graphic-conf.inc');
  //выбираем форму отображения результатов
  if($ChangeFormPerm == 'yes')
  {
    include_once('poll/graphconf/graph_access.inc');
    $ShowMode = $_POST['ShowMode'];
    if(empty($FileName))
      $FileName = $_POST['filename'];
    if(empty($EndPos))
      $EndPos = $_POST['endpos'];
    print "<form action='showresults.php' method = post>";
      print "<table cellspacing=2 cellpadding=5 width=60% border=1>";
      print "<tr>";
        print "<td>{$Cpt_ShowMode}</td>";
        print "<td>";
          print "<select name=ShowMode>";
            print "<option value=tables>{$Opt_Tables}";
            if($UseHorHist == 'on')
              print "<option value=horhistogrammes>{$Opt_HorHistogrammes}";
            if($UseVertHist == 'on')
              print "<option value=verthistogrammes>{$Opt_VertHistogrammes}";
            if($UsePie == 'on')
              print "<option value=pie>{$Opt_Pie}";
            if($UsePiePlot3D == 'on')
              print "<option value=PiePlot3D>{$Opt_PiePlot3D}";
            if($UseJpGraph_Bar1 == 'on')
              print "<option value=jpgraph_bar1>{$Opt_JpGraph_Bar1}";
            if($UseJpGraph_Bar2 == 'on')
              print "<option value=jpgraph_bar2>{$Opt_JpGraph_Bar2}";
            if($UseScatterPlot == 'on')
              print "<option value=scatterplot>{$Opt_ScatterPlot}";
            //
          print "</select>";
        print "</td>";
        print "<td><input type=submit value='{$Button_OK}'></td>";
      print "</tr>";
      print "</table>";
      print "<input type=hidden name=filename value={$FileName}>";
      print "<input type=hidden name=endpos value={$EndPos}>";
    print "</form>";
    if(!empty($ShowMode))
      $PresentForm = $ShowMode; else
      $PresentForm = $ShowResForm;
  } else $PresentForm = $ShowResForm;

  print $Message;
  if(empty($Polls))
    $Polls = @file($FileName);
  //выводим вопрос
  $Question = StripBBCode($Polls[0]);
  print "<table cellspacing=2 cellpadding=5 width=50% border=0>";
    print "<tr><td><b>{$Question}<b></td></tr>";
  print "</table>";
  //
  for($i = 1; $i < ($EndPos - 2); $i++)
    $PollChosen[] = StripBBCode($Polls[$i]);
  //подсчитываем сумму
  $Total = 0;
  for($i = 1; $i < count($PollChosen); $i = $i + 2)
  {
    $Tmp = (int)$PollChosen[$i];
    $Total += $Tmp;
  }
  //Подсчитываем один процент
  if($Total != 0)
    $_1Percent = 100 / $Total; else $_1Percent = 0;

  //отображаем результаты в виде таблицы
  if($PresentForm == 'tables')
  {
    print "<table cellspacing=2 cellpadding=5 width=50% border=1>";
	    for($i = 0; $i < count($PollChosen); $i = $i + 2)
	    {
	      print "<tr>";
	        print "<td>{$PollChosen[$i]}</td>";
	        print "<td>{$PollChosen[($i+1)]}</td>";
	        print "<td>";
	          print number_format(($PollChosen[($i+1)] * $_1Percent), 2)."%";
	        print "</td>";
	      print "</tr>";
	    }
	    print "<tr><td colspan=3>{$Cpt_Total} {$Total}</td></tr>";
	  print "</table>";
  }

  //горизонтальные гистограммы
  if($PresentForm == 'horhistogrammes')
  {
    $TdWidth = $HorHistImgWidth * 100;
    $TdWidth = "width = {$TdWidth}";
    $inx = 1;
    for($i = 0; $i < count($PollChosen); $i = $i + 2)
	  {

      $Percentage = $PollChosen[($i+1)] * $_1Percent;
      $ImageWidth = round($Percentage * $HorHistImgWidth, 0);
      print "<table cellspacing=2 cellpadding=5 border=1>";
        print "<tr>";
          print "<td colspan=3><b>{$PollChosen[$i]}</b></td>";
	      print "</tr>";
	      print "<tr>";
          if($ImageWidth != 0)
            print "<td {$TdWidth}><img src='poll/pictures/hor_hist_var{$inx}.jpg' width={$ImageWidth} height={$HorHistImgHeight}></td>";
          else
            print "<td>&nbsp</td>";
          print "<td align=center width=40>";
            print number_format($Percentage, 0)."%";
          print "</td>";
	        print "<td align=center width=60>{$PollChosen[($i+1)]}</td>";
	      print "</tr>";
	    print "</table>";
      $inx++;
    }
  }

  //отображаем результаты jpgraph_bar1
  elseif($PresentForm == 'jpgraph_bar1')
  {
    include_once ("poll/graphconf/bar1.inc");
    $inx = 1;
    for($i = 0; $i < count($PollChosen); $i = $i + 2)
    {
      $VotedArr[] = $_1Percent * $PollChosen[($i+1)];
      $LegendArr[] = $inx;
      $VariantsArr[] = "<tr><td width=25>{$inx}</td><td>{$PollChosen[$i]}</td></tr>";
      $inx++;
    }
    $Time = trim(StripBBCode($Polls[$EndPos]));
    $ImgFile = "poll/pictures/jpgraph_bar1{$Time}.jpg";
    if(!file_exists($ImgFile) || ($Message == $Cpt_Accepted))
      GenJpGraphBar1($VotedArr, $LegendArr, $ImgFile, $VarRGB);
    //показывам картинку
    print "<table cellspacing=0, cellpadding=0 border=0>";
      print "<tr><td><img src='{$ImgFile}' width={$Width} height={$Height}></td></tr>";
    print "</table>";
    print "<table cellspacing=0, cellpadding=0 width=400 border=0>";
      for($i = 0; $i < count($VariantsArr); $i++)
        print $VariantsArr[$i];
    print "</table>";
  }
   //отображаем результаты jpgraph_bar1
  elseif($PresentForm == 'jpgraph_bar2')
	{
    include_once ("poll/graphconf/bar2.inc");
    $inx = 1;
    for($i = 0; $i < count($PollChosen); $i = $i + 2)
    {
      $VotedArr[] = $_1Percent * $PollChosen[($i+1)];
      $LegendArr[] = $inx;
      $VariantsArr[] = "<tr><td width=25>{$inx}</td><td>{$PollChosen[$i]}</td></tr>";
      $inx++;
    }
    $Time = trim(StripBBCode($Polls[$EndPos]));
    $ImgFile = "poll/pictures/jpgraph_bar2{$Time}.jpg";
    if(!file_exists($ImgFile) || ($Message == $Cpt_Accepted))
      GenJpGraphBar2($VotedArr, $LegendArr, $ImgFile, $VarRGB);
    //показывам картинку
    print "<table cellspacing=0, cellpadding=0 border=0>";
      print "<tr><td><img src='{$ImgFile}' width={$Width} height={$Height}></td></tr>";
    print "</table>";
    print "<table cellspacing=0, cellpadding=0 width=400 border=0>";
      for($i = 0; $i < count($VariantsArr); $i++)
        print $VariantsArr[$i];
    print "</table>";
	}
  //круговые диаграммы
  elseif($PresentForm == 'pie')
  {
    include_once ("poll/graphconf/pie.inc");
    ///////////////////////////////
    $inx = 1;
    for($i = 0; $i < count($PollChosen); $i = $i + 2)
    {
      $VotedArr[] = $_1Percent * $PollChosen[($i+1)];
      $LegendArr[] = "{$inx} - ".trim($PollChosen[($i+1)]);
      $VariantsArr[] = "<tr><td width=25>{$inx}</td><td>{$PollChosen[$i]}</td></tr>";
      $inx++;
    }
    //print var_dump($LegendArr);
    ////////////////////////////
    $ColorsArr = RGBToHex();
    ///////////////////////////////
    $Time = trim(StripBBCode($Polls[$EndPos]));
    $ImgFile = "poll/pictures/pie{$Time}.jpg";
    if(!file_exists($ImgFile) || ($Message == $Cpt_Accepted))
      GenPie($VotedArr, $LegendArr, $ColorsArr, $ImgFile);
    print "<table cellspacing=0, cellpadding=0 width=400 border=0>";
      print "<tr><td><img src='{$ImgFile}' width={$Width} height={$Height}></td></tr>";
    print "</table>";
    print "<table cellspacing=0, cellpadding=0 width=400 border=0>";
      for($i = 0; $i < count($VariantsArr); $i++)
        print $VariantsArr[$i];
    print "</table>";
  }

  //отображаем результаты в виде 3д диаграммы
  elseif($PresentForm == 'PiePlot3D')
  {
    include_once ("poll/graphconf/pieplot3d.inc");
    ///////////////////////////////
    $inx = 1;
    for($i = 0; $i < count($PollChosen); $i = $i + 2)
    {
      $VotedArr[] = $_1Percent * $PollChosen[($i+1)];
      $LegendArr[] = "{$inx} - ".trim($PollChosen[($i+1)]);
      $VariantsArr[] = "<tr><td width=25>{$inx}</td><td>{$PollChosen[$i]}</td></tr>";
      $inx++;
    }
    //print var_dump($LegendArr);
    ////////////////////////////
    $ColorsArr = RGBToHex();
    ///////////////////////////////
    $Time = trim(StripBBCode($Polls[$EndPos]));
    $ImgFile = "poll/pictures/pieplot3d{$Time}.jpg";
    if(!file_exists($ImgFile) || ($Message == $Cpt_Accepted))
      GenPiePlot3d($VotedArr, $LegendArr, $ColorsArr, $ImgFile);
    print "<table cellspacing=0, cellpadding=0 width=400 border=0>";
      print "<tr><td><img src='{$ImgFile}' width={$Width} height={$Height}></td></tr>";
    print "</table>";
    print "<table cellspacing=0, cellpadding=0 width=400 border=0>";
      for($i = 0; $i < count($VariantsArr); $i++)
        print $VariantsArr[$i];
    print "</table>";
  }
  //scatterplot
  elseif($PresentForm == 'scatterplot')
	{
    include_once ("poll/graphconf/scatterplot.inc");
    $inx = 1;
    for($i = 0; $i < count($PollChosen); $i = $i + 2)
    {
      $VotedArr[] = $_1Percent * $PollChosen[($i+1)];
      $LegendArr[] = $inx;
      $VariantsArr[] = "<tr><td width=25>{$inx}</td><td>{$PollChosen[$i]}</td></tr>";
      $inx++;
    }
    $Time = trim(StripBBCode($Polls[$EndPos]));
    $ImgFile = "poll/pictures/scatterplot{$Time}.jpg";
    if(!file_exists($ImgFile) || ($Message == $Cpt_Accepted))
      GenScatterPlot($VotedArr, $LegendArr, $ImgFile);
    //показывам картинку
    print "<table cellspacing=0, cellpadding=0 border=0>";
      print "<tr><td><img src='{$ImgFile}' width={$Width} height={$Height}></td></tr>";
    print "</table>";
    print "<table cellspacing=0, cellpadding=0 width=400 border=0>";
      for($i = 0; $i < count($VariantsArr); $i++)
        print $VariantsArr[$i];
    print "</table>";
	}

  //вертикальные гистограммы
  if($PresentForm == 'verthistogrammes')
  {
    print "<table cellspacing=0 cellpadding=0 width=50% border=1>";
    //общая сумма за каждый голос
    print "<tr align=center>";
    $Count = count($PollChosen);
    for($i = 1; $i < $Count; $i = $i + 2)
    {
      print "<td>{$PollChosen[$i]}</td>";

    }
    print "</tr>";
    //вертикальная гистограмма
    $TrHeight = 100 * $VertHistImgHeight;
    $TrHeight = "height = {$TrHeight}";
    print "<tr align=center>";
    $inx = 1;
    for($i = 0; $i < $Count; $i = $i + 2)
    {
      $Percentage = number_format($PollChosen[($i+1)] * $_1Percent, 0);
      $PercentageArr[$inx] = $Percentage;
      $ImgHeight = $Percentage * $VertHistImgHeight;
      $ImgWidth = $VertHistImgWidth;
      print "<td valign=bottom {$TrHeight}><img src='poll/pictures/vert_hist_var{$inx}.jpg' width={$ImgWidth} height={$ImgHeight}></td>";
      $TrHeight = '';
      $inx++;
    }
    print "</tr>";
    //номера вариантов ответа
    print "<tr align=center>";
    for($i = 1; $i <= $Count/2; $i++)
      print "<td><b>{$i}</b></td>";
    print "</tr>";
    print "</table>";
    //легенда
    if($ShowLegend == 'on')
    {
      print "<table cellspacing=0 cellpadding=0 width=50% border=1>";
	    $inx=1;
	    for($i = 0; $i < $Count; $i = $i + 2)
	    {
	      print "<tr>";
	        print "<td width= 30>{$inx}</td>";
	        print "<td>{$PollChosen[$i]}</td>";
	        print "<td align=center width=40>{$PercentageArr[$inx]}%</td>";
	        $inx++;
	      print "</tr>";
	    }
	    print "</table>";
    }
  }
?>