<?php
session_start();
include("../include/config.php");
include("../include/functions.php"); 
include("../include/simpleimage.php");
 validate_admin();
 $bcat_id=mysqli_real_escape_string($GLOBALS['conn'],$_REQUEST['cat_id']);

  if($_REQUEST['submitForm']=='yes'){
	    $Image= new SimpleImage();
	  
  if($_REQUEST['id']==''){
	  foreach($_REQUEST['brand'] as $key=>$val){
		  if($val!=''){
			  $brand=mysqli_real_escape_string($GLOBALS['conn'],$val);
			 
				$bArr=$obj->query("select * from $tbl_brand where brand='$brand' and bcat_id='$bcat_id' ");  
			
			if($obj->numRows($bArr)==0){
				$img='';
				  if($_FILES['photo']['size'][$key]>0 && $_FILES['photo']['error'][$key]==''){
					  $img=time().substr($_FILES['photo']['name'][$key],-5);
					  move_uploaded_file($_FILES['photo']['tmp_name'][$key],"../upload_images/brand/".$img);
					  copy("../upload_images/brand/".$img,"../upload_images/brand/thumb/".$img);
					  $Image->load("../upload_images/brand/thumb/".$img);	  
					  $Image->resize(100,80);	  
					  $Image->save("../upload_images/brand/thumb/".$img);	 
				  } 			  
		         $obj->query("insert into $tbl_brand set brand='".ucfirst($brand)."',bcat_id='$bcat_id',logo='$img',status=1 ");
				 $brand_id=mysqli_insert_id($GLOBALS['conn']);
	             generateSlug($brand,$tbl_brand,$brand_id);
			}
		  }
	  }
	  $_SESSION['sess_msg']='Brand added sucessfully';  
	   
   header("location:brand-list.php");
   exit();
  } 
  }   
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo SITE_TITLE; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript">
function validate(obj)
{
	var fnameInput = document.getElementsByName('brand[]');
		for (i=0; i<fnameInput.length; i++)
			{
			 if (fnameInput[i].value == "")
				{
			 	 alert('Complete all brand name fields');		 
			 	 return false;
				}
			}
		
	
}
</script>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<?php include("header.php") ?>
<script type="text/javascript">
$(document).ready(function(){
	var counter = 1;
	 $("#add").click(function(){
		 var tblid="addedfield_"+counter;
		 $("#resultField").append('<table width="100%" border="0" cellspacing="0" cellpadding="0" id="'+tblid+'" ><tr><td align="right" class="paddBot11 paddRt14" width="275"><strong>Brand :</strong></td><td align="left" class="paddBot11"><input name="brand[]" type="text"  size="36" value="" /></td></tr><tr><td align="right" class="paddBot11 paddRt14" width="275"><strong>Logo :</strong></td><td align="left" class="paddBot11"><input type="file" name="photo[]"  /></td></tr></table>');
		 counter++;
		 })
		 $("#remove").click(function(){
		  counter=counter-1;
		  var tblid="#addedfield_"+counter;
		  $(tblid).remove();
		
		 })
	
	})
</script>

<tr>
	<td align="right" class="paddRtLt70" valign="top">
		<table width="99%" border="0" cellspacing="0" cellpadding="0">
			<tr>
			
				<td align="right" valign="top">
					<table width="100%" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td align="left" valign="middle" class="headingbg bodr text14">
					<em><img src="images/arrow2.gif" width="21" height="21" hspace="10" align="absmiddle" /></em>Admin: Add Brand 
					<span  style="float:right; padding-right:10px;">
					<input type="button" name="add" value="View Brands"  class="button" onclick="location.href='brand-list.php'" /></span></td>
						</tr>
						
						<tr>
							<td height="100" align="left" valign="top" bgcolor="#f3f4f6" class="bodr">
                            <form name="frm" method="POST" enctype="multipart/form-data" action="" onsubmit="return validate(this)">
						<input type="hidden" name="submitForm" value="yes" />
						<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
							<table width="100%" cellpadding="0" cellspacing="0">
									<tr>
										<td align="center" colspan="2" class="paddRt14 paddBot11">
										<font color="#FF0000"><strong><?php echo $_SESSION['sess_msg']; $_SESSION['sess_msg']='';?></strong></font></td>
									</tr>
									<tr>
										<td width="23%" align="right" class="paddBot11 paddRt14">&nbsp;</td>
										<td width="77%" align="left" class="paddBot11"></td>
									</tr>
								<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Category :</strong></td>
									  <td align="left" class="paddBot11"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="32%"><select name="cat_id" style="width:250px;" id="cat_id">
                                      <option value="">Select Category</option>
                                      <?php $catArr=$obj->query("select * from $tbl_maincategory where status=1 and parent_id=0 order by id ",$debug=-1); 
									  while($resultCat=$obj->fetchNextObject($catArr)){
									  ?>
                                      <option value="<?php echo $resultCat->id; ?>"<?php if($resultCat->id==$result->cat_id){?>selected<?php } ?>><?php echo stripslashes($resultCat->maincategory); ?></option>
                                      <?php } ?>
                                      </select></td>
    <td width="68%"><a href="maincategory-addf.php" target="_blank">Add New Category</a></td>
  </tr>
</table>
</td>
							  </tr>
                             
									<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Brand:</strong></td>
									  <td align="left" class="paddBot11"><input name="brand[]" type="text" size="36" value="" /></td>
							  </tr>
                              <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Logo:</strong></td>
									  <td align="left" class="paddBot11"><input type="file" name="photo[]"  /></td>
							  </tr>
									
									<tr  ><td colspan="2" id="resultField"></td>
</tr>
									<tr>
									  <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
									  <td align="left" class="paddBot11">&nbsp;</td>
							  </tr>
                              <tr>
									  <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
									  <td align="left" class="paddBot11">
                                     <a href="javascript:void(0)" id="add"> <img src="images/add.png" title="Add More" border="0" /></a>&nbsp;
                                     <a href="javascript:void(0)" id="remove"> <img src="images/remove.png" title="Remove" border="0" /></a>
                                      </td>
							  </tr>
                              <tr>
									  <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
									  <td align="left" class="paddBot11">&nbsp;</td>
							  </tr>
									<tr>
										<td width="21%" align="right" class="paddRt14 paddBot11">&nbsp;</td>
										<td width="79%" align="left" class="paddBot11">
											<input type="submit" name="submit" value="Submit"  class="submit" border="0"/> 	                  		 &nbsp;&nbsp;
											<input name="Reset" type="reset" id="Reset" value="Reset" class="submit" border="0" />									  </td>
									</tr>
								</table></form>
							</td>
						</tr>
						
					</table>
				</td>
			</tr>
		</table>
	</td>
</tr>
<?php include('footer.php'); ?>
</table>
</body>
</html>


