<?php
// Gestion de l'inscription des élèves :
// gestion du choix du créneau en paramètre
$crd = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_crendef'");
$crd_info = mysqli_fetch_assoc($crd);
if($crd_info['valeur'] == "suivant") {
	// Message en cas d'absence de créneau :
	if($soluce==0&&@$droits!=3) {
		echo '</h2><p name="noprint">'.FREQ_HORERR2;
		if($individu==0) { echo '<br />'.PARAM_LINK1; }
		echo '</p>';
	}
	else if($soluce==1) {
		$num_cren = $_SESSION['num_cren'] + 1;
	}
}
else {
	$crd_select = "SELECT param.valeur, hor.hor_name, hor.num_creneau, hor.debut, hor.fin
	FROM (SELECT *
		FROM ".$prefix."_parametres 
		WHERE param_nom = 'freq_crendef'
	) param
	INNER JOIN ".$prefix."_horaires hor
	ON param.valeur = hor.hor_name";
	$crd = mysqli_query($bdd,$crd_select);
	$crd_info = mysqli_fetch_assoc($crd);
	$num_cren = $crd_info['num_creneau'];
}

$datemax1 = new DateTime($dday);
$dateplus = new DateTime($dday);
if($individu==0) {
	$freq_res_dureemax = @$_SESSION['freq_res_dureemax'];
}
else {
	$freq_res_dureemax = $freq_reseleve_dureemax['valeur'];
}
$calplus=0;
$inttest = intval($freq_res_dureemax);
$minutesm = 0;

if($inttest!=0) {
	for($cal=1;$cal<=($inttest-1+$calplus);$cal++) {
		$dateplus = new DateTime($dday);
		$dateplus->modify('+'.$cal.' day');	
		if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".date_format($dateplus, 'Y-m-d')."' AND statut = 0"))>0) {
			$calplus+=1;
		}
	}
	$datemax1->modify('+'.($inttest-1+$calplus).' day');
}
else {
	if (@preg_match('/^[a-zA-Z]/', $freq_res_dureemax)) {
        // La variable commence par une lettre
	    $startDay = strtolower(substr($freq_res_dureemax, 0, 3));
		$endDay = strtolower(substr($freq_res_dureemax, 3));
		$currentDay = strtolower(date('D')); // Jour actuel en minuscules

		// Nombre de jours jusqu'au jour d'arrivée à partir du jour actuel
		$currentDayNumber = $daysOfWeek[$currentDay];
		$endDayNumber = $daysOfWeek[$endDay];

		$diffDays = ($endDayNumber - $currentDayNumber + 7) % 7;

		// Ajouter la différence de jours au $datemax1
		$datemax1->modify('+' . $diffDays . ' day');
    } else {
        $datemax1->modify('+'.($inttest-1+$calplus).' day');
    }
}

$esp0 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first'"));

$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = '".$num_cren."'");
$hor_nb = mysqli_num_rows($hor);
$hor_info = mysqli_fetch_assoc($hor);

if($hor_nb==0) {
	$num_cren = 1;
	$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = '".$num_cren."'");
	$hor_nb = mysqli_num_rows($hor);
	$hor_info = mysqli_fetch_assoc($hor);
}

// Eviction des créneaux fermés
if($freq_gestion_jourscren['valeur']==0) {
	while(substr($hor_info['hor_resa'],0,3)!="001") {
		$num_cren++;
		$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = '".$num_cren."'");
		$hor_nb = mysqli_num_rows($hor);
		if($hor_nb==0) {
			$num_cren = 1;
			$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = '".$num_cren."'");
		}
		$hor_info = mysqli_fetch_assoc($hor);
		break;
	}
}

// Gestion d'un créneau pour jour suivant, voire plus si jour fermé
if(isset($_SESSION['num_cren'])) {
	if($num_cren<=$_SESSION['num_cren']) {
		$date->modify('+1 day');
	}
}
$day = date_format($date, 'Y-m-d');
$jourcheck_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".$day."'"));
while($jourcheck_info['statut']==0) {
	$date->modify('+1 day');
	break;
}
// Gestion des demi-journées fermées
$demijournees=0;
do {
	$jourweek = date_format($date, 'w');
	$jourweektab = array('di','lu','ma','me','je','ve','sa');
	$crenres = $hor_info['hor_name'];
	$_SESSION['crenres'] = $crenres;
	$deb_cren = substr($crenres, 0, 1);
	if($deb_cren=="M") {
		$testdem = '_am';
	}
	else if($deb_cren=="A"||$deb_cren=="R") {
		$testdem = '_pm';
	}
	else {
		if(substr($hor_info['debut'],0,2)<12) { $testdem = '_am'; }
  		else if(substr($hor_info['debut'],0,2)>=12) { $testdem = '_pm'; }
	}
	if($deb_cren!="P") {
		$jourweekcheck = mysqli_query($bdd,"SELECT * FROM ".$prefix."_hordemis WHERE demijour = '".$jourweektab[$jourweek].$testdem."' AND statut = 0");
		if(mysqli_num_rows($jourweekcheck)==1) {
			if($crd_info['valeur'] == "suivant") {
				$num_cren++;
				$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = '".$num_cren."'");
				$hor_nb = mysqli_num_rows($hor);
				if($hor_nb==0) {
					$num_cren = 1;
					$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = '".$num_cren."'");
				}
				$hor_info = mysqli_fetch_assoc($hor);
			}
			else { $date->modify('+1 day'); }
		}
		else { $demijournees = 1;}
	}
	else { $demijournees = 1;}
} while ($demijournees==0);

// Gestion d'une annulation individuelle
if($individu==1) {
	if($reserv_could['valeur']==1) {
		echo '<p class="signal">'.FREQ_RESA4.'</p>';
	}
}
if($individu==1) { $ruser_id = 0; }
else { $ruser_id = $_SESSION['user_id']; }

if(@$droits!=3||$individu==1) {
	// Pour gérer ou non les exclusions
	$excluopt_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt'"));
	$limitopt_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt_limit'"));
	if($individu==0||($individu==1&&$reseleve_creneau['valeur']==1)) {
		// Affichage du créneau prévu dans les paramètres pour la réservation
		echo'<p name="noprint"><b>'.RESA7.' ';
		echo date_format($date, 'd/m/Y');
		echo ' - ';
		echo MOMENT[$deb_cren];
		$fin_cren = substr($crenres, 1, 1);
		echo ''.$fin_cren.' '.FROM.' ';
		$deb = $hor_info['debut'];
		$dh = substr($deb, 0, 2);
		$dm = substr($deb, 3, 2);
		echo ''.$dh.'h'.$dm.' '.TO.' ';
		$fin = $hor_info['fin'];
		$fh = substr($fin, 0, 2);
		$fm = substr($fin, 3, 2);
		echo ''.$fh.'h'.$fm.'</b></p>';
	}

	// Si choix du créneau possible :
	$crenchoix = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_crenchoix'");
	$crenchoix_info = mysqli_fetch_assoc($crenchoix);

	// Gestion du forcing en cas d'exclusion ou de refus de réservation, ou de dépassement de résas
	if(isset($_POST['insr_forcing'])) {
		for($i=1;$i<=$_SESSION['forcing_res'];$i++) {
			if(isset($_POST['forcr_check'.$i])) {
				if($espopt['valeur']==1) {
					mysqli_query($bdd,"INSERT INTO ".$prefix."_freqres (eleves_id, date_freqres, code_freqres, hor_name, espace_id, code_activ, user_id, statut, division, sexe) VALUES ('".$_SESSION['forcr'.$i][0]."', '".$_SESSION['forcr'.$i][1]."', '".$_SESSION['forcr'.$i][2]."', '".$_SESSION['forcr'.$i][3]."', '".$_SESSION['forcr'.$i][6]."', '".$_SESSION['forcr'.$i][4]."', '".$ruser_id."', '0', '".$_SESSION['forcr'.$i][9]."', '".$_SESSION['forcr'.$i][10]."')");
				}
				else {
					mysqli_query($bdd,"INSERT INTO ".$prefix."_freqres (eleves_id, date_freqres, code_freqres, hor_name, code_activ, user_id, espace_id, statut, division, sexe) VALUES ('".$_SESSION['forcr'.$i][0]."', '".$_SESSION['forcr'.$i][1]."', '".$_SESSION['forcr'.$i][2]."', '".$_SESSION['forcr'.$i][3]."', '".$_SESSION['forcr'.$i][4]."', '".$ruser_id."', '0', '0', '".$_SESSION['forcr'.$i][9]."', '".$_SESSION['forcr'.$i][10]."')");
				}
			}
		}
		if($individu==1) {
			?><script language="Javascript">
			<!--
			document.location.replace("reserver.php");
			// -->
			</script><?php
		}
	}

	$_SESSION['forcing_res']=0;
	// Gestion de l'enregistrement des réservations dans la BDD
	if(isset($_POST['reserve'])&&(@$_POST['eleve']!=""||@$_POST['classe']!=""||@$specific_res==1)) {
		$stop_ses = 0;
		if(($crenchoix_info['valeur']==1&&$_POST['datepick']!=""&&$_POST['crenpicker']!="")) {
			$dayevent = date('Ymd', strtotime(str_replace("/","-",$_POST['datepick'])));
			$day3 = date('D', strtotime(str_replace("/","-",$_POST['datepick'])));
			$codeevent = $_POST['crenpicker'];
			$crennum = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT hor_id, hor_resa, quota_resa FROM ".$prefix."_horaires WHERE hor_name = '".$codeevent."'"));
			if($freq_gestion_jourscren['valeur']==0) {
				$quota_resa = intval(substr($crennum['quota_resa'],0,3));
				$hor_resaok = 1;
			}
			else {
				$quota_resa = intval(substr($crennum['quota_resa'], strpos($crennum['quota_resa'], $day3) + strlen($day3), 3));
				if(intval(substr($crennum['hor_resa'], strpos($crennum['hor_resa'], $day3) + strlen($day3), 3))=="001") {
					$hor_resaok = 1;
				}
				else {
					$hor_resaok = 0;
				}
			}
			if($espopt['valeur']==1&&isset($_POST['selectespace'])) {
				$crenopen_verif = mysqli_query($bdd,"SELECT brik.code, brik.close FROM ".$prefix."_edtbrik brik INNER JOIN ".$prefix."_edtevent ev ON ev.brik_id = brik.brik_id LEFT JOIN ".$prefix."_espaces_event ee ON ee.uid = ev.uid WHERE brik.close = '1' AND ev.dtstart LIKE '".$dayevent."%' AND ev.hor_id = '".$crennum['hor_id']."' AND ee.espace_id = '".$_POST['selectespace']."'");
				$crenseance_verif = mysqli_query($bdd,"SELECT brik.code, brik.seance FROM ".$prefix."_edtbrik brik INNER JOIN ".$prefix."_edtevent ev ON ev.brik_id = brik.brik_id LEFT JOIN ".$prefix."_espaces_event ee ON ee.uid = ev.uid WHERE brik.seance = '1' AND ev.dtstart LIKE '".$dayevent."%' AND ev.hor_id = '".$crennum['hor_id']."' AND ee.espace_id = '".$_POST['selectespace']."'");
			}
			else {
				$crenopen_verif = mysqli_query($bdd,"SELECT brik.code, brik.close FROM ".$prefix."_edtbrik brik INNER JOIN ".$prefix."_edtevent ev ON ev.brik_id = brik.brik_id WHERE brik.close = '1' AND ev.dtstart LIKE '".$dayevent."%' AND ev.hor_id = '".$crennum['hor_id']."'");
				$crenseance_verif = mysqli_query($bdd,"SELECT brik.code, brik.seance FROM ".$prefix."_edtbrik brik INNER JOIN ".$prefix."_edtevent ev ON ev.brik_id = brik.brik_id WHERE brik.seance = '1' AND ev.dtstart LIKE '".$dayevent."%' AND ev.hor_id = '".$crennum['hor_id']."'");
			}
		}
		else if($individu==1&&$reseleve_creneau['valeur']==0) {
			$stop_ses = 2;
		}
		else {
			$dayevent = date_format($date, 'Ymd');
			$day3 = date_format($date, 'D');
			$codeevent = $_SESSION['crenres'];
			$crennum = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT hor_id, hor_resa, quota_resa FROM ".$prefix."_horaires WHERE hor_name = '".$codeevent."'"));
			if($freq_gestion_jourscren['valeur']==0) {
				$quota_resa = intval(substr($crennum['quota_resa'],0,3));
				$hor_resaok = 1;
			}
			else {
				$quota_resa = intval(substr($crennum['quota_resa'], strpos($crennum['quota_resa'], $day3) + strlen($day3), 3));
				if(intval(substr($crennum['hor_resa'], strpos($crennum['hor_resa'], $day3) + strlen($day3), 3))=="001") {
					$hor_resaok = 1;
				}
				else {
					$hor_resaok = 0;
				}
			}
			if($espopt['valeur']==1&&isset($_POST['selectespace'])) {
				$crenopen_verif = mysqli_query($bdd,"SELECT brik.code, brik.close FROM ".$prefix."_edtbrik brik INNER JOIN ".$prefix."_edtevent ev ON ev.brik_id = brik.brik_id LEFT JOIN ".$prefix."_espaces_event ee ON ee.uid = ev.uid WHERE brik.close = '1' AND ev.dtstart LIKE '".$dayevent."%' AND ev.hor_id = '".$crennum['hor_id']."' AND ee.espace_id = '".$_POST['selectespace']."'");
				$crenseance_verif = mysqli_query($bdd,"SELECT brik.code, brik.seance FROM ".$prefix."_edtbrik brik INNER JOIN ".$prefix."_edtevent ev ON ev.brik_id = brik.brik_id LEFT JOIN ".$prefix."_espaces_event ee ON ee.uid = ev.uid WHERE brik.seance = '1' AND ev.dtstart LIKE '".$dayevent."%' AND ev.hor_id = '".$crennum['hor_id']."' AND ee.espace_id = '".$_POST['selectespace']."'");				
			}
			else {
				$crenopen_verif = mysqli_query($bdd,"SELECT brik.code, brik.close FROM ".$prefix."_edtbrik brik INNER JOIN ".$prefix."_edtevent ev ON ev.brik_id = brik.brik_id WHERE brik.close = '1' AND ev.dtstart LIKE '".$dayevent."%' AND ev.hor_id = '".$crennum['hor_id']."'");
				$crenseance_verif = mysqli_query($bdd,"SELECT brik.code, brik.seance FROM ".$prefix."_edtbrik brik INNER JOIN ".$prefix."_edtevent ev ON ev.brik_id = brik.brik_id WHERE brik.seance = '1' AND ev.dtstart LIKE '".$dayevent."%' AND ev.hor_id = '".$crennum['hor_id']."'");
			}
		}		
		if($individu==1) {
			@$_SESSION['max_reserv']++;
			if($reserv_could['valeur']==1&&@$_SESSION['max_reserv']>50) {
				$msg_ses = '<p class="signal">'.FREQ_RESA5.'</p>';
				$stop_ses = 1;
			}
			else if($reserv_could['valeur']==2&&@$_SESSION['max_reserv']>500) {
				$msg_ses = '<p class="signal">'.FREQ_RESA6.'</p>';
				$stop_ses = 1;
			}
		}
		if($individu==1&&$stop_ses==1) {
			echo $msg_ses;
		}
		else if($individu==1&&$stop_ses==2) {
			echo '<p class="signal">'.FREQ_CREN1.'</p>';
		}
		else if (mysqli_num_rows($crenopen_verif)>0||$hor_resaok==0||($individu==1&&mysqli_num_rows($crenseance_verif)>0&&$nodiv['valeur']==0)) {
			echo '<p class="signal">'.FREQ_CREN2.'</p>';
		}
		else {
			$day = date('Y-m-d', strtotime(str_replace("/","-",$_POST['datepick'])));
			//sélection de l'élève
			if($individu==1) {
				if($reserv_could['valeur']==1) {
					include('./db/proc/elevecode_select.php');
				}
			}
			else {
				include('./db/proc/eleve_select.php');
			}
			if (!$eleve_info && empty($_POST['classe'])) {
				echo '<p class="signal">'.NOELEVREC.'</p>';
			}
			else if(($individu==1&&$freq_reseleve_dureemax['valeur']!='0'&&$day>date_format($datemax1, 'Y-m-d')||($individu==0&&$freq_res_dureemax!='0'&&$day>date_format($datemax1, 'Y-m-d')))) {
				echo '<p class="signal" name="noprint" style="text-align:start;">'.FREQ_RESA7.' '.TO3.' '.date_format($datemax1, 'd/m/Y').' '.INCLUS.'.</p>';
			}
			else {
				if($crenchoix_info['valeur']==1&&$_POST['datepick']!=""&&$_POST['crenpicker']!="") {
					$_SESSION['datepickres'] = $_POST['datepick'];
					$_SESSION['crenpickerres'] = $_POST['crenpicker'];
					$day = date('Y-m-d', strtotime(str_replace("/","-",$_POST['datepick'])));
					$code_freq = $day.$_POST['crenpicker'];
					$creneau = $_POST['crenpicker'];
				}
				else {
					if(isset($_SESSION['datepickres'])) {
						unset($_SESSION['datepickres']);
					}
					if(isset($_SESSION['crenpickerres'])) {
						unset($_SESSION['crenpickerres']);
					}
					$day = date_format($date, 'Y-m-d');
					$code_freq = $day.$_SESSION['crenres'];
					$creneau = $_SESSION['crenres'];
				}
				if(($individu==0&&$res_activ['valeur']==1)||($individu==1&&$reseleve_activ['valeur']==1)) { $code_activ = @$_POST['code_activ']; $_SESSION['code_activ'] = @$code_activ; }
				else { $code_activ=""; }
				// Vérification du jour
				$jourcheck = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".$day."' AND statut = 0"));
				$jourweek = date('w', strtotime($day));
				$jourweektab = array('di','lu','ma','me','je','ve','sa');
				$deb_cren = substr($creneau, 0, 1);
				$badcren=0;
				if($deb_cren=="M") {
					$testdem = '_am';
				}
				else if($deb_cren=="A"||$deb_cren=="R") {
					$testdem = '_pm';
				}
				if($deb_cren!="P") {
					$jourweekcheck = mysqli_query($bdd,"SELECT * FROM ".$prefix."_hordemis WHERE demijour = '".$jourweektab[$jourweek].$testdem."' AND statut = 0");
					if(mysqli_num_rows($jourweekcheck)==1) {
						$badcren=1;
					}
				}
				// option de blocage sur events :
				$eventblock = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_resblockevent'"));
				if($eventblock['valeur']==1) {
					$jourstart = date('Ymd', strtotime($day));
					$horid = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT hor_id FROM ".$prefix."_horaires WHERE hor_name = '".$creneau."'"));
					$eventcheck = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent edtevent INNER JOIN ".$prefix."_userevent userevent ON edtevent.uid = userevent.uid WHERE user_id = 0 AND dtstart LIKE '".$jourstart."%' AND hor_id = '".$horid['hor_id']."'"));
					if($eventcheck>0) {
						$badcren=1;
					}
				}

				if($jourcheck==1) {
					echo '<p class="signal">'.FREQ_NORESA1.'</p>';
				}
				else if($badcren==1) {
					echo '<p class="signal">'.FREQ_NORESA2.' '.FREQ_NORESA21.'</p>';
				}
				else {
					if((@$classe_info_valeur==1&&@$_POST['classe']!="")) { 
						// pour inscription de classe
						$classe=1;
						$array = array_keys($_POST);
						unset($array[0]);
						unset($array['eleve']);
						foreach($array as $checked_id) {
							$eleve_id = $checked_id;
							if(is_int($eleve_id)) {
								$nb_verif = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqres WHERE eleves_id = '".$eleve_id."' AND code_freqres LIKE '".$code_freq."%' AND statut = '0'"));
								// Vérification qu'il puisse réserver :
								$nb_autorise = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqresref WHERE eleves_id = '".$eleve_id."' AND todate >= '".$day."'"));
								// Vérification de non exclusion :
								if($excluopt_info['valeur']==1){
									$exc_check = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_exclusions WHERE eleves_id ='".$eleve_id."' AND date_fin >= '".$day."'"));
								}
								else { $exc_check=0; }
								if($limitopt_info['valeur']==1&&$espopt['valeur']==1){
									$limit_check = mysqli_query($bdd,"SELECT * FROM ".$prefix."_limits limits LEFT JOIN ".$prefix."_limitstxt limt ON limits.limittxt_id = limt.limittxt_id WHERE limits.eleves_id ='".$eleve_id."' AND limits.date_fin >= '".$day."' AND limt.espace_id = '".$_POST['selectespace']."'");
									$lim_check = mysqli_num_rows($limit_check);
									if($lim_check>0) {
										$lim_details= mysqli_fetch_assoc($limit_check);
									}
								}
								else { $lim_check=0; }
								$nb_check=0;
								if($freqres_max_origin['valeur']=='1'&&isset($_SESSION['num_cren'])) {
									$nb_check_total = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqres fr INNER JOIN ".$prefix."_horaires hor ON hor.hor_name = fr.hor_name WHERE eleves_id = '".$eleve_id."' AND statut = '0' AND ((date_freqres = '".date('Y-m-d')."' AND num_creneau >= '".$_SESSION['num_cren']."') OR date_freqres > '".date('Y-m-d')."')"));
									$nb_check_day = 0;
								    if (isset($freq_res_nbmaxday['valeur']) && intval($freq_res_nbmaxday['valeur']) > 0) {
								        $nb_check_day = mysqli_num_rows(mysqli_query(
								            $bdd,
								            "SELECT * FROM ".$prefix."_freqres fr
								             INNER JOIN ".$prefix."_horaires hor ON hor.hor_name = fr.hor_name
								             WHERE eleves_id = '".$eleve_id."'
								             AND (statut = '0' OR statut = '2') 
								             AND date_freqres = '".$day."'"
								        ));
								    }
								    if ($freqres_max['valeur']!='0'&&$nb_check_total >= $freqres_max['valeur']) {
									    echo '<p class="signal">'.FREQ_NORESA9.'.</p>';
									    $nb_check = 1;
									}
									else if ($freq_res_nbmaxday['valeur']!='0'&&isset($freq_res_nbmaxday['valeur']) && $nb_check_day >= intval($freq_res_nbmaxday['valeur'])) {
									    echo '<p class="signal">'.FREQ_NORESA9.FREQ_NORESA91.'.</p>';
									    $nb_check = 1;
									}
									else {
								        $nb_check = 0; // autorisé
								    }
								}
								$elevinf = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT division, sexe FROM ".$prefix."_eleves WHERE eleves_id = '".$eleve_id."'"));
								$eleve_div = $elevinf['division'];
								$eleve_sex = $elevinf['sexe'];
								if($nb_verif==0&&$nb_autorise==0&&$exc_check==0&&$lim_check==0&&$nb_check==0) {
									if($espopt['valeur']==1) { mysqli_query($bdd,"INSERT INTO ".$prefix."_freqres (eleves_id, date_freqres, code_freqres, hor_name, espace_id, code_activ, user_id, statut, division, sexe) VALUES ('".$eleve_id."', '".$day."', '".$code_freq."', '".$creneau."', '".$_POST['selectespace']."', '".$code_activ."', '".$ruser_id."', '0', '".$eleve_div."', '".$eleve_sex."')"); }
									else { mysqli_query($bdd,"INSERT INTO ".$prefix."_freqres (eleves_id, date_freqres, code_freqres, hor_name, code_activ, user_id, espace_id, statut, division, sexe) VALUES ('".$eleve_id."', '".$day."', '".$code_freq."', '".$creneau."', '".$code_activ."', '".$ruser_id."', '0', '0', '".$eleve_div."', '".$eleve_sex."')"); }
								}
								else if($nb_verif==0&&($nb_autorise==1||$exc_check==1||$lim_check>0||$nb_check>0)) {
									if(isset($_SESSION['forcing_res'])) { 
										$_SESSION['forcing_res']++; }
									else { $_SESSION['forcing_res'] = 1; }
									$_SESSION['forcr'.$_SESSION['forcing_res']]=array();
									array_push($_SESSION['forcr'.$_SESSION['forcing_res']], $eleve_id, $day, $code_freq, $creneau, $code_activ);
									if($nb_autorise==1) { array_push($_SESSION['forcr'.$_SESSION['forcing_res']], 'pb_a'); }
									else if($exc_check==1) { array_push($_SESSION['forcr'.$_SESSION['forcing_res']], 'pb_e'); }
									else if($nb_check==1) { array_push($_SESSION['forcr'.$_SESSION['forcing_res']], 'pb_f'); }
									else if($lim_check>0) { 
										array_push($_SESSION['forcr'.$_SESSION['forcing_res']], 'pb_l');
										$_SESSION['forcr'.$_SESSION['forcing_res']][7] = $lim_details['limits_txt'];
										$_SESSION['forcr'.$_SESSION['forcing_res']][8] =  $lim_details['espace_id'];
									}									
									if($espopt['valeur']==1) { 
										$_SESSION['forcr'.$_SESSION['forcing_res']][6] = $_POST['selectespace'];
									}
									$_SESSION['forcr'.$_SESSION['forcing_res']][9] =  $eleve_div;
									$_SESSION['forcr'.$_SESSION['forcing_res']][10] =  $eleve_sex;
								}
							}
						}
					}
					else {
						if($individu==1&&$freqres_raison_opt['valeur']=='1') {
							$raison_text = mysqli_real_escape_string($bdd,$_POST['raison_text']);
						}
						else { $raison_text = ''; }
						$minutesmax = intval($freq_res_minutesmax['valeur']); // délai en minutes
						// Récupération de l'heure de début du créneau
					    $req_cren = mysqli_query($bdd, "SELECT debut, fin FROM ".$prefix."_horaires WHERE hor_name = '".$creneau."'");
					    if ($req_cren && mysqli_num_rows($req_cren) > 0) {
					        $data_cren = mysqli_fetch_assoc($req_cren);
					        $debut_ts = strtotime($day . ' ' . $data_cren['debut']);
    						$fin_ts   = strtotime($day . ' ' . $data_cren['fin']);
					        $now = time();
					        $diff_minutes = ($debut_ts - $now) / 60;
					        $duree_creneau = ($fin_ts - $debut_ts) / 60;

					        if ($now >= $fin_ts) {
						        echo '<p class="signal">'.FREQ_NORESA2.'</p>';
						        $minutesm = 1;
						    }
					        // Si on est dans la période bloquée (moins de X minutes avant le créneau ou pendant)
					        if ($minutesmax > 0&&$individu=='1') {
						        if ($diff_minutes <= $minutesmax && $now < $fin_ts) {
						            // tu peux adapter -60 si tes créneaux durent 1h
						            echo '<p class="signal">'.FREQ_NORESA11.'</p>';
						            $minutesm = 1;
						        }
						    }
						}
						// Vérification qu'il ne soit pas déjà inscrit :
						$verif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqres WHERE eleves_id = '".$eleve_id."' AND code_freqres LIKE '".$code_freq."%' AND statut ='0'");
						$nb_verif = mysqli_num_rows($verif);
						// Vérification qu'il puisse réserver :
						$autorise = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqresref WHERE eleves_id = '".$eleve_id."'");
						$nb_autorise = mysqli_num_rows($autorise);
						if(isset($_POST['selectespace'])) {  
							$_SESSION['espace_id']=$_POST['selectespace'];
						}
						// Gestion du seuil :
						if($espopt['valeur']==0||($individu==1&&@$reseleve_esp['valeur']==0)) {
							$reserva_select = "SELECT freq.eleves_id, freq.code_freqres, freq.code_activ, eleves.nom, eleves.prenom, eleves.division, activites.activ_id, activites.code_activ, activites.nom_activ  
							FROM (SELECT * 
								FROM ".$prefix."_freqres 
								WHERE code_freqres LIKE '".$code_freq."%' AND statut = '0' 
							) freq 
							INNER JOIN ".$prefix."_eleves eleves
							ON freq.eleves_id = eleves.eleves_id
							LEFT JOIN ".$prefix."_freqactiv activites
							ON activites.code_activ = freq.code_activ
							ORDER BY eleves.division ASC, eleves.nom DESC, eleves.prenom ASC";
							$reserva = mysqli_query($bdd,$reserva_select);
							$nb_reserva = mysqli_num_rows($reserva);
							$insnb = mysqli_num_rows(mysqli_query($bdd,"SELECT freq_id FROM ".$prefix."_freq WHERE date_freq = '".substr($code_freq,0,10)."' AND hor_name = '".substr($code_freq,-2)."'"));
							if($quota_resa==0) {
								$srn = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilnbr'");
								$srn_info = mysqli_fetch_assoc($srn);
							}
							else {
								$srn_info['valeur'] = $quota_resa;
							}
							$srr = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilreacr'");
							$srr_info = mysqli_fetch_assoc($srr);
						}
						else {
							if($_POST['selectespace']==0) {
								$esp_var = "AND (espace_id = '0' OR espace_id IS NULL) ";
							}
							else { $esp_var = "AND espace_id = '".$_POST['selectespace']."'"; }
							$reserva_select = "SELECT freq.eleves_id, freq.code_freqres, espaces.espace_nom, espaces.espace_id, eleves.nom, eleves.prenom, eleves.division, activites.activ_id, activites.code_activ, activites.nom_activ 
							FROM (SELECT * 
								FROM ".$prefix."_freqres 
								WHERE statut = '0' AND code_freqres LIKE '".$code_freq."%' ".$esp_var."
							) freq 
							INNER JOIN ".$prefix."_eleves eleves
							ON freq.eleves_id = eleves.eleves_id
							LEFT JOIN ".$prefix."_espaces espaces
							ON espaces.espace_id = freq.espace_id
							LEFT JOIN ".$prefix."_freqactiv activites
							ON activites.code_activ = freq.code_activ
							ORDER BY eleves.division ASC, eleves.nom DESC, eleves.prenom ASC";
							$reserva = mysqli_query($bdd,$reserva_select);
							$nb_reserva = mysqli_num_rows($reserva);
							$insnb = mysqli_num_rows(mysqli_query($bdd,"SELECT freq_id FROM ".$prefix."_freq WHERE date_freq = '".substr($code_freq,0,10)."' AND hor_name = '".substr($code_freq,-2)."'"));
							if($_POST['selectespace']==0) {
								if($quota_resa==0) {
									$srn = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilnbr'");
									$srn_info = mysqli_fetch_assoc($srn);
								}
								else {
									$srn_info['valeur'] = $quota_resa;
								}
								$srr = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilreacr'");
								$srr_info = mysqli_fetch_assoc($srr);
							}
							else {
								$srn = mysqli_query($bdd,"SELECT seuilnbr FROM ".$prefix."_espaces WHERE espace_id = '".$_POST['selectespace']."'");
								$srn_info = mysqli_fetch_assoc($srn);
								$srr = mysqli_query($bdd,"SELECT seuilreacr FROM ".$prefix."_espaces WHERE espace_id = '".$_POST['selectespace']."'");
								$srr_info = mysqli_fetch_assoc($srr);
								$srn_info['valeur'] = $srn_info['seuilnbr'];
								$srr_info['valeur'] = $srr_info['seuilreacr'];
							}
						}
						if($nb_verif>0&&$individu==1&&$reserv_could['valeur']==1) {
							mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET statut = '1' WHERE eleves_id = '".$eleve_id."' AND code_freqres = '".$code_freq."'");
							echo '<p class="signal">'.FREQ_RESA8.' '.$sprenom.' '.$snom.' : '.$day.' '.ONCREN.' '.$creneau.'.</p>';				
						}
						else if($srn_info['valeur']==0||($nb_reserva+$insnb)<$srn_info['valeur']) {
							// Vérification de non exclusion :
							if($excluopt_info['valeur']==1){
								$exc_check = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_exclusions WHERE eleves_id ='".$eleve_id."' AND date_fin >= '".$day."'"));
							}
							else { $exc_check=0; }
							if($limitopt_info['valeur']==1&&$espopt['valeur']==1){
								$limit_check = mysqli_query($bdd,"SELECT * FROM ".$prefix."_limits limits LEFT JOIN ".$prefix."_limitstxt limt ON limits.limittxt_id = limt.limittxt_id WHERE limits.eleves_id ='".$eleve_id."' AND limits.date_fin >= '".$day."' AND limt.espace_id = '".@$_POST['selectespace']."'");
								$lim_check = mysqli_num_rows($limit_check);
								if($lim_check>0) {
									$lim_details= mysqli_fetch_assoc($limit_check);
								}
							}
							else { $lim_check=0; }
							$nb_check=0;
							if($freqres_max_origin['valeur']=='1'&&isset($_SESSION['num_cren'])) {
								$nb_check_total = mysqli_num_rows(mysqli_query(
						        $bdd,
						        "SELECT * FROM ".$prefix."_freqres fr
						         INNER JOIN ".$prefix."_horaires hor ON hor.hor_name = fr.hor_name
						         WHERE eleves_id = '".$eleve_id."'
						         AND statut = '0'
						         AND (
						             (date_freqres = '".date('Y-m-d')."' AND num_creneau >= '".$_SESSION['num_cren']."')
						             OR date_freqres > '".date('Y-m-d')."'
						         )"
							    ));
								$nb_check_day = 0;
							    if (isset($freq_res_nbmaxday['valeur']) && intval($freq_res_nbmaxday['valeur']) > 0) {
							        $nb_check_day = mysqli_num_rows(mysqli_query(
							            $bdd,
							            "SELECT * FROM ".$prefix."_freqres fr
							             INNER JOIN ".$prefix."_horaires hor ON hor.hor_name = fr.hor_name
							             WHERE eleves_id = '".$eleve_id."'
							             AND (statut = '0' OR statut = '2') 
							             AND date_freqres = '".$day."'"
							        ));
							    }
						    	if ($freqres_max['valeur']!='0'&&$nb_check_total >= $freqres_max['valeur']) {
								    echo '<p class="signal">'.FREQ_NORESA9.'.</p>';
								    $nb_check = 1;
								}
								else if ($freq_res_nbmaxday['valeur']!='0'&&isset($freq_res_nbmaxday['valeur']) && $nb_check_day >= intval($freq_res_nbmaxday['valeur'])) {
								    echo '<p class="signal">'.FREQ_NORESA9.FREQ_NORESA91.'.</p>';
								    $nb_check = 1;
								}
								else {
							        $nb_check = 0; // autorisé
							    }
							}							
							// Inscriptions sur réservation :
							if($nb_verif==0&&$nb_autorise==0&&$exc_check==0&&$lim_check==0&&$nb_check==0&&$minutesm==0) {
								if(($individu==0&&$espopt['valeur']==1)||($espopt['valeur']==1&&$individu==1&&@$reseleve_esp['valeur']==1)) {
									mysqli_query($bdd,"INSERT INTO ".$prefix."_freqres (eleves_id, date_freqres, code_freqres, hor_name, espace_id, code_activ, user_id, raison, statut, division, sexe) VALUES ('".$eleve_id."', '".$day."', '".$code_freq."', '".$creneau."', '".$_POST['selectespace']."', '".$code_activ."', '".$ruser_id."', '".$raison_text."', '0', '".$eleve_div."', '".$eleve_sex."')");
									if($individu==1) {
										if($reserv_could['valeur']==1) {
											echo '<p class="signal">'.FREQ_RESA9.' '.$sprenom.' '.$snom.' : '.$day.' '.ONCREN.' '.$creneau.' (';
											$selectespace = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT espace_nom FROM ".$prefix."_espaces WHERE espace_id = '".$_POST['selectespace']."'"));
											if(isset($selectespace['espace_nom'])) { echo $selectespace['espace_nom']; } else { echo $esp0['valeur']; }
											if($reseleve_activ['valeur']==1) { echo ' - '; $act_nom = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT nom_activ FROM ".$prefix."_freqactiv WHERE code_activ = '".$code_activ."'")); echo $act_nom['nom_activ']; }
											echo ').</p>';
										}
										else {
											?><script language="Javascript">
											<!--
											document.location.replace("reserver.php");
											// -->
											</script><?php
										}
									}
								}
								else { 
									mysqli_query($bdd,"INSERT INTO ".$prefix."_freqres (eleves_id, date_freqres, code_freqres, hor_name, code_activ, user_id, espace_id, raison, statut, division, sexe) VALUES ('".$eleve_id."', '".$day."', '".$code_freq."', '".$creneau."', '".$code_activ."', '".$ruser_id."', '0', '".$raison_text."', '0', '".$eleve_div."', '".$eleve_sex."')");
									if($individu==1) {
										if($reserv_could['valeur']==1) {
											echo '<p class="signal" name="noprint">'.FREQ_RESA9.' '.$sprenom.' '.$snom.' : '.$day.' '.ONCREN.' '.$creneau;
											if($reseleve_activ['valeur']==1) { echo ' ('; $act_nom = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT nom_activ FROM ".$prefix."_freqactiv WHERE code_activ = '".$code_activ."'")); echo $act_nom['nom_activ'].')'; }
											echo '.</p>';
										}
										else {
											?><script language="Javascript">
											<!--
											document.location.replace("reserver.php");
											// -->
											</script><?php
										}
									}
								}
							}
							else if($nb_verif==0&&($nb_autorise==1||$exc_check==1||$lim_check>0||$nb_check==1)) {
								if(isset($_SESSION['forcing_res'])) { 
									$_SESSION['forcing_res']++; }
								else { $_SESSION['forcing_res'] = 1; }
								$_SESSION['forcr'.$_SESSION['forcing_res']]=array();
								array_push($_SESSION['forcr'.$_SESSION['forcing_res']], $eleve_id, $day, $code_freq, $creneau, $code_activ);
								if($nb_autorise==1) { array_push($_SESSION['forcr'.$_SESSION['forcing_res']], 'pb_a'); }
								else if($exc_check==1) { array_push($_SESSION['forcr'.$_SESSION['forcing_res']], 'pb_e'); }
								else if($nb_check==1) { array_push($_SESSION['forcr'.$_SESSION['forcing_res']], 'pb_f'); }
								else if($lim_check>0) { 
									array_push($_SESSION['forcr'.$_SESSION['forcing_res']], 'pb_l'); 
									$_SESSION['forcr'.$_SESSION['forcing_res']][7] = $lim_details['limits_txt'];
									$_SESSION['forcr'.$_SESSION['forcing_res']][8] =  $lim_details['espace_id'];
								}
								if($espopt['valeur']==1) { 
									$_SESSION['forcr'.$_SESSION['forcing_res']][6] = @$_POST['selectespace'];
								}
							}
						}
						else if(($nb_reserva+$insnb)>=$srn_info['valeur']&&$srr_info['valeur']=='alert'&&$individu==0) {
							echo '<p class="signal" name="noprint">'.FREQ_NORESA3.'</p>';
							// Inscriptions sur réservation :
							if($nb_verif==0&&$nb_autorise==0) {
								if($espopt['valeur']==1) {
									mysqli_query($bdd,"INSERT INTO ".$prefix."_freqres (eleves_id, date_freqres, code_freqres, hor_name, espace_id, code_activ, user_id, raison, statut, division, sexe) VALUES ('".$eleve_id."', '".$day."', '".$code_freq."', '".$creneau."', '".$_POST['selectespace']."', '".$code_activ."', '".$ruser_id."', '".$raison_text."', '0', '".$eleve_div."', '".$eleve_sex."')");
									if($individu==1) {
										if($reserv_could['valeur']==1) {
											echo '<p class="signal" name="noprint">'.FREQ_RESA9.' '.$sprenom.' '.$snom.' : '.$day.' '.ONCREN.' '.$creneau.' (';
											$selectespace = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT espace_nom FROM ".$prefix."_espaces WHERE espace_id = '".$_POST['selectespace']."'"));
											if(isset($selectespace['espace_nom'])) { echo $selectespace['espace_nom']; } else { echo $esp0['valeur']; }
											if($reseleve_activ['valeur']==1) { echo ' - '; $act_nom = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT nom_activ FROM ".$prefix."_freqactiv WHERE code_activ = '".$code_activ."'")); echo $act_nom['nom_activ']; }
											echo ').</p>';
										}
										else {
											?><script language="Javascript">
											<!--
											document.location.replace("reserver.php");
											// -->
											</script><?php
										}
									}
								}
								else { 
									mysqli_query($bdd,"INSERT INTO ".$prefix."_freqres (eleves_id, date_freqres, code_freqres, hor_name, code_activ, user_id, espace_id, raison, statut, division, sexe) VALUES ('".$eleve_id."', '".$day."', '".$code_freq."', '".$creneau."', '".$code_activ."', '".$ruser_id."', '0', '".$raison_text."', '0', '".$eleve_div."', '".$eleve_sex."')");
									if($individu==1) {
										if($reserv_could['valeur']==1) {
											echo '<p class="signal" name="noprint">'.FREQ_RESA9.' '.$sprenom.' '.$snom.' : '.$day.' '.ONCREN.' '.$creneau;
											if($reseleve_activ['valeur']==1) { echo ' ('; $act_nom = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT nom_activ FROM ".$prefix."_freqactiv WHERE code_activ = '".$code_activ."'")); echo $act_nom['nom_activ'].')'; }
											echo '.</p>';
										}
										else {
											?><script language="Javascript">
											<!--
											document.location.replace("reserver.php");
											// -->
											</script><?php
										}
									}
								}
							}
							else if($nb_autorise>0) {
								echo '<p class="signal" name="noprint">'.FREQ_NORESA5.'</p>';
							}
						}
						else if($srr_info['valeur']=='stopp'||$individu==1) {
							echo '<p class="signal" name="noprint">'.FREQ_NORESA4.'</p>';
						}
					}
					if(isset($_SESSION['forcing_res'])&&$individu==0) {
						echo '<form method="POST" action="" name="ins_forcr" class="padd2">';
						for($i=1;$i<=$_SESSION['forcing_res'];$i++) {
							echo '<p class="signal"><input type="checkbox" id="forcr_check'.$i.'" name="forcr_check'.$i.'" checked /><label for="forcr_check'.$i.'"> ';
							if($_SESSION['forcr'.$i][5]=='pb_a') {
								echo FREQ_NORESA6;
								$forcing=0;
							}
							else if($_SESSION['forcr'.$i][5]=='pb_e') {
								echo FREQ_NORESA7;
								$forcing=0;
							}
							else if($_SESSION['forcr'.$i][5]=='pb_f') {
								echo FREQ_NORESA10;
								$forcing=0;
							}
							else if($_SESSION['forcr'.$i][5]=='pb_l') {
								echo FREQ_NORESA8.' ('.$_SESSION['forcr'.$i][7].' - ';
								if($_SESSION['forcr'.$i][8]==0) {
									echo ' '.$esp0['valeur'];
								}
								else {
									$espace_n = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE espace_id = '".$_SESSION['forcr'.$i][8]."'"));
									echo ' '.$espace_n['espace_nom']; 
								}
								echo ') : ';
								$forcing=1;
							}
							$el_aff = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT nom, prenom, division FROM ".$prefix."_eleves WHERE eleves_id = '".$_SESSION['forcr'.$i][0]."'"));
							echo $el_aff['prenom'].' '.$el_aff['nom'].' ('.$el_aff['division'].').</label></p>';
							$_SESSION['forcr'.$_SESSION['forcing_res']][9] = $eleve_div;
							$_SESSION['forcr'.$_SESSION['forcing_res']][10] = $eleve_sex;
						}
						if($_SESSION['forcing_res']>0) {
							if($individu==0||$forcing==1) {
								echo '<input class="bouton boutfreq" type="submit" name="insr_forcing" value="'.FREQ_FORCING1.'" /></form>';
							}
						}
					}
				}
			}
		}
	}

	//formulaire de suggestion
	echo '<div class="fleft"><form method="POST" name="inscript" action="" class="padd3">';
	
	if($crenchoix_info['valeur']==1||($individu==1&&$reseleve_creneau['valeur']==0)) {
		echo '<p name="noprint"><a href="javascript:;clearInterval(timer);" style="color:black;text-decoration:none;" style="color:black;text-decoration:none;" onmouseover="this.style.textDecoration=\'underline\';" onmouseout="this.style.textDecoration=\'none\';" onmousedown="toggleDiv(\'divcren\'); onclick = setInterval(timeinter, 240000);">
		<i class="fas fa-angle-double-right"></i> <b>Choisir un '; 
		if(($individu==1&&$reseleve_creneau['valeur']==1)||$individu==0) {
			echo 'autre ';
		}
		echo 'créneau</b></a></p>';
		echo '<div name="noprint" id="divcren"';
		if(isset($_SESSION['datepickres'])) {
			if($_SESSION['datepickres']!="") {
				echo '>';
			}
			else echo ' style="display:none;">';
		}
		else if($individu==1&&$reseleve_creneau['valeur']==0) {
			echo ' style="display:block;">';
		}
		else { echo ' style="display:none;">'; }
		if(($individu==1&&$freq_reseleve_dureemax['valeur']!='0')||($individu==0&&$freq_res_dureemax!='0')) {
			echo '<p name="noprint" style="text-align:start;">'.FREQ_RESA71.' '.TO3.' '.date_format($datemax1, 'd/m/Y').'.</p>';
		}
		echo '<input type="button" class="boutontab" id="clear" value="'.EMPTYC.'" />';?>
		<script>
			var element = document.getElementById("clear");
			element.addEventListener("click", function() {
			document.getElementById("datepick").value="";
			document.getElementById("crenpicker").value="";
		});
		</script><?php
		echo '<p name="noprint">'.DATE.DOTS.'<br> <input type="date" class="datepick inptxt" id="datepick" name="datepick" value="';
		if(isset($_SESSION['datepickres'])) {
			if($_SESSION['datepickres']!="") {
				echo $_SESSION['datepickres'];
			}
		}
		echo '"  autocomplete="off" aria-autocomplete="none" />
		<br><br>'.CREN1.DOTS.' <div class="box boxc"><select name="crenpicker" id="crenpicker" value="">';
		echo '<option></option>';
		if($freq_gestion_jourscren['valeur']==0) {
			$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE hor_resa LIKE '001%' ORDER BY num_creneau ASC");
		}
		else {
			$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE hor_resa LIKE '%001%' ORDER BY num_creneau ASC");
		}
		foreach($hor as $liste) {
			$cren = $liste['hor_name'];
			$deb_cren = substr($cren, 0, 1);
			echo '<option value="'.$liste['hor_name'].'"';
			if(isset($_SESSION['crenpickerres'])) { 
				if($_SESSION['crenpickerres']==$liste['hor_name']) {
					echo ' selected ';
				}
			}
			echo '>'.$liste['hor_name'].' - ';
			echo MOMENT[$deb_cren];
			$deb = $liste['debut'];
			$dh = substr($deb, 0, 2);
			$dm = substr($deb, 3, 2);
			echo ' '.FROM.' '.$dh.'h'.$dm.' '.TO.' ';
			$fin = $liste['fin'];
			$fh = substr($fin, 0, 2);
			$fm = substr($fin, 3, 2);
			echo ''.$fh.'h'.$fm.'';
			echo '</option>';
		}
		echo '</select></div></p></div>';
	}
	//Si possibilité d'inscrire une classe :
	if(@$classe_info_valeur==1&&$individu==0&&$onlycode_check_info['valeur']=='0') {
		$page_check = 'res';
		include('groupe_select.php');
	}

	echo '<table name="form"><tr>';
	if($individu==1) {
		if($reserv_could['valeur']==1) {
			echo '<td><label for="eleves">'.CODE3.DOTS.'</label></td>
			<td><input type="text" class="inptxt" name="eleve" size="20" value="" autocomplete="off" aria-autocomplete="none"></td>';
		}
	}	
	else {
		echo '<td><label for="eleves">';
			if($onlycode_check_info['valeur']=='1') {
				echo CODE3.DOTS;
			}
			else {
				echo NAMECH;
			}
			echo '</label></td>
			<td><input type="text" autocomplete="off" aria-autocomplete="none" class="inptxt" name="eleve" size="20" ';
			if($onlycode_check_info['valeur']=='0') {
				echo 'id="eleves"';
			}
			echo ' value=""></td>';
	}
	echo '</tr>';
	//choix de l'activité :
	if(($individu==0&&$res_activ['valeur']==1)||($individu==1&&$reseleve_activ['valeur']==1)) {
		$nb_activ = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqactiv WHERE activ_activ = 1"));
		if($nb_activ>0) {
			echo'<tr name="noprint"><td name="noprint">'.ACTIV.DOTS.'</td><td name="noprint">';
			$activd = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqactiv WHERE activ_activ = 1");
			while($liste = mysqli_fetch_assoc($activd)) {
				if($liste['code_activ']=="ZERO") { }
				else {
					echo '<input type="radio" name="code_activ" id="act'.$liste['code_activ'].'" value="'.$liste['code_activ'].'" '; 
					if(isset($_SESSION['code_activ'])) {
						if($_SESSION['code_activ']==$liste['code_activ']) {
							echo 'checked';
						}
					}
					else {
						if($liste['default_activ']==1) { echo 'checked'; }
					}
					echo ' /><label for="act'.$liste['code_activ'].'">'.$liste['nom_activ'].'</label>';
					if(@$_SESSION['align_activ']=='v'||$individu==1) { echo '<br>'; }
				}
			}
			echo '</td></tr>';
		}
	}
	//choix de l'espace :
	if($espopt['valeur']==1) {
		if($individu==1) { $espnb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE resa_dist = 1")); }
		else { $espnb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE resa = 1")); }
		if($espnb>0&&($individu==0||($individu==1&&@$reseleve_esp['valeur']==1))) {
			echo'<tr name="noprint"><td name="noprint">'.ZONE.DOTS.'</td><td name="noprint">';
			if($individu==1) { $espaces = mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE resa_dist = 1"); }
			else { $espaces = mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE resa = 1"); }
			$esp0_resa = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first_resamulti'"));
			$esp0_resadist = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first_resadistmulti'"));
			$esp0 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first'"));
			if(($individu==0&&$esp0_resa['valeur']==1)||($individu==1&&$esp0_resadist['valeur']==1)) {
				echo '<input type="radio" name="selectespace" id="esp0" value="0" checked /><label for="esp0">'.$esp0['valeur'].'</label>'; 
			}
			if(@$_SESSION['align_espace']=='v'||$individu==1) { echo '<br>'; }
			foreach($espaces as $espace) {
				if($espnb>6) { echo '<br>'; }
				echo '<input type="radio" name="selectespace" id="esp'.$espace['espace_id'].'" value="'.$espace['espace_id'].'" ';
				if(isset($_SESSION['espace_id'])&&$verresp['valeur']==0) {
					if($_SESSION['espace_id']==$espace['espace_id']) {
						echo 'checked';
					}
				}
				else {
					if(isset($_SESSION['espuser'])) {
						if($_SESSION['espuser']==$espace['espace_id']) { echo 'checked'; }
					}
					else if($espace['default_espace']==1) { echo 'checked'; }
					else if(($individu==0&&$esp0_resa['valeur']==0)||($individu==1&&$esp0_resadist['valeur']==0)) { echo ' checked '; }
				}
				echo ' /><label for="esp'.$espace['espace_id'].'">'.$espace['espace_nom'].'</label>';
				if(@$_SESSION['align_espace']=='v'||$individu==1) { echo '<br>'; }
			}
			echo '</td></tr>';
		}
	}
	echo '</table>';
	if($individu==1&&$freqres_raison_opt['valeur']=='1') {
		echo RESAREASON1.' <input class="inptxt" type="text" name="raison_text" maxlength="100" value="" />';
	}
	// Suppression de résidus de réservations (affinés au créneau) :
	$dday = date("Y-m-d");
	mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET statut = '4' WHERE date_freqres < DATE(CONVERT_TZ(UTC_TIMESTAMP(), 'UTC', '".$fuseau."')) AND statut = '0'");
	$esp0_resa = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first_resamulti'"));
	$esp0_resadist = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first_resadistmulti'"));
	if($espopt['valeur']==1) {
		if(($individu==0&&$esp0_resa['valeur']==1)||($individu==1&&$esp0_resadist['valeur']==1)||$espnb>0) {
			echo '<div class="fleft">
			<p class="paddvalid1">';
			$button = 1;
			if($individu==1&&$freqres_max['valeur']>0&&isset($_SESSION['num_cren'])) {
				$nb_verif_total = mysqli_num_rows(mysqli_query(
			        $bdd,
			        "SELECT * FROM ".$prefix."_freqres fr
			         INNER JOIN ".$prefix."_horaires hor ON hor.hor_name = fr.hor_name
			         WHERE eleves_id = '".$eleve_id."'
			         AND user_id = '0'
			         AND statut = '0'
			         AND (
			             (date_freqres = '".date('Y-m-d')."' AND num_creneau >= '".$_SESSION['num_cren']."')
			             OR date_freqres > '".date('Y-m-d')."'
			         )"
			    ));

			    $nb_verif_day = 0;
			    if (isset($freq_res_nbmaxday['valeur']) && intval($freq_res_nbmaxday['valeur']) > 0) {
			        $nb_verif_day = mysqli_num_rows(mysqli_query(
			            $bdd,
			            "SELECT * FROM ".$prefix."_freqres fr
			             INNER JOIN ".$prefix."_horaires hor ON hor.hor_name = fr.hor_name
			             WHERE eleves_id = '".$eleve_id."'
			             AND user_id = '0'
			             AND (statut = '0' OR statut = '2') 
			             AND date_freqres = '".$day."'"
			        ));
			    }

			    if ($nb_verif_total >= $freqres_max['valeur']) {
			        echo '<p class="signal">'.FREQ_NORESA9.'</p>'; // message existant : limite globale atteinte
			        $button = 0;
			    }
			    else if (isset($freq_res_nbmaxday['valeur']) && intval($freq_res_nbmaxday['valeur']) > 0 && $nb_verif_day >= intval($freq_res_nbmaxday['valeur'])) {
			        echo '<p class="signal">'.FREQ_NORESA9.FREQ_NORESA91.'.</p>';
			        $button = 1;
			    }
			    else {
			        $button = 1;
			    }
			}
			if($button==1) {
				echo '<input class="bouton inptxtf boutfreq" type="submit" value="'.RESABUTTON.'" name="reserve" />';
			}
			echo '</p>
			</div>';
		}
	}
	else {
		echo '<div class="fleft">
			<p class="paddvalid1">';
			$button = 1;
			if($individu==1&&$freqres_max['valeur']>0&&isset($_SESSION['num_cren'])) {
				$nb_verif_total = mysqli_num_rows(mysqli_query(
			        $bdd,
			        "SELECT * FROM ".$prefix."_freqres fr
			         INNER JOIN ".$prefix."_horaires hor ON hor.hor_name = fr.hor_name
			         WHERE eleves_id = '".$eleve_id."'
			         AND user_id = '0'
			         AND statut = '0'
			         AND (
			             (date_freqres = '".date('Y-m-d')."' AND num_creneau >= '".$_SESSION['num_cren']."')
			             OR date_freqres > '".date('Y-m-d')."'
			         )"
			    ));

			    $nb_verif_day = 0;
			    if (isset($freq_res_nbmaxday['valeur']) && intval($freq_res_nbmaxday['valeur']) > 0) {
			        $nb_verif_day = mysqli_num_rows(mysqli_query(
			            $bdd,
			            "SELECT * FROM ".$prefix."_freqres fr
			             INNER JOIN ".$prefix."_horaires hor ON hor.hor_name = fr.hor_name
			             WHERE eleves_id = '".$eleve_id."'
			             AND user_id = '0'
			             AND (statut = '0' OR statut = '2')
			             AND date_freqres = '".$day."'"
			        ));
			    }

			    if ($nb_verif_total >= $freqres_max['valeur']) {
			        echo '<p class="signal">'.FREQ_NORESA9.'</p>'; // message existant : limite globale atteinte
			        $button = 0;
			    }
			    else if (isset($freq_res_nbmaxday['valeur']) && intval($freq_res_nbmaxday['valeur']) > 0 && $nb_verif_day >= intval($freq_res_nbmaxday['valeur'])) {
			        echo '<p class="signal">'.FREQ_NORESA9.FREQ_NORESA91.'.</p>';
			        $button = 1;
			    }
			    else {
			        $button = 1;
			    }
			}
			if($button==1) {
				echo '<input class="bouton inptxtf boutfreq" type="submit" value="'.RESABUTTON.'" name="reserve" />';
			}
			echo '</p>
		</div>';
	}
	echo '</form>';
	if($onlycode_check_info['valeur']=='0') {
	?><script type="text/javascript">
		document.getElementById('eleves').focus();
	</script><?php
	}
}
?>