<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php 
error_reporting(E_ALL ^ E_DEPRECATED);

require_once('../Connections/ebanking.php');

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  global $ebanking;

  $theValue = mysqli_real_escape_string($ebanking, $theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "transferform")) {
  $insertSQL = sprintf("INSERT INTO transfers (transfertype, beneficiaryname, bankname, bankaddress, Country, swiftcode, iban, beneficiaryacctno, amount, `currency`, accounttodebit, accountno) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['transfertype'], "text"),
                       GetSQLValueString($_POST['beneficiaryname'], "text"),
                       GetSQLValueString($_POST['bankname'], "text"),
                       GetSQLValueString($_POST['bankaddress'], "text"),
                       GetSQLValueString($_POST['Country'], "text"),
                       GetSQLValueString($_POST['swiftcode'], "text"),
                       GetSQLValueString($_POST['iban'], "text"),
                       GetSQLValueString($_POST['beneficiaryacctno'], "text"),
                       GetSQLValueString($_POST['amount'], "text"),
                       GetSQLValueString($_POST['currency'], "text"),
                       GetSQLValueString($_POST['accounttodebit'], "text"),
                       GetSQLValueString($_POST['accountno'], "text"));

  //mysql_select_db($database_ebanking, $ebanking);
  $Result1 = mysqli_query($ebanking,$insertSQL);

  $insertGoTo = "processingtransfer.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

$colname_account = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_account = $_SESSION['MM_Username'];
}
$colname_account = mysqli_real_escape_string($ebanking, $colname_account);
$query_account = "SELECT * FROM balance WHERE account = '$colname_account'";
$account = mysqli_query($ebanking, $query_account);
$row_account = mysqli_fetch_assoc($account);
$totalRows_account = mysqli_num_rows($account);

$colname_transfermessage = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_transfermessage = $_SESSION['MM_Username'];
}
//mysqli_select_db($database_ebanking, $ebanking);
$query_transfermessage = sprintf("SELECT * FROM transfermessage WHERE customer = %s", GetSQLValueString($colname_transfermessage, "text"));
$transfermessage = mysqli_query($ebanking, $query_transfermessage);
$row_transfermessage = mysqli_fetch_assoc($transfermessage);
$totalRows_transfermessage = mysqli_num_rows($transfermessage);
?>
<!DOCTYPE html>
<html lang="en">
<script type="text/javascript">
var count = 65;
var redirect = "done.php";
 
function countDown(){
    var timer = document.getElementById("timer");
    if(count > 0){
        count--;
        timer.innerHTML = "Completing Your Transfer in: "+count+" seconds.";
        setTimeout("countDown()", 1000);
    }else{
        window.location.href = redirect;
    }
}
</script>
<?php 
error_reporting(E_ALL ^ E_DEPRECATED);
?>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Wolfville Credit Union - Online Banking Platform</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/all.css">
    <link rel="stylesheet" href="css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="css/roc-dachboard.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/sidebar.css">
    <link rel="stylesheet" href="css/floating-chatbox.css">
    <script type="text/javascript" src="../js/jquery-3.3.1.js"></script>
    <script type="text/javascript" src="../js/bootstrap.js"></script>
    <style>
        .content-list li .transfers.active{
            background-image: linear-gradient(to bottom,#5864aaa1 0,#070b21 100%);
        }
    </style>
</head>
<body onload="updateClock(); setInterval('updateClock()', 1000 )">
    <?php include "includes/header.php"; ?>

    <div id="content">
        <div class="container">
            <div class="row">
                <div class="col-lg-12">
                    <div class="row">
                        <div class="col-lg-8 col-lg-offset-2">
                            <div class="row">
                                <div class="col-lg-12">
                                    <div class="panel panel-default">
                                        <div class="panel-heading">
                                            <h4>Funds Transfer Failed</h4>
                                        </div>
                                        <div class="panel-body">
                                            <div class="row">
                                                <div class="col-lg-10 col-lg-offset-1">
                                                    <h3 class="text-center page-subtitle"><img src="failed-icon.png" width="100" height="100"><br>
                                                  <?php echo $row_transfermessage['title']; ?></h3>
                                                    <p class="text-center"><span style="color: #c0392b;"><?php echo $row_transfermessage['message']; ?></span>
                                                    <br>
                                                        
                                                  </p>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php include "includes/footer.php"; ?>
    
</body>
</html>
<?php
mysql_free_result($account);
?>