<?php
ob_start();
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php

include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";
include_once('../includes/S3.php');


if (get_magic_quotes_gpc()) {
	$_POST = array_map('stripslashes_deep', $_POST);
	$_GET = array_map('stripslashes_deep', $_GET);
	$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

if(isset( $_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"]!= ""){
	$adminname = $_SESSION["sess_adminname"];
}else{
	header("Location:login.php");
	exit;
}


include "../includes/htmltop.php";

/*taken from the general site settings table*/

//$banner_image_size = getSettingsValue('banner_size');
$banner_image_size = '1500,443';
$cdn_enabled = "N";
if(ENABLE_AMAZON_S3_IMAGES=="Y"){
    $cdn_enabled  = "Y";
}
	
if(isset($_POST['add']))
{


	/*check whether banner with the same name exists in the database*/



	$bannerfiletype = $_FILES['txtBannerImage']['type'];
	$bannerfilename = $_FILES['txtBannerImage']['name'];
	$bannertempname	= $_FILES['txtBannerImage']['tmp_name'];
         $txtLink       =   $_POST['txtLink'];
		
	if ($_FILES['txtBannerImage']['name'][0] != "")
	{
		if (!isValidWebImageType($bannerfiletype,$bannerfilename,$bannertempname))
		{

			$message .= " * ".INVALID_IMAGE. "<br>";

		}
		else
		{


			$imagewidth_height_type_array = explode(":", ImageTypeBanner($_FILES['txtBannerImage']['tmp_name']));
			$imagetype = $imagewidth_height_type_array[0];

			$assignedname = "banner" . time() . "." . $imagetype;

			if (move_uploaded_file($_FILES['txtBannerImage']['tmp_name'], "../homepagebanners/" . $assignedname))
			{

					
				chmod("../homepagebanners/$assignedname", 0777);

				//$banner_size  = split(',',$banner_image_size);
				//generateThumbNail("../homepagebanners/".$assignedname,"../homepagebanners/".$assignedname,$banner_size[0],$banner_size[1]);
				$imagepath = "../homepagebanners/".$assignedname;
                                moveImageToCDN($imagepath,'homepagebanners/'.$assignedname);

				$insert_banner = "INSERT INTO ".$tableprefix."homepagebanners(image,link,cdn_images_moved)
                                    VALUES('".mysql_real_escape_string($assignedname). "','".mysql_real_escape_string($txtLink). "','".  mysql_real_escape_string($cdn_enabled)."')";
				
					
					
				mysql_query($insert_banner) or die(mysql_error());

				header('Location:homepagebanners.php?msg='.BANNER_ADD_SUCCESS);
			}
				
		}


	}else
	{
		$message .= " * " .PLS_SEL_IMG_FORMAT. "<br>";
	}


		

}
?>
<body topmargin="0">
	<script>
function checkAddBannerForm()
{

  if($('#txtBannerImage').val()=='')
        {
            alert('<?php echo BANNER_IMAGE_REQUIRE;?>');
            return false;
        }

         if($('#txtBannerImage').val()!='')
        {
             if(!isValidImage($('#txtBannerImage').val()))
             {
                alert('<?php echo BANNER_IMAGE_INVALID;?>');
                return false;
             }
        }

         if($('#txtLink').val()!='')
        {
            var url     =  $('#txtLink').val();
            
             var pattern = /^(http(s)?:\/\/)?(www\.)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/;

            if(pattern.test(url)){
                return true;
            } else {
                alert('<?php echo INVALID_LINK_NOTE;?>');
                return false;
            }
        }
       else {
            return true;
       }
  
   
}

</script>
<body>

	<center>

		<div align="right" id="layout">




			<!--header area start -->
			<?php

			include_once("../includes/topheader_admin.php")

			?>

			<!--header area end -->




			<div align="right" class="main_body">
				<!--main body start -->



				<?php
				include_once("../includes/admintopmenu.php")
				?>


				<div align="right" class="content_area">

					<!--content area start -->

					<div align="left" class="page_title">
						<?php echo BANNER_ADDING;?>
					</div>
					<div align="center" class="text_information grey_tr">
						<?php echo $message?>
					</div>
					<br>

					<div align="right">
						<form name="frmAddBanner" action="#" method="POST"
							enctype="multipart/form-data"
							onSubmit="return checkAddBannerForm();">

							<table width="100%" border="0" cellpadding="0" cellspacing="0"
								class="manage_table">

								<tr>
									<td colspan="3" align="right"><span class="required">*</span>&nbsp;<?php echo FIELDS_ARE_MANDATORY;?>
									</td>
								</tr>


								<tr>
								<td align="right" valign=top><?php echo BANNER_IMAGE;?>&nbsp;<span
										class="required">*</span>
									</td>
									<td>&nbsp;</td>
									<td valign=top align="left"><input type="file"
										name="txtBannerImage" size="30" id="txtBannerImage"><br> &nbsp;[<?php echo JPG_GIF_PNG;?>]&nbsp;[<?php echo str_replace(',','*',$banner_image_size)?>]&nbsp;
									</td>
									
									
								</tr>
                                                                 <tr>
								<td align="right" valign=top><?php echo BANNER_LINK;?>&nbsp;
									</td>
									<td>&nbsp;</td>
									<td valign=top align="left">
                                                                            <input type="text" id="txtLink" size="100" value="<?php echo $_POST['txtLink'];?>" class="textarea txtLink" name="txtLink">
                                                                            &nbsp;&nbsp;<?php echo BANNER_LINK_NOTE;?>
									</td>


								</tr>
								<tr>
									<td colspan="3">&nbsp;</td>
								</tr>
								<tr>
									<td colspan="3" align="center"><input type="button"
										class="button" name="back"
										value="&nbsp;<?php echo BACK;?>&nbsp;"
										onClick="history.go(-1)"> <input type="submit"
										value="&nbsp;<?php echo ADD_TEXT;?>&nbsp;" name="add"
										class="button"></td>
								</tr>
								<tr>
									<td colspan="3">&nbsp;</td>
								</tr>
							</table>

						</form>

					</div>


					<!--content area end-->

				</div>


				<div align="right" class="clear_float">
					<img src="<?php echo $imagefolder;?>/clear.gif" alt="" width="1"
						height="1" />
				</div>
				<!--main body end -->
			</div>


			<?php
			include "../includes/footer.php";

			ob_end_flush();
			?>