<?
if (count(get_included_files()) ==1) die("<h4>You don't have right permission to access this file directly.</h4>");

// check baki
function sumWall($col, $table) {
  global $con;
  $res = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(".$col.") FROM ".$table." WHERE userid = '".$_SESSION['aidi']."'"));
  return $res[0];
}

// check baki dari ID
function jumLah($col, $table, $id) {
  global $con;
  $res = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(".$col.") FROM ".$table." WHERE userid = '".$id."'"));
  return $res[0];
}

/*
$cwallet = sumWall("masuk", "cwallet") - sumWall("keluar", "cwallet");
$rwallet = sumWall("masuk", "rwallet") - sumWall("keluar", "rwallet");
$gwallet = sumWall("masuk", "gwallet") - sumWall("keluar", "gwallet");
$mwallet = sumWall("masuk", "mwallet") - sumWall("keluar", "mwallet");
*/

function reKod($col1, $table, $col2, $input) {
  global $con;
  $hasil = mysqli_fetch_array(mysqli_query($con, "SELECT ".$col1." FROM ".$table." WHERE ".$col2." = '".$input."'"));
  return $hasil[0];
}

/*
function telUser($input) {
  global $con;
  $r = mysqli_fetch_array(mysqli_query($con, "SELECT telefon, username, sms FROM ahli WHERE id = '".$input."'"));
  $info = $r[0]."|".$r[1]."|".$r[2];
  return $info;
}
*/

function chkPenaja($p) {
  global $con;
  return mysqli_num_rows(mysqli_query($con, "SELECT id FROM ahli WHERE kp = '".$p."'"));
}

function chkPin($n) {
  global $con;
  return mysqli_num_rows(mysqli_query($con, "SELECT id FROM bestpin WHERE no = '".$n."'"));
}

function chkUsr($x) {
  global $con;
  return mysqli_num_rows(mysqli_query($con, "SELECT id FROM ahli WHERE username = '".$x."'"));
}

function chkAny($field, $input) {
  global $con;
  return mysqli_num_rows(mysqli_query($con, "SELECT id FROM ahli WHERE ".$field." = '".$input."'"));
}
/*
function strCom($data) {
  str_replace(array('register', 'company'), array('daftar', 'company'), array('malaysia', 'company'), array('registration', 'company'), $data);
}
*/
function logSub($tarikh, $jenis, $keterangan, $amaun) {
  global $con;
  mysqli_query($con, "INSERT INTO `log` (tarikh, jenis, keterangan, amaun) 
  VALUES ('".$tarikh."', '".$jenis."', '".$keterangan."', '".$amaun."')");  
}

function mysqli_result($result,$row,$field=0) {
    if ($result===false) return false;
    if ($row>=mysqli_num_rows($result)) return false;
    if (is_string($field) && !(strpos($field,".")===false)) {
        $t_field=explode(".",$field);
        $field=-1;
        $t_fields=mysqli_fetch_fields($result);
        for ($id=0;$id<mysqli_num_fields($result);$id++) {
            if ($t_fields[$id]->table==$t_field[0] && $t_fields[$id]->name==$t_field[1]) {
                $field=$id;
                break;
            }
        }
        if ($field==-1) return false;
    }
    mysqli_data_seek($result,$row);
    $line=mysqli_fetch_array($result);
    return isset($line[$field])?$line[$field]:false;
}


function randomPass() {
  $len = 5;
  $base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  $max = strlen($base)-1;
  $activatecode = '';
  mt_srand((double)microtime()*1000000);
  while (strlen($activatecode)<$len+1)
  //$activatecode .= $base{mt_rand(0,$max)};
  $activatecode .= $base[mt_rand(0,$max)];
  return $activatecode;
}


function randomPin() {
  $len = 9;
  $base = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
  $max = strlen($base)-1;
  $activatecode = '';
  mt_srand((double)microtime()*1000000);
  while (strlen($activatecode)<$len+1)
  //$activatecode .= $base{mt_rand(0,$max)};
  $activatecode .= $base[mt_rand(0,$max)];
  return $activatecode;
}

function noTis($jenis, $bold, $pengumuman) {
  echo "<div>";
  echo "  <div class=\"alert alert-".$jenis."\">\n";
  echo "    <strong>".$bold."!</strong> ".$pengumuman."\n";
  echo "  </div>\n";
  echo "</div>";
}
?>