<?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/settings.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.";
}//end if
else 
{
	$objS		= new settings($objDB);
	$nBId = $_GET['nBId'];
	
	if (intval($nBId) != 0)
	{
		$sql=mysql_query("select * from ".TABLEPREFIX."Banners where nBId='".$_GET['nBId']."'") or die(mysql_error());
		if(mysql_num_rows($sql)>0)
		{
			$txtBannerName=mysql_result($sql,0,'vName');
			$txtBannerLoc=mysql_result($sql,0,'vlocUrl');
			$radActive=mysql_result($sql,0,'vActive');
			$assignedname=mysql_result($sql,0,'vImg');
			$radLocation=mysql_result($sql,0,'vLocation');
			$http='';
		}//end if
		$mode='edit';
	}//end if
	else
	{
		$http='http://';
	}//end else

	if(isset($_POST["btnSubmit"]) && $_POST["btnSubmit"]!='')
	{
		$http='';
		//validation
		$error = false;
		if (trim($txtBannerName) == "") 
		{
			$message .= "* Please Enter banner name<br>";
			$error = true;
		} //end if
		if (trim($txtBannerLoc) == "") 
		{
			$message .= "* Please Enter banner Location<br>";
			$error = true;
		} //end if
		
		if(@get_magic_quotes_gpc())
		{
			$nBId = stripslashes($_POST['nBId'] );
			$txtBannerName = stripslashes($_POST["txtBannerName"]);
			$txtBannerLoc = stripslashes($_POST["txtBannerLoc"]);
			$radLocation=stripslashes($_POST["radLocation"]);
			$radActive=stripslashes($_POST['radActive']);
		}//end if
		else
		{
			$nBId = $_POST['nBId'] ;
			$txtBannerName = $_POST["txtBannerName"];
			$txtBannerLoc = $_POST["txtBannerLoc"];
			$radLocation=$_POST["radLocation"];
			$radActive=$_POST['radActive'];
		}//end else
	
		if($_FILES['txtPic']['name']!='')
		{
			if ($_FILES['txtPic']['size'] <= 0 or !$objS->isValidWebImageType($_FILES['txtPic']['type'],$_FILES['txtPic']['name'],$_FILES['txtPic']['tmp_name'])) 
			{
				$message .= "* Please upload a valid banner image(jpg/gif/png)<br>";
				$error = true;
			} //end if
	
			list($newwidth,$newheight)=@getimagesize($_FILES['txtPic']['tmp_name']);
			
				$chkWidth='468';
				$chkHight='60';		
	
			if($newwidth>=$chkWidth && $newheight>=$chkHight)
			{
				@chmod('../banners',0777);
				//remove the old image from the banner folder
				@unlink('../banners/'.$_POST['assignedname']);
	
				if (!$error)
				{
					if ($_FILES['txtPic']['size'] > 0) 
					{
						$imagewidth_height_type_array = explode(":", $objS->ImageType($_FILES['txtPic']['tmp_name']));
						$imagetype = $imagewidth_height_type_array[0];
						$imagetype_width=$imagewidth_height_type_array[1];
						$imagetype_height=$imagewidth_height_type_array[2];
						$assignedname = "banner" . time() . "." . $imagetype;
					}//end if
					else
					{
						$assignedname=$_POST['assignedname'];
						list($imagetype_width,$imagetype_height)=@getimagesize("../banners/".$assignedname);
					}//end else
					
					@chmod("../banners/$assignedname", 0777);
					$assignedname_rmv=$assignedname;
					@copy($_FILES['txtPic']['tmp_name'], "../banners/" . $assignedname);
					$assignedname=$objS->resizeImg("../banners/" . $assignedname, $chkWidth ,$chkHight, false,100, 0,"");
					@unlink('../banners/'.$assignedname_rmv);
				}//end if
				$imgFlag=true;
			}//end if
			else
			{
				$imgFlag=false;
				$_SESSION['sessionMsg'] = "Banner size should be ".$chkWidth."x".$chkHight." or greater";
			}//end else
		}//end if	
		else
		{
			//if(isset($_POST["btnSubmit"]) && $_POST["btnSubmit"]=='Edit Banners')
			//{
				if($_POST['nBId']!='')
				{
					mysql_query("update ".TABLEPREFIX."Banners set vName='".addslashes(trim($txtBannerName))."',
								vlocUrl='".addslashes(trim($txtBannerLoc))."',vWidth='".$imagetype_width."',
								vImg='".addslashes($assignedname)."',vHeight='".$imagetype_height."',vLocation='".addslashes($radLocation)."',
								vActive='".addslashes($radActive)."' where nBId='".$_POST['nBId']."'") or die(mysql_error());
					header('location:banner.php?msg=e');
					exit();
				}//end if
			//}//end if
		}//end else
	}//end if	
	
	//if(isset($_POST["btnSubmit"]) && $_POST["btnSubmit"]=='Add Banners')
	//{
		if($imgFlag==true && $_POST['nBId']=='')
		{
			$sql = mysql_query("SELECT MAX(nPosition) as max from ".TABLEPREFIX."Banners") or die(mysql_error());
			$rw = mysql_fetch_array($sql);
			 $maxorder = $rw['max']+1;
	
			mysql_query("insert into ".TABLEPREFIX."Banners (vName,vlocUrl,vImg,nDate,vWidth,vHeight,nPosition,vActive,vLocation) values 
								('".addslashes(trim($txtBannerName))."','".addslashes(trim($txtBannerLoc))."','".addslashes($assignedname)."',
								now(),'".$imagetype_width."','".$imagetype_height."','".$maxorder."','".addslashes($radActive)."',
								'".addslashes($radLocation)."')") or die(mysql_error());
		
			header('location:banner.php?msg=a');
			exit();
		}//end if
	//}//end if
	
	//if(isset($_POST["btnSubmit"]) && $_POST["btnSubmit"]=='Edit Banners')
	//{
		if($imgFlag==true && $_POST['nBId']!='')
		{
			mysql_query("update ".TABLEPREFIX."Banners set vName='".addslashes(trim($txtBannerName))."',
								vlocUrl='".addslashes(trim($txtBannerLoc))."',vWidth='".$imagetype_width."',
								vImg='".addslashes($assignedname)."',vHeight='".$imagetype_height."',vLocation='".addslashes($radLocation)."',
								vActive='".addslashes($radActive)."' where nBId='".$_POST['nBId']."'") or die(mysql_error());
			header('location:banner.php?msg=e');
			exit();
		}//end if
	//}//end if
	
	
	$message=($message!='')?$message:$_SESSION['sessionMsg'];
	unset($_SESSION['sessionMsg']);
}//end else

include_once('title.inc.php');
?>
<script language="javascript" type="text/javascript">
function validateBanners(){
        var frm = document.frmBanners;
		
        if(trim(frm.txtBannerName.value) == ""){
                alert("Banner Name cannot be empty.");
                frm.txtBannerName.focus();
                return false;
        }else if(trim(frm.txtBannerLoc.value)=="http://"){
                alert("Banner Location cannot be empty.");
                frm.txtBannerLoc.focus();
                return false;
        }
		<?php
			 if($_GET['id']=='')
			 {
		?>
		else if(trim(frm.txtPic.value)==""){
                alert("Banner cannot be empty.");
                frm.txtPic.focus();
                return false;
        }
		<?php }//end if?>
        if(frm.txtPic.value!='')
		{
	    	UploadFile();
		}//end if
		return true;
}


function showPrograsBar()
{
	FreezePage();
	document.getElementById('uploadimageloader').style.display='inline';
		//PrograsBar =window.open("prograssbar.php",'welcome','resizable=no,width=0,height=0,screenX=150,screenY=150,top=150,left=150');

}
function closePrograsBar(){
		if (PrograsBar && PrograsBar.open && !PrograsBar.closed) PrograsBar.close();
}

function UploadFile(){
      showPrograsBar();
}
function FreezePage()
{
	document.getElementById("parentDiv").style.width = window.screen.width;
	document.getElementById("parentDiv").style.height = window.screen.height*20;
	document.getElementById("parentDiv").style.display = 'inline';

	ShowIndicator();
}

function ShowIndicator()

{
	document.getElementById("waitIndicatorDiv").style.display = 'block';
}
function change_sitemap(){
	document.frmBanners.img1.src=document.frmBanners.txtPic.value;
}

//size display check
function ImageSize(s)
{
	if(s=='H')
	{
		document.getElementById('home1').style.display='';
		document.getElementById('sub1').style.display='none';
	}//end if
	else
	{
		document.getElementById('home1').style.display='none';
		document.getElementById('sub1').style.display='';
	}//end else
}//end function
</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"><?if($nBId!=''){?>
              Edit
                <?}else{?>
                Add New
                <?}?>
              Banners</td>
                <td width="59%" class="headings1">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
        </table>
<div id="parentDiv" class="transparent" style="position:absolute; background-color:Gray; 
width:100%; height:500%; display:none; top:0px; left:0px; z-index:5000;">
</div>


<div id="waitIndicatorDiv" class="Indicator">
<table id="uploadimageloader" width="50%" style="display:none;" align="center">
	<tr>
		<td align="center"><img src="../images/loading.gif"></td>
	</tr>
</table>
</div>

<form name="frmBanners" method ="POST" action="<?php echo $_SERVER['PHP_SELF']?>" onsubmit="return validateBanners();" enctype="multipart/form-data">
<input name="nBId" type="hidden" id="nBId" value="<?php echo $_GET['nBId'];?>">
<input type="hidden" name="assignedname" value="<?php echo $assignedname;?>">
          <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="3" class="maintext2 warning"><?php echo $message;?></td>
                  </tr>
					<?php  	}//end if?>
  				<tr align="left">
                  <td width="3%" class="maintext2">&nbsp;</td>
                  <td width="27%" valign="top" class="maintext2">Banner Display Location</td>
                  <td colspan="2" valign="top" class="maintext2"><input type="radio" value="Home" name="radLocation" <?php if($radLocation=='Home'){echo 'checked';}?> onClick="ImageSize('H');">Homepage<input name="radLocation" type="radio" value="Sub" <?php if($radLocation=='Sub'){echo 'checked';}if($radLocation==''){echo 'checked';}?> onClick="ImageSize('S');">
        Subpage</td>
  				</tr>
                 <tr align="left">
                  <td class="maintext2">&nbsp;</td>
                  <td valign="top" class="maintext2">Banner Name <span class="warning">*</span></td>
                  <td colspan="2" valign="top" class="maintext2"><input type="text" name="txtBannerName" class="textbox3" size="65" maxlength="200" value="<?php echo htmlentities($txtBannerName);?>"></td>
                 </tr>
                 <tr align="left">
                   <td class="maintext2">&nbsp;</td>
                   <td valign="top" class="maintext2">Target Location (URL) <span class="warning">*</span></td>
                   <td colspan="2" valign="top" class="maintext2"><input type="text" name="txtBannerLoc" class="textbox3" size="65" maxlength="200" value="<?php echo $http.htmlentities($txtBannerLoc);?>"></td>
                 </tr>
                 <tr align="left">
                   <td class="maintext2">&nbsp;</td>
                   <td valign="top" class="maintext2">Banner Image</td>
                   <td width="54%" valign="top" class="maintext2"><input type="file" name="txtPic" class="textbox3" onChange="change_sitemap();">
	  <br><br><font color="#FF0000"><div id="home1" style="<?php if($radLocation!='Home'){echo 'display:none;';}?>">Image size should be 468x60 or greater</div>
	  <div id="sub1" style="<?php if($radLocation!='Sub'){echo 'display:none;';}if($radLocation==''){echo 'display:inline';}?>">Image size should be 468x60 or greater</div></font></td>
                   <td width="16%" valign="top" class="maintext2"><?php if($assignedname!=''){
	  echo ' <img src="../banners/'.$assignedname.'" name="img1" width="100" height="100">';}//end if
	  else{echo '<img src="../images/default_album.gif" name="img1" width="69" height="51">';}//end else?></td>
                 </tr>
                <tr align="left">
                  <td class="maintext2">&nbsp;</td>
                  <td valign="top" class="maintext2">Active</td>
                  <td colspan="2" valign="top" class="maintext2"><input type="radio" name="radActive" value="1" <?php if($radActive=='1'){echo 'checked';}if($radActive==''){echo 'checked';}?>>Yes <input type="radio" name="radActive" value="0" <?php if($radActive=='0'){echo 'checked';}?>>No</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="11%"  border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td><input class="loginbutton" type="submit" value="<?if($nBId!=''){?>Edit<?}else{?>Add<?}?>  Banners" name="btnSubmit"></td>
                              <?php if ($nBId > 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>
<?php include_once('bottomhtml.php');?>