<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');
?>
<script type="text/javascript">
function chk_valid_date() {
	if (document.bannerFrm.validFrom.value==0) {
		alert("Please select Validity Period");
		return false;
	}
	if (document.bannerFrm.validTo.value==0) {
		alert("Please select Validity Period");
		return false;
	}
}
</script>
<?php
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)<0 or $start=="")?0:$start;
$pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;

$heading="Manage Banners";
$record_type="Banner";
$script_name="manage_banners_allpages.php";
$table_name="banner_tbl";
####################################################

if($id=="add_record" && $_FILES[banner][name]!="") {
	if($_FILES[banner][name]!="") {
		$file_extension=strtolower(getExtension($_FILES[banner][name]));
		if($file_extension!="jpg" && $file_extension!="gif" && $file_extension!="jpeg" && $file_extension!="png") {
			$form_check_alert .="<li>Please upload small image(.jpg,.gif,.png Format Only)</li>";
		}
	}
	if(!strlen($form_check_alert)>0) {
		if($_FILES[banner][name]!="") {
			$img_name="banner_".date("d_m_Y_H_i_s");
			$img1=upload_file($img_name,"banner","uploaded_files/banner_images");
		}
		$bannerUrl="http://".$bannerUrl;
		$sql=db_query("insert into $table_name set personName='$personName',companyName	='$companyName',pageid='$pageid', position='$position', bannerUrl='$bannerUrl', banner='$img1', activationDate='$validFrom', expiryDate='$validTo'");
		$_SESSION[session_message]='New '.$record_type.' has been added successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	else {
		$msg=html_head();
		$msg.=$form_check_alert;
		$msg.=html_foot();
	}
}

if($id=="update_record") {
	if($_FILES[banner][name]!="") {
		$file_extension=strtolower(getExtension($_FILES[banner][name]));
		if($file_extension!="jpg" && $file_extension!="gif" && $file_extension!="jpeg" && $file_extension!="png") {
			$form_check_alert .="<li>Please upload small image(.jpg,.gif,.png Format Only)</li>";
		}		
	}
	if(!strlen($form_check_alert)>0) {
		if($_FILES[banner][name]!="") {
			$res1=getResult($table_name," where slno='$recordID'");
			$filenm1=SITE_DIR_PATH."/uploaded_files/banner_images/".$res1[banner];
			if (file_exists($filenm1)) {
				unlink($filenm1);
			}
			$img_name="banner_".date("d_m_Y_H_i_s");
			$img1=upload_file($img_name,"banner","uploaded_files/banner_images");
			updateTable($table_name," banner='$img1' where slno='$recordID'");
		}
		$bannerUrl=$bannerUrl;
		updateTable($table_name," personName='$personName',companyName	='$companyName',pageid='$pageid', position='$position', bannerUrl='$bannerUrl',  activationDate='$validFrom', expiryDate='$validTo' where slno='$recordID'");
		$_SESSION[session_message]=$record_type.' has been updated successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	else {
		$msg=html_head();
		$msg.=$form_check_alert;
		$msg.=html_foot();
	}	
}

if($id=="delete_record") {
	db_query("delete from $table_name where slno='$recordID'");
	$_SESSION[session_message]=$record_type.' has been deleted successfully';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

admin_header("");
?>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
 <tr>
	<td align="left" width="50%" class="myTitle"><?=$heading;?></td>
	<td align="right">
	 <form name="pgimg_form" action="<?=$PHP_SELF?>" method="post" style="margin:0px;padding:0px;">
	 <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="right">Records Per Page:
	    <select name="pagesize" onchange="this.form.submit();">
	     <option value="0">--Select--</option>
	     <?php
	     foreach($_GLOBALS[pgsize_arr] as $val) {
		     if(trim($pagesize)==trim($val)) {
			     echo '<option value="'.$val.'" selected>'.$val.'</option>';
		     }
		     else{
			     echo '<option value="'.$val.'">'.$val.'</option>';
		     }
	     }
	     ?>
	    </select>	    
		 </td>
		</tr>
	 </table>
	 </form>
	</td>
 </tr>
</table>
<hr size="2" width="99%" color="#DA251C">

<?php
$qry1= db_query("select * from $table_name where banner !='' and pageid !=''");
$reccnt = NumRows($qry1);
$qry= db_query("select * from $table_name where banner!='' and pageid !='' order by slno desc limit $start, $pagesize");
echo pageNavigation($reccnt,$pagesize,$start);
print_message();
?>

<table width="99%" border="0" cellpadding="2" cellspacing="2" class="lightBorder">
 <?php
 if($reccnt>0) {
	 ?>
	 <tr height="25" bgcolor="#EAEAEA">
	  <td width="5%" align="center"><strong>S.No.</strong></td>
	  <td width="20%" align="left"><strong>Company Name</strong></td>
	  <td width="25%" align="left"><strong>Banner URL</strong></td>	  
	  <td width="25%" align="left"><strong>Banner Validity</strong></td>
	  <td width="10%" align="center"><strong>Image</strong></td>
	  <td width="10%" align="left"><strong>Options</strong></td>
	 </tr>
	 <?php
	 $sl=$start+1;
	 while($arr=mysql_fetch_array($qry)) {
		 $arr=ms_htmlentities_decode($arr);
		 if($sl%2!=0) {
			 $color="#f9f9f9";
		 }
		 else{
			 $color="#f0f0f0";
		 }
		 ?>		 
		 <tr bgcolor="<?=$color;?>">
		  <td align="center"><?=$sl;?></td>
		  <td align="left"><?=$arr[companyName];?></td>
		  <td align="left">Banner Url = <?=$arr[bannerUrl];?><br>Banner Dispaly Url = <?php if ($arr[pageid] == '/products.php') {echo "All Product Category";}?><?php if ($arr[pageid] == '/classified_listing.php') {echo "All Companies/Services Category";}?><?php if ($arr[pageid] == '/city/index.php') {echo "City Index Page";}?><?php if ($arr[pageid] == '/city/products.php') {echo "City Product Page";}?><?php if ($arr[pageid] == '/city/suppliers_country.php') {echo "City Companies/Services Page";}?><?php if ($arr[pageid] == '/city/companies.php') {echo "City Companies Directory Page";};?><?php if ($arr[pageid] == '/city/products-directory.php') {echo "City Products Directory Page";}?></td>
		  <td align="left"><?=show_date($arr[activationDate]);?> to <?=show_date($arr[expiryDate]);?><br>Banner Position = <?php if ($arr[position] == '1') {echo "Body";}?><?php if ($arr[position] == '2') {echo "right";};?></td>
		  <td align="center">
		   <?php
		   if($arr[banner]!="") {
			   $filenm=SITE_DIR_PATH."/uploaded_files/banner_images/".$arr[banner];
			   if(file_exists($filenm)) {
				   $dimens=GetImageSize($filenm);
				   if($dimens[0]>130) {
					   $dimens[0]=$dimens[0]+50;
					   $dimens[1]=$dimens[1]+80;
				   }
				   else{
					   $dimens[0]=200;
					   $dimens[1]=200;
				   }
				   ?>
				   <a href="javascript:void(0);" onClick="openwin('<?=SITE_ADMIN_URL;?>/popup.php?img=banner_images/<?=$arr[banner];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>View <?=$record_type;?><strong></a>
				   <?php
			   }
		   }
		   else {
			   ?>
			   <strong>No Image Uploaded</strong>
			   <?php
		   }
		   ?>
			</td>
		  <td align="center">&nbsp;<a href="<?=$script_name;?>?id=edit_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr[slno];?>#bot"><img src="<?=SITE_ADMIN_URL?>/images/edit.gif" alt="Edit This <?=$record_type;?>" title="Edit This <?=$record_type;?>" border="0"></a>&nbsp;<a href="<?=$script_name;?>?id=delete_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr[slno];?>" onClick="return confirm('Are you sure to delete this record...');"><img src="<?=SITE_ADMIN_URL?>/images/cancel.gif" alt="Delete This <?=$record_type;?>" border="0"></td>
		 </tr>
		 <?php
		 $sl++;
	 }
 }
 else{
	 ?>
	 <tr><td><b>No Record Found........</b></td></tr>
	 <?php
 }
 ?>
</table><br />


<form name="bannerFrm" method=post action="" enctype="multipart/form-data" onsubmit="return validate('bannerFrm') && chk_valid_date();">
<table width="99%"  border="1" cellpadding="2" cellspacing="2" class="lightBorder">
 <?php
 if($_GET[recordID]=="" || $id!="edit_record") {
	 ?>
	 <tr bgcolor="#EAEAEA"><td colspan="2" align="left" height="20"><b><a name="bot">Add New <?=$record_type;?></a></b></td></tr>
	 <?php
 }
 else {
	 ?>
	 <tr bgcolor="#EAEAEA">
	  <td align="left" height="20"><a name="bot"></a><b>Edit <?=$record_type;?></b></td>
	  <td align="right" height="20"><a href="<?=$script_name;?>"><b>Add New <?=$record_type;?></b></a></td>
	 </tr>
	 <?php
 }
 $res=getResult($table_name," where slno='$_GET[recordID]'");
 ?>
 <tr>
  <td class="brdrbtm" valign="top"><b>Banner Image <font color="red">*</font></b></td>
  <td class="brdrbtm" valign="top">
   <?php
   if($res[banner]!="") {
	   ?>
	   <input name="banner" type="file" size="40" class="button">
	   <?php
	   $file1=SITE_DIR_PATH."/uploaded_files/banner_images/".$res[banner];
	   if(file_exists($file1)) {
		   $dimens=GetImageSize($file1);
		   if($dimens[0]>130) {
			   $dimens[0]=$dimens[0]+50;
			   $dimens[1]=$dimens[1]+80;
		   }
		   else{
			   $dimens[0]=200;
			   $dimens[1]=200;
		   }
		   echo "&nbsp;<a href=\"javascript:void(0);\" onClick=\"openwin('".SITE_ADMIN_URL."/popup.php?img=banner/".$res[banner]."', '$dimens[0]', '$dimens[1]')\" class=\"linkgray fontbold\"><b>View Image</b></a>"; 
	   }
   }
   else {
	   ?>
	   <input name="banner" type="file" size="40" class="button" id="NOBLANK~Please upload image.~DM~">
	   <?php
   }
   ?>
   <br/>
  
  </td>
 </tr>
 <tr>
  <td class="brdrbtm"><b>Name <font color="red">*</font></b></td>
  <td class="brdrbtm"><input type="text" name="personName" size="60" value="<?=$res[personName];?>" id="NOBLANK~Please enter Person Name.~DM~" /></td>
 </tr> 
  <tr>
  <td class="brdrbtm"><b>Company Name <font color="red">*</font></b></td>
  <td class="brdrbtm"><input type="text" name="companyName" size="60" value="<?=$res[companyName];?>" id="NOBLANK~Please enter Company name.~DM~" /></td>
 </tr>
 <tr>
  <td class="brdrbtm"><b>Banner URL <font color="red">*</font></b></td>
  <td class="brdrbtm"><strong>http://</strong><input type="text" name="bannerUrl" size="46" value="<?=$res[bannerUrl];?>" id="NOBLANK~Please enter banner url.~DM~" />
  </td>
 
 </tr>
  <tr>
  <td class="brdrbtm"><b>Banner Display URL <font color="red">*</font></b></td>
  <td class="brdrbtm"> <select name="pageid" class="p5px ml10" id="position">
		       <option value="/products.php" <?php if ($res[pageid] == '/products.php') {echo "selected";};?>>All Product Category</option>      
               <option value="/classified_listing.php" <?php if ($res[pageid] == '/classified_listing.php') {echo "selected";};?>>All Companies/Services Category</option>
		       <option value="/city/index.php" <?php if ($res[pageid] == '/city_index.php') {echo "selected";};?>>City Index Page</option>    
		       <option value="/city/products.php" <?php if ($res[pageid] == '/city/products.php') {echo "selected";};?>>City Product Page</option> 
		       <option value="/city/suppliers_country.php"  <?php if ($res[pageid] == '55') {echo "selected";};?>>City Companies/Services Page</option> 
		        <option value="/city/companies.php" <?php if ($res[pageid] == '/city/companies.php') {echo "selected";};?>>City Companies Directory Page</option> 
		        <option value="/city/products-directory.php" <?php if ($res[pageid] == '/city/products-directory.php') {echo "selected";};?>>City Products Directory Page</option> 
		     </select>
    <br/>
   Submit without http
  </td>
 
 </tr>
 <tr>
   <tr>
  <td class="brdrbtm"><b>Banner Position <font color="red">*</font></b></td>
  <td class="brdrbtm"><strong></strong> <select name="position" class="p5px ml10" id="position">
		       <option value="1" <?php if ($res[position] == '1') {echo "selected";};?>>Body</option>      

		       <option value="2"<?php if ($res[position] == '2') {echo "selected";};?>>Right</option>      
		      

		     </select>
 </tr>
 <tr>
  <td class="brdrbtm"><b>Banner Validity <font color="red">*</font></b></td>
  <td class="brdrbtm"><input  type="text" name="validFrom" id="validFrom" size="15" maxlength="12" value="<?=$res[activationDate];?>" class="txtfieldadmin" readonly="readonly" />&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" onclick="if(self.gfPop)gfPop.fPopCalendar(document.bannerFrm.validFrom);return false;" ><img name="popcal" align="absmiddle" src="<?=SITE_URL_PATH;?>/js/calendar/themes/Normal/calbtn.gif" width="34" height="22" border="0" alt=""></a>&nbsp;&nbsp;&nbsp;&nbsp;<strong>To</strong>&nbsp;&nbsp;&nbsp;&nbsp;<input  type="text" name="validTo" id="validTo" size="15" maxlength="12" value="<?=$res[expiryDate];?>" class="txtfieldadmin" readonly="readonly" />&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" onclick="if(self.gfPop)gfPop.fPopCalendar(document.bannerFrm.validTo);return false;" ><img name="popcal" align="absmiddle" src="<?=SITE_URL_PATH;?>/js/calendar/themes/Normal/calbtn.gif" width="34" height="22" border="0" alt=""></a></td>
 </tr>
 <tr>
  <td class="brdrbtm"></td>
  <td class="brdrbtm">
   <?php
   if($recordID!="") {
	   ?>
	   <input type="submit" name="submit" value="Update <?=$record_type;?>" class="button">
	   <input type="hidden" name="recordID" value="<?=$res[slno];?>">
	   <input type="hidden" name="id" value="update_record">
	   <?php	   
   }
   else{
	   ?>
	   <input type="submit" name="submit" value="Add New <?=$record_type;?>" class="button">
	   <input type="hidden" name="id" value="add_record">
	   <?php
   }
   ?>
   <input type="hidden" name="start" value="<?=$start;?>">
   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
  </td>
 </tr>
</table>
</form>
<iframe width=174 height=189 name="gToday:normal:agenda.js" id="gToday:normal:agenda.js" src="<?=SITE_URL_PATH;?>/js/calendar/themes/Normal/ipopeng.htm" scrolling="no" frameborder="0" style="visibility:visible; z-index:999; position:absolute; top:-500px; left:-500px;">
   </iframe>
<?php
admin_footer();
exit;
?>