<?
//error_reporting(E_ALL^E_WARNING^E_NOTICE);
  require("sendmail.php"); 
 // autoSendWinnerMail();//自动发送中标邮件
 if($_SESSION["language_name"]!="cn")
{
	$_SESSION["language_name"]="cn";
}
if($_SESSION["language_name"]=="cn")
{
  include("language/China.php");
}
 check();//检查已结束的竞拍
  function autoSendWinnerMail()
  {  	
		$jobsql = "select * from won_auctions where mail_flag=0";
		$resjob = mysql_query($jobsql);				
				
		$totalqry1 = mysql_num_rows($resjob);
				
		if($totalqry1>0)
		{
			for($i=1;$i<=$totalqry1;$i++)
			{
				$v1 = mysql_fetch_array($resjob);
				$aucId = $v1["auction_id"];
				SendWinnerMail($aucId);
				mysql_query("update won_auctions set mail_flag=1 where auction_id=".$aucId);	
			}
    }
		
  }

  function check(){
            $now=time();
			$qrysel = "select * from auc_due_table adt left join auction a on a.auctionID=adt.auction_id where adt.auc_due_time < $now and a.is_close=0 order by auc_due_time desc";
			$ressel = mysql_query($qrysel);
			$total = mysql_num_rows($ressel);
			for($i=1;$i<=$total;$i++)
			{
				$obj = mysql_fetch_object($ressel);
               	$qr = "select * from bid_account ba left join registration r on ba.user_id=r.id where auction_id='".$obj->auction_id."' and bid_flag='d' order by ba.id desc limit 0,1";
				$res2 = mysql_query($qr);
				$total2 = mysql_num_rows($res2);
				$obj2 = mysql_fetch_object($res2);
				$username = $obj2->username;
              if($total2>0){
				//$now=time();
				$qryupa = "update auction  set is_close=1,auc_final_price=".$obj->auc_due_price.",auc_final_end_date=".$obj->auc_due_time.",auc_status=3 ,buy_user=".$obj2->user_id." where auctionID=".$obj->auctionID;
		             $result2 = mysql_query($qryupa);
					 //echo $qryupa;
					 
				$sql = "INSERT INTO won_auctions (auction_id,userid,won_date) VALUES (".$obj->auctionID.",".$obj2->user_id.",".$obj->auc_due_time.")";

		             $q = mysql_query($sql) or die ("insert error".mysql_error());
			}
			else{
		      	$qryupa = "update auction  set is_close=1,auc_final_price=".$obj->auc_due_price.",auc_final_end_date=".$obj->auc_due_time.",auc_status=3 where auctionID=".$obj->auctionID;
		             $result2 = mysql_query($qryupa);
			 
			}
			}
  
  }
  
//functions added for transcommit in sql
  	function begin() 
	{
		mysql_query("BEGIN");
	}
	function commit()
	{
		mysql_query("COMMIT");
	}
	function rollback()
	{
		mysql_query("ROLLBACK");
	}
 // ended functions
 
  function SendHTMLMail2($to,$subject,$mailcontent,$from)
  { 
  	SendHTMLMail($to,$subject,$mailcontent,$from);
  	/*
    $array = split("@",$from,2);
    $SERVER_NAME = $array[1];
    $username =$array[0];
    $headers = "From: $username@$SERVER_NAME\nReply-To:$username@$SERVER_NAME\nX-Mailer: PHP\n";

    $limite = "_parties_".md5 (uniqid (rand()));

    $headers .= "Date: ".date("l j F Y, G:i")."\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: text/html;\n";
    $headers .= " boundary=\"----=$limite\"\n\n";
	
    mail($to,$subject,$mailcontent,$headers);
    */
  }
	
	
	function setdate($month,$date,$year)
	{
		$bdate1 = $date."-".$month."-".$year;
		return $bdate1;
	}
	
	function arrangedate($date)
	{
		$year = substr($date,0,4);
		$month = substr($date,5,2);
		$day = substr($date,8,2);
		$date = $day."/".$month."/".$year;
		return $date;
	}
	
	function choose_short_desc($shortdesc,$length)
	{
			$long_desc = $shortdesc;
			$totallen = strlen($long_desc);
			for($i=$length;$i<$totallen;$i++){
				if(substr($long_desc,$i,1)==" "){
					$length = $i;
					break;
				}
			}
			if(strlen($long_desc)>$length){
				$short_desc = nl2br(substr($long_desc,0,$length));
				$short_desc .= "...";
			}else{
				$short_desc = nl2br($long_desc)."...";
			}	
			return $short_desc;
	}
	
			function getBidHistory($aid,$uid)
			{
				global $lng_bidbuddy;
				global $lng_backbooking;
				global $lng_biddingsinglebid;
				global $lng_biddingsmsbid;
				$qry = "select * from bid_account where user_id=$uid and auction_id=$aid   and bid_flag='d' and bidding_type='s' order by id desc";
				$res = mysql_query($qry);
				$totalqry = mysql_num_rows($res);

				$qry1 = "select * from bidbutler where user_id='$uid' and auc_id='$aid'";
				$res1 = mysql_query($qry1);
				$totalqry1 = mysql_num_rows($res1);
				
				if($totalqry1>0)
				{
					for($i=1;$i<=$totalqry1;$i++)
					{
						$v1 = mysql_fetch_array($res1);
			?>
					<div class="normal_text">
						<div class="normal_text" style="font-size: 12px; float: left; width: 65px; margin-left: 5px;" align="center"><?=arrangedate(substr($v1["place_date"],0,10));?></div>
						<div class="normal_text"  style="font-size: 12px; float:left; width: 65px;" align="center"><?=substr($v1["place_date"],11);?></div>
						<div class="normal_text"  style="font-size: 12px; float:left; text-align: center; width: 120px;"><?=$lng_bidbuddy;?></div>
						<div align="center" class="red-text-12-b" style="font-size: 12px; float: right; width: 20px; padding-right: 5px;"><? echo "-".$v1["butler_bid"]; ?></div>
					</div>
			<?		
					}
				}
				
				$qry2 = "select * from bid_account where user_id='$uid' and auction_id='$aid' and bid_flag='b'";
				$res2 = mysql_query($qry2);
				$totalqry2 = mysql_num_rows($res2);
				if($totalqry2>0)
				{
					for($i=1;$i<=$totalqry2;$i++)
					{
						$v2 = mysql_fetch_array($res2);
			?>
					<div class="normal_text">
						<div class="normal_text" style="font-size: 12px; float: left; width: 65px; margin-left: 5px;" align="center"><?=arrangedate(substr($v2["bidpack_buy_date"],0,10));?></div>
						<div class="normal_text"  style="font-size: 12px; float:left; width: 65px;" align="center"><?=substr($v2["bidpack_buy_date"],11);?></div>
						<div class="normal_text"  style="font-size: 12px; float:left; text-align: center; width: 120px;"><?=$lng_backbooking;?></div>
						<div align="center" class="greenfont" style="font-size: 12px; float: right; width: 20px; padding-right: 5px;"><b><? echo $v2["bid_count"]; ?></b></div>
					</div>
			<?		
					}
					
				}
				
				while($v = mysql_fetch_array($res))
				{
			?>
					<div class="normal_text">
						<div class="normal_text" style="font-size: 12px; float: left; width: 65px; margin-left: 5px;" align="center"><?=substr(arrangedate($v["bidpack_buy_date"]),0,10);?></div>
						<div class="normal_text"  style="font-size: 12px; float:left; width: 65px;" align="center"><?=substr($v["bidpack_buy_date"],11);?></div>
						<div class="normal_text"  style="font-size: 12px; float:left; text-align: center; width: 120px;"><? if($v["bidding_type"]=='s'){ echo $lng_biddingsinglebid; } elseif($v["bidding_type"]=='b'){ echo $lng_bidbuddy; } elseif($v["bidding_type"]=='m'){ echo $lng_biddingsmsbid; } ?></div>
						<div align="center" class="red-text-12-b" <? if($v["bidding_type"]!='m') { ?>style="font-size: 12px; float: right; width: 20px; padding-right: 5px;"<? } else { ?>style="font-size: 12px; float: right;  width: 20px; padding-right: 5px;"<? } ?>><? echo "-".$v["bid_count"]; ?></div>
					</div>
					<div style="clear: both;"></div>
			<?		
				}
			}

	function countTime($newtime)
	{
		$h = substr($newtime,0,2);
		$m = substr($newtime,3,2);
		$se = substr($newtime,6,2);
	
		if($h!="00")	
			{ $hou = ($h * 60) * 60; }
		if($m!="00")	
			{ $mi = $m * 60; }

		$time = $hou + $mi + $se;
		return $time;
	}
function getshipping($id)
{
	$sqlgetshipping = "select shippingcharge from shipping where id='$id'";
	$resgetshipping = mysql_query($sqlgetshipping) or die(mysql_error());
	if(0<mysql_num_rows($resgetshipping))
	{
		$rowgetshipping = mysql_fetch_array($resgetshipping);
		$shippingchr = $rowgetshipping["shippingcharge"];
		return $shippingchr;
	}
	else
	{
		return 0.00;
	}
}	

	function UpdateAuction($auctionid)
	{
		$qrysel = "select * from bid_account where auction_id='$auctionid' and bid_flag='d' order by id desc limit 0,1";
		$ressel = mysql_query($qrysel);
		$objsel = mysql_fetch_object($ressel);
		$user = $objsel->user_id;
		$fprice = $objsel->bidding_price;
		
		if($fprice=="")
		{
			$qr = "select * from auction where auctionID='$auctionid'";
			$r = mysql_query($qr);
			$ob = mysql_fetch_object($r);
			$fixprice = $ob->auc_start_price;

			$pricenew = $fixprice;

			$fprice=$pricenew;
		}

		begin();
/*		if($user=="")
		{
			$user = 3;
			$qryins1 = "Insert into bid_account (user_id,bidpack_buy_date,bid_count,auction_id,product_id,bid_flag,bidding_price,bidding_type) values('3',NOW(),'1','$auctionid','".$ob->productID."','d','$pricenew','s')";
			$result1 = mysql_query($qryins1);
			if(!$result1)
			{
				rollback();
				echo "Failed";
			}
			
			$sel = "select final_bids from registration where id='3'";
			$rssreg = mysql_query($sel);
			$objselreg = mysql_fetch_object($rssreg);
			
			$qryregupd = "update registration set final_bids='".($objselreg->final_bids-1)."' where id='3'";
			$result2 = mysql_query($qryregupd);
			if(!$result2)
			{
				rollback();
				echo "Failed";
			}
		}
*/
		$qryupd = "update auction set auc_status=3, buy_user='$user', auc_final_price='$fprice',auc_final_end_date=NOW() where auctionID='$auctionid'";
		$result3 = mysql_query($qryupd);
		if(!$result3)
		{
			rollback();
			echo "Failed";
		}
		
		$qryins = "Insert into won_auctions (auction_id,userid,won_date) values('$auctionid','$user',NOW())";
		$result4 = mysql_query($qryins) or die(mysql_error());
		if(!$result4)
		{
			rollback();
			echo "Failed";
		}
		commit();
		SendWinnerMail($auctionid);
		UpdateHighButlers($auctionid,$fprice);
	}

	function FutureAuctionManage()
	{
		$qrysel = "select * from auction where future_tstamp<='".time()."' and auc_status='1'";
		$ressel = mysql_query($qrysel);
		$total = mysql_num_rows($ressel);
		
		if($total>0)
		{
			begin();
			while($obj = mysql_fetch_object($ressel))
			{
				$aid = $obj->auctionID;
				$totaltime = $obj->total_time;
				$price = $obj->auc_start_price;
				$qryupd = "update auction set auc_status=2 where auctionID=$aid";
				$result1 = mysql_query($qryupd);
				if(!$result1)
				{
					rollback();
					echo "Failed";
				}
				
				$qryins = "insert into auc_due_table (auction_id,auc_due_time,auc_due_price) values('$aid','$totaltime','$price')";
				$result2 = mysql_query($qryins);
				if(!$result2)
				{
					rollback();
					echo "Failed";
				}
			}	
			commit();
		}
	}
	
	function PauseDayAuction()
	{
		$qryauc = "select * from auction where auc_status='2' and nightauction='0'";
		$resauc = mysql_query($qryauc);
		$totalauc = mysql_num_rows($resauc);
		while($obj = mysql_fetch_object($resauc))
		{
			$aucid = $obj->auctionID;
			$qryupd = "update auction set pause_status='1' where auctionID='$aucid'";
			mysql_query($qryupd) or die(mysql_error());
		}
	}
		function getPaypalInfo($stat)
	{
		$qrysel = "select * from paypal_info where id='1'";
		$ressel = mysql_query($qrysel);
		$obj = mysql_fetch_object($ressel);
		$businessid = $obj->business_id;
		$token = $obj->token;
		if($stat==1)
		{
			return $businessid;
		}
		if($stat==2)
		{
			return $token;
		}
	}

	function RunDayAuction()
	{
		$qryauc = "select * from auction where auc_status=2 and pause_status=1";
		$resauc = mysql_query($qryauc);
		$totalauc = mysql_num_rows($resauc);
		while($obj = mysql_fetch_object($resauc))
		{
			$aucid = $obj->auctionID;
			$qryupd = "update auction set pause_status=0 where auctionID=$aucid";
			mysql_query($qryupd) or die(mysql_error());
		}
	}

	
	
	
	function enddatefunction($date)
	{
		$year = substr($date,0,4);
		$month = substr($date,5,2);
		$day = substr($date,8,2) + 5;
			
			if($day>31)
			{
				if($month==1 || $month==3 || $month==5 || $month==7 || $month==8 || $month==10 || $month==12)
				{
					if($month==12)
					{
						$month = 01;
						$year = $year + 1;
						$day1 = 31;						
						$diff = $day - $day1;						
					}
					else
					{
					$month = $month + 01;
					$day1 = 31;
					$diff = $day - $day1;
					}
				}
				else
				{
					$month = $month + 01;
					$day1 = 30;
					$diff = $day - $day1;
				}
				$date = $diff."/".$month."/".$year;
			}
			else
			{
				$date = $day."/".$month."/".$year;
			}
		return $date;
	}
	
	function getTotalPlaceBids($aid)
	{
		$qrysel = "select *,sum(bid_count) as totalbid from bid_account where auction_id='$aid' and bid_flag='d' and bidding_type='s' and user_id='".$_SESSION["userid"]."' group by auction_id";
		$ressel = mysql_query($qrysel);
		$obj = mysql_fetch_object($ressel);

		$qrysel1 = "select *,sum(butler_bid) as butlerbid from bidbutler where auc_id='$aid' and user_id='".$_SESSION["userid"]."' group by auc_id";
		$ressel1 = mysql_query($qrysel1);
		$obj1 = mysql_fetch_object($ressel1);

		$qrysel2 = "select *,sum(bid_count) as savebid from bid_account where auction_id='$aid' and bid_flag='b' and user_id='".$_SESSION["userid"]."' group by auction_id";
		$ressel2 = mysql_query($qrysel2);
		$obj2 = mysql_fetch_object($ressel2);

		$totalbid = $obj->totalbid;
		$butlerbid  = $obj1->butlerbid;
		$savebid = $obj2->savebid;

		$placebid = ($butlerbid  + $totalbid) - $savebid;

		return $placebid;
	}
	function GetUserFinalBids($userfid)
	{
		$qrybal = "select final_bids from registration where id='".$_SESSION["userid"]."'";
		$resbal = mysql_query($qrybal);
		$objbal = mysql_fetch_object($resbal);
		$final = $objbal->final_bids;
		return $final;
	}	

	function AcceptDateFunction($date)
	{
		$year = substr($date,0,4);
		$month = substr($date,5,2);
		$day = substr($date,8,2) + 7;
			
			if($day>31)
			{
				if($month==1 || $month==3 || $month==5 || $month==7 || $month==8 || $month==10 || $month==12)
				{
					if($month==12)
					{
						$month = 01;
						$year = $year + 1;
						$day1 = 31;						
						$diff = $day - $day1;						
					}
					else
					{
					$month = $month + 01;
					$day1 = 31;
					$diff = $day - $day1;
					}
				}
				else
				{
					$month = $month + 01;
					$day1 = 30;
					$diff = $day - $day1;
				}
				$date = str_pad($diff,2,0,STR_PAD_LEFT)."-".str_pad($month,2,0,STR_PAD_LEFT)."-".$year;
			}
			else
			{
				$date = str_pad($day,2,0,STR_PAD_LEFT)."-".str_pad($month,2,0,STR_PAD_LEFT)."-".$year;
			}
		return $date;
	}
function AcceptDateFunctionStatus($date)
	{
		$date=date("Y-m-d H:i:s",$date);
		$year = substr($date,0,4);
		$month = substr($date,5,2);
		$day = substr($date,8,2) + 7;	
		$newdate = explode(" ",$date);
		$exdate = explode("-",$newdate[0]);
		$newyear = $exdate[0];
		$newmonth = $exdate[1];
		$newday = $exdate[2];
		$newtime = explode(":",$newdate[1]);
		$newhour = $newtime[0];
		$newmin = $newtime[1];
		$newsec = $newtime[2];
		$returndate1 = date("Y-m-d H:i:s",mktime($newhour,$newmin,$newsec,$newmonth,$newday+7,$newyear));
		
		$newdate1 = explode(" ",$returndate1);
		$exdate1 = explode("-",$newdate1[0]);
		$newyear1 = $exdate1[0];
		$newmonth1 = $exdate1[1];
		$newday1 = $exdate1[2];
		$newtime1 = explode(":",$newdate1[1]);
		$newhour1 = $newtime1[0];
		$newmin1 = $newtime1[1];
		$newsec1 = $newtime1[2];
		
		
		$returndate = array("Hour"=>$newhour1,"Min"=>$newmin1,"Sec"=>$newsec1,"Month"=>$newmonth1,"Day"=>$newday1,"Year"=>$newyear1);
		return $returndate;
	}	
	
	function getBidHistorySMS($aid,$uid)
	{
		global $lng_biddingsmsbid;
		$qrysms = "select * from bid_account where user_id='$uid' and auction_id='$aid'   and bid_flag='d' and bidding_type='m' order by id desc";
		$ressms = mysql_query($qrysms);
		$totalqrysms = mysql_num_rows($ressms);

		if($totalqrysms>0)
		{
				while($vsms = mysql_fetch_array($ressms))
				{
	?>
			<div class="normal_text">
				<div class="normal_text" style="font-size: 12px; float: left; width: 65px; margin-left: 5px;" align="center"><?=substr(arrangedate($vsms["bidpack_buy_date"]),0,10);?></div>
				<div class="normal_text"  style="font-size: 12px; float:left; width: 65px;" align="center"><?=substr($vsms["bidpack_buy_date"],11);?></div>
				<div class="normal_text"  style="font-size: 12px; float:left; text-align: center; width: 120px;"><? if($vsms["bidding_type"]=='m'){ echo $lng_biddingsmsbid; } ?></div>
				<div align="center" class="red-text-12-b" style="font-size: 12px; float: right; width: 20px; padding-right: 5px;"><b><? echo $vsms["bid_count"]; ?></b></div>
			</div>
	<?		
			}
		}
	}
	
function SendWinnerMail($auctionid)
	{
		global $SITE_URL,$adminemailadd;
		global $lng_mailwonaucinfo, $lng_mailhi,$lng_mailcongratulation,$lng_mailyouhavewon,$lng_mailauctionid,$lng_mailname,$lng_mailprice,$lng_mailclosingdate,$lng_mailclosingnote,$lng_mailaccdenclick1,$lng_mailaccdenclick2,$lng_mailsubjectauctionwon;

		$qrysel = "select * from won_auctions w left join auction a on w.auction_id=a.auctionID left join registration r on w.userid=r.id left join products p on a.productID=p.productID where w.auction_id='".$auctionid."'";
		$ressel = mysql_query($qrysel);
		$objsel = mysql_fetch_object($ressel);
		if($objsel->fixedpriceauction==1) { $winprice = $objsel->auc_fixed_price; }
		elseif($objsel->offauction==1) { $winprice = "0.01"; } 
		else { $winprice = $objsel->auc_final_price; }

		$content1='';

		$content1.= "<font style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>"."</font><br>"."<br>"."<p align='center' style='font-size: 14px;font-family: Arial, Helvetica, sans-serif;font-weight:bold;'>".$lng_mailwonaucinfo."</p>"."<br>".	
	
	"<table border='0' cellpadding='3' cellspacing='0' width='100%' align='center' class='style13'>";

		$content1.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>".$lng_mailhi.$objsel->username.", </td>
		</tr>";

		$content1.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>".$lng_mailcongratulation.":</td>
		</tr>";

		$content1.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>".$lng_mailyouhavewon."</td>
		</tr>";

		$content1.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>".$lng_mailauctionid.": ".$auctionid."</td>
		</tr>";

		$content1.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>".$lng_mailname.": ".$objsel->name."</td>
		</tr>";

		$content1.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>".$lng_mailprice.": ".number_format($winprice,2)."</td>
		</tr>";

		$content1.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>".$lng_mailclosingdate.": ".arrangedate(substr($objsel->won_date,0,10))."</td>
		</tr>";

		$content1.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>".$lng_mailclosingnote."</td>
		</tr>";

		$content1 .= "<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>".$lng_mailaccdenclick1."<a href='".$SITE_URL."wonauctionaccept.php?winid=".base64_encode($auctionid)."'>".$lng_mailaccdenclick2."</a></td>
		</tr>
		</table>";

		$subject=$lng_mailsubjectauctionwon;
		$from=$adminemailadd;
		$email = $objsel->email;

//		echo $content1."<br>";
//		exit;

//		exit;
		//$ADMIN_EMAIL = "scah@comcast.net";
		//$ADMIN_EMAIL = "testmailfunction@gmail.com";
		SendHTMLMail2($email,$subject,$content1,$from);				
	}	

		function calc_counter_from_time_php($diff)
		{
		  if ($diff > 0) {
			$hours=floor($diff / 3600);
		
			$minutes=floor(($diff / 3600 - $hours) * 60);
		
			$seconds=floor(((($diff / 3600 - $hours) * 60) - $minutes) * 60);
		  } else {
			$hours = 0;
			$minutes = 0;
			$seconds = 0;
		  }
		
		  if ($seconds == 60) {
			$seconds = 0;
		  }
		
		  if ($minutes < 10) {
			if ($minutes < 0) {
			  $minutes = 0;
			}
			$minutes = '0'.$minutes;
		  }
		  if ($seconds < 10) {
			if ($seconds < 0) {
			  $seconds = 0;
			}
			$seconds = '0'.$seconds;
		  }
		  if ($hours < 10) {
			if ($hours < 0) {
			  $hours = 0;
			}
			$hours = '0'.$hours;
		  }
		  return $hours.":".$minutes.":".$seconds;
		}
		
	function AcceptDateFunctionStatus_Voucher($date,$validity)
	{
		$year = substr($date,0,4);
		$month = substr($date,5,2);
		$day = substr($date,8,2) + $validity;	
		$newdate = explode(" ",$date);
		$exdate = explode("-",$newdate[0]);
		$newyear = $exdate[0];
		$newmonth = $exdate[1];
		$newday = $exdate[2];
		$newtime = explode(":",$newdate[1]);
		$newhour = $newtime[0];
		$newmin = $newtime[1];
		$newsec = $newtime[2];
		$returndate1 = date("Y-m-d H:i:s",mktime($newhour,$newmin,$newsec,$newmonth,$newday+$validity,$newyear));
		
		$newdate1 = explode(" ",$returndate1);
		$exdate1 = explode("-",$newdate1[0]);
		$newyear1 = $exdate1[0];
		$newmonth1 = $exdate1[1];
		$newday1 = $exdate1[2];
		$newtime1 = explode(":",$newdate1[1]);
		$newhour1 = $newtime1[0];
		$newmin1 = $newtime1[1];
		$newsec1 = $newtime1[2];
		
		$returndate = array("Hour"=>$newhour1,"Min"=>$newmin1,"Sec"=>$newsec1,"Month"=>$newmonth1,"Day"=>$newday1,"Year"=>$newyear1);

		return $returndate1;
	}	

	function getUserName($uid)
	{
		$qryusername = "select * from registration where id='".$_SESSION["userid"]."'";
		$resusername = mysql_query($qryusername);
		$objusername = mysql_fetch_object($resusername);
		$uname = $objusername->username;
		return $uname;
	}
	
	function SendMailToAdmin()
	{
		$email = "admin@snabid.com";
		$from = "admin@snabid.com";
		$subject = "Recharge your sms credit account";
		
		$content1='';

		$content1.= "<font style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>Dear Member,"."</font><br>"."<br>"."<p align='center' style='font-size: 14px;font-family: Arial, Helvetica, sans-serif;font-weight:bold;'></p>"."<br>".	
	
	"<table border='0' cellpadding='3' cellspacing='0' width='100%' align='center' class='style13'>";

		$content1.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>Dear Admin, </td>
		</tr>";

		$content1.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>There is no credit available for send SMS.</td>
		</tr>";
		
		$content1.="<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>
		<td>Please recharge your sms credit account.</td>
		</tr>
		</table>";

//		echo $content1."<br>";
//		exit;

//		exit;
		//$ADMIN_EMAIL = "scah@comcast.net";
		//$ADMIN_EMAIL = "testmailfunction@gmail.com";
		SendHTMLMail2($email,$subject,$content1,$from);		
	}

	function PlaceSystemBid($aid)
	{
		$qrysel1 = "select * from auc_due_table adt left join auction a on a.auctionID=adt.auction_id left join auction_management am on a.time_duration=am.auc_manage where adt.auction_id='".$aid."'";
		$resse11 = mysql_query($qrysel1);
		$objsel1 = mysql_fetch_object($resse11);
		
//		$qryhis = "select * from bid_account ba left join registration r on ba.user_id=r.id where auction_id='".$aid."' and r.admin_user_flag='1' and bid_flag='d' order by ba.id desc limit 0,1";
		$qryhis = "select ba.user_id as user_id from bid_account ba left join registration r on ba.user_id=r.id where auction_id='".$aid."' and r.admin_user_flag='1' and bid_flag='d' order by ba.id desc limit 0,1";
		$reshis = mysql_query($qryhis);
		$objhis = mysql_fetch_object($reshis); 
 
//		$qryreg1 = "select * from registration where admin_user_flag='1' and id not in('".$objhis->user_id."') and user_delete_flag!='d' order by rand() limit 0,1";
		$qryreg1 = "select id from registration where admin_user_flag='1' and id not in('".$objhis->user_id."') and user_delete_flag!='d' order by rand() limit 0,1";
		$resreg1 = mysql_query($qryreg1);
		$totaladminuser = mysql_num_rows($resreg1);
		$objreg1 = mysql_fetch_object($resreg1); 
		
		if($objsel1->auc_due_price>$objsel1->min_win_price && $objsel1->min_win_price!='0.00')
		{
//			$qryhis2 = "select * from bid_account ba left join registration r on ba.user_id=r.id where auction_id='".$aid."' and bid_flag='d'  order by ba.id desc limit 0,1";
			$qryhis2 = "select r.admin_user_flag as admin_user_flag from bid_account ba left join registration r on ba.user_id=r.id where auction_id='".$aid."' and bid_flag='d'  order by ba.id desc limit 0,1";
			$reshis2 = mysql_query($qryhis2);
			$objhis2 = mysql_fetch_object($reshis2); 
			
			if($objhis2->admin_user_flag!='1')
			{
				if($totaladminuser>0)
				{
						if($objsel1->pennyauction==1) { $pluspriceforbid = "0.01"; }
						else { $pluspriceforbid = $objsel1->auc_plus_price; }
						$biddingprice = $objsel1->auc_due_price + $pluspriceforbid;
						$biddingtime = $objsel1->auc_due_time + $objsel1->auc_plus_time;
				
						begin();
				
						$qupd = "update auc_due_table set auc_due_time='".$biddingtime."',auc_due_price='".$biddingprice."' where auction_id=$aid";
						$result1 = mysql_query($qupd);
						if(!$result1)
						{
							rollback();
							echo "Failed1";
						}	
						
						$qryins1 = "Insert into bid_account (user_id,bidpack_buy_date,bid_count,bid_flag,recharge_type,credit_description) values('".$objreg1->id."',NOW(),'1','c','ad','Added by system')";
						$result2 = mysql_query($qryins1);
						if(!$result2)
						{
							rollback();
							echo "Failed2";
						}
				
				
						$qryins2 = "Insert into bid_account (user_id,bidpack_buy_date,bid_count,auction_id,product_id,bid_flag,bidding_price,bidding_type,bidding_time) values('".$objreg1->id."',NOW(),'1','".$objsel1->auctionID."','".$objsel1->productID."','d','".$biddingprice."','s','".$biddingtime."')";
						$result3 = mysql_query($qryins2);
						if(!$result3)
						{
							rollback();
							echo "Failed3";
						}
						commit();
				}
			}
		}
		else
		{
			if($totaladminuser>0)
			{
					if($objsel1->pennyauction==1) { $pluspriceforbid = "0.01"; }
					else { $pluspriceforbid = $objsel1->auc_plus_price; }
					$biddingprice = $objsel1->auc_due_price + $pluspriceforbid;
					$biddingtime = $objsel1->auc_due_time + $objsel1->auc_plus_time;
			
					begin();
			
					$qupd = "update auc_due_table set auc_due_time='".$biddingtime."',auc_due_price='".$biddingprice."' where auction_id=$aid";
					$result1 = mysql_query($qupd);
					if(!$result1)
					{
						rollback();
						echo "Failed1";
					}	
					
					$qryins1 = "Insert into bid_account (user_id,bidpack_buy_date,bid_count,bid_flag,recharge_type,credit_description) values('".$objreg1->id."',NOW(),'1','c','ad','Added by system')";
					$result2 = mysql_query($qryins1);
					if(!$result2)
					{
						rollback();
						echo "Failed2";
					}
			
					$qryins2 = "Insert into bid_account (user_id,bidpack_buy_date,bid_count,auction_id,product_id,bid_flag,bidding_price,bidding_type,bidding_time) values('".$objreg1->id."',NOW(),'1','".$objsel1->auctionID."','".$objsel1->productID."','d','".$biddingprice."','s','".$biddingtime."')";
					$result3 = mysql_query($qryins2);
					if(!$result3)
					{
						rollback();
						echo "Failed3";
					}
					commit();
			}
		}
	}	

	function CheckTimeGreater($starthour,$startmin,$startsec,$endhour,$endmin,$endsec)
	{
		if($starthour>$endhour || $starthour<$endhour)
		{
			if($starthour>$endhour)
			{
				$returnfalg = 0;
			}
			elseif($starthour<$endhour)
			{
				$returnfalg = 1;
			}
		}
		else
		{
			if($startmin>$endmin || $startmin<$endmin)
			{
				if($startmin>$endmin)
				{
					$returnfalg = 0;
				}
				elseif($startmin<$endmin)
				{
					$returnfalg = 1;
				}
			}
			else
			{
				if($startsec>$endsec)
				{
					$returnfalg = 0;
				}
				elseif($startsec<$endsec)
				{
					$returnfalg = 1;
				}
			}
		}
		
		if($returnfalg==0)
		{
			return 1;
		}
		elseif($returnfalg==1)
		{
			return 2;
		}
	}

	function GetAuctionMinPrice($aucid,$minbidprice,$fixedpriceauction,$offauction,$auctionfixedprice,$auctiondueprice)
	{
/*		$qrysel = "select * from auction a left join auc_due_table adt on a.auctionID=adt.auction_id where auctionID='".$aucid."'"; // Delete this 
		$ressel = mysql_query($qrysel);
		$objsel = mysql_fetch_object($ressel);*/
		
		$qrysel1 = "select sum(bid_count) as totalubids from bid_account ba left join registration r on ba.user_id=r.id where ba.auction_id='".$aucid."' and r.admin_user_flag!='1' and bid_flag='d' group by ba.auction_id";
		// change is possible select only sum of bids not all fields
		$ressel1 = mysql_query($qrysel1);
		$objsel1 = mysql_fetch_object($ressel1);

		if($fixedpriceauction==1 || $offauction==1)
		{
			$totalincom = ($objsel1->totalubids * $minbidprice) + $auctionfixedprice;
			
			$aucprice = number_format($totalincom,2);
		}
		else
		{
			$totalincom = $auctiondueprice + ($objsel1->totalubids * $minbidprice);

			$aucprice = number_format($totalincom,2);
		}
		return $aucprice;
	}

	function Makeentryinlog($sessionstarttime,$sessionendtime,$time,$globalcounter)
		{
$logcontent = "Counter:".$globalcounter."
Server Time:".date("d/m/Y H:i:s",$time)."
Start Time:".date("d/m/Y H:i:s",$sessionstarttime)."
End Time:".date("d/m/Y H:i:s",$sessionendtime)."
----------------------------------------------
";
			$fp = fopen("auctionpause.log","a");
			fwrite($fp,$logcontent);
			fclose($fp);
		}
		
	function errlog($time,$errmsg)
		{
			$logcontent = date("d/m/Y H:i:s",$time)."  ".$errmsg;			
			$fp = fopen("errors.log","a");
			fwrite($fp,$logcontent);
			fclose($fp);
		}

function fnc_date_calc($this_date,$num_days){
    
    $my_time = strtotime ($this_date); //converts date string to UNIX timestamp
    $timestamp = $my_time + ($num_days * 86400); //calculates # of days passed ($num_days) * # seconds in a day (86400)
    $return_date = date("Y-m-d",$timestamp);  //puts the UNIX timestamp back into string format
    
    return $return_date;//exit function and return string
}

function GetIP()
{
	if(!empty($_SERVER["HTTP_CLIENT_IP"]))
	{
		$cip = $_SERVER["HTTP_CLIENT_IP"];
	}
	else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
	{
		$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	}
	else if(!empty($_SERVER["REMOTE_ADDR"]))
	{
		$cip = $_SERVER["REMOTE_ADDR"];
	}
	else
	{
		$cip = '';
	}
	preg_match("/[\d\.]{7,15}/", $cip, $cips);
	$cip = isset($cips[0]) ? $cips[0] : 'unknown';
	unset($cips);
	return $cip;
}		

function getWonCount()
{
  $uid = $_SESSION["userid"];  
  if($uid!=''){
		$sql2 = "select count(*) as won_count from `won_auctions` a where a.userid=$uid and a.`won_date`>= DATE_SUB(now(), INTERVAL 28 DAY) ";
  	$res2 = mysql_query($sql2);
  	$obj2 = mysql_fetch_object($res2);
		$won_count = $obj2->won_count;		
  }
  if($won_count>8)$won_count=8;
  return $won_count;
}
?>
