<?php
#######################################################|#
#|                    HabboMCMS - TheRox
#|                    Powered by HoloCMS 
#|            Copyright 2011-2012 ALL RIGHTS RESERVED
#######################################################|#

require_once('../global.php');
require_once('../INC/class.session.php');

if(!session_is_registered(username)){ echo "<p>\nConnecte-toi avant.\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.close(); return false;\"><b>Terminer</b><i></i></a>\n</p>"; exit; }

$do = Filter($_GET['do']);

	// Make sure the user meets the requirements to buy a group. If not, this part
	// should cut off the script.
	
	if(getContent('allow-group-purchase') !== "0"){

			echo "<p id=\"purchase-result-error\">Achat du clan &eacute;chou&eacute;. Merci de r&eacute;essayer.</p>\n<div id=\"purchase-group-errors\">\n<p>\nLes clans ont &eacute;t&eacute;s d&eacute;sactiv&eacute;s temporairement par les administrateurs de l'hotel. Merci de r&eacute;essayer plus tard.<br />\n</p>\n</div>\n<p>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.close(); return false;\"><b>Terminer</b><i></i></a>\n</p>\n<div class=\"clear\"></div>"; exit;

	} elseif($myrow['credits'] < 20){

			echo "<p id=\"purchase-result-error\">Achat du clan &eacute;chou&eacute;. Merci de r&eacute;essayer.</p>\n<div id=\"purchase-group-errors\">\n<p>\nTu n'as pas assez de cr&eacute;dits. <a href=\"credits.php\">Cliques ici!</a><br />\n</p>\n</div>\n<p>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.close(); return false;\"><b>Terminer</b><i></i></a>\n</p>\n<div class=\"clear\"></div>"; exit;

	} else {

			$groups_owned = mysql_evaluate("SELECT COUNT(*) FROM groups_details WHERE ownerid = '".$my_id."' LIMIT 10");

			if($groups_owned > 10){
				echo "<p id=\"purchase-resu
				lt-error\">>Achat du clan &eacute;chou&eacute;. Merci de r&eacute;essayer.</p>\n<div id=\"purchase-group-errors\">\n<p>\nVous avez atteint la limite de groupe vous appartenant (3).<br />\n</p>\n</div>\n<p>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.close(); return false;\"><b>Terminer</b><i></i></a>\n</p>\n<div class=\"clear\"></div>"; exit;
			}

	}
	

	// The buy part. If the script has not been cut off yet, we should be ready to go.

	if(empty($do) || $do !== "purchase_confirmation"){

		echo "<p>\n<img src='./habbo-imaging/badge.php?badge=b0503Xs09114s05013s05015' border='0' align='right'>Prix: <b>10 Cr&eacute;dits.</b><br>Tu as: <b>10 Cr&eacute;dit(s).</b>\n</p>\n\n<p>\n<b>Nom du groupe:</b><br /><input type='text' name='name' id='group_name' value='' length='10' maxlength='25'>\n</p>\n\n<p>\n<b>Description du groupe:</b><br />\n<textarea name='description' id='group_description' maxlength='200'></textarea>\n</p>\n\n<p>\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.confirm(); return false;\"><b>Acheter ce groupe</b><i></i></a>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.close(); return false;\"><b>Annuler</b><i></i></a>\n</p>"; exit;

	} elseif($do == "purchase_confirmation"){

		$group_name = Filter($_POST['name']);
		$group_desc = Filter($_POST['description']);

		if(empty($group_name) || empty($group_desc)){

			echo "<p>\nRemplis tout les champs!\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.close(); GroupPurchase.open(); return false;\"><b>Retour</b><i></i></a>\n</p>"; exit;

		} else {

			if(strlen($group_name > 25) && !is_numeric($group_name)){

				echo "<p>\nLe nom que tu as choisi est trop long.\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.close(); GroupPurchase.open(); return false;\"><b>Retour</b><i></i></a>\n</p>"; exit;

			} elseif(strlen($group_desc > 200) && !is_numeric($group_desc)){

				echo "<p>\nLa description que tu as faite est trop longue.\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.close(); GroupPurchase.open(); return false;\"><b>Retour</b><i></i></a>\n</p>"; exit;

			} else {

				$check = mysql_query("SELECT id FROM groups_details WHERE name = '".$group_name."' LIMIT 1") or die(mysql_error());
				$already_exists = mysql_num_rows($check);

				if($already_exists > 0){

					echo "<p>\nUn clan avec ce nom existe d&eacute;ja.\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.close(); GroupPurchase.open(); return false;\"><b>Retour</b><i></i></a>\n</p>";

				} else {

					$orname = $group_name;
					$group_name = Filter($orname);
					$group_desc = Filter($group_desc);

					mysql_query("INSERT INTO groups_details (name,description,ownerid,created,badge,type) VALUES ('".$group_name."','".$group_desc."','".$my_id."','".$date_full."','b0503Xs09114s05013s05015','0')") or die(mysql_error());

					$check = mysql_query("SELECT id FROM groups_details WHERE ownerid = '".$my_id."' ORDER BY id DESC LIMIT 1") or die(mysql_error());
					$row = mysql_fetch_assoc($check);
					$group_id = $row['id'];

					mysql_query("INSERT INTO groups_memberships (userid,groupid,member_rank,is_current) VALUES ('".$my_id."','".$group_id."','2','0')") or die(mysql_error());
					mysql_query("UPDATE users SET credits = credits - 20 WHERE id = '".$my_id."' LIMIT 1") or die(mysql_error());
					//mysql_query("INSERT INTO cms_transactions (userid,descr,date,amount) VALUES ('".$my_id."','Group purchase','".$date_full."','-20')") or die(mysql_error());
					// @SendMUSData('UPRC' . $my_id);

					echo "<p>\n<b>Clan achet&eacute; !</b><br /><br /><img src='./habbo-imaging/badge-fill/b0503Xs09114s05013s05015.gif' border='0' align='left'>F&eacute;licitations! Tu es le cr&eacute;ateur du nouveau clan <b>".HabboCMSTEXT($orname)."</b>.<br /><br />CLICK <a href='group_profile.php?id=".$group_id."'>ICI</a> pour voir et modifier ton nouveau clan ou clique sur Terminer pour revenir &agrave; la page pr&eacute;c&eacute;dente.\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.close(); return false;\"><b>Terminer</b><i></i></a>\n</p>";

				}

			}

		}

	} else {

		echo "<p>\nUne erreur inconnue s'est produite! Merci de r&eacute;essayer dans quelques minutes.\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" onclick=\"GroupPurchase.close(); return false;\"><b>OK</b><i></i></a>\n</p>";

	}
	


?>



