<?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');
			$radType=mysql_result($sql,0,'nType');
			$txtOpenDes=mysql_result($sql,0,'vOpenxDes');
			$http='';
		}//end if
		$mode='edit';
	}//end if
	else
	{
		$http='http://';
	}//end else

	if(isset($_POST["btnSubmit"]) && $_POST["btnSubmit"]!='')
	{
		$http='';
		//validation
		$error = false;


			$nBId = $_POST['nBId'] ;
			$txtBannerName = $_POST["txtBannerName"];
			$txtBannerLoc = $_POST["txtBannerLoc"];
			$radLocation=$_POST["radLocation"];
			$radActive=$_POST['radActive'];
			$radType=$_POST["radType"];
			$txtOpenDes=$_POST["txtOpenDes"];
		

	 if($radType=='Normal')
	 {
		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($_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']);
			
			if($radLocation=='Home')
			{
				$chkWidth='200';
				$chkHight='150';
			}//end if
			else
			{
				$chkWidth='234';
				$chkHight='60';
			}//end else
	
			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);
					@copy($_FILES['txtPic']['tmp_name'], "../banners/" . $assignedname);
					$assignedname=$objS->resizeImg("../banners/" . $assignedname, $chkWidth ,$chkHight, false,100, 0,"");
				}//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($_POST['nBId']!='')
				{
					mysql_query("update ".TABLEPREFIX."Banners set vName='".addslashes(trim($txtBannerName))."',
								vlocUrl='".addslashes(trim($txtBannerLoc))."',vWidth='".$chkWidth."',
								vImg='".addslashes($assignedname)."',vHeight='".$chkHight."',vLocation='".addslashes($radLocation)."',
								vActive='".addslashes($radActive)."' where nBId='".$_POST['nBId']."'") or die(mysql_error());
					header('location:banner.php?msg=e');
					exit();
				}//end if
		}//end else

		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(),'".$chkWidth."','".$chkHight."','".$maxorder."','".addslashes($radActive)."',
								'".addslashes($radLocation)."')") or die(mysql_error());
		
			header('location:banner.php?msg=a');
			exit();
		}//end if
	
		if($imgFlag==true && $_POST['nBId']!='')
		{
			mysql_query("update ".TABLEPREFIX."Banners set vName='".addslashes(trim($txtBannerName))."',
								vlocUrl='".addslashes(trim($txtBannerLoc))."',vWidth='".$chkWidth."',
								vImg='".addslashes($assignedname)."',vHeight='".$chkHight."',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 banner type check

	//banner type is OpenX
	if($radType=='OpenX')
	{
		if($radLocation=='Home')
		{
			$chkWidth='200';
			$chkHight='150';
		}//end if
		else
		{
			$chkWidth='234';
			$chkHight='60';
		}//end else
		if($_POST['nBId']!='')
		{	
			$qry = "update ".TABLEPREFIX."Banners set 
						 nType='".addslashes(trim($radType))."',
						 vOpenxDes='".addslashes(trim($txtOpenDes))."', 
						 vActive='" . addslashes(trim($radActive)) . "',
						 vWidth='".$chkWidth."',
						 vHeight='".$chkHight."',
						 vLocation='".addslashes($radLocation)."'
						 where nBId='".$_POST['nBId']."'";
																		
			mysql_query($qry) or die(mysql_error());
			header('location:banner.php?msg=e');
			exit();
		}//end if
		else
		{
		 	$sql = mysql_query("SELECT MAX(nPosition) as max from ".TABLEPREFIX."Banners") or die(mysql_error());
	     	$rw = mysql_fetch_array($sql);
	     	$maxorder = $rw['max']+1;
		 
		 	$qry = "INSERT INTO ".TABLEPREFIX."Banners(nType,vOpenxDes,nDate,nPosition,vActive,vLocation,vWidth,vHeight) values(
				 	'".addslashes(trim($radType))."','".addslashes(trim($txtOpenDes))."',now(),
					'".$maxorder."','".$radActive."','".addslashes($radLocation)."','".$chkWidth."','".$chkHight."')";
		 	mysql_query($qry) or die(mysql_error());
			header('location:banner.php?msg=a');
			exit();
		}//end else
	}//end if openx checking
   }//end if	

	$message=($message!='')?$message:$_SESSION['sessionMsg'];
	unset($_SESSION['sessionMsg']);
}//end else

if(isset($_POST['BackNow']) && $_POST['BackNow']=='Back')
{
	header('location:banner.php');
	exit();
}//end if

include_once('title.inc.php');
?>
<script language="javascript" type="text/javascript" src="../js/validation.js"></script>
<script language="javascript" type="text/javascript">
function validateBanners(){
        var frm = document.frmBanners;
	if((frm.radType[0].checked==true) && (trim(frm.radType[0].value)=='Normal'))
	{
        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['nBId']=='')
			 {
		?>
		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
	}//end if
	if((frm.radType[1].checked==true) && (trim(frm.radType[1].value)=='OpenX'))
	{
		if(trim(frm.txtOpenDes.value)=="")
		{
             alert("OpenX Banner Code cannot be empty.");
             frm.txtOpenDes.focus();
              return false;
        }//end if
	}//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';
		document.getElementById('home11').style.display='';
		document.getElementById('sub11').style.display='none';
	}//end if
	else
	{
		document.getElementById('home1').style.display='none';
		document.getElementById('sub1').style.display='';
		document.getElementById('home11').style.display='none';
		document.getElementById('sub11').style.display='';
	}//end else
}//end function

//show field for openx and normal
function ShowField(nType)
{
	if(nType=='OpenX')
	{
		document.getElementById('showFeld1').style.display='none';
		document.getElementById('showFeld2').style.display='none';
		document.getElementById('showFeld3').style.display='none';
		document.getElementById('showFeld4').style.display='';
	}//end if
	else
	{
		document.getElementById('showFeld1').style.display='';
		document.getElementById('showFeld2').style.display='';
		document.getElementById('showFeld3').style.display='';
		document.getElementById('showFeld4').style.display='none';
	}//end else
}//end function
</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">
                        <?php if($nBId!=''){ ?>
                        Edit
                        <?php }else{?>
                        Add New
                        <?php }?>
                        Banners</td>

                        </tr>
                    </table>
                <div class="bordernew padding2">
                    <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="" 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" class="listing_tbl_admin">
                            <?php
                            if ($objDB) {
                                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 class="maintext2">&nbsp;</td>
                                        <td valign="top" class="maintext2">Banner Type</td>
                                        <td colspan="2" valign="top" class="maintext2"><input name="radType" type="radio" value="Normal" <?php if($radType=='Normal'){echo 'checked';}if($radType==''){echo 'checked';}?> onClick="ShowField(this.value);"><?php echo $siteTitle['sitename'];?> <input name="radType" type="radio" value="OpenX" <?php if($radType=='OpenX'){echo 'checked';}?> onClick="ShowField(this.value);">OpenX</td>
                                    </tr>
                                    <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" id="showFeld1" style="<?php if($radType=='OpenX'){echo 'display:none;';}?>">
                                        <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="textbox txt-bx" size="65" maxlength="200" value="<?php echo htmlentities($txtBannerName);?>"></td>
                                    </tr>
                                    <tr align="left" id="showFeld2" style="<?php if($radType=='OpenX'){echo 'display:none;';}?>">
                                        <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="textbox txt-bx" size="65" maxlength="200" value="<?php echo $http.htmlentities($txtBannerLoc);?>"></td>
                                    </tr>
                                    <tr align="left" id="showFeld3" style="<?php if($radType=='OpenX'){echo 'display:none;';}?>">
                                        <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="textbox txt-bx" onChange="change_sitemap();">
                                        <br><br><font color="#FF0000"><div id="home1" style="<?php if($radLocation!='Home'){echo 'display:none;';}?>">Image size should be 200x150 or greater</div>
                                        <div id="sub1" style="<?php if($radLocation!='Sub'){echo 'display:none;';}if($radLocation==''){echo 'display:inline';}?>">Image size should be 234x60 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" id="showFeld4" style="<?php if($radType=='Normal' || $radType==''){echo 'display:none;';}?>">
                                        <td class="maintext2">&nbsp;</td>
                                        <td valign="top" class="maintext2">OpenX Banner Code <span class="warning">*</span></td>
                                        <td colspan="2" valign="top" class="maintext2"><textarea  name="txtOpenDes" cols="45" rows="8" class="textbox_textarea txt-area"><?php echo $txtOpenDes;?></textarea>
                                        <br><br><font color="#FF0000"><div id="sub11" <?php if($radLocation!='Sub'){echo 'display:none;';}?>>Image size should be 234x60</div>
                                        <div id="home11" style="display:none;" <?php if($radLocation!='Home'){echo 'display:none;';}?>>Image size should be 200x150</div></font></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 ($objDB)
                            {
                            ?>
                            <table width="100%"  border="0" cellspacing="0" cellpadding="5">
                            <tr>
                            <td align="left"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                            <tr>
                            <td width="30%">&nbsp;</td>
                            <td width="7%">
                            <input name="BackNow" type="submit" class="back-btn" value="Back"></td>
                            <?php //if ($nBId > 0) { ?>
                            <td><input class="loginbutton" type="submit" value="<?php if($nBId!=''){?>Edit<?php }else{?>Add<?php }?>" name="btnSubmit" onclick="return validateBanners();" >


                            </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>
<?php include_once('bottomhtml.php');?>