/**
 * calculate checkout info
 */
function CalculateCheckoutInfo($source)
{
    if (!isset($source['checkout_action']) || strlen($source['data']) == 0) {
        return false;
    }
    $res = array();
    $res['checkout_action'] = $source['checkout_action'];
    $res['data'] = $source['data'];
    if (isset($source['allow_subscribe'])) {
        $res['allow_subscribe'] = $source['allow_subscribe'];
    }
    if (isset($source['subscribe_days'])) {
        $res['subscribe_days'] = $source['subscribe_days'];
    }
    switch ($source['checkout_action']) {
        case 'membership':
            $res['amount'] = $source['amount'];
            $prices = getMembershipPrices($res['data']);
            $pricingOptionExists = false;
            foreach ($prices as $days => $price) {
                if ($res['amount'] == $price) {
                    if ($days == 0) {
                        $res['allow_subscribe'] = '';
                        $res['subscribe_days'] = 0;
                    } else {
                        $res['allow_subscribe'] = 'on';
                        $res['subscribe_days'] = $days;
                    }
                    $pricingOptionExists = true;
                    break;
                }
            }
            if (!$pricingOptionExists) {
                return false;
            } else {
                return $res;
            }
        case 'speeddating':
            return $res;
        case 'credits':
            $credIndex = (int) $source['data'];
            $res['amount'] = getCreditsPriceByIndex($credIndex);
            return $res;
        case 'profiles':
            return $res;
        default:
            return false;
    }
}
/**
 * purchase memberships
 */
function PageCompMemberships()
{
    global $site;
    global $en_credits;
    global $doll;
    global $oTemplConfig;
    $expl_win_h = 400;
    //not global
    $purchasableOnly = true;
    $purchasableMemberships = getMemberships($purchasableOnly);
    $credit2money = (double) getParam('credit2money');
    // how many credits for one money unit
    $ret = '';
    foreach ($purchasableMemberships as $ID => $name) {
        ob_start();
        ?>
<div class="membership_line">
	<form id="membership<?php 
        echo $ID;
        ?>
Form" action="<?php 
        echo $site['url'] . 'checkout.php';
        ?>
" method="post" style="margin: 2px">
	<input type="hidden" name="action" value="calculate" />
	<input type="hidden" name="checkout_action" value="membership" />
	<input type="hidden" name="allow_subscribe" value="on" />
	<input type="hidden" name="data" value="<?php 
        echo $ID;
        ?>
" />
	<table cellpadding="4" cellspacing="0" border="0" width="100%">
		<tr>
			<td align="left"><?php 
        echo htmlspecialchars($name);
        ?>
</td>
			<td align="right">
				<select class="no" name="amount" style="vertical-align: middle;">
<?php 
        $prices = getMembershipPrices($ID);
        foreach ($prices as $days => $price) {
            $optionText = $days ? $days . ' ' . _t('_days') : _t('_Lifetime');
            $optionText .= ' - ' . $doll . $price;
            if ($en_credits) {
                $optionText .= ' (' . sprintf('%.2f', $price * $credit2money) . ' ' . _t('_credits') . ')';
            }
            echo "<option value=\"{$price}\">{$optionText}</option>\n";
        }
        ?>
				</select>
			</td>
			<td align="right" width="110">
				<input type="submit" class="no" value="<?php 
        echo _t("_Check Out");
        ?>
" style="width: 100px; vertical-align: middle;" />
			</td>
			<td align="right" width="60">
				<a href="javascript:void(0);" onClick="javascript: window.open('explanation.php?explain=membership&type=<?php 
        echo $ID;
        ?>
', '', 'width=<?php 
        echo $oTemplConfig->popUpWindowWidth;
        ?>
,height=<?php 
        echo $expl_win_h;
        ?>
,menubar=no,status=no,resizable=no,scrollbars=yes,toolbar=no, location=no' );"><?php 
        echo _t("_Explanation");
        ?>
</a>
			</td>
		</tr>
	</table>
	</form>
</div>
<?php 
        $ret .= ob_get_contents();
        ob_end_clean();
    }
    $ret = "<div class=\"memberships_box\">\n{$ret}\n</div>\n";
    return DesignBoxContent(_t("_Membership NEW"), $ret, $oTemplConfig->PageCompMemberships_db_num);
}
/**
 * Buy a membership for a member
 *
 * @param int $memberID			- member that is going to get the membership
 * @param int $membershipID		- bought membership
 * @param float $price			- one of the pricing options for the given membership
 * @param int $transactionID	- internal key of the transaction (ID from Transactions table)
 * @param boolean $startsNow	- if true, the membership will start immediately;
 *								  if false, the membership will start after the current
 *								  membership expires
 *
 * @return boolean				- true in case of success, false in case of failure
 *
 *
 */
function buyMembership($memberID, $membershipID, $price, $transactionID, $startsNow = false)
{
    //input validation
    $memberID = (int) $memberID;
    $membershipID = (int) $membershipID;
    $price = (double) $price;
    $transactionID = (int) $transactionID;
    //check for predefined non-purchasable memberships
    if ($membershipID == MEMBERSHIP_ID_NON_MEMBER || $membershipID == MEMBERSHIP_ID_STANDARD || $membershipID == MEMBERSHIP_ID_PROMOTION) {
        return false;
    }
    //check if membership is active and purchasable
    $resMemLevels = db_res("SELECT Active, Purchasable FROM MemLevels WHERE ID = {$membershipID}");
    if (mysql_num_rows($resMemLevels) <= 0) {
        return false;
    }
    //membership not found
    $arrMembership = mysql_fetch_assoc($resMemLevels);
    $membershipActive = $arrMembership['Active'] == 'yes' ? true : false;
    $membershipPurchasable = $arrMembership['Purchasable'] == 'yes' ? true : false;
    if (!$membershipActive || !$membershipPurchasable) {
        return false;
    }
    //check if the given pricing option is valid for the given membership
    $membershipPrices = getMembershipPrices($membershipID);
    if (count($membershipPrices) <= 0) {
        return false;
    }
    $pricingOptionExists = false;
    foreach ($membershipPrices as $daysOption => $priceOption) {
        if ($priceOption == $price) {
            $pricingOptionExists = true;
            break;
        }
    }
    if (!$pricingOptionExists) {
        return false;
    }
    return setMembership($memberID, $membershipID, $daysOption, $startsNow, $transactionID);
}