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

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

$groupid = Filter($_POST['groupId']);

if(is_numeric($groupid) && $groupid > 0){

	$check = mysql_query("SELECT type FROM groups_details WHERE id = '".$groupid."' LIMIT 1") or die(mysql_error());
	$exists = mysql_num_rows($check);

	if($exists > 0){

		$check2 = mysql_query("SELECT groupid FROM groups_memberships WHERE userid = '".$my_id."' AND groupid = '".$groupid."' LIMIT 1") or die(mysql_errors());
		$already_member = mysql_num_rows($check2);

		$memberships = mysql_evaluate("SELECT COUNT(*) FROM groups_memberships WHERE userid = '".$my_id."'");
		if($memberships > 9){
			echo "<p>\nTu es d&eacute;j&agrave; membre ou tu as des demandes en attente sur plus de 10 groupes.\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" id=\"group-action-ok\"><b>OK</b><i></i></a>\n</p>\n\n\n<div class=\"clear\"></div>";
			exit;			
		}

		if($already_member > 0){

			echo "<p>\nTu es d&eacute;j&agrave; membre de ce clan ou d'une demande d'adh&eacute;sion &agrave; ce clan a d&eacute;j&agrave; &eacute;t&eacute; r&eacute;alis&eacute;.\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" id=\"group-action-ok\"><b>OK</b><i></i></a>\n</p>\n\n\n<div class=\"clear\"></div>";
			exit;

		} else {

			$groupdata = mysql_fetch_assoc($check);
			$type = $groupdata['type'];
			$members = mysql_evaluate("SELECT COUNT(*) FROM groups_memberships WHERE groupid = '".$groupid."' AND is_pending = '0'");

			if($type == "0" || $type == "3"){ // we're free to join
				if($type == "0" && $members < 500 || $type == "3"){
					echo "<p>\nTu as rejoinds ce clan.\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" id=\"group-action-ok\"><b>OK</b><i></i></a>\n</p>\n\n\n<div class=\"clear\"></div>";
					mysql_query("INSERT INTO groups_memberships (userid,groupid,member_rank,is_current,is_pending) VALUES ('".$my_id."','".$groupid."','1','0','0')") or die(mysql_error());
					exit;
				} else {
					echo "<p>\nCe clan est plein.\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" id=\"group-action-ok\"><b>OK</b><i></i></a>\n</p>\n\n\n<div class=\"clear\"></div>";
					exit;
				}
			} elseif($type == "1"){ // we need to request join
				echo "<p>\nUne demande d'adh&eacute;sion &agrave; ce clan &agrave; d&eacute;ja &eacute;t&eacute; r&eacute;alis&eacute;. Le propri&eacute;taire du clan devra vous accepter avant que tu puisses faire partit de ce clan.\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" id=\"group-action-ok\"><b>OK</b><i></i></a>\n</p>\n\n\n<div class=\"clear\"></div>";
				mysql_query("INSERT INTO groups_memberships (userid,groupid,member_rank,is_current,is_pending) VALUES ('".$my_id."','".$groupid."','1','0','1')") or die(mysql_error());
				exit;
			} elseif($type == "2"){ // noone can join
				echo "<p>\nD&eacute;sol&eacute;, mais ce clan est ferm&eacute;, personne peut le rejoindre!\n</p>\n\n<p>\n<a href=\"#\" class=\"new-button\" id=\"group-action-ok\"><b>OK</b><i></i></a>\n</p>\n\n\n<div class=\"clear\"></div>";
				exit;
			}

		}

	} else {

		echo "1";
		exit;

	}

}

?>