<?php
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');
$obj = new DBClass();
$dat = date('Y-m-d');
$headin = 'Add New Option Value';
$buton = 'Add Option Value';
$cupage = 'cat';
$sql1 = "SELECT * FROM ".TBL_OPTIONS;
$rs1 = $obj->executeQry($sql1,$link);
$row1 = $obj->fetchrow();	 

if(isset($_GET['action']) && $_GET['action'] == 'edit'){
	$headin = 'Edit Option Value';
	$buton = 'Edit Option Value';
	$obj2 = new DBClass();
	$sql = "SELECT * FROM ".TBL_OPTIONS." WHERE id = '".$_GET['id']."' ";
	$rs = $obj2->executeQry($sql,$link);
	$row = $obj2->fetchrow();
}

if(isset($_POST['submit'])){
        $valid = validmess($_POST['option'], "Listing Option"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
        $valid = validmess($_POST['value'], "Option Value"); if(isset($valid) && $valid != ''){ $error[] = $valid;}

	if($error == ''){
		if($_POST['submit'] == 'Add Option Value'){
			if($row1['option'] == $_POST['option'] && $row1['value'] == $_POST['value']){
				$error[] = 'This option value already exist...';
			} else {
				echo $sqll = "INSERT INTO ".TBL_OPTIONS." (`option`, `value`) VALUES ('".trim($_POST['option'])."','".trim($_POST['value'])."') ";
				$rs2 = $obj->executeQry($sqll,$link);
				if($rs2){
					@header('Location: option_value.php?msg=add');
				}
			}
	        }
		if($_POST['submit'] == 'Edit Option Value'){
			$sqll = "UPDATE ".TBL_OPTIONS." SET value = '".trim($_POST['value'])."' WHERE id = '".$_GET['id']."'";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: option_value.php?msg=edit');
			}
		}
	}	
}

require_once(INCLUDES_DIR.ADMHEADER); 
?>
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="29" align="center" class="submenu"><a href="options.php">Manage Listing Options</a></td>
    <td align="center" class="submenu"><a href="add_options.php">Add New Listing Option</a></td>
    <td align="center" class="submenu"><a href="option_value.php">Manage Option Value</a></td>
    <td align="center" class="submenu">Add Option Value</td>
  </tr>
</table>
<table width="1000" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="17"></td>
  </tr>
  <tr>
    <td><table width="910" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td class="pagehead"><table width="900" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="315" height="35"><?php echo $headin;?></td>
            </tr>
        </table></td>
      </tr>
      <tr>
        <td valign="top" bgcolor="#FFFFFF" class="innermain"><table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="25"><?php ShowError($error); ShowInfo($info);?></td>
          </tr>
	<form name="pageform" id="pageform" method="post" action="">
          <tr>
            <td valign="top" class="bdr4"><table width="800" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td bgcolor="#990066" class="form_head">&nbsp;</td>
              </tr>
              <tr>
                <td align="left" valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
                    <td align="right" width="30%">Listing Option:</td>
                    <td width="70%"><select name="option">
					<option value="">Select</option>
<?php 
$obj1 = new DBClass();
$sql2 = "SELECT DISTINCT(`option`) FROM ".TBL_OPTIONS." WHERE status = 0";
$rs2 = $obj1->executeQry($sql2,$link);
while($row2 = $obj1->fetchrow()){ 
?>          
			<option value="<?php echo $row2['option'];?>" <?php if(($row['option'] == $row2['option']) || ($_POST['option'] == $row2['option'])) echo 'selected'; ?>><?php echo $row2['option'];?></option>
<?php } ?>
			</select></td>
                  </tr>
                  <tr>
                    <td align="right" width="30%">Option Value:</td>
                    <td width="70%"><input type="text" name="value" id="value" value="<?php echo ($_POST['value'] == '') ? $row['value'] : $_POST['value']; ?>" /></td>
                  </tr>
                  </table></td>
              </tr>
              <tr>
                <td align="right" valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td align="center"><input name="submit" type="submit" class="bg_bt" id="button" value="<?php echo $buton;?>" /></td>
                  </tr>
                  </table></td>
              </tr>
</form>
              </table></td>
          </tr>
          </table></td>
      </tr>
      </table></td>
  </tr>
  <tr>
    <td height="40">&nbsp;</td>
  </tr>
</table>

<?php require_once(INCLUDES_DIR.ADMFOOTER); ?>