<?php
session_start();
include("../include/config.php");
include("../include/functions.php"); 
include("../include/simpleimage.php"); 
  validate_admin();
  $headline=mysqli_real_escape_string($GLOBALS['conn'],$_POST['headline']);
  $precart_id=mysqli_real_escape_string($GLOBALS['conn'],$_POST['precart_id']);
   
  if($_REQUEST['submitForm']=='yes'){
  if($_REQUEST['ppid']!=''){
	$ids=implode(",",$_REQUEST['ppid']);  
  }
  
  if($_REQUEST['id']==''){
	  $obj->query("insert into $tbl_precartproduct set headline='$headline',precart_id='$precart_id',product_price_ids='$ids',status=1 ");
	  $_SESSION['sess_msg']='Precart product added sucessfully';  
	  
       }else{ 
	 
	   $sql=" update $tbl_precartproduct set  headline='$headline',precart_id='$precart_id',product_price_ids='$ids' ";
	   $sql.=" where id='".$_REQUEST['id']."'";
	   $obj->query($sql);
	  
	  $_SESSION['sess_msg']='Precart product updated sucessfully';   
        }
   header("location:precartproduct-list.php?precart_id=".$_REQUEST['precart_id']);
   exit();
  }      
	   
	   
if($_REQUEST['id']!=''){
$sql=$obj->query("select * from $tbl_precartproduct where id=".$_REQUEST['id']);
$result=$obj->fetchNextObject($sql);
}
	
?>
<!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=utf-8" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript">
function validate(obj)
{
if(obj.headline.value==''){
alert("Please enter headline");
obj.headline.focus();
return false;
}
if(obj.cart_amount.value==''){
alert("Please enter amount");
obj.cart_amount.focus();
return false;
}

}
</script>

<script type="text/javascript" src="../include/ckeditor/ckeditor.js"></script>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<?php include("header.php") ?>
<script type="text/javascript">
function callSubcategories(cat_id){
	$.ajax({
		url:"callPreSubcat.php",
		data:{cat_id:cat_id},
		success:function(data){
			$("#resultSubcats").html(data);
			}
		
		
		})
}
</script>

<script type="text/javascript">
function doSearch(val){
	$("#search_drop").load("ajaxp-search.php?q="+val).show();
	$("#search_drop").mouseleave(function(){
    $(this).hide();
});
}
</script> 
<script type="text/javascript">
function addToCartSearch(product_id,product_price_id){
        var qty=$('#qtysearch_'+product_price_id).val();
		var prodname=$("#searchprodname_"+product_price_id).val();
        var pclass="#added > div.prod_"+product_price_id;
		var eleclass=".prod_"+product_price_id;
		if(product_id){
		$.ajax({
			url:"ajaxp-process.php",
			data:{product_id:product_id,product_price_id:product_price_id,qty:qty,action:"add_cart"},
			success:function(data){
				if ($(pclass).length > 0){
					$(eleclass).html(data);
					}else{
				$("#added").append(data);
					}
				}
			
			})	
			
		}
  
	}
</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 Precart Products for precart <?php echo getField('precart_title',$tbl_precart,$_REQUEST['precart_id']); ?>
					<span  style="float:right; padding-right:10px;">
                    <input type="button" name="add" value="Back To Precart"  class="button" onclick="location.href='precart-list.php'" />&nbsp;&nbsp;
					<input type="button" name="add" value="View Precart Products"  class="button" onclick="location.href='precartproduct-list.php?precart_id=<?php echo $_REQUEST['precart_id']; ?>'" /></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="" autocomplete="off" onsubmit="return validate(this)">
						<input type="hidden" name="submitForm" value="yes" />
						<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
                        <input type="hidden" name="precart_id"  value="<?php echo $_REQUEST['precart_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 align="right" class="paddBot11 paddRt14"><h2>&nbsp;</h2></td>
									  <td align="left" class="paddBot11">&nbsp;</td>
							       </tr>
									<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Headline:</strong></td>
									  <td align="left" class="paddBot11"><input name="headline" type="text" id="headline" size="36" value="<?php echo stripslashes($result->headline);?>" /></td>
							       </tr>
                                  
                                   
							    
                                    <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong> Products:</strong></td>
									  <td align="left" class="paddBot11">
                                      <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="54%" class="text14"><input type="text" name="q"  value="" onkeyup="return doSearch(this.value)" />
     <div id="search_drop"></div></td>
    <td width="46%" class="text14" id="added" >
    <?php if($_REQUEST['id']!=''){ 
	if($result->product_price_ids!=''){
		$itmesArr=$obj->query("select * from $tbl_productprice where id in (".$result->product_price_ids.") ",$debug=-1);
									
	while($resultItem=$obj->fetchNextObject($preItmes)){
		$product_name=getField('product_name',$tbl_product,$resultItem->product_id)." ".$resultItem->size;
	?>
    <div class="prod_<?php echo $resultItem->id; ?>">
<input type="hidden" name="ppid[]" value="<?php echo $resultItem->id; ?>" />
<div  align="left"><?php echo $product_name;?><a href="javascript:void(0)" onclick="$('.prod_<?php echo $resultItem->id; ?>').html('')"><img src="images/del.png" /></a></div>
</div>
<?php } } }?>
    </td>
  
  </tr>
</table> </td>
							  </tr> 
									<tr>
									  <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
									  <td align="left" class="paddBot11">&nbsp;</td>
							  </tr>
									<tr>
										<td width="18%" align="right" class="paddRt14 paddBot11">&nbsp;</td>
										<td width="82%" 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>