<?php
if ($_GET[addphoto] == 1)
include '../library/query.php';
?>

<?php

$SQL         = "SELECT * from LISTINGS WHERE ID = '$_GET[id]'";
$result         = @mysql_query( $SQL ); 
$row         = @mysql_fetch_array( $result );

if ($_GET[unlink] == 1) {
@unlink("$svr_rootscript/product_images/thumb/$_GET[img].jpg");
@unlink("$svr_rootscript/product_images/large/$_GET[img].jpg");
@unlink("$svr_rootscript/product_images/tmp/$_GET[img].jpg");
@unlink("$svr_rootscript/product_images/$_GET[img].jpg");

$newpot = str_replace("$_GET[img]", "", "$row[IMAGEPOT]");

        $sql            = "UPDATE LISTINGS SET
        IMAGEPOT        = '$newpot'
        WHERE ID        = '$_GET[id]'";
        $query = mysql_query($sql);

$SQL         = "SELECT * from LISTINGS WHERE ID = '$_GET[id]'";
$result = @mysql_query( $SQL ); 
$row         = @mysql_fetch_array( $result );

$newpot = str_replace(',,', ",", "$row[IMAGEPOT]");
}

if ($_GET[unlink] == 2) {
@unlink("$svr_rootscript/product_images/thumb/$_GET[img].jpg");
@unlink("$svr_rootscript/product_images/large/$_GET[img].jpg");
@unlink("$svr_rootscript/product_images/tmp/$_GET[img].jpg");
@unlink("$svr_rootscript/product_images/$_GET[img].jpg");

        $sql            = "UPDATE LISTINGS SET
        IMAGENAME        = ''
        WHERE ID        = '$_GET[id]'";
        $query = mysql_query($sql);

}
//--------------------------------------
?>
<?PHP
$chars = "abcdefghijklmnopqrstuvwxyz";
$len = strlen($chars);
mt_srand((double)microtime() * 1000000);
for ($i = 0; $i < 15; $i++)
{
    $randomize = mt_rand(0, $len);
    $imagename .= $chars{$randomize};
}
?>
<?PHP
$chars = "abcdefghijklmnopqrstuvwxyz";
$len = strlen($chars);
mt_srand((double)microtime() * 1000000);
for ($i = 0; $i < 15; $i++)
{
    $randomize = mt_rand(0, $len);
    $imagepot .= $chars{$randomize};
}
?>

<form action="?page=add_photos&addphoto=1&id=<?php echo $_GET[id]; ?>" method="post" enctype="multipart/form-data" name="form" id="form">
  <input type="hidden" name="controller" value="LISTINGS~update~add_photos&amp;id=<?php echo $_GET[id]; ?>~<?php echo $_GET[id]; ?>" />
<table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr> 
      
<td class="td3" colspan="2"><b>Main Photo</b></td>
    </tr>
    <tr> 
      
<td class="td4"  width="50%"> Upload Photo</td>
      <td class="td4"  width="50%"> 
        <input type="hidden" name="IMAGENAME" value="<?php if ($row[IMAGENAME] == $nil) echo $imagename; else echo $row[IMAGENAME]; ?>" />
        <input type="file" name="1" />
        <input type="submit" name="Submit" value="Upload" />
      </td>
    </tr>
    <tr><?php if (!empty($row[IMAGENAME]) && $_GET[unlink] != 2) { ?> 
      
<td class="td4"  width="50%"> Main Photo</td>
<td class="td4"  width="50%"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100%"><img src="../product_images/thumb/<?php echo $row[IMAGENAME]; ?>.jpg" /></td>
<td><a href="?page=add_photos&unlink=2&img=<?php echo "$content"; ?>&id=<?php echo $_GET[id]; ?>"><img src="../images/delete-icon.png" width="48" height="48" border="0"></a></td>
</tr>
</table>
</td>
    </tr><?PHP }?>
  </table>
  
<br>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr> 
      
<td class="td3" colspan="2"> <b>Gallery Photos</b></td>
    </tr>
    <tr> 
      
<td class="td4"  width="50%">Upload Photo</td>
      <td class="td4"  width="50%"> 
        <input type="hidden" name="IMAGEPOT" value="<?php if ($row[IMAGEPOT] != $nil) echo "$row[IMAGEPOT],$imagepot"; else echo "$imagepot," ?>" />
        <input type="hidden" name="IMAGEPOTNEW" value="<?php echo $imagepot; ?>" />
        <input type="file" name="2" />
        <input type="submit" name="Submit" value="Upload" />
      </td>
    </tr>
  </table>
  
<?PHP IF ($row[IMAGEPOT] != $nil) { ?>
<table  width="100%" cellpadding="0" cellspacing="0" border="0">
    <?PHP $pieces = explode(",", "$row[IMAGEPOT]"); 
foreach ($pieces AS $content) { if ($content != $nil) { ?>
    <tr> 
      <td class="td4">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td width="100%"><b> <img src="../product_images/thumb/<?php echo $content; ?>.jpg" /></b></td>
            <td><img src="../images/transparent.gif" width="10" height="10"></td>
            
<td nowrap><a href="?page=add_photos&unlink=1&img=<?php echo "$content"; ?>&id=<?php echo $_GET[id]; ?>"><img src="../images/delete-icon.png" width="48" height="48" border="0"></a></td>
            <td>&nbsp;</td>
          </tr>
        </table>
      </td>
    </tr>
    <?PHP }} ?>
  </table>
  </form>
<?PHP } ?>
