<?php
require_once ("../config.inc.php");
require_once ("../includes/adminlogincheck.inc.php");
require_once ("../includes/mysql.inc.php");
require_once ("../includes/arrays.inc.php");
require_once ("../classes/miscellaneous.class.php");

$page		= isset($_REQUEST['page'])	?	trim($_REQUEST['page'])	:	"1";
$id			= isset($_REQUEST['id'])	?	trim($_REQUEST['id'])	:	"0";
$message	= "";

if (!$objDB) {
	$message	= "Unable to Locate Database Server.";
} else {
	$objM		= new miscellaneous($objDB);
	$objM->set_free_codes(array_merge($_POST, $_FILES));
	if (isset($_POST['SaveNow'])) {
		$message	= $objM->save_free_code($id);
		if ($message == "") {
			header("Location: freecodes.php?page=$page&act=1");
			exit;
		}
	} elseif (isset($_POST['BackNow'])) {
		if ($message == "") {
			header("Location: freecodes.php?page=$page");
			exit;
		}
	}else {
		$array	= $objM->get_free_code($id);
		$objM->set_free_codes($array);
	}
}

include_once('title.inc.php');
?>
<script type="text/javascript" src="../date/date-picker.js"></script>
<style type="text/css">
  div.calendar_widget { position: absolute; top: 0px; left: 0px; width:140px; height: 200px; display: none; }
</style>
<script language="javascript" type="text/javascript">
function validate()
{
	var frm	= document.frmUser;
	
	if (frm.vCode.value == "") {
		alert("Please enter free code.");
		frm.vCode.focus();
		return false;
	}
	if (frm.dStart.value == "") {
		alert("Please enter start date.");
		frm.dStart.focus();
		return false;
	}
	if (frm.dEnd.value == "") {
		alert("Please enter end date.");
		frm.dEnd.focus();
		return false;
	}
	return true;
}
</script>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" class="white"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="72%" valign="top"><?php include_once('tophtml.php');?>
			<table width="100%"  border="0" cellspacing="0" cellpadding="5">
          <tr>
            <td><table width="100%"  border="0" cellspacing="0" cellpadding="5">
              <tr>
                <td width="41%" class="headings1">Manage Free Code</td>
                <td width="59%" class="headings1">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
        </table>
		<form action="" method="post" name="frmUser" onSubmit="return validate();">
          <table width="100%"  border="0" cellspacing="0" cellpadding="5">
            <tr>
              <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
				<?php if (is_resource($objDB)) 
					  { 
				?>
					<?php 	if(isset($message) && $message!='')
							{
					?>
                <tr align="left">
                  <td align="center" class="maintext2">&nbsp;</td>
                  <td colspan="2" class="maintext2 warning"><?php echo $message;?></td>
                  </tr>
					<?php  	}//end if?>
  				<tr align="left">
                  <td width="3%" class="maintext2">&nbsp;</td>
                  <td width="27%" class="maintext2">Free Code</td>
                  <td width="70%" class="maintext2"><input name="vCode" id="ac_me" type="text" value="<?php echo htmlspecialchars($objM->vCode)?>" size="40" maxlength="100" class="textbox"></td>
  				</tr>
                 <tr align="left">
                  <td class="maintext2">&nbsp;</td>
                  <td class="maintext2">Start Date</td>
                  <td class="maintext2"><input id='date' onclick="javascript:void(0);" name="dStart" type="text" value="<?php echo $objM->dStart?>" size="30" maxlength="100" readonly class="textbox">
									<a href="#" onclick="javascript:show_calendar_widget(this);" id="date_link" class="calendar-link"><img src="../images/toggleButton.gif" width="20" height="23" border="0"></a></td>
                 </tr>
                <tr align="left">
                  <td class="maintext2">&nbsp;</td>
                  <td class="maintext2">End Date</td>
                  <td class="maintext2"><input id='date' onclick="javascript:void(0);" name="dEnd" type="text" value="<?php echo $objM->dEnd?>" size="30" maxlength="100" readonly class="textbox">
									<a href="#" onclick="javascript:show_calendar_widget(this);" id="date_link" class="calendar-link"><img src="../images/toggleButton.gif" width="20" height="23" border="0"></a></td>
                </tr>
				<?php 			
						}//end first if
				?>
              </table>
                <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td align="right"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td>&nbsp;</td>
                      </tr>
                    </table>
                      <?php if (is_resource($objDB)) 
					  { 
					?>
                      <table width="100%"  border="0" cellspacing="0" cellpadding="5">
                      <tr>
                        <td align="center"><table width="10%"  border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td><input name="SaveNow" type="submit" class="loginbutton" value="Save"><input type="hidden" name="page" value="<?php echo $page?>">
                                      <input type="hidden" name="id" value="<?php echo $id?>"></td>
								<?php if ($id > 0) { ?>
                              <td><input name="BackNow" type="submit" class="signupbutton" value="Back"></td>
								<?php }//end if?>
                              </tr>
                        </table></td>
                      </tr>
                    </table>
					<?php 		
						}//end first if
				?></td>
                  </tr>
                </table></td>
            </tr>
          </table></form></td>
        </tr>
    </table>      
    </td>
  </tr>
</table>
<div class="calendar_widget" id="calendar_widget" style="display:none "><iframe id='calendar_widget_iframe' name='calendar_widget_iframe' style="border: none;" width=100% height=100% src="../date/calendar_widget.html" /></div>
<?php include_once('bottomhtml.php');?>