<?php session_start();  //Developed by ITechScripts
error_reporting(0);
?>
<?
if(strlen($_SESSION["adminuser"])==0)
{
//echo '<meta http-equiv="refresh" content="0; url=index.php">';
//exit();
}
require 'include/configurations.php'; // ITECHBIDS Configuration File
	$type=$_GET['type'];
	if(!$type) $type='e';
	$mode=$_GET['mode'];
	$id=$_GET['id'];
	if($mode=='edit') {

		$remove=$_GET['remove'];
		$modifysql="select * from small_banners where banner_id=$id";
		$modifyres=mysql_query($modifysql);
		$modifyrow=mysql_fetch_array($modifyres);	
		if($remove==1)  {
			unlink($modifyrow['banner_path']);
			 $up_ban_sql="update small_banners set banner_path='null' where banner_id=$id";
			$up_ban_res=mysql_query($up_ban_sql);
		}
		$modifysql="select * from small_banners where banner_id=$id";
		$modifyres=mysql_query($modifysql);
		$modifyrow=mysql_fetch_array($modifyres);				
	}

?>

<script language="javascript">
	function disp(val,bpath) {
		if(val==1) {
			document.frmAdd.rdBtype[0].checked=true;
			opt.innerHTML="Upload File</td><td><input type=file name=userfile> Please Select a Image for Uploading";
		}
		else if(val==2) {
			document.frmAdd.rdBtype[1].checked=true;
			opt.innerHTML="Please Enter the URL of the Banner&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=text name=txtBurl size='60'  value='http://'>";
		}
		else if(val=='default' || val=='ad') {
			opt.innerHTML="Upload File</td><td><input type=file name=userfile> Please Select a Image for Uploading";
		}
		else if(val=='On' || val=='Off') {
			document.frmAdd.rdBtype[1].checked=true;
			if(bpath)
			opt.innerHTML="Please Enter the URL of the Banner&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=text name=txtBurl class=text value='"+ bpath +"' size='60'>";
			else 
			opt.innerHTML="Please Enter the URL of the Banner&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=text name=txtBurl  value='http://' size='60'>";
		}
	}
</script>
<style type="text/css">
<!--
.style1 {
	color: #666666;
	font-weight: bold;
}
-->
</style>
<body <? if($mode=='add') { ?>onLoad="disp(1,'')"<? } else if($mode=='edit') { ?>onLoad="disp('<?=$modifyrow['status']?>','<?=$modifyrow['banner_path']?>')"<? }?>><?php
require 'include/header.php';
?><br>
<table border="0" align="center" cellpadding="5" cellspacing="2" width="80%" bgcolor="#FFCC00">
<tr>
<td align="center"><a href="small_banners.php?type=d" id="tablink">Default Banner</a></td>
<td align="center"><a href="small_banners.php?type=e" id="tablink">Existing Banner</a></td>
</tr>
</table>
<br>
<?
	if($mode=='add' || $mode=='edit') {
	 $canadd=$_POST['canAdd'];
	if($canadd==1) {
			$bname=$_POST['txtBanner'];
			if(strlen($_FILES['userfile']['name'])==0) {
			$burl=$_POST['txtBurl'];
			if($mode=='add')
			$status='off';	
			else $status=$modifyrow['status'];
			}
			else {
				if($_FILES['userfile']['type']=='image/pjpeg' || $_FILES['userfile']['type']=='image/jpg' ||  $_FILES['userfile']['type']=='image/gif') {
					$uploaddir="images/".$_FILES['userfile']['name'];
					move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir);
					$burl=$uploaddir;
					$status='default';
				}
			}	
			$surl=$_POST['txtSurl'];
			if($mode=='add') 
			$ins_sql="insert into small_banners(banner_name,banner_path,site_url,status) values('$bname','$burl','$surl','$status')";
			else if($mode=='edit')
			$ins_sql="update small_banners set banner_name='$bname',banner_path='$burl',site_url='$surl',status='$status' where banner_id=$id";
			$ins_res=mysql_query($ins_sql);	
			echo '<meta http-equiv="refresh" content="0;url=small_banners.php">';
		}
?>
<table border="0" align="center" cellpadding="5" cellspacing="2" width="80%" class="ft">
<form name="frmAdd" action="<? $_SERVER['PHP_SELF']?>" method="post" onSubmit="this.canAdd.value=1;return true;" enctype="multipart/form-data">
<tr><td class="ct" height="25" colspan="2"><? echo ucwords($mode); if($type=='d') echo ' Default' ?> Banner</td></tr>
<tr ><td >Banner Name</td>
<td>
<input type="text" class="bluetext"name="txtBanner" value="<?=$modifyrow['banner_name']?>" size="60">&nbsp;&nbsp;&nbsp;
</td>
</tr>
<?
	if((strlen($modifyrow['banner_path'])!=0 && $modifyrow['banner_path']!='null') && ($modifyrow['status']=='default' || $modifyrow['status']=='ad')) {
?>
<tr >
  <td colspan="2">
<img src="<?=$modifyrow['banner_path']?>"><br>
<div align="center"><a href="small_banners.php?mode=edit&id=<?=$id?>&remove=1" id="tablink" onClick="return condel()">Remove</a></div>
<div>To Change the Banner you need to remove the Banner and Upload a New Banner</div>
<?
	}
	else {
?>
<tr >
  <td colspan="2">Banner Image (Please Select Upload image to locate the Banner in your System else Choose type URL to type the URL of the Banner)
</td>
</tr>
<tr ><td colspan="2">
<input type="radio" name="rdBtype" value="1" checked onClick="disp(1)">Upload Image
&nbsp;&nbsp;<input type="radio" name="rdBtype" value="2" onClick="disp(2)">Type URL
</td></tr>
<tr bgcolor=#ffffff><td colspan="2"><div id="opt">asdf</div></td></tr>
<?
	}
?>
<tr ><td>Site Url</td><td><input type="text" class="bluetext"name="txtSurl" value="<? if($mode=='edit') echo $modifyrow['site_url']; else echo 'http://';?>" size="60"> </td></tr>
<tr ><td colspan="2" align="center"><input type="hidden" name="type" value="<?=$type?>">
<input type="hidden" name="canAdd" value="0">
<input type="submit" class="button"   name="btnAdd" value="  Add   " >
</td></tr>
</form>
</table>
<?
	}
	else if($mode=='del') {
		$del_sql="delete from small_banners set banner_id=$id";
		$del_res=mysql_query($del_sql);
		echo '<meta http-equiv="refresh" content="0;url=small_banners.php">';
	}
	else {

	if($type=='e') {
?>
<table border="0" align="center" cellpadding="5" cellspacing="2" width="80%" class="ft">
<tr>
<td align="center"><a href="small_banners.php?type=e&mode=s" id="tablink">Suspend</a></td>
<td align="center"><a href="small_banners.php?type=e&mode=a" id="tablink">Activate</a></td>
</tr>
</table><br>
<?		
	}
		if(isset($_POST['btn_Act'])) {
			if($type=='e') {
				$act=$_POST['chkAct'];
				foreach($act as $id) {
					$up_sql="update small_banners set status='on' where banner_id=$id";	
					$up_res=mysql_query($up_sql);
				}
			}
			else if($type=='d') {
				$act=$_POST['rdAct'];
				$up_sql="update small_banners set status='ad' where banner_id=$act";
				$up_res=mysql_query($up_sql);
				$up_sql1="update small_banners set status='default' where banner_id <> $act and status='ad'";
				$up_res1=mysql_query($up_sql1);
			}
		}
		else if(isset($_POST['btn_Sus'])) {
			if($type=='e') {
				$act=$_POST['chkAct'];
				foreach($act as $id) {
					$up_sql="update small_banners set status='off' where banner_id=$id";	
					$up_res=mysql_query($up_sql);
				}
			}
			else if($type=='d') {
				$act=$_POST['rdAct'];
				$up_sql="update small_banners set status='off' where banner_id=$act";
					$up_res=mysql_query($up_sql);
			}
		}
		if($type=='e') {
			if(!$mode) $mode='a';
			if($mode=='s') $ban_sql="select * from small_banners where status <> 'default' and status='off'";
			else if($mode=='a') $ban_sql="select * from small_banners where status <> 'default' and status='on'";
		}
		else if($type=='d') $ban_sql="select * from small_banners where status = 'default' or status='ad'";
		$ban_result=mysql_query($ban_sql);
?>
<table border="0" align="center" cellpadding="5" cellspacing="2" width="80%" class="ft">
<form name="frmBan" action="<? $_SERVER['PHP_SELF']?>" method="post" <? if($type=='e') {?>onSubmit="return validate()"<? } ?>>
<tr><td colspan="6" class="ct" height="25">Banner Details</td></tr>
<?
	if(mysql_num_rows($ban_result)>0) {

?>
<tr >
<td><? if($type=='e') echo "<input type='checkbox' name='chkMain' value=1 onClick='selectall()'>";else echo '&nbsp;'?></td>
<td nowrap>Banner Name</td><td>Path</td><td>Website</td><td colspan="2">Action</td>
</tr>
<?
	$i=1;
	while($row=mysql_fetch_array($ban_result)) {
?>
<tr >
<td><input type="<? if($type=='d') echo 'radio';else if($type=='e') echo 'checkbox';?>" name="<? if($type=='d') echo 'rdAct'; else if($type=='e') echo 'chkAct[]'?>" <? if($type=='e') echo 'id="chkAct"'?> value="<?=$row['banner_id']?>" <? if($type=='d' && $row['status']=='ad') echo 'checked';?>></td>
<td><?=$row['banner_name']?></td><td><?=$row['banner_path']?></td><td><?=$row['site_url']?></td><td><a href="small_banners.php?mode=edit&id=<?=$row['banner_id']?>" id="tablink">Edit</a></td><td><a href="small_banners.php?mode=del&id=<?=$row['banner_id']?>" id="tablink" onClick="return condel()">Delete</a></td>
</tr>
<?
	$i+=1;
	}
?>
<tr >
  <td colspan="6"><input type="submit" class="button"   name="<? if($mode=='s' || $type=='d') echo 'btn_Act';else echo 'btn_Sus';?>" value="<? if($mode=='s' || $type=='d') echo 'Activate';else echo 'Suspend';?>" ></td>
</tr>
<?	}
	else {
?>
<tr >
<td align="center" colspan="6">No small_banners Found</td>
</tr>
<?
	}
?>
<tr >
<td colspan="6"><a href="small_banners.php?mode=add&type=<?=$type?>" id="tablink">Add New</a></td>
</tr>
</form>
</table>
<?
	}
?>
<br>
<?
	require 'include/footer.php';
?>

<script language="Javascript">
function validate() {
	len1=document.frmBan.chkAct.length;
	cval=0;
	if(len1>1) {
	for(i=0;i<len1;i++) {
		if(document.frmBan.chkAct[i].checked==true) cval=cval+1;
	}
	}
	else {
		if(document.frmBan.chkAct.checked==true) cval=cval+1;
	}
	if(cval>=1) {
		return true;
	}
	else {
		alert("You Have'nt Selected any Banner to Activate");
		return false;
	}
}
function selectall() {
	len=document.frmBan.chkAct.length;
	if(len>1) {
		for(i=0;i<len;i++) {
			if(document.frmBan.chkMain.checked==true) document.frmBan.chkAct[i].checked=true;
			else document.frmBan.chkAct[i].checked=false;
		}
	}
	else {
			if(document.frmBan.chkMain.checked==true) document.frmBan.chkAct.checked=true;
			else document.frmBan.chkAct.checked=false;
	}
}
function condel() {
	a=confirm("Are you Sure to Delete this Banner");
	return a;
}
	
</script>