<?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);
	}
}

//rearrange date start
if($array['dStart']!='')
{
	$_SESSION['chek_dStart']=$array['dStart'];
}//end if
else
{
	$array['dStart']=$_SESSION['chek_dStart'];
}//end else

if($array['dEnd']!='')
{
	$_SESSION['chek_dEnd']=$array['dEnd'];
}//end if
else
{
	$array['dEnd']=$_SESSION['chek_dEnd'];
}//end else

$arrDob=explode("-",$array['dStart']);
list($year,$month,$date)=$arrDob;
if($year !='' && $month!='' && $date!=''){
    $objM->dStart=$month."/".$date."/".$year;
}
else{
   $objM->dStart=""; 
}

$arrDob=explode("-",$array['dEnd']);
list($year,$month,$date)=$arrDob;
$objM->dEnd=$month."-".$date."-".$year;
if($year !='' && $month!='' && $date!=''){
    $objM->dEnd=$month."/".$date."/".$year;
}
else
{
   $objM->dEnd=""; 
}

//rearrage date end
include_once('title.inc.php');
?>
<!--<script type="text/javascript" src="../date/date-picker.js"></script>-->
<script type="text/javascript" src="../scripts/calendar.js"></script>
<script type="text/javascript" src="../scripts/calendar-setup.js"></script>
<script type="text/javascript" src="../scripts/calendar-en.js"></script>
<link href="../scripts/calendar.css" rel="stylesheet" type="text/css">
<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>
<?php include_once('tophtml.php');?>
<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">
			<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" colspan="2">Manage Free Code</td>
              </tr>
            </table>
			<div class="bordernew padding2">
				<form action="" method="post" name="frmUser" >
          <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)){ 
                                if ($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="20%" class="maintext2">Free Code<span class="warning">*</span></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 txt-bx"></td>
  				</tr>
                 <tr align="left">
                  <td class="maintext2">&nbsp;</td>
                  <td class="maintext2">Start Date<span class="warning">*</span></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>-->
                      <input name="dStart" id="dStart" type="text" value="<?php echo isset($objM->dStart)?stripslashes($objM->dStart):""?>" maxlength="50" readonly class="textbox txt-bx">
                      <input type="button" value="v" id="button1" name="button1"  class="button_stylenew_new" style=" width:30px;">
                        <script type="text/javascript" language="javascript">
                                Calendar.setup({
                                      inputField    : "dStart",
                                      button        : "button1",
                                      ifFormat      : "%m/%d/%Y",       // format of the input field
                                      showsTime     : false,
                                      timeFormat    : "24"
                                });
                       </script>
                  </td>
                 </tr>
                <tr align="left">
                  <td class="maintext2">&nbsp;</td>
                  <td class="maintext2">End Date<span class="warning">*</span></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>-->
                      <input name="dEnd" id="dEnd" type="text" value="<?php echo $objM->dEnd?>" maxlength="50" readonly class="textbox txt-bx">
                      <input type="button" value="v" id="button12" name="button12"  class="button_stylenew_new" style=" width:30px;">
                        <script type="text/javascript" language="javascript">
                                Calendar.setup({
                                      inputField    : "dEnd",
                                      button        : "button12",
                                      ifFormat      : "%m/%d/%Y",       // format of the input field
                                      showsTime     : false,
                                      timeFormat    : "24"
                                });
                       </script>
                      </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)) { 
                      if ($objDB){ 
					?>
                      <table width="100%"  border="0" cellspacing="0" cellpadding="5">
                      <tr>
                        <td align="left"><table width="100%"  border="0" cellspacing="0" cellpadding="0" class="tblestyle_admin2">
                            <tr>
								<td width="24%">&nbsp;</td>
                              <td width="4%"><input name="BackNow" type="submit" class="back-btn" value="Back"></td>
								<?php //if ($id > 0) { ?>
                              <td width="10%"><input name="SaveNow" type="submit" class="loginbutton" value="Save" onclick="return validate();"><input type="hidden" name="page" value="<?php echo $page?>">
                                      <input type="hidden" name="id" value="<?php echo $id?>"></td>
							  <td>&nbsp;</td>
								<?php //}//end if?>
                              </tr>
                        </table></td>
                      </tr>
                    </table>
					<?php 		
						}//end first if
				?></td>
                  </tr>
                </table></td>
            </tr>
          </table></form>
			</div>
			</td>
          </tr>
        </table>
		</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');?>