<?php

function form($n, $form1, $form2, $form5) {
    $n = abs($n) % 100;
    $n1 = $n % 10;
    if ($n > 10 && $n < 20)
        return $form5;
    if ($n1 > 1 && $n1 < 5)
        return $form2;
    if ($n1 == 1)
        return $form1;
    return $form5;
}

function vremja_sek($tl) {
    $d = 3600 * 24;
    $day = floor($tl / $d);
    $tl = $tl - ($d * $day);
    $hour = floor($tl / 3600);
    $tl = $tl - (3600 * $hour);
    $minute = floor($tl / 60);
    $tl = $tl - (60 * $minute);
    $second = floor($tl);
    $dayt = "" . ($day > 0 ? "$day " . form($day, 'день', 'дня', 'дней') . " " : null) . "";
    $hourt = "" . ($hour > 0 ? "$hour " . form($hour, 'час', 'часа', 'часов') . " " : null) . "";
    $minutet = "" . ($minute > 0 ? "$minute " . form($minute, 'минута', 'минуты', 'минут') . " " : null) . "";
    $secondt = "" . ($second > 0 ? "$second сек. " : null) . "";
    if ($hour > 0 && $day == 0) {
        $dayt = NULL;
    }
    return "$dayt$hourt$minutet$secondt";
}

function discount($discount = null) {
    global $user, $set;
    
    $db = new db;
    if ($set['rekl_discount'] == 1) {
        $discount = $db->super_query("SELECT SUM(time_rekl) as time_rekl FROM `rekl_story` WHERE `id_user` = '" . $user['id'] . "'");
        $discount = $discount['time_rekl'];
        $discount = $discount / $set['rekl_discount_sec'];
        $discount = $discount * $set['rekl_discount_per'];
        $discount = floor($discount);
        if ($discount > 100)
            $discount = 99;
    }elseif ($set['rekl_discount'] == 2) {
        $discount = $set['rekl_discount_per'];
    } else {
        $discount = null;
    }
    return $discount;
}

function deduction($n) {
    global $set;
    switch ($n) {
        case'1':return $set['rekl_money_1'] - (floor(($set['rekl_money_1'] * discount()) / 100));
            break;
        case'2':return $set['rekl_money_2'] - (floor(($set['rekl_money_2'] * discount()) / 100));
            break;
        case'3':return $set['rekl_money_3'] - (floor(($set['rekl_money_3'] * discount()) / 100));
            break;
        case'4':return $set['rekl_money_4'] - (floor(($set['rekl_money_4'] * discount()) / 100));
            break;
    }
}

?>
