<?php
require_once ("../config.inc.php");
require_once ("../includes/adminlogincheck.inc.php");
require_once ("../includes/mysql.inc.php");
require_once ("../includes/Pager/Pager.php");
require_once ("../classes/settings.class.php");
$message	= "";
$pages		= "";
$data		= array();
$array		= array();
$page		= isset($_REQUEST['page'])	?	trim($_REQUEST['page'])	:	"1";

if (!$objDB) 
{
	$message	= "Unable to Locate Database Server.";
}//end if
else 
{
    $objS		= new settings($objDB);
	//delete State category
	if(isset($_POST['ids']) && $_POST['ids']!='')
	{
		foreach ($_POST['ids'] as $val)
		{
			$sqlImg = mysql_query("select vImg FROM ".TABLEPREFIX."Banners where nBId='".$val."'") or die(mysql_error());
			if(mysql_num_rows($sqlImg)>0)
			{
				//remove deleted banner image from the banners folder
				@unlink("../banners/".mysql_result($sqlImg,0,'vImg'));
			}//end if
			mysql_query("delete from ".TABLEPREFIX."Banners where nBId='".$val."'") or die(mysql_error());
			$message = "<font color='green'>Banner deleted successfully.</font>";
		}//end foreach
	}//end if

	//add this link to ordering 
	if(isset($ddlCountry) && $ddlCountry!='')
	{
		$order_category='&ddlCountry='.$ddlCountry;
	}//end if
					
	//all category listing
	$query = "SELECT * from ".TABLEPREFIX."Banners order by nPosition desc";

	$res=mysql_query($query) or die(mysql_error());
	$cnt=mysql_num_rows($res);

	if (@mysql_num_rows($res) > 0) 
	{
		while($row = @mysql_fetch_array($res)) 
		{
			$array[]	= $row;
		}//end while loop
		@mysql_free_result($res);
	}//end if
	
	$params 	= array(
		'mode'      => 'Sliding',
		'perPage'   => PAGESIZE,
		'urlVar'    => 'page',
		'extraVars' => array(),
		'itemData'  => $array
	);
	$objP 		= & Pager::factory($params);
	$data  		= $objP->getPageData();
	if (empty($data))
		$message	.= "No record(s) exists.<br>";
}//end else

include_once('title.inc.php');

//ordering content
if(isset($_GET['Action']) && $_GET['Action']=='ordering')
{
	$oldId=$_GET['id'];
	$oldPosition=$_GET['pos'];
	$table=TABLEPREFIX."Banners";
	$PositionfieldName='nPosition'; //db field name
	$IdfieldName='nBId'; //db field name
	$returnPath='location:banner.php';
	
	if(isset($_GET['move']) && $_GET['move']=='up')
	{
		$objS->OrderUp($table,$oldId,$oldPosition,$PositionfieldName,$IdfieldName,$returnPath);
		listing();
	}//end if

	if(isset($_GET['move']) && $_GET['move']=='down')
	{
		$objS->OrderDown($table,$oldId,$oldPosition,$PositionfieldName,$IdfieldName,$returnPath);
		listing();
	}//end if
}//end if

switch($_GET['msg'])
{
	case "a":
		$message='<font color="green">Banners added successfully</font>.';
	break;
	
	case "e":
		$message='<font color="green">Banners updated successfully</font>.';
	break;
	
	case "d":
		$message='<font color="green">Banners deleted successfully.</font>';
	break;
	
	default:
		$message=$message;
	break;
	
}//end if
?>
<script language="javascript" type="text/javascript">
function confirm_check()
{
	var chkVar;
	for (var i = 0; i < document.form1.elements.length; i++) 
	{
		var e = document.form1.elements[i];
		if (e.name == "ids[]") 
		{
			if(e.checked==true)
			{
				chkVar=true;
			}//end if
		}//end if
	}//end for loop

	 if(chkVar==true)
	{
		 var where_to= confirm("Do you want to permanently delete this Banner ?");
		 if (where_to== true)
		 {
		   return true;
		 }
		 else
		 {
		  return false;
		  }
	}//end if
}

function OpenNewWin(nId,nWid,nHgt,ntxt)
{
	var windowZ;
	windowZ=window.open('showopenx.php?nId='+nId,'OpenX','toolbars=no,scrollbars=yes');
	windowZ.resizeTo(nWid,nHgt+100);
}//end function
$(function(){
        // add multiple select / deselect functionality
        $("#checkAll").click(function () {
            $('.case').attr('checked', this.checked);
        });

        // if all checkbox are selected, check the selectall checkbox
        // and viceversa
        $(".case").click(function(){

            if($(".case").length == $(".case:checked").length) {
                $("#checkAll").attr("checked", "checked");
            } else {
                $("#checkAll").removeAttr("checked");
            }

        });
    });
</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 height="10">&nbsp;</td>
                    </tr>
              <tr>
                <td width="32%" class="headings1" colspan="2">Banner</td>
                <!--td width="68%" align="right" class="headings1"><a href="add_banner.php" class="signupbuttonlink">Add New Banner</a></td-->
              </tr>
            </table>
			<div class="bordernew padding2">
				<div class="btns-pnl ryt">
					<a href="add_banner.php" class="add-btn">Add New Banner</a>
				</div>
                            <div class="clear"></div>
			
				<form name="form1" method="post" action="" onSubmit="return confirm_check();">
          <table width="100%"  border="0" cellspacing="0" cellpadding="5">
            <tr>
              <td><table width="100%"  border="0" cellspacing="1" cellpadding="0" class="listing_tbl_admin">
					<?php 	if(isset($message) && $message!='')
							{
					?>
                <tr align="left">
                  <td align="center" class="maintext2">&nbsp;</td>
                  <td colspan="7" class="maintext2 warning"><?php echo $message;?></td>
                  </tr>
				<?php }//end if
					  if ($objDB) 
					  { 
                 			if (!empty($data)) 
							{ 
				?>
                <tr align="left">
                    <th class="maintext2" align="center">&nbsp;<input type="checkbox" id="checkAll" name="checkAll" title="Check All / Uncheck All"/></th>
                  <th align="center" class="maintext2">#</th>
                  <th class="maintext2">Title</th>
                  <th class="maintext2">Banners</th>
                  <th class="maintext2">Create Date</th>
                  <th class="maintext2">Set Order</th>
                  <th class="maintext2">Active</th>
                  <th class="maintext2" align="center" >Action</th>
                  
                  </tr>
					<?php  	$count='1';
							foreach ($data as $key => $value) 
							{ 
								if($value['nType']=='OpenX')
								{
									$showBannerCode='<a href="javascript:OpenNewWin('.$value['nBId'].','.$value["vWidth"].','.$value["vHeight"].');">OpenX</a>';
								}//end if
								else
								{
									if($value["vImg"]!='')
									{
										$showBannerCode='<a href="'.$value["vlocUrl"].'" target="_blank" class="thumbnail"><img src="../banners/'.$value["vImg"].'" name="img1" width="234" height="60" border="0"><span><img src="../banners/'.$value["vImg"].'" width="'.$value["vWidth"].'" height="'.$value["vHeight"].'" border="0"/></a>';
									}//end if
								}//end else
				?>
                <tr align="left">
                    <td width="5%" align="center" class="maintext2"><input name="ids[]" class="case" type="checkbox" value="<?php echo $value['nBId']?>"></td>
                  <td width="6%" align="center" class="maintext2"><?php echo $key+1;?></td>
                  <td width="27%" class="maintext2"><?php echo htmlspecialchars(stripslashes($value['vName']))?></td>
                  <td width="20%" class="maintext2"><?php if($showBannerCode!='')
									{
	  									echo $showBannerCode;
									}//end if
	  								else
									{
										echo ' <img src="../images/nobanner.gif" name="img1" width="400" height="70" border="0">';
									}//end else?></td>
                  <td width="12%" align="left" class="maintext2"><?php echo date('m/d/Y',strtotime($value['nDate']));?></td>
                  <td width="8%" align="center" class="maintext2"><?php if($cnt!=1){?>
	<a href='banner.php?Action=ordering&move=up&id=<?php echo $value["nBId"];?>&pos=<?php echo $value["nPosition"];?>'>
<?php } ?>
<img src="../images/up.gif" alt="Up" border=0></a>

<?php if($cnt!=$count){?>
<a href="banner.php?Action=ordering&move=down&id=<?php echo $value["nBId"];?>&pos=<?php echo $value["nPosition"];?>">
<?php } ?>
<img src="../images/down.gif" alt="Down" border=0></a></td>
                  <td width="8%" align="center" class="maintext2"><?php if($value['vActive']=='1'){echo 'Yes';}else{echo 'No';}?></td>
                  <td width="8%" align="center" class="maintext2"><a href="add_banner.php?nBId=<?php echo $value['nBId']?>&page=<?php echo $objP->getCurrentPageID()?>" class="signupbuttonlink">Edit</a></td>
                  
                  </tr>
				<?php 		$count++;
						}//end foreach
				?>
			 <!--  <tr align="left">
                  <td align="center" class="maintext2">&nbsp;</td>
                  <td colspan="6" class="maintext2">&nbsp;</td>
                  <td align="center" class="maintext2"><input type="submit" name="DelNow" value="Delete" class="loginbutton"></td>
                </tr>-->
			   <?php
					 		}//end if
						}//end first if
				?>
              </table>
				<table width="100%" cellspacing="1" cellpadding="1" border="0" style="margin-top:10px;">
                                                <tr align="left">

                                                    <td align="left" >
                                                        <!--
                                                        <input type="button" name="DelNow" value="Delete" class="loginbutton" style="cursor:pointer" title="Delete Selected" onClick="deleteProducts(this.form);">
                                                        -->
                                                        <input type="submit" name="DelNow" value="Delete" class="loginbutton" style="cursor:pointer" title="Delete Selected" >

                                                    </td>
                                                    <td width="18%"><?php if(!empty($data)) {
    ?><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                                                            <tr>
                                                                <td width="82%">&nbsp;</td>
                                                                <td width="18%"><table width="100%"  border="0" cellspacing="2" cellpadding="0" class="pagination_style">
                                                                        <tr align="center" class="textbox">
                                                                            <td><?php echo $objP->links;?></td>
                                                                        </tr>
                                                                    </table></td>
                                                            </tr>
                                                        </table><?php }//end if?></td>
                                                </tr>

                                            </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>
                     </td>
                  </tr>
                </table></td>
            </tr>
          </table></form>
			</div>
			</td>
          </tr>
        </table>
		</td>
        </tr>
    </table>      
    </td>
  </tr>
</table>
<?php include_once('bottomhtml.php');?>