<?php

	include "library.php";

	$cxn = start_code( true );


		/* get variables */

	$id = val( $_SESSION, "id" );


		/* if the user is not logged in them direct them to the login page then back to here */

	if ($id == "")
	{
		$_SESSION['nextpage'] = url_encode( "buy_credits.php" );

		jump_to_page( "login.php" );		/* must happen before print output */

		exit();
	}


	page_header( "Buy Credits", $cxn );


	if (! has_paypal( $cxn ))
		echo red( "Western Union is the only payment type supported in your country<br><br>" );

		/* get current credits balance */

	$query = "SELECT credits_balance FROM profile WHERE id = ".$id;

	$result = run_query( $query, $cxn );

	$data = get_row( $result );


		/* setup variables */

	$credits_code = md5(time());

	$magic = md5( $id."|".$credits_code );

	$custom = "credits,".$id.",".$credits_code.",".$magic;

	$custom_subs = "subscription,".$id.",".$credits_code.",".$magic;


		/* save the credits check code */

	$query = "UPDATE profile SET credits_code = '".$credits_code."' WHERE id = ".$id;

	$result = run_query( $query, $cxn );

	echo "<br>";
	
	if ($data['credits_balance'] == "")
		echo red( "You currently have 0 credits" );
	else
	if ($data['credits_balance'] == "1")
		echo red( "You currently have 1 credit" );
	else
		echo red( "You currently have ".$data['credits_balance']." credits" );

	if (has_paypal( $cxn ))
	{
		echo black( "<br><br>Click a button below to buy credits<br><br>" );

//		echo black( "The first 3 new contacts each day are free. To send more messages than this you will need to purchase credits or be a monthly member.<br><br>" );

		echo black( "To purchase credits click one of the buttons below and enter your payment details into the PayPal site." );
	}

	echo "<br><br><br>";

	echo "<table width='100%'><tr><td width='10%'></td><td width='15%' valign='center'>";

//	if (has_paypal( $cxn ))
//	{
//		generate_button( $id, "8.00", button_filename( "buy_10_credits" ), $custom, "Academic Dating (10 Credits)" );
//
//		echo "</td><td valign='top'>".sp(3).black( "US $8" )."</td>";
//	}

	echo "<td width='20%'></td>";

	echo "<td width='20%' rowspan='6' align='center'>".paraheading( "Western Union" )."<br><br>".black( "You can buy credits by sending money through Western Union. This is slower and more expensive than using a credit card. The Western Union option is only available for purchasing 50 credits or 100 credits. For details " ).show_link( "click&nbsp;here", "western_union.php" );

	echo "<td width='20%'></td></tr>";

	echo "<tr><td></td><td valign='centre' align='centre'>";


		/* NOTE: must update several places in 'payments_successful' */
		/* when changing prices */

	if (has_paypal( $cxn ))
	{
		$query = "SELECT COUNT(*) as count FROM message WHERE sender_id = ".$id;

		$result = run_query( $query, $cxn );

		$data = get_row( $result );

		if ($data['count'] != 0)
		{
			generate_button( $id, "10.00", button_filename( "buy_25_credits" ), $custom, $_SESSION['sitename']." (25 Credits)" );

			echo "</td><td valign='centre'>".sp(3).black( "US $10" )."</td></tr><tr><td></td><td valign='centre' align='centre'>";
		}

		generate_button( $id, "20.00", button_filename( "buy_50_credits" ), $custom, $_SESSION['sitename']." (50 Credits)" );

		echo "</td><td valign='centre'>".sp(3).black( "US $20" )."</td></tr><tr><td></td><td valign='centre' align='centre'>";

		generate_button( $id, "40.00", button_filename( "buy_100_credits" ), $custom, $_SESSION['sitename']." (100 Credits)" );

		echo "</td><td valign='centre'>".sp(3).black( "US $40" )."</td></tr><tr><td></td><td valign='centre' align='centre'>";

		$domain = val( $_SERVER, "HTTP_HOST" );

		if ($domain != "www.academicdating.net")
		{
			generate_button( $id, "1.20", button_filename( "buy_1_credit" ), $custom, $_SESSION['sitename']." (1 Credit)" );
			echo "</td><td valign='top'>".sp(3).black( "US 40c" )."</td></tr><tr><td></td><td valign='center' align='center'>";
		}
	}
	
//	generate_subscribe( $custom_subs );
//	echo "</td><td valign='top'>".sp(3).black( "US $25 per month" )."</td></tr><tr><td></td><td valign='center' align='center'>";

	echo "</table>";

	echo black( "<br><br>" );

	echo black( "It costs 1 credit to send an email<br><br>" );
	echo black( "It costs 0<b>.</b>25 credits to send a 'kiss' message<br><br>" );
	echo black( "Once the first email has been sent you can communicate with that member for free" );


	function generate_button( $member_id, $cost, $gif, $custom, $description )
	{
		echo "<form action='https://www.paypal.com/cgi-bin/webscr' method='post'>";
		echo "<input type='hidden' name='cmd' value='_xclick'>";
		echo "<input type='hidden' name='business' value='enquiries@academicdating.net'>";
		echo "<input type='hidden' name='item_name' value='".$description."'>";
//		echo "<input type='hidden' name='item_number' value='".$member_id."'>";
		echo "<input type='hidden' name='amount' value='".$cost."'>";
		echo "<input type='hidden' name='quantity' value='1'>";
		echo "<input type='hidden' name='no_shipping' value='0'>";
		echo "<input type='hidden' name='no_note' value='1'>";
		echo "<input type='hidden' name='currency_code' value='USD'>";
//		echo "<input type='hidden' name='lc' value='AU'>";
		echo "<input type='hidden' name='bn' value='PP-BuyNowBF'>";
		echo "<input type='hidden' name='custom' value='".$custom."'>";
		echo "<input type='hidden' name='cancel_return' value='http://www.academicdating.net/payment_failed.php'>";
		echo "<input type='image' src='/buttons/".$gif."' border='0' name='submit' alt='Buy Credits'>";
		echo "<img alt='' border='0' src='https://www.paypal.com/en_AU/i/scr/pixel.gif' width='1' height='1'>";
		echo "</form>";

			/* try this method then don't need url in the paypal account */
/*		echo "<input type='hidden' name='cancel_return' value='http://www.academicdating.net/payment_successful.php'>";*/
		
	}

?>
