<?php session_start();  //Developed by ITechScripts
require 'include/announcements.php'; // ITECHBIDS Configuration File
require 'include/itechbids.php'; // Server compatibility Patch
require 'payments/api.php';
require 'payments/w_display.php';
if(strlen($_SESSION["username"])==0)
{
echo '<meta http-equiv="refresh" content="0; url=index.php">';
exit();
} 
$mode = $_GET['mode'];
if($mode=="confirm")
{
$today=date('Y-m-d');
$user_id=$_SESSION[userid];

$wthamount=$_SESSION['amount'];
$wthid=$_SESSION['payment_gateway'];
$Twithdraw=$_SESSION['Tamount'];

if(($wthid!=7)&&($wthid!=8)){
$wthuser=$_POST[wthuser];
}

if($wthid==6){
$contact_name=$_POST[contact_name];
$acc_title=$_POST[acc_title];
$bank_name=$_POST[bank_name];
$swift_code=$_POST[swift_code];
$acc_number=$_POST[acc_number];
$country=$_POST[cbocountry];
$Other=$_POST[Other];

$auction_query="select * from offline_payments where username='$user_id' and type='0'";
$table=mysql_query($auction_query);
$retch=mysql_num_rows($table);
if($retch>=0)
{
$up_sql="update offline_payments set contact_name='$contact_name',acc_title='$acc_title',bank_name='$bank_name',swift_code='$swift_code',acc_number='$acc_number',country='$country' where username='$user_id'";
$res=mysql_query($up_sql);
}
if($retch == 0)
{
$in_sql="INSERT INTO `offline_payments` VALUES ('', '$user_id', '$contact_name', '$acc_title', '$bank_name', '$swift_code', '$acc_number', '$country', '', '0')";
$res=mysql_query($in_sql);
}
}


if($wthid==7){
$con_name=$_POST[con_name];
$pay_name=$_POST[pay_name];
$address=$_POST[address];
$city=$_POST[city];
$state=$_POST[state];
$country=$_POST[cbocountry];
$Other=$_POST[Other];


$sql="update member_details set ";
   $sql.="address='$address', ";
   $sql.="city='$city',state='$state',country='$country',pin_code='$code',home_phone='$primary'";
   $sql.=" where user_id=".$user_id;
   $res=mysql_query($sql);
}

$w_sql="INSERT INTO withdraw_requests VALUES ('', '$user_id', '$wthid', '$wthamount', '$wthuser', '$today', '$pay_name', 'pending', 'no', '$Other')";
$w_res=mysql_query($w_sql);
$sql = "update addfunds set fund_date='$today',fund_amount=fund_amount-$wthamount where member_id='$user_id'";
$result = mysql_query($sql);
echo '<meta http-equiv="refresh" content="0; url=index.php">';
exit();
}
 
if($mode=="complete")
{
$wthamount=$_SESSION['amount'];
$wthid=$_SESSION['payment_gateway'];
$Twithdraw=$_SESSION['Tamount'];
if(($wthid!=7)&&($wthid!=8)){
$wthuser=$_POST[wthuser];
if($wthuser=="")
{
$werror="noerr";
$serror="yes";
}
else
{
$serror="no";
}
}
if($wthid==6){
$contact_name=$_POST[contact_name];
$acc_title=$_POST[acc_title];
$bank_name=$_POST[bank_name];
$swift_code=$_POST[swift_code];
$acc_number=$_POST[acc_number];
$country=$_POST[cbocountry];
$Other=$_POST[Other];
if(($contact_name=="")||($acc_title=="")||($swift_code=="")||($acc_number=="")||($country==""))
{
$werror="noerr";
$serror="yes";
}
else
{
$serror="no";
}
}

if($wthid==7){
$con_name=$_POST[con_name];
$pay_name=$_POST[pay_name];
$address=$_POST[address];
$city=$_POST[city];
$state=$_POST[state];
$country=$_POST[cbocountry];
$Other=$_POST[Other];

if(($con_name=="")||($pay_name=="")||($address=="")||($state=="")||($city=="")||($country==""))
{
$werror="noerr";
$serror="yes";
}
else
{
$serror="no";
}
}
}
if($mode=="start"){
$wthamount = $_POST['wthamount'];
$wthid = $_POST['paymentid'];
if($wthamount!=""){
$wthdraw="select * from withdraw_gateway where gateway_id='$wthid'";
$wthdraw_res=mysql_query($wthdraw);
$wthdraw_rec=mysql_fetch_array($wthdraw_res);
$wthdraw_fee=$wthdraw_rec[fees];
$wthdraw_min=$wthdraw_rec[min_ammount];		
$wthdraw_com=$wthdraw_rec[comments];	
$wthdraw_gat=$wthdraw_rec[payment_gateway];

$_SESSION['wthdraw_fee'] = $wthdraw_fee;
$_SESSION['wthdraw_com']=$wthdraw_com;
$_SESSION['wthdraw_gat'] = $wthdraw_gat;





$user_id=$_SESSION[userid];
	$query="select * from addfunds where member_id='$user_id'";
	$table=mysql_query($query);
	$total_fund_amount=0;
	
	if(mysql_num_rows($table)>0)
	while($row=mysql_fetch_array($table))
	$total_fund_amount +=$row['fund_amount'];	

$Twithdraw = $wthamount - $wthdraw_fee;

$auction_query="select * from offline_payments where username='$user_id'";
$table=mysql_query($auction_query);
$ret=mysql_fetch_array($table);
$contact_name=$ret['contact_name'];
$acc_title=$ret['acc_title'];
$bank_name=$ret['bank_name'];
$swift_code=$ret['swift_code'];
$acc_number=$ret['acc_number'];
$countryid=$ret['country'];

$user_sql="select * from member_details where user_id='$user_id'";
$user_result=mysql_query($user_sql);
$row=mysql_fetch_array($user_result);
$first=$row[first_name];
$last=$row[last_name];
$address=$row[address];
$city=$row[city];
$state=$row[state];
$countryid=$row[country];
$code=$row[pin_code];
$con_name="$first $last";
$pay_name="$first $last";

$werror="noerr";

$country_sql="select * from site_countries where country_id=$countryid";
$country_res=mysql_query($country_sql);
$country_row=mysql_fetch_array($country_res);
$country=$country_row['country'];
}
if($wthamount==""){
$werror="noamm";
}
if($wthamount>$total_fund_amount){
$werror="maxim";
}
}


?>
<? 
$asd1 = rand(100, 1000);
$asd2 = date(gis);
$asd=$asd1+$asd2;
$userid=$_SESSION['user_id'];
$tr_id = ''.$userid.''.$asd.'||withdraw'; 

require'themes/withdraw.tpl'; ?>
</table>
</tr>
<tr><td>
<?
  require 'templates/'.$tfold.'/footer.php';
?>
</td></tr>
</table>	
 