<?php
//<HEAD>
if (isset($_REQUEST['mockfbmltext'])) {
  echo $_REQUEST['mockfbmltext'];
  exit;
}

require_once 'dbappinclude.php';

//--------------------------------------------
// get data
//--------------------------------------------
$rs = query("select distinct task from times where user = $user order by task");
$tasks = array();
$sums = array();
$log = array();
$x = 0;
while ($row = mysql_fetch_assoc($rs)) {
  $tasks[$x] = $row["task"];
  $rsSum = query("select SUM(seconds) as \"sum\" from times where user = $user and task =\"$tasks[$x]\"");
  if ($rowSum = mysql_fetch_assoc($rsSum)) {
    $sums[$x] = $rowSum["sum"];
  } else {
    $sums[$x] = -1;
  }
  $rsRun = query("select UNIX_TIMESTAMP(logdate) as log from times where user = $user and code = 1 and task=\"$tasks[$x]\"");
  if ($rowRun = mysql_fetch_assoc($rsRun)) {
    $log[$x] = $rowRun["log"];
  } else {
    $log[$x] = -1;
  }
  $x++;
}
//--------------------------------------------
// create the Javascript to update runing times
//--------------------------------------------
echo "<SCRIPT language=\"JavaScript\">\n";
echo "var startSeconds=Math.round(new Date().getTime()/1000);\n";
echo "<!--\n";
echo "function startlocker(){\n";
echo "var seconds=Math.round(new Date().getTime()/1000);\n";

  $i=0;
  $clockflag=0;
  $moriaTime = time();
  while($i <$x){
     if ($log[$i]<0){
       echo "document.form".$i.".update.value=convertSec($sums[$i]);\n";
     } else {
       echo "document.form".$i.".update.value=convertSec($sums[$i]+seconds-startSeconds-$log[$i]+$moriaTime)+\" **\";\n";
       $clockflag=1;
     }
     $i++;
  }
  echo "setTimeout('startlocker()',1000);\n";
  echo "} \n";
  //--------------------------------------------
  echo "function convertSec(sec){\n";
  echo "var s = sec % 60;";
  echo "var m = Math.floor(sec / 60) % 60;";
  echo "var h = Math.floor(sec / 3600);";
  echo "if (h<1 && m<1) ";
  echo "  return s;";
  echo "if (h<1)";
  echo '  return m + ":" + ( s < 10 ? "0" : "" )+ s;';
  echo 'return h+  ":" + ( m < 10 ? "0" : "" )+ m + ":" + ( s < 10 ? "0" : "" )+ s;';
  echo "}\n";
  //--------------------------------------------

  echo "</SCRIPT>\n";
//  echo "</HEAD>\n";
  if ($clockflag = 0)
    echo "<BODY>\n";
  else
    echo "<BODY onLoad=\"startlocker()\">\n";
//--------------------------------------------
// greet
//--------------------------------------------
echo "<p>hello $user (hmmm a name would be nice...)";
//--------------------------------------------
// make table
//--------------------------------------------
echo "<table border=0><tr><td align=right>task</td><td>time</td><td>adjust</td></tr>\n";
$i=0;
while($i <$x){
    echo '<td align=right><form action="startstop.php" method="submit">';
    echo "<input name=\"taskname\" type=\"submit\" value=\"$tasks[$i]\">";
    echo "</form></td>\n";
    if ($log[$i] > 0)
      echo "<td valign=middle BGCOLOR=\"#CCCCAA\">";
    else
      echo "<td valign=middle>";
    echo "<FORM name=\"form$i\">\n";
    echo "<INPUT TYPE=\"text\" name=\"update\" size=\"8\"></FORM>";
    echo "</td>\n";
    echo "<td><form action=\"adjust.php?taskname=$tasks[$i]\" method=\"get\">";
    echo '<input name="adjust" text="text" size="8" value="">';
    echo "<input type=hidden name=\"taskname\" value=\"$tasks[$i]\">";
    echo "</form></td>\n";
    echo "</tr>\n";
     $i++;
}
echo "</table>\n";
//--------------------------------------------
// delete and add tasks
//--------------------------------------------
echo "<table border=0><tr>\n";
echo '<td>new task<form action="insert.php" method="get">';
echo '<input name="taskname" type="text" size="20" value=""><br>';
echo "</form></td></tr><tr>\n";

echo "<td><form action =\"delete.php\" method=get>";
echo "<label for=\"taskname\"></label>\n <select id=\"taskname\" name=\"taskname\" >";
$i=0;
while($i <$x){
   echo "<option>".$tasks[$i]."</option>\n";
   $i++;
}
echo "</select><label for=\"dir\"></label><input type=\"submit\" value=\"delete\" ></form>";
echo "</td></tr></table\n";

?>
</BODY>
