<?php
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][4]!="2")
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="./index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		?><style type="text/css">
		#mpa3 { background-color: #fff; }
		#mpa3 td { border: 3px #7D7777 solid; }
		#mpa2 td { border: 0; }
		</style><?php	
		// pour gérer gestion des effectifs et des périodes :
		$isset_first = mysqli_query($bdd,"SELECT date_freq FROM ".$prefix."_freq ORDER BY date_freq ASC LIMIT 1");
		if(mysqli_num_rows($isset_first)>0) {
			$first_freq = mysqli_fetch_assoc($isset_first);
		}
		
		// création de l'établissement
		if(isset($_POST['etab_val'])) {
			// Récupération des infos des champs
			$nom = mysqli_real_escape_string($bdd,trim($_POST['nom']));
			$lieu_nom = mysqli_real_escape_string($bdd,trim($_POST['lieu_nom']));
			$lieu_genre = $_POST['lieu_genre'];
			$adresse = mysqli_real_escape_string($bdd,trim($_POST['adresse']));
			$cp = mysqli_real_escape_string($bdd,trim($_POST['cp']));
			$ville = mysqli_real_escape_string($bdd,trim($_POST['ville']));
			$pays = $_POST['pays'];
			$tel = mysqli_real_escape_string($bdd,trim($_POST['tel']));
			$fax = mysqli_real_escape_string($bdd,trim($_POST['fax']));
			$mail = mysqli_real_escape_string($bdd,trim($_POST['mail']));
			$uai = mysqli_real_escape_string($bdd,trim($_POST['uai']));
			$acad = mysqli_real_escape_string($bdd,$_POST['acad']);
			if($pays!='fra') { $acad=''; }
			
			if($nom==""||$ville=="") {
				echo '<p class="signal">'.EDITC_CON81.'</p>';
			}
			else if($mail!=""&&preg_match('/^[a-z0-9]+([._-]?[a-z0-9]+)*'.'@'.'[a-z0-9]+([._-]?[a-z0-9]+)*\.[a-z]{2,4}$/i',$mail)==0) {
				echo '<p class="signal">'.MAILNO.'</p>';
			}
			else {
				if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab"))==0) {
					mysqli_query($bdd,"INSERT INTO ".$prefix."_etab (etab_id, nom, adresse, cp, ville, tel, fax, mail, rne, pays, academie, lieu_nom, lieu_genre) VALUES (1, '".$nom."', '".$adresse."', '".$cp."', '".$ville."', '".$tel."', '".$fax."', '".$mail."', '".$uai."', '".$pays."', '".$acad."', '".$lieu_nom."', '".$lieu_genre."')");
				}
				else {
					mysqli_query($bdd,"UPDATE ".$prefix."_etab SET nom='".$nom."', adresse='".$adresse."', cp='".$cp."', ville='".$ville."', tel='".$tel."', fax='".$fax."', mail='".$mail."', rne='".$uai."', academie='".$acad."', pays='".$pays."', lieu_nom='".$lieu_nom."', lieu_genre='".$lieu_genre."' WHERE etab_id = '".$_SESSION['etab']."'");
				}
			}
		}
				
		echo '<h1>'.PARAC_ETAB1.'</h1>';
		
		$etab = mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab");
		$etab_nb = mysqli_num_rows($etab);
		$etab_info = mysqli_fetch_assoc($etab);
		@$_SESSION['etab'] = @$etab_info['etab_id'];
	
		echo '<br><form method="POST" name="etab_info" action=""><table id="install" class="paramform">
		<tr><td>'.NAME.' <span class="signal">*</span>'.DOTS.'</td><td><input type="text" class="inptxt" id="etab_nom" name="nom" maxlength="100" value="';
		if($etab_nb>0) { echo $etab_info['nom']; }
		echo '" /></td></tr>
		<tr><td>'.PARAC_ETAB181.'</td><td><input type="text" class="inptxt" id="lieu_nom" name="lieu_nom" maxlength="50" value="';
		if($etab_nb>0) { echo $etab_info['lieu_nom']; }
		else { echo 'CDI'; }
		echo '" /> <div class="box"><select id="lieu_genre" name="lieu_genre">';
		$gcode = array('MASC','FEMI',"ELUS");
		$gname = array(PARAC_ETAB182,PARAC_ETAB183,PARAC_ETAB184);
		for($gn=0;$gn<count($gcode);$gn++) {
			echo '<option value="'.$gcode[$gn].'"';
			if(@$etab_info['lieu_genre']==$gcode[$gn]) {
				echo ' selected ';
			}
			echo '>'.$gname[$gn].'</option>';
		}
		echo '</select></div></td></tr>
		<tr><td>'.PARAC_ETAB2.'</td><td><input type="text" class="inptxt" id="etab_adresse" name="adresse" maxlength="200" value="';
		if($etab_nb>0) { echo $etab_info['adresse']; }
		echo '" /></td></tr>
		<tr><td>'.PARAC_ETAB3.'</td><td><input type="text" class="inptxt" id="etab_cp" name="cp" maxlength="5" value="';
		if($etab_nb>0) { echo $etab_info['cp']; }
		echo '" /></td></tr>
		<tr><td>'.PARAC_ETAB4.' <span class="signal">*</span>'.DOTS.'</td><td><input type="text" class="inptxt" id="etab_ville" name="ville" maxlength="100" value="';
		if($etab_nb>0) { echo $etab_info['ville']; }
		echo '" /></td></tr>
		<tr><td>'.PARAC_CAL10.'</td><td><div class="box"><select id="etab_pays" name="pays">';
		$paycode = array('fra','bel','mon','swi','fnc','aut', 'civ');
		$payname = array(PAYS_FRA,PAYS_BEL,PAYS_MON,PAYS_SWI,PAYS_FNC,PAYS_AUT,PAYS_CIV);
		for($pn=0;$pn<count($paycode);$pn++) {
			echo '<option value="'.$paycode[$pn].'"';
			if(@$etab_info['pays']==$paycode[$pn]) {
				echo ' selected ';
			}
			echo '>'.$payname[$pn].'</option>';
		}
		echo '</select></div></td></tr>
		<tr><td>'.PARAC_ETAB5.'</td><td><input type="text" id="etab_tel" class="inptxt" name="tel" maxlength="20" value="';
		if($etab_nb>0) { echo $etab_info['tel']; }
		echo '" /></td></tr>
		<tr><td>'.PARAC_ETAB6.'</td><td><input type="text" class="inptxt" id="etab_fax" name="fax" maxlength="20" value="';
		if($etab_nb>0) { echo $etab_info['fax']; }
		echo '" /></td></tr>
		<tr><td>'.MAIL2.DOTS.'</td><td><input type="text" class="inptxt" id="etab_mail" name="mail" maxlength="200" value="';
		if($etab_nb>0) { echo $etab_info['mail']; }
		echo '" /></td></tr>
		<tr><td>'.PARAC_ETAB7.'</td><td><input type="text" class="inptxt" id="etab_uai" name="uai" maxlength="20" value="';
		if($etab_nb>0) { echo $etab_info['rne']; }
		echo '" /></td></tr>
		<tr><td>'.PARAC_ETAB8.'</td><td>';
		include('./include/acadschoix.php');
		echo '</td></tr>
		<tr><td colspan="2" style="text-align:right;"><input class="bouton boutparam" id="etabid" type="submit" value="'.VALID.'" name="etab_val" /></td></tr>
		</table>
		</form>';
		
		echo '<br><hr class="param"><br>';	

		//NIVEAUX (pour stats)
		echo '<h2>'.PARAC_ETAB9.'</h2>';
		include('db/proc/etab_config_check.php');
		echo '<p class="maxwidth">'.PARAC_ETAB10.'</p>';
		$verif_nivs = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_statsniv'");
		$verif_nivs_info = mysqli_fetch_assoc($verif_nivs);
		//COL-LYC-CIT-LYA-CIA
		echo '<input type="radio" name="niveaux" id="COL" value="COL" ';
		if($verif_nivs_info['valeur']=="COL") { echo 'checked'; }
		echo ' />'.NIV_COL.'
		<br><input type="radio" name="niveaux" id="LYC" value="LYC" '; 
		if($verif_nivs_info['valeur']=="LYC") { echo 'checked'; }
		echo ' />'.NIV_LYC.'
		<br><input type="radio" name="niveaux" id="LYA" value="LYA" '; 
		if($verif_nivs_info['valeur']=="LYA") { echo 'checked'; }
		echo ' />'.NIV_LYA.'
		<br><input type="radio" name="niveaux" id="CIT" value="CIT" '; 
		if($verif_nivs_info['valeur']=="CIT") { echo 'checked'; }
		echo ' />'.NIV_CIT.'
		<br><input type="radio" name="niveaux" id="CIA" value="CIA" '; 
		if($verif_nivs_info['valeur']=="CIA") { echo 'checked'; }
		echo ' />'.NIV_CIA.'
		<br><input type="radio" name="niveaux" id="PER" value="PER" '; 
		if($verif_nivs_info['valeur']=="PER") { echo 'checked'; }
		echo ' />'.NIV_PER.'
		<br><p class="paddvalid1"><input class="bouton boutparam" type="submit" onclick="niveauxv(this);colorval(\'niveauxv\');" id="niveauxv" value="'.VALID.'" name="niveaux_valid" /></p>';

		echo '<script type="text/javascript">
			function niveauxv(param) {
			var param = "niveauxv";
			var typ = document.getElementsByName("niveaux");              
            for(i = 0; i < typ.length; i++) {
                if(typ[i].checked)
                var typc = typ[i].value;
            }
            if(typc=="PER") {
            	document.getElementById("regroupnivs").style.display = "block";
            }
           	else {
            	document.getElementById("regroupnivs").style.display = "none";
            }
			$.ajax({
			    type: "POST",
			    data: { param: param, typc: typc },
			    url: "js/idl/param_syn.php",
			   });
			}
		</script>';

		// Gestion de regroupements personnalisés
		
		echo '<div id="regroupnivs" style="display:';
		if($verif_nivs_info['valeur']=="PER") { echo 'block'; }
		else { echo 'none'; }
		echo ';">';
		echo '<h3>'.PARAC_ETAB11.'</h3>';
		echo PARAC_ETAB12.'
		<table class="paramform"><tr><th>'.PARAC_ETAB13.'</th><th>'.NAME.'</th><th>'.COLOR1.'</th></tr>';
		$tabCarac = array();
		$tabNom = array();
		$carac_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_statsniv_persoc'"));
		$nom_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_statsniv_person'"));
		$caracs = explode(",", $carac_info['valeur']);
		foreach($caracs as $checked_caracs) {
			array_push($tabCarac, $checked_caracs);
		}
		$noms = explode(",", $nom_info['valeur']);
		foreach($noms as $checked_noms) {
			array_push($tabNom, $checked_noms);
		}
		$color = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'colors_stats_array'")); $j=28;
		for($i=0;$i<20;$i++) {
			echo '<tr><td><input type="text" size="2" id="carac'.$i.'" name="carac'.$i.'" value="';
			if($carac_info['valeur']!="") {
				if($i==0) { echo @$tabCarac[0]; } else { echo @$tabCarac[$i]; }
			}
			echo '" /></td><td><input type="text" size="5" id="nom'.$i.'" name="nom'.$i.'" value="';
			if($nom_info['valeur']!="") {
				if($i==0) { echo @$tabNom[0]; } else { echo @$tabNom[$i]; }
			}
			echo '" /></td>
			<td>';
			
			echo '<input type="color" id="couleur'.$i.'" name="couleur'.$i.'" maxlength="30" value="';
			if($i==0) { 
				$color_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$tabCarac[0]."'")); 
			} 
			else {
				if(isset($tabCarac[$i])) {
					$color_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$tabCarac[$i]."'"));  
				}
				else { unset($color_info['bgcolor']); }
			}
			if(isset($color_info['bgcolor'])) { echo $color_info['bgcolor']; }
			else { echo substr($color['valeur'],$j,7); }
			echo '" /></td></tr>';
			$j+=7;
		}
		echo '</table><br><p class="paddvalid1"><input class="bouton boutparam" type="submit" onclick="persogr(this);colorval(\'persogr_val\');" id="persogr_val" value="'.VALID.'" name="perniv_valid" /></p></div>';

		echo '<script type="text/javascript">
			function persogr(param) {
			var param = "persogroupes";
			var typ = document.getElementsByName("niveaux");';
			for($i=0;$i<20;$i++) {
            	echo 'carac'.$i.' = document.getElementById("carac'.$i.'").value;
            	nom'.$i.' = document.getElementById("nom'.$i.'").value;
            	couleur'.$i.' = document.getElementById("couleur'.$i.'").value;';
            }
			echo '$.ajax({
			    type: "POST",
			    data: { param: param';
			    for($i=0;$i<20;$i++) {
	            	echo ', carac'.$i.': carac'.$i.', nom'.$i.' : nom'.$i.', couleur'.$i.' : couleur'.$i;
	            }
			    echo ' },
			    url: "js/idl/param_syn.php",
			   });
			}
		</script>';


		echo '<hr class="param"><br>';	

		// ESPACES
		echo '<h2 id="espaces">'.PARAC_ETAB14.'</h2>';
		if(isset($_POST['activesp'])) {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '1' WHERE param_nom = 'espaces_gestion'");
		}
		if(isset($_POST['desactivesp'])) {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '0' WHERE param_nom = 'espaces_gestion'");
			$esp_ordre = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_ordreaff'"));
			if($esp_ordre['valeur']=='esp') { 
				mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = 'div' WHERE param_nom = 'freq_ordreaff'");
			}
		}
		$edtesp_param = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_espmulti'"));
		$verif_nivs = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_gestion'");
		$verif_nivs_info = mysqli_fetch_assoc($verif_nivs);
		if($verif_nivs_info['valeur']==0) {
			echo '<p>'.PARAC_ETAB15.'</p>
			<form action="#espaces" name="activesp" method="POST"><p><input type="checkbox" id="activesp" name="activesp" /><label for="activesp"> '.PARAC_ETAB16.'</label></p><p class="paddvalid1"><input class="bouton boutparam" type="submit" value="'.VALID.'" name="activesp_valid" /></p></form>';
		}
		else if($verif_nivs_info['valeur']==1) {

			echo '<p><b>'.PARAC_ETAB17.'</b></p>';
			$esp0 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first'"));
			$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'"));
			echo PARAC_ETAB18.' <input type="text" class="inptxt" id="esp0nom" name="esp0nom" value="'.$esp0['valeur'].'" /><br><input type="checkbox" id="first_resa" name="first_resa"'; 
			if($esp0_resa['valeur']=='1') { echo ' checked'; }
			echo ' /><label for="first_resa"> '.FREQ_RESA71.'</label><br><input type="checkbox" id="first_resadist" name="first_resadist"';
			if($esp0_resadist['valeur']=='1') { echo ' checked'; }
			echo ' /><label for="first_resadist"> '.FREQ_RESA72.'</label> <p class="paddvalid1"><input class="bouton boutparam" onclick="espinit(this);colorval(\'espinit_val\');" id="espinit_val" type="submit" value="'.MODIFY.'" name="esp0chgt" /></p>';

			echo '<script type="text/javascript">
				function espinit(param) {
				var param = "espinit";
				var nom_init = document.getElementById("esp0nom").value;
				var checked1 = document.getElementById("first_resa").checked;
				var checked2 = document.getElementById("first_resadist").checked;
				if(checked1===false) {
					var resap = "0";
				}
				else {
					var resap = "1";
				}
				if(checked2===false) {
					var resad = "0";
				}
				else {
					var resad = "1";
				}';
				$espaces = mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces");
				foreach($espaces as $espace) {
					echo 'var d'.$espace['espace_id'].' = document.getElementsByName("'.$espace['espace_id'].'_replid")[0];
					d'.$espace['espace_id'].'.options[0].innerHTML = nom_init;	';		
				}	
				echo 'var d = document.getElementsByName("defaultespace")[0];
				d.options[0].innerHTML = nom_init;
				document.getElementById("espdef").textContent = nom_init;
				$.ajax({
				    type: "POST",
				    data: { param: param, nom_init: nom_init, resap: resap, resad: resad },
				    url: "js/idl/param_syn.php",
				   });
				}
			</script>';

			echo '<p><b>'.PARAC_ETAB19.'</b></p>';
			if(isset($_POST['creaesp_valid'])) {
				$espaces = mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces");
				if(mysqli_num_rows($espaces)>0) {
					foreach($espaces as $espace) {
						$espace_id = $espace['espace_id'];
						$nom = mysqli_real_escape_string($bdd,trim($_POST[''.$espace_id.'_name']));
						if(isset($_POST[$espace_id.'_auth_freq'])) { $auth_freq = 1; } 
						else { $auth_freq = 0; }
						if(isset($_POST[$espace_id.'_resa'])) { $resa = 1; } 
						else { $resa = 0; }
						if(isset($_POST[$espace_id.'_resadist']))  { $resadist = 1; } 
						else { $resadist = 0; }
						if(isset($_POST[$espace_id.'_edtopt'])) { 
							$edtopt = 1; 							
						} 
						else { $edtopt = 0; }
						if(isset($_POST[$espace_id.'_edtoptresa']))  { 
							$edtopt = 1; 
							$edtoptresa = 1; 
						} 
						else { $edtoptresa = 0; }
						if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE espace_nom = '".$nom."' AND espace_id != '".$espace_id."'"))==0&&$nom!=$esp0['valeur']) {
							mysqli_query($bdd,"UPDATE ".$prefix."_espaces SET espace_nom = '".$nom."', seuilnbi = '".$_POST[$espace_id.'_nbi']."', seuilreaci = '".$_POST[$espace_id.'_reaci']."', seuilnbr = '".$_POST[$espace_id.'_nbr']."', seuilreacr = '".$_POST[$espace_id.'_reacr']."', auth_freq = '".$auth_freq."', resa = '".$resa."', resa_dist = '".$resadist."', edtopt = '".$edtopt."', edtoptresa = '".$edtoptresa."' WHERE espace_id = '".$espace['espace_id']."'");
							if($edtopt==1) {
								if(substr($edtesp_param['valeur'],0,1)==0) {
									if(substr($edtesp_param['valeur'],2,10)=='0000-00-00') {
										$datec = date('Y-m-d');
									}
									else {
										$datec = substr($edtesp_param['valeur'],2,10);
									}
									mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '1-".$datec."' WHERE param_nom = 'edt_espmulti'");
								}
							}
						}
						else { $up_exist = '<p class="signal">'.PARAC_ETAB20.'</p>'; }
					}
				}
				if(trim($_POST['new1_name'])!='') {
					$nom = mysqli_real_escape_string($bdd,trim($_POST['new1_name']));
					if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE espace_nom = '".$nom."'"))==0&&$nom!=$esp0['valeur']) {
						if(isset($_POST['new1_auth_freq'])) { $auth_freq = 1; } 
						else { $auth_freq = 0; }
						if(isset($_POST['new1_resa'])) { $resa = 1; } 
						else { $resa = 0; }
						if(isset($_POST['new1_resadist']))  { $resadist = 1; } 
						else { $resadist = 0; }
						if(isset($_POST['new1_edtopt'])) { $edtopt = 1; } 
						else { $edtopt = 0; }
						if(isset($_POST['new1_edtoptresa']))  { $edtopt = 1; $edtoptresa = 1; } 
						else { $edtoptresa = 0; }
						mysqli_query($bdd,"INSERT INTO ".$prefix."_espaces (espace_id, espace_nom, seuilnbi, seuilreaci, seuilnbr, seuilreacr, resa, resa_dist, auth_freq, edtopt, edtoptresa) VALUES ('', '".$nom."', '".$_POST['new1_nbi']."', '".$_POST['new1_reaci']."', '".$_POST['new1_nbr']."', '".$_POST['new1_reacr']."', '".$resa."', '".$resadist."', '".$auth_freq."', '".$edtopt."', '".$edtoptresa."')");
						if($edtopt==1) {
							if(substr($edtesp_param['valeur'],0,1)==0) {
								if(substr($edtesp_param['valeur'],2,10)=='0000-00-00') {
									$datec = date('Y-m-d');
								}
								else {
									$datec = substr($edtesp_param['valeur'],2,10);
								}
								mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '1-".$datec."' WHERE param_nom = 'edt_espmulti'");
							}
						}
					}
					else { $new1_exist = '<p class="signal">'.PARAC_ETAB21.' '.$nom.' '.PARAC_ETAB22.'</p>'; }
				}
				if(trim($_POST['new2_name'])!='') {
					$nom = mysqli_real_escape_string($bdd,trim($_POST['new2_name']));
					if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE espace_nom = '".$nom."'"))==0&&$nom!=$esp0['valeur']) {
						if(isset($_POST['new2_auth_freq'])) { $auth_freq = 1; } 
						else { $auth_freq = 0; }
						if(isset($_POST['new2_resa'])) { $resa = 1; } 
						else { $resa = 0; }
						if(isset($_POST['new2_resadist']))  { $resadist = 1; } 
						else { $resadist = 0; }
						if(isset($_POST['new2_edtopt'])) { $edtopt = 1; } 
						else { $edtopt = 0; }
						if(isset($_POST['new2_edtoptresa']))  { $edtopt = 1; $edtoptresa = 1; } 
						else { $edtoptresa = 0; }
						mysqli_query($bdd,"INSERT INTO ".$prefix."_espaces (espace_id, espace_nom, seuilnbi, seuilreaci, seuilnbr, seuilreacr, resa, resa_dist, auth_freq, edtopt, edtoptresa) VALUES ('', '".$nom."', '".$_POST['new2_nbi']."', '".$_POST['new2_reaci']."', '".$_POST['new2_nbr']."', '".$_POST['new2_reacr']."', '".$resa."', '".$resadist."', '".$auth_freq."', '".$edtopt."', '".$edtoptresa."')");
						if($edtopt==1) {
							if(substr($edtesp_param['valeur'],0,1)==0) {
								if(substr($edtesp_param['valeur'],2,10)=='0000-00-00') {
									$datec = date('Y-m-d');
								}
								else {
									$datec = substr($edtesp_param['valeur'],2,10);
								}
								mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '1-".$datec."' WHERE param_nom = 'edt_espmulti'");
							}
						}
					}
					else { $new2_exist = '<p class="signal">'.PARAC_ETAB21.' '.$nom.' '.PARAC_ETAB22.'</p>'; }
				}
			}
			if(isset($up_exist)) { echo $up_exist; }
			if(isset($new1_exist)) { echo $new1_exist; }
			if(isset($new2_exist)) { echo $new2_exist; }
			$espaces = mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces");
			echo '<div style="overflow: auto;  white-space: nowrap;"><form method="POST" action="#espaces" name="formesp" class="padd1">
			<table class="paramform"><tr><th>Id.</th><th>'.NAME.'</th><th>'.PARAC_ETAB23.'</th><th>'.PARAC_ETAB24.'</th><th>'.EDT1.'</th><th>'.PARAC_ETAB25.'</th></tr>';
			if(mysqli_num_rows($espaces)>0) {
				foreach($espaces as $espace) {
					echo '<tr><td>'.$espace['espace_id'].'</td><td><input type="text" class="inptxt" maxsize="30" name="'.$espace['espace_id'].'_name" value="'.$espace['espace_nom'].'" /><br><br><div class="box" style="margin-left:-30px;"><select name="'.$espace['espace_id'].'_replid">';
					$liste_activ = mysqli_query($bdd,"SELECT espace_id, espace_nom FROM ".$prefix."_espaces WHERE espace_id <> '".$espace['espace_id']."' ORDER BY espace_nom ASC");
					echo '<option value="0">'.substr($esp0['valeur'],0,17).'</option>';
					foreach($liste_activ as $liste) {
						echo '<option value="'.$liste['espace_id'].'">'.substr($liste['espace_nom'],0,17).'</option>';
					}
					echo '</select></div> <input class="boutontab" type="submit" value="'.REPLACE_TINY.'" name="'.$espace['espace_id'].'_replace" /></td><td><input type="number" class="number" min="0" name="'.$espace['espace_id'].'_nbi" value="'.$espace['seuilnbi'].'" /><div style="float:right;"><input type="checkbox" id="'.$espace['espace_id'].'_auth_freq" name="'.$espace['espace_id'].'_auth_freq"'; 
						if($espace['auth_freq']=='1') { echo ' checked'; }
						echo ' /><label for="'.$espace['espace_id'].'_auth_freq"> '.PARAC_ETAB26.'</label></div><br> <br><div class="box"><select name="'.$espace['espace_id'].'_reaci">';
						if($espace['seuilreaci']=='alert') { echo '<option value="alert">'.PARAC_ETAB27.'</option><option value="stopp">'.PARAC_ETAB28.'</option>'; }
						else { echo '<option value="stopp">'.PARAC_ETAB28.'</option><option value="alert">'.PARAC_ETAB27.'</option>'; }
						echo '</select></div></td><td><div style="float:left;"><input type="number" class="number" min="0" name="'.$espace['espace_id'].'_nbr" value="'.$espace['seuilnbr'].'" /></div><div style="float:right;"><input type="checkbox" id="'.$espace['espace_id'].'_resa" name="'.$espace['espace_id'].'_resa"'; 
						if($espace['resa']=='1') { echo ' checked'; }
						echo ' /><label for="'.$espace['espace_id'].'_resa"> '.FREQ_RESA71.'</label><br><input type="checkbox" id="'.$espace['espace_id'].'_resadist" name="'.$espace['espace_id'].'_resadist"';
						if($espace['resa_dist']=='1') { echo ' checked'; }
						echo ' /><label for="'.$espace['espace_id'].'_resadist"> '.FREQ_RESA72.'</label></div><br> <br><div class="box"><select name="'.$espace['espace_id'].'_reacr">';
						if($espace['seuilreacr']=='alert') { echo '<option value="alert">'.PARAC_ETAB27.'</option><option value="stopp">'.PARAC_ETAB28.'</option>'; }
						else { echo '<option value="stopp">'.PARAC_ETAB28.'</option><option value="alert">'.PARAC_ETAB27.'</option>'; }
						echo '</select></div></td><td><input type="checkbox" id="'.$espace['espace_id'].'_edtopt" name="'.$espace['espace_id'].'_edtopt"'; 
						if($espace['edtopt']=='1') { echo ' checked'; }
						echo ' /><label for="'.$espace['espace_id'].'_edtopt"> '.EDT1.'</label><br><input type="checkbox" id="'.$espace['espace_id'].'_edtoptresa" name="'.$espace['espace_id'].'_edtoptresa"';
						if($espace['edtoptresa']=='1') { echo ' checked'; }
						echo ' /><label for="'.$espace['espace_id'].'_edtoptresa"> '.PARAC_ETAB29.'</label></td><td><input class="boutontab" type="submit" value="'.SUPPR_TINY.'" name="'.$espace['espace_id'].'_suppr" /></td></tr>';
					if(isset($_POST[$espace['espace_id'].'_replace'])) {
						echo '<p class="signal">'.PARAC_ETAB30.' <input type="hidden" name="replace_id" value="'.$_POST[$espace['espace_id'].'_replid'].'" /><input class="boutontab" type="submit" value="'.PARAC_ETAB31.'" name="'.$espace['espace_id'].'_confrep" />';
					}
					if(isset($_POST[$espace['espace_id'].'_confrep'])) {
						mysqli_query($bdd,"UPDATE ".$prefix."_freq SET espace_id = '".$_POST['replace_id']."' WHERE espace_id = '".$espace['espace_id']."'");
						mysqli_query($bdd,"UPDATE ".$prefix."_freqgrappes SET espace_id = '".$_POST['replace_id']."' WHERE espace_id = '".$espace['espace_id']."'");
						mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET espace_id = '".$_POST['replace_id']."' WHERE espace_id = '".$espace['espace_id']."'");
						mysqli_query($bdd,"UPDATE ".$prefix."_limitstxt SET espace_id = '".$_POST['replace_id']."' WHERE espace_id = '".$espace['espace_id']."'");
						mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces WHERE espace_id = '".$espace['espace_id']."'");
						echo '<script language="Javascript">
						<!--
						document.location.replace("param.php?page=etab");
						// -->
						</script>';	
					}
					if(isset($_POST[$espace['espace_id'].'_suppr'])) {
						if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq WHERE espace_id = '".$espace['espace_id']."'"))>0||mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqres WHERE espace_id = '".$espace['espace_id']."'"))>0) {
								echo '<p class="signal">'.PARAC_ETAB21.' &laquo;&nbsp;'.$espace['espace_nom'].'&nbsp;&raquo; '.PARAC_ETAB32.'</p>';
						}
						else {
							mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces WHERE espace_id = '".$espace['espace_id']."'");
							echo '<script language="Javascript">
							<!--
							document.location.replace("param.php?page=etab");
							// -->
							</script>';	
						}
					}
				}
			}
			echo '<tr><td></td><td><input type="text" class="inptxt" maxsize="30" name="new1_name"></td><td><input type="number" class="number" value="0" min="0" name="new1_nbi" /><div style="float:right;"><input type="checkbox" id="new1_auth_freq" name="new1_auth_freq" checked /><label for="new1_auth_freq"> '.PARAC_ETAB26.'</label></div><br> <br><div class="box"><select name="new1_reaci"><option value="alert">'.PARAC_ETAB27.'</option><option value="stopp">'.PARAC_ETAB28.'</option></select></div></td><td><div style="float:left;"><input type="number" class="number" value="0" min="0" name="new1_nbr" /></div><div style="float:right;"><input type="checkbox" id="new1_resa" name="new1_resa" checked /><label for="new1_resa"> '.FREQ_RESA71.'</label><br><input type="checkbox" id="new1_resadist" name="new1_resadist" checked /><label for="new1_resadist"> '.FREQ_RESA72.'</label></div><br> <br><div class="box"><select name="new1_reacr"><option value="alert">'.PARAC_ETAB27.'</option><option value="stopp">'.PARAC_ETAB28.'</option></select></div></td><td><input type="checkbox" id="new1_edtopt" name="new1_edtopt" /><label for="new1_edtopt"> '.EDT1.'</label><br><input type="checkbox" id="new1_edtoptresa" name="new1_edtoptresa" /><label for="new1_edtoptresa"> '.PARAC_ETAB29.'</label></td><td></td></tr>';
			echo '<tr><td></td><td><input type="text" class="inptxt" maxsize="30" name="new2_name"></td><td><input type="number" class="number" value="0" min="0" name="new2_nbi" /><div style="float:right;"><input type="checkbox" id="new2_auth_freq" name="new2_auth_freq" checked /><label for="new2_auth_freq"> '.PARAC_ETAB26.'</label></div><br> <br><div class="box"><select name="new2_reaci"><option value="alert">'.PARAC_ETAB27.'</option><option value="stopp">'.PARAC_ETAB28.'</option></select></div></td><td><div style="float:left;"><input type="number" class="number" value="0" min="0" name="new2_nbr" /></div><div style="float:right;"><input type="checkbox" id="new2_resa" name="new2_resa" checked /><label for="new2_resa"> '.FREQ_RESA71.'</label><br><input type="checkbox" id="new2_resadist" name="new2_resadist" checked /><label for="new2_resadist"> '.FREQ_RESA72.'</label></div><br> <br><div class="box"><select name="new2_reacr"><option value="alert">'.PARAC_ETAB27.'</option><option value="stopp">'.PARAC_ETAB28.'</option></select></div></td><td><input type="checkbox" id="new2_edtopt" name="new2_edtopt" /><label for="new2_edtopt"> '.EDT1.'</label><br><input type="checkbox" id="new2_edtoptresa" name="new2_edtoptresa" /><label for="new2_edtoptresa"> '.PARAC_ETAB29.'</label></td><td></td></tr>';
			echo '<tr><td colspan="5" class="right"><input class="bouton boutparam" type="submit" value="'.VALID.'" name="creaesp_valid" /></td><td></td></tr></table></form></div>';
			
			// Choix de l'espace par défaut
			echo '<br><p><b>'.PARAC_ETAB33.'</b></p>';
			$esp = mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces ORDER BY espace_nom ASC");
			$nb_esp = mysqli_num_rows($esp);
			if($nb_esp==0) {
				echo '<p>'.PARAC_ETAB34.'</p>';
			}
			else {
				$activd = mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE default_espace = 1");
				echo '<p>'.PARAC_ETAB21.' "<b><span id="espdef">';
				if(mysqli_num_rows($activd)!=0) {
					$nom_activd = mysqli_fetch_assoc($activd);
					echo $nom_activd['espace_nom'];
				}
				else { echo $esp0['valeur']; }
				echo '</span></b>" '.PARAC_ETAB35.'</p>';
				echo'<p>'.PARAC_ETAB35.' <div class="box"><select id="defaultespace" name="defaultespace" value="">';
				$liste_activ = mysqli_query($bdd,"SELECT espace_id, espace_nom FROM ".$prefix."_espaces ORDER BY espace_nom ASC");
				echo '<option value="0">'.$esp0['valeur'].'</option>';
				while($liste = mysqli_fetch_assoc($liste_activ)) {
					echo '<option value="'.$liste['espace_id'].'"';
					if(mysqli_num_rows($activd)!=0) {
						if($nom_activd['espace_id']==$liste['espace_id']) {
							echo ' selected ';
						}
					}
					echo '>'.$liste['espace_nom'].'</option>';
				}
				echo '</select></div><br><i>'.PARAC_ETAB37.'</i>
				<br><br><input type="checkbox" id="verrespace" name="verrespace"';
				$verrespace = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_verrespace'"));
				if($verrespace['valeur']==1) { echo 'checked'; }
				echo ' /><label for="verrespace">'.PARAC_ETAB38.'</label><p class="paddvalid1"><input class="bouton boutparam" onclick="defdefaut(this);colorval(\'defdefaut_val\');" id="defdefaut_val" type="submit" value="'.PARAC_ETAB39.'" name="espace_default" /></p>';
				echo '<script type="text/javascript">
					function defdefaut(param) {
					var param = "defdefaut";
					var val = document.getElementById("defaultespace").value;
					var chspan = document.getElementById("defaultespace").options[document.getElementById("defaultespace").selectedIndex].innerHTML;
					var checked1 = document.getElementById("verrespace").checked;
					if(checked1===false) {
						var verr = "0";
					}
					else {
						var verr = "1";
					}
					document.getElementById("espdef").textContent = chspan;
					$.ajax({
					    type: "POST",
					    data: { param: param, val: val, verr: verr },
					    url: "js/idl/param_syn.php",
					   });
					}
				</script>';
			}

			// Formulaire de désaction de la gestion de différents espaces
			echo '<p><b>'.PARAC_ETAB40.'</b></p>';
			echo '<form action="#espaces" name="desactivesp" method="POST"><p><input type="checkbox" id="desactivesp" name="desactivesp" /><label for="desactivesp"> '.PARAC_ETAB41.'</label></p><span class="paddvalid1 right"><input class="bouton boutparam" type="submit" value="'.VALID.'" name="desactivesp_valid" /></span></p></form>';
		}

		echo '<hr class="param"><br>';	

		//EFFECTIFS
		echo '<h2 id="pluria">'.PARAC_ETAB42.'</h2>';
		// traitement d'ajout d'effectifs d'années précédentes:
		if(isset($_POST['addyear'])&&$etab_nb>0) {
			if($_POST['yearsco']!='') {
				$etab = mysqli_query($bdd,"SELECT etab_id FROM ".$prefix."_etab");
				if(mysqli_num_rows($etab)>0) {
					$etab_info = mysqli_fetch_assoc($etab);
					$etab_id = $etab_info['etab_id'];
				}
				else { $etab_id = ''; }
				for($j=0;$j<=50;$j++) {
					if(($_POST['divl'.$j.'']!=''||$_POST['div'.$j.'']!='')&&$_POST['nb'.$j.'']!='') {
						if($_POST['div'.$j.'']!='') { 
							$div = mysqli_real_escape_string($bdd,$_POST['div'.$j.'']);
						}
						else { $div = $_POST['divl'.$j.'']; }
						mysqli_query($bdd,"INSERT INTO ".$prefix."_etab_effectifs (etab_id, anneesco, division, effectif) VALUES ('".$etab_id."', '".$_POST['yearsco']."', '".$div."', '".$_POST['nb'.$j]."')");
					}
				}
			}
		}
		if($etab_nb==0) {
			echo PARAC_ETAB43;
		}
		else {
			// Formulaires :
			echo PARAC_ETAB44;
			$eff_annees = mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab_effectifs GROUP BY anneesco ORDER BY anneesco DESC");
			$v=1;
			foreach($eff_annees as $eff_annee) {
				echo '<div id="ifsuppr'.str_replace("/","",$eff_annee['anneesco']).'"><p><i class="fas fa-angle-double-right"></i> <a style="color:black;text-decoration:none;" onmouseover="this.style.textDecoration=\'underline\';this.style.cursor = \'pointer\';" onmouseout="this.style.textDecoration=\'none\';" onmousedown="toggleDiv(\'diveff'.$eff_annee['anneesco'].'\');">'.$eff_annee['anneesco'].'</a></p><div id="diveff'.$eff_annee['anneesco'].'" style="display:none;">';
				$eff_divs = mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab_effectifs WHERE anneesco = '".$eff_annee['anneesco']."' ORDER BY division ASC");
				echo '<table><thead><tr><th>'.DIV.'</th>
				<th>'.FDIR_STATS_INFP3.'</th></tr></thead><tbody>';
				foreach($eff_divs as $eff_div) {
					echo '<script type="text/javascript">
					function div_nb'.$v.'(div,an,nb){
					  var param = "effectifs";
					  var nb = document.getElementById("nombre'.$eff_annee['anneesco'].$eff_div['division'].'").value;
					  $.ajax({
					    type: "POST",
					    data: { param: param, div: div, an: an, nb: nb},
					    url: "js/idl/param_syn.php",
					    
					   });
					}
					</script>';
					echo '<tr><td class="center">'.$eff_div['division'].'</td>
					<td class="center"><input type="number" id="nombre'.$eff_annee['anneesco'].$eff_div['division'].'" value="'.$eff_div['effectif'].'" /> <input type="button" class="boutontab" name="" value="'.MODIFY.'" onclick="div_nb'.$v.'(\''.$eff_div['division'].'\',\''.$eff_annee['anneesco'].'\',this);colormod(\'divnb'.$v.'\');" id="divnb'.$v.'"" /></td></tr>';
					$v++;
				}
				echo '<tr><td colspan="2"><input type="hidden" name="yearsco" value="'.$eff_annee['anneesco'].'"><input type="submit" class="boutontab" name="ysuppreff" onclick="suppra'.str_replace("/","",$eff_annee['anneesco']).'(this);" value="'.PARAC_ETAB45.'" /></td></tr>';
				echo '</tbody></table>';
				echo '<script type="text/javascript">
					function suppra'.str_replace("/","",$eff_annee['anneesco']).'(param){
					  var param = "suppra";
					  var an = "'.$eff_annee['anneesco'].'";
					  document.getElementById("ifsuppr'.str_replace("/","",$eff_annee['anneesco']).'").style.display = "none";
					  var x = document.getElementById("yearsco");
					  var option = document.createElement("option");
					  option.text = "'.$eff_annee['anneesco'].'";
					  x.add(option);
					  $.ajax({
					    type: "POST",
					    data: { param: param, an: an},
					    url: "js/idl/param_syn.php",
					    
					   });
					}
				</script>';
				echo '</div></div>';
			}
			
			// Ajout des effectifs d'une autre année
			// formulaire :
			echo '<p><i class="fas fa-angle-double-right"></i> <a style="color:black;text-decoration:none;" onmouseover="this.style.textDecoration=\'underline\';this.style.cursor = \'pointer\';" onmouseout="this.style.textDecoration=\'none\';" onmousedown="toggleDiv(\'diveffadd\');">'.PARAC_ETAB46.'</a></p>
			<div id="diveffadd" style="display:none;">
			<form method="POST" name="addyear" action="param.php?page=etab#pluria">
			<div class="box">
			<select id="yearsco" name="yearsco"><option></option>';
			if(mysqli_num_rows($isset_first)>0) {
				if(substr($first_freq['date_freq'],0,4)==date('Y')) {
					if($etab_info['pays']=="fnc"||$etab_info['pays']=="civ") {
						if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab_effectifs WHERE anneesco = '".date('Y')."'"))==0) {
							echo '<option>'.date('Y').'</option>';
						}
					} else {
						if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab_effectifs WHERE anneesco = '".date('Y')."/".(date('Y')+1)."'"))==0) {
							echo '<option>'.date('Y').'/'.(date('Y')+1).'</option>';
						}
					}
				}
				else {
					if(substr($first_freq['date_freq'],0,4)=="0000") {
						$first_freq_y=2015;
					}
					else {
						$first_freq_y=substr($first_freq['date_freq'],0,4);
					}
					$pos_years = range(date('Y'),$first_freq_y);
					for($i=0;$i<count($pos_years);$i++) {
						if($etab_info['pays']=="fnc"||$etab_info['pays']=="civ") {
							if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab_effectifs WHERE anneesco = '".$pos_years[$i]."'"))==0) {
								echo '<option>'.$pos_years[$i].'</option>';
							}
						} else {
							if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab_effectifs WHERE anneesco = '".$pos_years[$i]."/".($pos_years[$i]+1)."'"))==0) {
								if($pos_years[$i]==date('Y')) {
									if(date('m')>9) {
										echo '<option>'.$pos_years[$i].'/'.($pos_years[$i]+1).'</option>';
									}
								}
								else {
									echo '<option>'.$pos_years[$i].'/'.($pos_years[$i]+1).'</option>';
								}
							}
						}
					}
				}
			}
			echo '</select></div>';
			echo '<br>';
			for($j=0;$j<=50;$j++) {
				if($j>0) { echo '<div id="divdiv'.$j.'"';
				if(!isset($nivsseq[$j])) { echo ' style="display:none; '; }
				echo '">'; }
				echo '<br>'.DIV.' ('.($j+1).') : ';
				echo '<div class="box"><select name="divl'.$j.'"><option></option>';
				$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE categ = '1' GROUP BY division ORDER BY division ASC");
				while($liste = mysqli_fetch_assoc($liste_division)) {
					echo '<option>'.$liste['division'].'</option>';
				}
				echo '</select></div>
				<input type="text" class="inptxt" name="div'.$j.'" size="20" id="nivs'.$j.'" value="" placeholder="'.PARAC_ETAB47.'" /> 
				'.FDIR_STATS_INFP2.DOTS.' <input type="number" name="nb'.$j.'" />';
			    if($j<50) {
					echo ' <a onmousedown="toggleModu(\'divdiv'.($j+1).'\',\'lplus'.$j.'\',\'lminus'.$j.'\',\'lminus'.($j-1).'\');"><i id="lplus'.$j.'" class="fas fa-plus-circle"';
					if(isset($nivsseq[($j+1)])) { echo ' style="display:none;" '; }
					echo '></i><i id="lminus'.$j.'" class="fas fa-minus-circle"';
					if(!isset($nivsseq[($j-1)])||!isset($nivsseq[($j+1)])) { echo ' style="display:none;" '; }
					echo '></i></a>';
				}
				if($j>0) { echo '</div>'; }
			}
			echo '<br><p style="float:right;width:40%;"><input type="submit" class="bouton boutparam" name="addyear" value="'.PARAC_ETAB48.'" /></p>
			</form></div><br>';
		}

		if($v>2) {
			$freq_divs = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq GROUP BY division ORDER BY division ASC");
			$month = date("n");
			if($month>0&&$month<8) {
				$anneesco = (date("Y")-1).'/'.date("Y");
			}
			else {
				$anneesco = date("Y").'/'.(date("Y")+1);
			}
			$divchgs = array();
			$nbdivs=0;
			foreach($freq_divs as $freq_div) {
				$nb_equal_divs = mysqli_num_rows(mysqli_query($bdd,"SELECT division FROM ".$prefix."_etab_effectifs WHERE anneesco = '".$anneesco."' AND division = '".$freq_div['division']."'"));	
				if($nb_equal_divs==0) {
					array_push($divchgs,$freq_div['division']);
					$nbdivs++;
				}				
			}
			if($nbdivs>0) {
				$w=1;
				echo DIVNEW;
				$actuel_divs = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves GROUP BY division ORDER BY division ASC");				
				echo '<table><thead><tr><th>'.DIV1.'</th>
				<th>'.DIV2.'</th></tr></thead><tbody>';
				foreach($divchgs as $divchg) {
					echo '<script type="text/javascript">
					function div_new'.$w.'(div,newdiv){
					  var param = "aligndivs";
					  var newdiv = document.getElementById("chgdiv'.$divchg.'").value;
					  $.ajax({
					    type: "POST",
					    data: { param: param, div: div, newdiv: newdiv},
					    url: "js/idl/param_syn.php",
					    
					   });
					}
					</script>';
					echo '<tr><td class="center">'.$divchg.'</td>
					<td class="center"><select id="chgdiv'.$divchg.'"><option></option>';
					foreach($actuel_divs as $actuel_div) {
						echo '<option>'.$actuel_div['division'].'</option>';
					}
					echo '</select>

					<input type="button" class="boutontab" name="" value="'.MODIFY.'" onclick="div_new'.$w.'(\''.$divchg.'\',this);colormod(\'divnew'.$w.'\');" id="divnew'.$w.'"/></td></tr>';
					$w++;
				}
				echo '</tbody></table>';
			}
		}

		echo '<hr class="param"><br>';	
		
		//PERIODES
		echo '<br>&nbsp; <br><h2 id="periods">'.PARAC_ETAB49.'</h2>';
		if(mysqli_num_rows($isset_first)>0) {
			if(substr($first_freq['date_freq'],0,4)=="0000") {
				$first_freq_y=2015;
			}
			else {
				$first_freq_y=substr($first_freq['date_freq'],0,4);
			}
			$pos_years = range(date('Y'),$first_freq_y);
		}		
		else {
			echo PARAC_ETAB50;
		}
		//traitement:
		if(isset($_POST['periods_val'])) {
			for($i=0;$i<count($pos_years);$i++) {
				$periodes = "";
				$noms = "";
				if(@$etab_info['pays']=="fnc"||@$etab_info['pays']=="civ") {
					$yea = $pos_years[$i];
				}
				else {
					$yea = $pos_years[$i].($pos_years[$i]+1);
				}
				for($per=1;$per<10;$per++) {
					if($_POST['1-'.$per.'-'.$pos_years[$i]]!=""&&$_POST['1-'.$per.'-'.$pos_years[$i]]!="0000-00-00") {
						$periodes .= $_POST['1-'.$per.'-'.$pos_years[$i]].';';
					}
					if($_POST['2-'.$per.'-'.$pos_years[$i]]!=""&&$_POST['1-'.$per.'-'.$pos_years[$i]]!="0000-00-00") {
						$periodes .= $_POST['2-'.$per.'-'.$pos_years[$i]].';';
					}
					if($_POST['n-'.$per.'-'.$pos_years[$i]]!="") {
						$noms .= $_POST['n-'.$per.'-'.$pos_years[$i]].';';
					}					
				}
				if($periodes!="") {
					if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_periodes WHERE anneesco = '".$yea."'"))>0) {
						mysqli_query($bdd,"UPDATE ".$prefix."_periodes SET periodes = '".$periodes."', nom = '".$noms."' WHERE anneesco = '".$yea."'");
					}
					else {
						mysqli_query($bdd,"INSERT INTO ".$prefix."_periodes (periodes,anneesco,nom) VALUES ('".$periodes."','".$yea."','".$noms."')");
					}
				}
			}
		}
		//formulaire:
		if(mysqli_num_rows($isset_first)>0) {
			echo '<form method="POST" action="param.php?page=etab#periods" class="padd4">';
			for($i=0;$i<count($pos_years);$i++) {
				if(@$etab_info['pays']=="fnc"||@$etab_info['pays']=="civ") {
					echo '<p><i class="fas fa-angle-double-right"></i> <a style="color:black;text-decoration:none;" onmouseover="this.style.textDecoration=\'underline\';this.style.cursor = \'pointer\';" onmouseout="this.style.textDecoration=\'none\';" onmousedown="toggleDiv(\'periods'.$pos_years[$i].'\');"><b>'.$pos_years[$i].'</b></a></p>';
					$yea = $pos_years[$i];
				} 
				else {
					echo '<p><i class="fas fa-angle-double-right"></i> <a style="color:black;text-decoration:none;" onmouseover="this.style.textDecoration=\'underline\';this.style.cursor = \'pointer\';" onmouseout="this.style.textDecoration=\'none\';" onmousedown="toggleDiv(\'periods'.$pos_years[$i].'\');"><b>'.$pos_years[$i].'/'.($pos_years[$i]+1).'</b></a></p>';	
					$yea = $pos_years[$i].($pos_years[$i]+1);		
				}
				$per_check = mysqli_query($bdd,"SELECT * FROM ".$prefix."_periodes WHERE anneesco = '".$yea."'");
				if(mysqli_num_rows($per_check)>0) {
					$per_infos = mysqli_fetch_assoc($per_check);
					$per_pv = explode(";", $per_infos['periodes']);
					$per_nom = explode(";", $per_infos['nom']);
				}
				$pt = 0;
				echo '<div id="periods'.$pos_years[$i].'" style="display:none;">';
				for($per=1;$per<10;$per++) {
					echo '<i class="fas fa-bullseye"></i> '.PERIOD.' '.$per.' : 
					'.FROM2.' <input type="date" name="1-'.$per.'-'.$pos_years[$i].'"';
					if(mysqli_num_rows($per_check)>0&&@$per_pv[$pt]!="") {
						echo ' value="'.$per_pv[$pt].'" ';
					}
					echo ' /> ';
					echo TO2.' <input type="date" name="2-'.$per.'-'.$pos_years[$i].'"';
					if(mysqli_num_rows($per_check)>0&&@$per_pv[($pt+1)]!="") {
						echo ' value="'.$per_pv[($pt+1)].'" ';
					}
					echo ' /> '.NAME.DOTS.' <input type="text" size="10" maxlength="100" name="n-'.$per.'-'.$pos_years[$i].'"';
					if(mysqli_num_rows($per_check)>0&&@$per_pv[($pt+1)]!="") {
						echo ' value="'.$per_nom[($per-1)].'" ';
					}
					echo ' /><br>';
					$pt = $pt+2;
				}
				echo '</div><br>';
			}
			echo '<br><p class="paddvalid1"><input type="submit" class="bouton boutparam" name="periods_val" value="'.VALID.'" /></p>';
			echo '</form>';
		}
		echo '<script type="text/javascript">
		function colorval(divs) {
			var couleur = "black 2px solid";
			var couleurw = "white 2px solid";
			var color = "black";
			var colorw = "white";			    
            document.getElementById(divs).style.border = couleur;
			document.getElementById(divs).style.color = color;
			setTimeout(() => {  
				document.getElementById(divs).style.color = colorw; 
				document.getElementById(divs).style.border = couleurw;
				}, 2000);
			}
		</script>';
		echo '<script type="text/javascript">
		function colormod(divs) {
			var couleur = "green 2px solid";
			var couleurw = "grey 1px solid";
			var color = "green";
			var colorw = "black";			    
            document.getElementById(divs).style.border = couleur;
			document.getElementById(divs).style.color = color;
			setTimeout(() => {  
				document.getElementById(divs).style.color = colorw; 
				document.getElementById(divs).style.border = couleurw;
				}, 2000);
			}
		</script>';
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="./index.php">'.ACCUEIL.'</a></p>';
}
?>
