<?
require 'at_dbconnect.php';
$hasmagnet=false;
if($_COOKIE['magnet'])
{
     $hasmagnet=true;
}

$result = mysql_query("SELECT COUNT(*) FROM tblMagnets");
    if (!$result) {
      echo("error=5");
      exit();
    }
    while ($row=mysql_fetch_row($result)){
          if($row[0]<=30&&!$hasmagnet)
          {
             //30 or less - ok to proceed
             //calculate expiry date, now + 1 week(in seconds)
             $ts=time();
             $ts=$ts+604800;
             echo($ts);
             $add_magnet="INSERT INTO tblMagnets SET Expires=FROM_UNIXTIME($ts)";
             if (mysql_query($add_magnet))
             {
                echo("<P>Your magnet has been added.</P>");
                //set cookie using id (cast as string) as value
                $new_id=mysql_insert_id();
                $insert_time=time();
                setCookie("magnet","".mysql_insert_id(),$ts);
                $add_move="INSERT INTO tblMoves (Magnet, Xpos, Ypos, Symbol, MovedAt)";
                $add_move.= " VALUES ($new_id, 208, 174, 4, FROM_UNIXTIME($insert_time))";
                if (!mysql_query($add_move))
                {
                echo("<P>Error adding magnet: " .
                mysql_error() . "</P>");
                exit();
                }
                echo("start=1&mymagnet=$new_id&mymagnet_x=208&mymagnet_y=174&end=1");
             }
             else
             {
                echo("<P>Error adding magnet: " .
                mysql_error() . "</P>");
             }


          }
          else
          {
             //no magnets available - try again when next magnet expires
             echo("start=1&mymagnet=-1&mymagnet_x=261&mymagnet_y=231&end=1");
          }
    }
       //header("Location: http://www.spikeit.org.uk/beta/atmosphere.html");
       exit();
?>
<HTML>
<HEAD>
 <TITLE>New Document</TITLE>
</HEAD>
<BODY>

</BODY>
</HTML>
