<?php
// INCLUDE HEADER IN PAGE
include('other_header.php');
// GET CURRENT DATE AND TIME
$date = date('Y-m-d H:i:s');
if(isset($_POST['success'])) {
	if($_POST['success'] == 'loggedin') {
	?>
	<div style="margin-top: -80px;" class="alert alert-success" role="alert">
		<strong>Well done!</strong> Successfully logged in.
	</div>
	<?php
	}
}
if(isset($_POST['text1'])) {
	$text1 = $_POST['text1'];
	$newbalance1 = $_POST['newbalance1'];
	$text2 = $_POST['text2'];
	$newbalance2 = $_POST['newbalance2'];
	if(isset($_POST['text2'])) {
	?>
		<div style="margin-top: -80px;" class="alert alert-success" role="alert">
			<strong>Success!</strong> <?php echo $text1." ".$acu['ISO']." ".$newbalance1.". ".$text2." ". $acu['ISO']." ".$newbalance2."."; ?>
		</div>
	<?php
	}else {
	?>
		<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
			<strong>Sorry!</strong> <?php echo $text1; ?>
		</div>
	<?php	
	}
}
if($_POST['submit']) {
	$amount = $_POST['amount'];
	$amount = AntiXSS::setEncoding($amount, "UTF-8");
	$amount = str_replace('"','',$amount);
	$amount = str_replace("'","",$amount);
	$amount = str_replace(">","",$amount);
	$amount = str_replace("<","",$amount);
	$amount = str_replace("</","",$amount);
	$amount = str_replace("&%","",$amount);
	$amount = str_replace("script","",$amount);
	$amount = str_replace("(","",$amount);
	$amount = str_replace(")","",$amount);
	$accountfrom = $_POST['accountfrom'];
	$accountfrom = AntiXSS::setEncoding($accountfrom, "UTF-8");
	$accountfrom = str_replace('"','',$accountfrom);
	$accountfrom = str_replace("'","",$accountfrom);
	$accountfrom = str_replace(">","",$accountfrom);
	$accountfrom = str_replace("<","",$accountfrom);
	$accountfrom = str_replace("</","",$accountfrom);
	$accountfrom = str_replace("&%","",$accountfrom);
	$accountfrom = str_replace("script","",$accountfrom);
	$accountfrom = str_replace("(","",$accountfrom);
	$accountfrom = str_replace(")","",$accountfrom);
	if($accountfrom == 'publishers') {
		$oldbalance1 = $seeA['balance'];
		$newbalance1 = $seeA['balance'] + $amount;
		$oldbalance2 = $seeP['balance'];
		$newbalance2 = $seeP['balance'] - $amount;
		$text1 = "Balance of your advertiser's account was increased to";
		$text2 = "Balance of your publisher's account was decreased to";
		if(($oldbalance2 - $amount) >= 0) {
			$completed = 1;
			$command = 1;
		}else {
			$completed = 0;
		}
	}elseif($accountfrom == 'advertisers') {
		$oldbalance1 = $seeP['balance'];
		$newbalance1 = $seeP['balance'] + $amount;
		$oldbalance2 = $seeA['balance'];
		$newbalance2 = $seeA['balance'] - $amount;
		$text1 = "Balance of your publisher's account was increased to";
		$text2 = "Balance of your advertiser's account was decreased to";
		if(($oldbalance2 - $amount) >= 0) {
			$completed = 1;
			$command = 2;
		}else {
			$completed = 0;
		}
	}
	if($completed == 1) {
		if (ctype_digit($amount)) {
        	if($command == 1) {
        		$sql = mysql_query("UPDATE Accounts_Classic_Publishers SET balance='$newbalance2' WHERE username='$session_username'") or die(mysql_error());
        		$sql = mysql_query("UPDATE Accounts_Classic_Advertisers SET balance='$newbalance1' WHERE username='$session_username'") or die(mysql_error());
        	}elseif($command == 2) {
        		$sql = mysql_query("UPDATE Accounts_Classic_Advertisers SET balance='$newbalance2' WHERE username='$session_username'") or die(mysql_error());
        		$sql = mysql_query("UPDATE Accounts_Classic_Publishers SET balance='$newbalance1' WHERE username='$session_username'") or die(mysql_error());
        	}
        	?>
        						<form action='p.BalanceExchange' method='post' name='frm'>
									<input type='hidden' name='text1' value="<?php echo $text1; ?>">
									<input type='hidden' name='newbalance1' value="<?php echo $newbalance1; ?>">
									<input type='hidden' name='text2' value="<?php echo $text2; ?>">
									<input type='hidden' name='newbalance2' value="<?php echo $newbalance2; ?>">
								</form>
								<script language="JavaScript">
									document.frm.submit();
								</script>
			<?php
    	}else{
        	?>
				        		<form action='p.BalanceExchange' method='post' name='frm'>
									<input type='hidden' name='text1' value='Please enter real value, whole numbers, without any decimals.'>
								</form>
								<script language="JavaScript">
									document.frm.submit();
								</script>
			<?php
    	}
	}else{
        	?>
        						<form action='p.BalanceExchange' method='post' name='frm'>
									<input type='hidden' name='text1' value="You don't have enough money.">
								</form>
								<script language="JavaScript">
									document.frm.submit();
								</script>
			<?php
   }
}
?>
  <div class="container">
    <div class="text-center">
      <h1><?php echo $title; ?>
      	<small>/
      			<?php
						$accounttypenow = $_SESSION['acctype'];
						if($accounttypenow == 0){//Publisher
					?>
							Active is: Publisher Account
					<?php
						}
						if($accounttypenow == 1){//Advertiser
					?>
							Active is: Advertiser Account
					<?php
						}
					?>
			</small>
		</h1>
 	 </div>
		</br>
	<div class="text-center">
      <h1><small><i class="fa fa-exchange"></i> Exchange</small></h1>
 	</div>
		<div class="row">
      <div class="col-md-8 col-md-offset-2">
		<div class="well">
			<div class="row">
				<center>
				<form name="csrf_form" id="shrinkform" action="" method="POST">
					<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="555" class="input-group">
							<input class="form-control" placeholder="50.00" name="amount" type="number">
							<span class="input-group-btn">
								<input value="Exchange" class="btn btn-default" style="width: 150px;" name="submit" type="submit">
							</span>
					</div>
					<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="555" class="input-group">
							<center>
								<input id="publishers" class="toggle toggle-left" name="accountfrom" value="publishers" type="radio" checked><label style="width:auto" class="btn btn-default" for="publishers" class="btn">Publisher <i class="fa fa-arrow-right"></i> Advertiser</label>
								<input id="advertisers" class="toggle toggle-right" name="accountfrom" value="advertisers" type="radio"><label style="width:auto" class="btn btn-default" for="advertisers" class="btn">Advertiser <i class="fa fa-arrow-right"></i> Publisher</label>
							</center>					
					</div>
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				</form>
				</center>
			</div>
        </div>
      </div>
    </div>
	<div class="text-center">
      <h1><small>Balances</small></h1>
 	</div>
		<div class="col-sm-6">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3 class="panel-title">
              Publisher's balance</h3>
            </div>
            <div class="panel-body">
            	<?php echo $acu['ISO']." ".$seeP['balance']; ?>
            </div>
          </div>
        </div>
        <div class="col-sm-6">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3 class="panel-title">
              Advertiser's balance</h3>
            </div>
            <div class="panel-body">
            	<?php echo $acu['ISO']." ".$seeA['balance']; ?>
            </div>
          </div>
        </div>
</div>

<?php
include('other_footer.php');
?>
