<?php session_start();  //Developed by ITechScripts
error_reporting(0);
require 'include/announcements.php'; // ITECHBIDS Configuration File 
require 'include/itechbids.php'; // Server compatibility Patch
$user_id=$_SESSION[userid];
if($_GET)
$productid=$_GET['productid'];
else if($_POST) 
$productid=$_POST['productid'];

$_SESSION[productid]=$productid;

$sql_user1="select * from auction_details where productid=$productid";
   $res_user1=mysql_query($sql_user1);
   $row=mysql_fetch_array($res_user1);
$test_user=$row[user_id];
$description=$row['detailed_descrip'];
$producttitle=$row[producttitle];
$selling_method = $row['selling_method'];
$sql_user12="select * from auction_details where productid='$productid' and producttitle='$producttitle' and user_id='$test_user'";
$res_user12=mysql_query($sql_user12);
$countt=mysql_num_rows($res_user12);
if($countt==0)
{
include 'unauthorized.php';
exit();
}
$mode=$_GET[mode];
$err_flag=$_POST['err_flag'];
$email_to_status=$_GET['email_to_status'];
if($email_to_status==1)
{
$email_to_status="You Have Emailed this item to your friend!";
}
else if($email_to_status==2)
{
$email_to_status="Problem in sending mail.";
}
if($mode=="watch")
{
if(empty($_SESSION[userid]))
{
 echo '<meta http-equiv="refresh" content="0;url=signin.php?url=itechd.php&productid='.$productid.'&mode='.$mode.'">';
 echo "<font size=+1 color=#003366>$lang_676....</font>"; 
 exit();
}
else
{
    $watch_tot_sql="select count(*) from watch_list where user_id=$user_id and productid=$productid";
    $watch_ins_sql=mysql_query($watch_tot_sql);
	$watch_res_sql=mysql_fetch_array($watch_ins_sql);
	$watch_tot=$watch_res_sql[0];
	if($watch_tot==0)
	{
    $sql="insert into watch_list(productid,user_id) values($productid,$user_id)";
    $ins=mysql_query($sql);
	}
    $watch_flag=1;
}

}
if($_POST)
{
$productid=$_POST['productid'];
$max_bid=$_POST['max_bid'];
//$productid=$_GET['productid'];
$chk=$_GET['chk'];
$qty=$_POST['qty'];
$user_id=$_SESSION[userid];

if(empty($_SESSION[userid]))
{
 echo '<meta http-equiv="refresh" content="0;url=signin.php?url=itechd.php&productid='.$productid.'">';
 echo "<font size=+1 color=#003366>$lang_676....</font>"; 
 exit();
}
else if($_SESSION[userid]) 
{
 $member_acc="select * from member_details where user_id=$_SESSION[userid]";
 $memebr_rec=mysql_query($member_acc);
 $member_res=mysql_fetch_array($memebr_rec);
 $sql_user1="select * from auction_details where productid=$productid";
 $res_user1=mysql_query($sql_user1);
 $row=mysql_fetch_array($res_user1);
 
 
$bid_sql_max="select MAX(bidding_amount) as amt from site_bids where productid=".$productid;
$bid_res_max=mysql_query($bid_sql_max);
$bidrow_max=mysql_fetch_array($bid_res_max);
$max_item_bid=$bidrow_max['amt'];
if(!empty($max_item_bid))
{
$bid_sql2="select MAX(bidding_amount) as amt from site_bids where user_id=$user_id and productid=".$productid;
$bid_res2=mysql_query($bid_sql2);
if(mysql_numrows($bid_res2)>0)
{
$bid_row2=mysql_fetch_array($bid_res2);
$user_bid_amt=$bid_row2['amt'];
}

// $bid_$row=mysql_fetch_array($bid_res);
if($max_item_bid==$user_bid_amt)
 {
 $warning="You are the Highest Bidder for this item.You can't bid for this item.";
 $err_flag=1;
 }
 }

 
if($member_res[member_account]==1)
{
$tot_bid=5;
}

if($member_res[member_account]==1)
{
 $no_of_bidded="select * from site_bids a, auction_details b where b.status='Active' and a.user_id=$user_id and b.user_id=$user_id group by a.productid";
 $bidded_res=mysql_query($no_of_bidded);
 $bidded_row=mysql_num_rows($bidded_res);
if($bidded_row  > $tot_bid)
{
$warning="Sorry!You Have Already Bidded $bidded_$row Items.Please Select Basic or Superior Account to Bid More Items!.";
$err_flag=1;
}
}
if(isset($user_id))
{
if($row[user_id]==$user_id)
{
$err_flag=1;
$warning="You are the Seller for this item.You can't bid for this item.";
?>
<? 
}
else if($row[user_id]!=$user_id)
{
$postmethod=$_POST[postmethod];
if($postmethod=="fix")
{
$date1=date('Y-m-d');
$ex="select * from site_bids where user_id=$user_id and productid=$productid";
$ex_res=mysql_query($ex);
$ex_row=mysql_num_rows($ex_res);
$fix=$row[quick_buy_price];
 if(empty($ex_row))
 { 
 $bid_ins="insert into site_bids(productid,user_id,bidding_amount,bidding_date,quantity) values('$productid','$user_id','$fix','$date1','$qty')";
 $ins=mysql_query($bid_ins);
 }
 else
 {
 $bid_up="update site_bids set bidding_amount=$fix, bidding_date='$date1',quantity=$qty where user_id=$user_id and productid=$productid";
 $exe=mysql_query($bid_up);
 }
$alert_ins="insert into user_alert(productid,seller_id,buyer_id,date,alert_type) values($productid,$row_user[user_id],$_SESSION[userid],'$date1','R')";
$alert_exe=mysql_query($alert_ins);
//fetch site name 
$query="select * from genaralsettings where set_id = 1";
$table=mysql_query($query);
if($row=mysql_fetch_array($table))
$site_name = $row['set_value'];

//fetching admin mail id;
$query="select * from genaralsettings where set_id = 3";
$table=mysql_query($query);
if($row=mysql_fetch_array($table))
$admin_mail_id = $row['set_value'];

$expiry_date = $row['expire_date'];
        $date1 = strtotime(now);
		$date2=strtotime($expiry_date);
      $date1 = date("Y,m,j,G,i,s",$date1);
      $date2 = date("Y,m,j,G,i,s",$date2); 

// update auction_details; 
$query="select * from auction_details where productid='$productid'";
$table=mysql_query($query);
if($row=mysql_fetch_array($table))
{
 $quantity=$row['Quantity'];
}
if($quantity==1)
{
 $query="update auction_details set status='Closed',quantity=quantity-1,quantity_sold=quantity_sold+1 where productid='$productid'";
 mysql_query($query);
}
else if($quantity >  1)
{
 $quantity=$quantity-1;
 $query="update auction_details set quantity='$quantity',quantity_sold=quantity_sold+1  where productid='$productid'";
 mysql_query($query);
}
 $update_bid="update site_bids set user_pos='Yes' where productid='$productid' and user_id=$user_id";
 mysql_query($update_bid);

$message = $bidder_win[mail_message];

$message=str_replace("NAME" , $_SESSION['username'],$message);
$message=str_replace("PRODUCTID" , $productid , $message);

$query="select * from auction_details where productid=$productid";
$table=mysql_query($query);
$row=mysql_fetch_array($table);

 $seller_id=$row['user_id'];
 $producttitle=$row['producttitle'];
 $sale_price=$row['currency'].$row['quick_buy_price'];
 $item_shipping_cost=$row['currency'].$row['shipping_cost'];
 $item_shipping_route=$row['shipping_route'];
 $item_shipping_tax=$row['currency'].$row['tax'];
 $item_total_quantity=$row['Quantity'];
 $payment_gateway=$row['payment_gateway'];
 $higher_quantity=1;

$subject = $bidder_win[mail_subject];
//fetching user mail id;
$query="select * from member_details where user_id = '$_SESSION[userid]'";
$table=mysql_query($query);
if($row=mysql_fetch_array($table))
$mail_to = $row['email'];
$message=str_replace("NAME" , $_SESSION['username']	,$message);
$message=str_replace("<PRODUCTTITLE>" , $producttitle , $message);
$message=str_replace("SALE_PRICE" , $sale_price, $message);
$message=str_replace("QUANTITY" , $higher_quantity , $message);
$message=str_replace("SHIPPING_COST" , $item_shipping_cost , $message);
$message=str_replace("SHIPPING_ROUTE" , $item_shipping_route , $message);
$message=str_replace("SALE_TAX" , $item_shipping_tax , $message);
$message=str_replace("<GET_VIEW_ITEM>" , "$site_name/itechd.php?productid=".$productid , $message);
$message=str_replace("<sitename>" , $site_name , $message);
$total_price=$sale_price;
$get_you_item="$site_name/postpay.php?id=$productid&pm=$total_price&quantity=$higher_quantity";
if($payment_gateway!=0)
{
   $message=str_replace("GET_PAY_NOW" , "$get_you_item" , $message);
}
else
{
    $message=str_replace("PAY NOW" , "" , $message);
}
$subject=$bidder_win['mail_subject'];  
$mail_from=$bidder_win['mail_from'];
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: ". $mail_from."\r\n";
          
 
  $mail=mail($mail_to,$subject,$message,$headers);
 
 
 
// mail_to seller;
		  

$message = $mail_seller[mail_message];		
$subject=$mail_seller['mail_subject'];  
$mail_from=$mail_seller['mail_from'];  		  
$query="select * from member_details where user_id = '$seller_id'";
$table=mysql_query($query);
if($row=mysql_fetch_array($table))
{
$seller_name=$row['user_name'];
$seller_email=$row['email']; 
}
$mail_to=$seller_email;
$buyer_detail="<tr><td>Buyer Name</td><td> $_SESSION[username] </td></tr>";
$buyer_detail.="<tr><td>Quantity</td><td>$higher_quantity</td></tr>";
$buyer_detail.="<tr><td>Price</td><td>$sale_price</td></tr>";

$message=str_replace("<sellername>" , $seller_name , $message);
$message=str_replace("<itemname>" , $producttitle , $message);
$message=str_replace("<itemid>" , $productid , $message);
$message=str_replace("<Buyer Details>" , $buyer_detail , $message);
$message=str_replace("<sitename>" , $site_name , $message);
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: ". $mail_from."\r\n";

 $mail=mail($mail_to,$subject,$message,$headers);
 
$success_flag="Item Successfully Purchased!";
$err_flag=1;

} 
else // if($postmethod!=fix)
{

$bid_sql="select * from site_bids where productid=".$productid;
$bid_res=mysql_query($bid_sql);
$bid_row=mysql_fetch_array($bid_res);
    if(!empty($bid_row[productid]))
{
$bid_sql1="select MAX(bidding_amount) as amt from site_bids where productid=".$productid;
$bid_res1=mysql_query($bid_sql1);
$bid_row1=mysql_fetch_array($bid_res1);
 $max_bid_amt=$bid_row1['amt']+$row['bidincrement'];
}
   else
{
$max_bid_amt=$row[min_bid_amount]+$row['bidincrement'];
}
if(!is_numeric($max_bid))
{
//$err="Your Bid Amount is Invalid";
$err_flag=1;
//$warning="Your Bid Amount is Invalid";
?>
<!-- <tr><td align="center">
	<font size=2 color="red"><? echo "Your Bid Amount is Invalid"; ?></font>
	</td></tr> -->
<?
} 
//if(!is_numeric($max_bid))
//else if($row[user_id]!=$user_id)

if($err_flag!=1)
{
if($max_bid>=$max_bid_amt)
{
$date1=date('Y-m-d');
$ex="select * from site_bids where user_id=$user_id and productid=$productid";
$ex_res=mysql_query($ex);
$exrow=mysql_num_rows($ex_res);
 if(empty($ex_row))
{
  $bid_ins="insert into site_bids(productid,user_id,bidding_amount,bidding_date,quantity) values($productid,$user_id,$max_bid,'$date1',$qty)";
  $ins=mysql_query($bid_ins);
}
 else
{
 $bid_up="update site_bids set bidding_amount=$max_bid,bidding_date='$date1',quantity=$qty where user_id=$user_id and productid=$productid";
 $exe=mysql_query($bid_up);
}
// mail to seller (reserve price met)

if($row[reserve_price])
{
 if($row[reserve_price] <= $max_bid)
 {
 
 //
 
$message = $reserve_price_met[mail_message];		
$subject=$reserve_price_met['mail_subject'];  
$query="select * from member_details where user_id =". $row[user_id];
$table=mysql_query($query);
if($postrow=mysql_fetch_array($table))
{
$seller_name=$postrow['user_name'];
$seller_email=$postrow['email']; 
} 
$mail_to=$seller_email;

$query="select * from genaralsettings where set_id = 3";
$table=mysql_query($query);
if($row=mysql_fetch_array($table))
$admin_mail_id = $reserve_price_met['mail_from'];  

$buyer_detail="<tr><td>Buyer Name</td><td> $_SESSION[username] </td></tr>";
$buyer_detail.="<tr><td>Bid Amount</td><td>$$max_bid</td></tr>"; 
$message=str_replace("<sellername>" , $seller_name , $message);
$message=str_replace("<itemname>" , $row[producttitle] , $message);
$message=str_replace("<itemid>" , $productid , $message);
$message=str_replace("<Buyer Details>" , $buyer_detail , $message);
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: ". $admin_mail_id."\r\n";
$mail=mail($mail_to,$subject,$message,$headers);
 }
}

// check the bid
$chk=$_POST['chk'];
if ($chk=='chk') 
{
 $updsql="update site_bids set checkbid='yes' where productid=$productid and user_id=$user_id";
 $updexe=mysql_query($updsql);
}
$sql="select *from site_bids where checkbid='yes' and productid=$productid";
$exe=mysql_query($sql);
while($ret=mysql_fetch_array($exe))
{
$sql1="select * from member_details where user_id=".$ret['user_id'];
$exe1=mysql_query($sql1);

while ($selret=mysql_fetch_array($exe1))
{
$emailid=$selret['email'];
$receiver=$selret['user_name'];


$message = $outbid_query_res['mail_message'];		
$subject=$outbid_query_res['mail_subject'];  

$fromid=$outbid_query_res['mail_from'];


$message=str_replace("<user>" , $receiver , $message);
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: ". $from."\r\n";
mail($emailid,$subject,$message,$headers);  
}
}
 $alert_ins="insert into user_alert(productid,seller_id,buyer_id,date,alert_type) values($productid,$row[user_id],'$_SESSION[userid]','$date1','R')";
 $alert_exe=mysql_query($alert_ins);
  echo '<meta http-equiv="refresh" content="0;url=bidconfirm.php?productid='.$productid.'&max_bid='.$max_bid.'">';
  exit();
}   
else //if($max_bid>=$max_bid_amt)
{
if(empty($err))
$warning="Please!Choose Maximum Bid Amount";
}
}
} //else if($row[user_id]!=$user_id)
} //else if($_SESSION[userid]) 
} //else 
}
}
$sql="select * from auction_details where productid=$productid";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$click="update auction_details set clicks=$row[clicks]+1 where productid=$productid";
$click_exe=mysql_query($click);
$cat="select category_name from site_categories where category_id=".$row['category_id'];
$cat_res=mysql_query($cat);
$cat_row=mysql_fetch_array($cat_res);

function itechitems_11($itechitems_11id,$cat_string)
{
$cat_sql1="select * from site_categories where category_id=".$itechitems_11id;
$cat_sql_res1=mysql_query($cat_sql1);
$cat_sql_row1=mysql_fetch_array($cat_sql_res1);
$cat_string.="<a href=subcat.php?cate_id=$cat_sql_row1[category_id]&view=list>$cat_sql_row1[category_name]</a>".",";
if($cat_sql_row1[category_head_id])
{
 itechitems_11($cat_sql_row1[category_head_id],$cat_string);
}
else
{
$cat_string=explode(",",$cat_string);
$cat_count=count($cat_string); 
$cat_str=" ";
for($ci=$cat_count-1;$ci>=0;$ci--)
{
if($cat_string[$ci])
{
$cat_str="$cat_str"."$cat_string[$ci] "." > ";
}
} 
echo "$cat_str";
}
} 
if($row['category_id'])
{
$cat_sql="select * from site_categories where category_id=".$row['category_id'];
$cat_sql_res=mysql_query($cat_sql);
$cat_sql_row=mysql_fetch_array($cat_sql_res);
}
$user_sql="select * from member_details where user_id=".$row['user_id'];
$user_res=mysql_query($user_sql);
$user=mysql_fetch_array($user_res);

$bid_sql="select * from site_bids where productid=".$row['productid'];
$bid_res=mysql_query($bid_sql);
$bid=mysql_fetch_array($bid_res);
if(empty($bid[0]))
{
$bid_sql="select * from auction_details where productid=".$row['productid'];
$bid_res=mysql_query($bid_sql);
$bid=mysql_fetch_array($bid_res);
$current_price=$bid['min_bid_amount'];
$bidincrement=$bid['bidincrement'];

$user_bid_amt = $current_price + $bidincrement;

}
else
{
 $max_sql="select * from site_bids where productid=".$row['productid']." order by bidding_amount desc";
 $max_res=mysql_query($max_sql);
 $max=mysql_fetch_array($max_res);
 
 $cur_sql="select max(bidding_amount) from site_bids where productid=".$row['productid'];
 $cur_res=mysql_query($cur_sql);
 $cur_row=mysql_fetch_array($cur_res);
 $current_price=$cur_row['0'];
 if($bidrow_max[user_id]!=""){
   $highest_bidder_sql="select * from member_details where user_id=".$bidrow_max[user_id];
   $highest_bidder_res=mysql_query($highest_bidder_sql);
   $highest_bidder_res=mysql_fetch_array($highest_bidder_res);
   $highest_bidder=$highest_bidder_res[user_name];
   if($highest_bidder==""){
$highest_bidder="None";
}}
   $high_feed_sql="select count(*) as feedbacktotal from feedback where feedback_type='Positive' and feedback_to=".$max['user_id'];
   $high_feed_recordset=mysql_query($high_feed_sql);
   $high_feed_tot=mysql_fetch_array($high_feed_recordset);
   
 $feedbackicon_sql="select * from membership_level where feedback_score_from <= "." $high_feed_tot[feedbacktotal] "." and  feedback_score_to >= "." $high_feed_tot[feedbacktotal] " ;  
 $feedbackicon_res=mysql_query($feedbackicon_sql);
 $feedbackicon_row=mysql_fetch_array($feedbackicon_res); 
 $feedback_highimg=$feedbackicon_row[icon];
 
  
 
   
   
   
 
}
 $tot_sql="select count(*) from site_bids where productid=".$row['productid'];
 $tot_res=mysql_query($tot_sql);
 $tot=mysql_fetch_array($tot_res);
 ?>
 
  <?

  function AddDays2($date,$interval) 
{ 
  if (!isset($date)) 
  $date = date("Y-m-d H:i:s"); 
  $first = explode(" ", $date); 

  $elts1 = explode("-", $first[0]);
  $elts2 = explode(":", $first[1]);
  $inter=$interval; 
  $dcour=mktime($elts2[0],$elts2[1],$elts2[2],$elts1[1],$elts1[2],$elts1[0]); 
  $dres=$dcour+$inter; 
  $date1=date("Y-m-d H:i:s",$dres);
  $ret_date="$date1";
  return $ret_date; 
}
if($selling_method!=11){
require'themes/detail.tpl';
}
if($selling_method==11){
require'themes/detail_m.tpl';
}
?>
<? 
 require 'templates/'.$tfold.'/footer.php';					
?>
</table>
</body>
</html>
