function dorepeats($user)
{
    $query = "SELECT * FROM repeats WHERE ExpireTime>'" . time() . "' AND repeats.UserID='{$user}'";
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_assoc($result)) {
        $repeatid = $row['RepeatID'];
        $repeatinsertid = $row['PairedID'];
        $account = $row['AccountID'];
        $amount = $row['PaymentAmount'];
        if ($row['ToAccount'] != 0) {
            $theotherparty = getaccountname($account);
            $toamount = -$amount;
        }
        if ($row['Frequency'] == 'm') {
            $d = date("j", $row['Timestamp']);
            $m = date("n", $row['Timestamp']);
            $y = date("Y", $row['Timestamp']);
            if ($m == 12) {
                $m = 1;
                $y++;
            } else {
                $m++;
            }
            $time = strtotime($m . "/" . $d . "/" . $y);
            $i = 2;
            while ($time < time() + 604800 && $i <= $rt) {
                $insertid = 0;
                $query = "SELECT * FROM payments WHERE Timestamp='{$time}' AND RepeatID='" . $row['RepeatID'] . "'";
                $out = mysql_query($query) or die(mysql_error());
                if (mysql_num_rows($out) == 0) {
                    if ($row['PairedID'] != 0) {
                        $query = "INSERT INTO payments (UserID, AccountID, Timestamp, PaymentName, PaymentDesc, PaymentAmount, PaymentType, ToAccount, RepeatID, LabelID) VALUES ('{$user}', '" . $row['ToAccount'] . "', '{$time}', '{$theotherparty}', '" . $row['PaymentDesc'] . "', '{$toamount}', '" . $row['PaymentType'] . "', '{$account}', '{$repeatinsertid}', '" . $row['LabelID'] . "')";
                        mysql_query($query) or die(mysql_error() . " dorepeat#001");
                        $insertid = mysql_insert_id();
                    }
                    $query = "INSERT INTO payments (UserID, AccountID, Timestamp, PaymentName, PaymentDesc, PaymentAmount, PaymentType, ToAccount, PairedID, RepeatID, LabelID) VALUES ('{$user}', '{$account}', '{$time}', '" . $row['PaymentName'] . "', '" . $row['PaymentDesc'] . "', '{$amount}', '" . $row['PaymentType'] . "', '" . $row['ToAccount'] . "', '{$insertid}', '{$repeatid}', '" . $row['LabelID'] . "')";
                    mysql_query($query) or die(mysql_error() . " dorepeat#002");
                    if ($insertid != 0) {
                        $paymentid = mysql_insert_id();
                        $query = "UPDATE payments SET PairedID='{$paymentid}' WHERE PaymentID='{$insertid}'";
                        mysql_query($query) or die(mysql_error() . " dorepeat#003");
                    }
                }
                if ($m == 12) {
                    $m = 1;
                    $y++;
                } else {
                    $m++;
                }
                $time = strtotime($m . "/" . $d . "/" . $y);
                $i = 2;
            }
        } else {
            $time = $row['Timestamp'] + $row['Frequency'] * 86400;
            $i = 2;
            while ($time < time() + 604800 && $i <= $row['Times']) {
                $query = "SELECT * FROM payments WHERE Timestamp='{$time}' AND RepeatID='" . $row['RepeatID'] . "'";
                $out = mysql_query($query) or die(mysql_error());
                if (mysql_num_rows($out) == 0) {
                    if ($row['PairedID'] != 0) {
                        $query = "INSERT INTO payments (UserID, AccountID, Timestamp, PaymentName, PaymentDesc, PaymentAmount, PaymentType, ToAccount, RepeatID, LabelID) VALUES ('{$user}', '" . $row['ToAccount'] . "', '{$time}', '{$theotherparty}', '" . $row['PaymentDesc'] . "', '{$toamount}', '" . $row['PaymentType'] . "', '{$account}', '{$repeatinsertid}', '" . $row['LabelID'] . "')";
                        mysql_query($query) or die(mysql_error() . " dorepeat#004");
                        $insertid = mysql_insert_id();
                    }
                    $query = "INSERT INTO payments (UserID, AccountID, Timestamp, PaymentName, PaymentDesc, PaymentAmount, PaymentType, ToAccount, PairedID, RepeatID, LabelID) VALUES ('{$user}', '{$account}', '{$time}', '" . $row['PaymentName'] . "', '" . $row['PaymentDesc'] . "', '{$amount}', '" . $row['PaymentType'] . "', '" . $row['ToAccount'] . "', '{$insertid}', '{$repeatid}', '" . $row['LabelID'] . "')";
                    mysql_query($query) or die(mysql_error() . " dorepeat#005");
                    if ($insertid != 0) {
                        $paymentid = mysql_insert_id();
                        $query = "UPDATE payments SET PairedID='{$paymentid}' WHERE PaymentID='{$insertid}'";
                        mysql_query($query) or die(mysql_error() . " dorepeat#006");
                    }
                }
                $i++;
                $time = $time + $row['Frequency'] * 86400;
            }
        }
    }
}
 if ($desc != NULL) {
     $query = $query . "PaymentDesc='{$desc}', ";
 }
 if ($amount != NULL) {
     $query = $query . "PaymentAmount='{$amount}', ";
 }
 if ($type != NULL) {
     $query = $query . "PaymentType='{$type}', ";
 }
 $query = $query . "AccountID='{$account}', Timestamp='{$time}', ToAccount='{$toaccount}', LabelID='{$label}' WHERE UserID='{$user}' AND PaymentID='{$id}'";
 mysql_query($query) or die(mysql_error());
 $query = "SELECT * FROM payments LEFT JOIN accounts ON payments.AccountID=accounts.AccountID WHERE payments.UserID='{$user}' AND PaymentID='{$id}'";
 $result = mysql_query($query) or die(mysql_error());
 $row = mysql_fetch_assoc($result);
 if ($toaccount != 0) {
     $otherparty = getaccountname($account);
     $amount = -$amount;
     $query = "UPDATE payments SET ";
     if ($otherparty != NULL) {
         $query = $query . "PaymentName='{$otherparty}', ";
     }
     if ($desc != NULL) {
         $query = $query . "PaymentDesc='{$desc}', ";
     }
     if ($amount != NULL) {
         $query = $query . "PaymentAmount='{$amount}', ";
     }
     if ($type != NULL) {
         $query = $query . "PaymentType='{$type}', ";
     }
     $query = $query . "AccountID='{$toaccount}', Timestamp='{$time}', ToAccount='{$account}', LabelID='{$label}' WHERE UserID='{$user}' AND PaymentID='" . $row['PairedID'] . "'";