<?
  function AddDays($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; 
}

function getDateDifference2($dateFrom, $dateTo)
     {
     $difference = null;
     $dateFromElements = split(' ', $dateFrom);
     $dateToElements = split(' ', $dateTo);
     $dateFromDateElements = split('-', $dateFromElements[0]);
     $dateFromTimeElements = split(':', $dateFromElements[1]);
     $dateToDateElements = split('-', $dateToElements[0]);
     $dateToTimeElements = split(':', $dateToElements[1]); 
     $date1 = mktime($dateFromTimeElements[0], $dateFromTimeElements[1], $dateFromTimeElements[2], $dateFromDateElements[1], $dateFromDateElements[0], $dateFromDateElements[2]);
     $date2 = mktime($dateToTimeElements[0], $dateToTimeElements[1], $dateToTimeElements[2], $dateToDateElements[1], $dateToDateElements[0], $dateToDateElements[2]); 
     if( $date1 > $date2 )
     {
         return null;
     }
     $diff = $date2 - $date1;

     return $diff;
  }

function bids($productid,$user_id)
{
require 'include/databaseinfo.php';
	$sql=mysql_connect($dbhost,$dbuser,$dbpass);
 	if(!$sql) {
		echo "<h1>Unable to Establish Connection to the Server</h1><hr noshade size=2 color=#000000>";
		exit();
	}
	$db_sel=mysql_select_db($dbname,$sql);
	if(!$db_sel) {
		echo "<h1>Unable to Connect to the Database</h1><hr noshade size=2 color=#000000>";
		exit();
	}
$feature_sql="select * from auction_details where productid=$productid";
$feature_res=mysql_query($feature_sql);
$feature_row=mysql_fetch_array($feature_res);
$expire_date = $feature_row['expire_date']; 
$timetoadd = $feature_row['timetoadd'];
$expire_date = $feature_row['expire_date']; 
$status=$feature_row['status'];
$min_bid_amount=$feature_row['min_bid_amount'];
$bidincrement=$feature_row['bidincrement'];
$stopped=$feature_row['stopped'];
$bid_starting_date=$feature_row['bid_starting_date'];

 $date = "$expire_date"; 
  $first = explode(" ", $date); 

  $elts1 = explode("-", $first[0]);
  $elts2 = explode(":", $first[1]);
  $dcour=mktime($elts2[0],$elts2[1],$elts2[2],$elts1[1],$elts1[2],$elts1[0]); 
  $dres=$dcour+$timetoadd; 
  $expir_date=date("Y-m-j G:i:s",$dres);


//$expir_date = AddDays($expire_date,$timetoadd); 

$bid_sql2="select bidding_amount, user_id from site_bids where productid=".$productid." order by bidding_amount desc";
$bid_res2=mysql_query($bid_sql2);
$bid_row2=mysql_fetch_row($bid_res2);
$user_bid_amt=$bid_row2[0];
$user_bid_id=$bid_row2[1];
if($user_bid_amt!=""){
$max_bid = $user_bid_amt+$bidincrement;
}
if($user_bid_amt==""){
$max_bid = $min_bid_amount;
}


$qty =1;

$sqlsus="select user_id from member_details where user_id=$user_id and bid_num>0";
$resultsus=mysql_query($sqlsus);
$totsus=mysql_num_rows($resultsus);
$datecheck=date("Y-m-d H:i:s");
	if(($totsus>0)&&($user_bid_id!=$user_id)&&($bid_starting_date<="$datecheck")&&($expire_date>"$datecheck")&&($status=="Active")&&($stopped=='no'))
{
$bid_ins="insert into site_bids(productid,user_id,bidding_amount,bidding_date,quantity) values($productid,$user_id,$max_bid,now(),$qty)";
$ins=mysql_query($bid_ins);
$bid_ins2="update member_details set bid_num=bid_num-1 where user_id=$user_id";
$ins2=mysql_query($bid_ins2);
$f_sql="update auction_details set expire_date='$expir_date',totbid=totbid+1 where productid=$productid";
$f_res=mysql_query($f_sql);
//echo "Successfully Posted";
}
elseif($user_bid_id==$user_id) {
echo "You are the highest bidder";
}
else {
echo "Insufficient Balance";
}
}









function single_bids($productid,$user_id)
{
require 'include/databaseinfo.php';
	$sql=mysql_connect($dbhost,$dbuser,$dbpass);
 	if(!$sql) {
		echo "<h1>Unable to Establish Connection to the Server</h1><hr noshade size=2 color=#000000>";
		exit();
	}
	$db_sel=mysql_select_db($dbname,$sql);
	if(!$db_sel) {
		echo "<h1>Unable to Connect to the Database</h1><hr noshade size=2 color=#000000>";
		exit();
	}
$feature_sql="select * from auction_details where productid=$productid";
$feature_res=mysql_query($feature_sql);
$feature_row=mysql_fetch_array($feature_res);
$expire_date = $feature_row['expire_date']; 
$timetoadd = $feature_row['timetoadd'];
$expire_date = $feature_row['expire_date']; 
$status=$feature_row['status'];
$min_bid_amount=$feature_row['min_bid_amount'];
$bidincrement=$feature_row['bidincrement'];
$stopped=$feature_row['stopped'];
$bid_starting_date=$feature_row['bid_starting_date'];

 $date = "$expire_date"; 
  $first = explode(" ", $date); 

  $elts1 = explode("-", $first[0]);
  $elts2 = explode(":", $first[1]);
  $dcour=mktime($elts2[0],$elts2[1],$elts2[2],$elts1[1],$elts1[2],$elts1[0]); 
  $dres=$dcour+$timetoadd; 
  $expir_date=date("Y-m-j G:i:s",$dres);


//$expir_date = AddDays($expire_date,$timetoadd); 

$bid_sql2="select bidding_amount, user_id from site_bids where productid=".$productid." order by bidding_amount desc";
$bid_res2=mysql_query($bid_sql2);
$bid_row2=mysql_fetch_row($bid_res2);
$user_bid_amt=$bid_row2[0];
$user_bid_id=$bid_row2[1];
if($user_bid_amt!=""){
$max_bid = $user_bid_amt+$bidincrement;
}
if($user_bid_amt==""){
$max_bid = $min_bid_amount;
}


$feature_sql3="select maxbid, minbid from autobids where productid=".$productid." and user_id=".$user_id;
$feature_res3=mysql_query($feature_sql3);
$feature_row3=mysql_fetch_row($feature_res3);
$maxbid = $feature_row3[0];
$minbid = $feature_row3[1];








$qty =1;

$sqlsus="select user_id from member_details where user_id=$user_id and bid_num>0";
$resultsus=mysql_query($sqlsus);
$totsus=mysql_num_rows($resultsus);
$datecheck=date("Y-m-d H:i:s");
	if(($totsus>0)&&($max_bid<=$maxbid)&&($max_bid>=$minbid)&&($user_bid_id!=$user_id)&&($bid_starting_date<="$datecheck")&&($expire_date>"$datecheck")&&($status=="Active")&&($stopped=='no'))
{
$bid_ins="insert into site_bids(productid,user_id,bidding_amount,bidding_date,quantity) values($productid,$user_id,$max_bid,now(),$qty)";
$ins=mysql_query($bid_ins);
$bid_ins2="update member_details set bid_num=bid_num-1 where user_id=$user_id";
$ins2=mysql_query($bid_ins2);
$f_sql="update auction_details set expire_date='$expir_date',totbid=totbid+1 where productid=$productid";
$f_res=mysql_query($f_sql);
//echo "Successfully Posted";
}
elseif($user_bid_id==$user_id) {
echo "You are the highest bidder";
}
else {
echo "Insufficient Balance";
}
}













function autobids($productid)
{
require 'include/databaseinfo.php';
	$sql=mysql_connect($dbhost,$dbuser,$dbpass);
 	if(!$sql) {
		echo "<h1>Unable to Establish Connection to the Server</h1><hr noshade size=2 color=#000000>";
		exit();
	}
	$db_sel=mysql_select_db($dbname,$sql);
	if(!$db_sel) {
		echo "<h1>Unable to Connect to the Database</h1><hr noshade size=2 color=#000000>";
		exit();
	}

$feature_m="select MAX(bidnum) from autobids where productid=".$productid." and bidnum>0";
$feature_mr=mysql_query($feature_m);

$feature_mrow=mysql_fetch_array($feature_mr);
$tot_count = $feature_mrow['MAX(bidnum)'];

if($tot_count>0){

for($coun=1;$coun<=$tot_count;$coun=$coun+1)
{
$feature_sql="select * from auction_details b, autobids a where a.productid=b.productid and a.productid=".$productid." and a.bidnum>0 and b.status='Active' order by maxbid";
$feature_res=mysql_query($feature_sql);
while($feature_row=mysql_fetch_array($feature_res)){
$user_id = $feature_row['user_id'];
$maxbid = $feature_row['maxbid'];
$minbid = $feature_row['minbid'];
$bidnum=$feature_row['bidnum'];
$auto = $feature_row['auto'];
$expire_date = $feature_row['expire_date']; 
$stopped=$feature_row['stopped'];
$timetoadd = $feature_row['timetoadd']; 

$min_bid_amount=$feature_row['min_bid_amount'];
$bidincrement=$feature_row['bidincrement'];
$status=$feature_row['status'];

$nowdate = date("Y-m-d H:i:s"); 

$date2=strtotime($expire_date);

$date1 = date("d-m-Y H:i:s");
$date2 = date("d-m-Y H:i:s",$date2); 
$dateFrom=$date1;
$dateTo = $date2;

$diffd = getDateDifference2($dateFrom, $dateTo);

if($timetoadd >= $diffd)
{
$new_expir_date = AddDays($nowdate,$timetoadd); 
}
if($timetoadd < $diffd)
{
$new_expir_date = "$expire_date"; 
}
//$new_expir_date = AddDays($expire_date,$timetoadd); 


$bid_sql2="select bidding_amount, user_id from site_bids where productid=".$productid." order by bidding_amount desc";
$bid_res2=mysql_query($bid_sql2);
$bid_row2=mysql_fetch_array($bid_res2);
$user_bid_amt=$bid_row2[0];
$user_bid_id=$bid_row2[1];
if($user_bid_amt!=""){
$max_bid = $user_bid_amt+$bidincrement;
}
if($user_bid_amt==""){
$max_bid = $min_bid_amount;
}
$qty=1;

$sqlus="select bid_num as bn from member_details where user_id=$user_id";
$resultus=mysql_query($sqlus);
$bid_us=mysql_fetch_array($resultus);
$left_bid = $bid_us['bn'];

//
$datecheck=date("Y-m-d H:i:s");
if(($left_bid>0)&&($max_bid<=$maxbid)&&($max_bid>=$minbid)&&($bidnum>0)&&($user_bid_id!=$user_id)&&($expire_date>"$datecheck")&&($stopped=='no'))
{
$bid_ins="insert into site_bids(productid,user_id,bidding_amount,bidding_date,quantity,bid_type) values($productid,$user_id,$max_bid,now(),$qty,'Auto Bidder')";
$ins=mysql_query($bid_ins);
$bid_ins2="update member_details set bid_num=bid_num-1 where user_id=$user_id";
$ins2=mysql_query($bid_ins2);
$bid_ins3="update autobids set bidnum=bidnum-1 where auto=$auto";
$ins3=mysql_query($bid_ins3);
$f_sql="update auction_details set expire_date='$new_expir_date',totbid=totbid+1 where productid=$productid";
$f_res=mysql_query($f_sql);
}//if()
}//while()
}//for()
}//if($tot_count>0)
}//function()




function Mysterybids($productid,$user_id)
{
require 'include/configurations.php';
$feature_sql="select * from auction_details where productid=$productid";
$feature_res=mysql_query($feature_sql);
$feature_row=mysql_fetch_array($feature_res);
$expire_date = $feature_row['expire_date']; 
$status=$feature_row['status'];
$min_bid_amount=$feature_row['min_bid_amount'];
$bidincrement=$feature_row['bidincrement'];
$stopped=$feature_row['stopped'];
$locked=$feature_row['locked'];


$bid_sql2="select bidding_amount, user_id from site_bids where productid=".$productid." order by bidding_amount asc";
$bid_res2=mysql_query($bid_sql2);
$bid_row2=mysql_fetch_row($bid_res2);
$user_bid_amt=$bid_row2[0];
$user_bid_id=$bid_row2[1];
if($user_bid_amt!=""){
$max_bid = $user_bid_amt-$bidincrement;
}
if($user_bid_amt==""){
$max_bid = $min_bid_amount;
}


$qty =1;

$sqlsus="select user_id from member_details where user_id=$user_id and bid_num>0";
$resultsus=mysql_query($sqlsus);
$totsus=mysql_num_rows($resultsus);
$datecheck=date("Y-m-d H:i:s");

	if(($totsus>0)&&($expire_date>"$datecheck")&&($status=="Active")&&($locked=="no")&&($stopped=='no'))
{
$bid_ins="insert into site_bids(productid,user_id,bidding_amount,bidding_date,quantity) values($productid,$user_id,$max_bid,now(),$qty)";
$ins=mysql_query($bid_ins);
$bid_ins2="update member_details set bid_num=bid_num-1 where user_id=$user_id";
$ins2=mysql_query($bid_ins2);
$f_sql="update auction_details set totbid=totbid+1, last_bidder=$user_id, locked='yes' where productid=$productid";
$f_res=mysql_query($f_sql);
echo "y";
}
elseif($locked=="yes") {
echo "This auction is locked";
}
else {
echo "Insufficient Balance";
}
}









?>