<?php

require '../../lib/glob.php';
require '../../lib/muoribene.php';
require '../../lib/sessionstart.php';
require '../../lib/myconn.php';
require '../../lib/getadmacc.php';
require '../../lib/n2es.php';
require '../../lib/menu.php';
$menu['menu']['selected']=true;
$menu['menu']['submenu']['instances']['selected']=true;
buildmenu($menu);

$dbg='';

use function mysqli_real_escape_string as myesc;

// praticamente una macro
function hspech($str) {
	return(htmlspecialchars(n2es($str),ENT_QUOTES|ENT_HTML5,'UTF-8'));
}

$dbg.=$dlang.'<br>'.N;
$dbg.='<pre>'.print_r($_GET,1).'</pre>';

if (array_key_exists('id',$_GET) && preg_match('/^[0-9]+$/',$_GET['id'])===1) {
	$_GET['id']+=0;
	if ($account['Level']=='guest' && !in_array($_GET['id'],$account['Insts']))
		muoribene('You can’t edit data for an instance you don’t own.',true);
	$res=mysqli_query($link,'SELECT * FROM Instances WHERE ID='.$_GET['id'])
		or muoribene(__LINE__.': '.mysqli_error($link),true);
	if (mysqli_num_rows($res)!=1)
		muoribene(__LINE__.': There is no instance with ID='.$_GET['ID'].'.',true);
	$inst=mysqli_fetch_assoc($res);
	if (is_null($inst['NoxReason']) || preg_match('/^\s*$/',$inst['NoxReason'])===1) $inst['NoxReason']='';
} else {
	muoribene('Malformed input.',true);
}
$dbg.='<pre>'.print_r($inst,1).'</pre>'.N;

function check($row,$col) {
	if ($row[$col]>0)
		return(' checked');
	else
		return('');
}

if ($account['Level']!='guest') {
	require '../../lib/notifs.php';
	$notifs=notifs($link);
}

?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Mustard - Editing «<?php echo($inst['URI']); ?>» form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="description" content="Admin pages for Mastodon Help">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="icon" type="image/png" href="imgs/icona-32.png" sizes="32x32">
<link rel="icon" type="image/png" href="imgs/icona-192.png" sizes="192x192">
<link rel="icon" type="image/png" href="imgs/icona-512.png" sizes="512x512">
<link rel="apple-touch-icon-precomposed" href="imgs/icona-180.png">
<script language="JavaScript" src="js/menu.js?v=<?php echo($cjrand); ?>"></script>
<script language="JavaScript" src="js/alerta.js?v=<?php echo($cjrand); ?>"></script>
<link rel="stylesheet" type="text/css" href="css/theme.css?v=<?php echo($cjrand); ?>">
<script language="JavaScript">
<!--
<?php if ($account['Level']!='guest') require 'js/notifs.js.php'; ?>
function deselect(selid) {
	var sel=document.getElementById(selid);
	sel.selectedIndex=-1;
}
function getselarr(selid) {
	var sel=document.getElementById(selid), selc=sel.length, i, arr=[];
	for (i=0; i<selc; i++)
		arr.push([sel.options[i].text,sel.options[i].value,sel.options[i].selected]);
	return arr;
}
function updselarr(selid,selarr) {
	var sel=document.getElementById(selid), selc=sel.length, selarrc=selarr.length, i, ii;
	sel=sel.options;
	ii=0;
	for (i=0; i<selc; i++) {
		while (ii<selarrc) {
			if (selarr[ii][1]==sel[i].value) {
				(sel[i].selected) ? selarr[ii][2]=true : selarr[ii][2]=false;
				break;
			}
			ii++;
		}
	}
	return selarr;
}
function filtsel(selarr,filt,selid,recbid,inpid,ocol,ecol) {
	var sel=document.getElementById(selid), selarrc=selarr.length, i, ii, go=true;
	if (!document.getElementById(recbid).checked) {
		filt=filt.replace(/([.?*+^$[\]\\(){}|-])/g,'\\$1');
	} else {
		try { (RegExp(filt)); }
		catch(e) { go=false; }
	}
	if (go) {
		sel.length=0;
		for (i=0; i<selarrc; i++) {
			if (RegExp(filt,'i').test(selarr[i][0]))
				sel.options.add(new Option(selarr[i][0],selarr[i][1],false,selarr[i][2]));
		}
		if (ocol!=null)
			document.getElementById(inpid).style.backgroundColor=ocol;
	} else if (ecol!=null) {
		document.getElementById(inpid).style.backgroundColor=ecol;
	}
}
function selup(selid) {
	var sel=document.getElementById(selid), len=sel.length, i, prevopt, curopt;
	if (sel.selectedIndex>0) {
		for (i=0; i<len; i++) {
			if (sel.options[i].selected) {
				prevopt=sel.options[i-1];
				curopt=sel.options[i];
				sel.remove(i-1);
				sel.remove(i-1);
				sel.add(curopt,i-1);
				sel.add(prevopt,i);
			}
		}
	}
}
function seltop(selid) {
	var sel=document.getElementById(selid);
	while (sel.selectedIndex>0)
		selup(selid);
}
function seldown(selid) {
	var sel=document.getElementById(selid), len=sel.length, i, nextopt, curopt;
	if (!sel.options[len-1].selected) {
		for (i=len-1; i>-1; i--) {
			if (sel.options[i].selected) {
				curopt=sel.options[i];
				nextopt=sel.options[i+1];
				sel.remove(i);
				sel.remove(i);
				sel.add(curopt,i);
				sel.add(nextopt,i);
			}
		}
	}
}
function selbot(selid) {
	var sel=document.getElementById(selid), len=sel.length;
	if (sel.selectedIndex>=0) {
		while (!sel.options[len-1].selected)
			seldown(selid);
	}
}
function selonebyval(refarr,val) {
	var len=refarr.length, i;
	for (i=len-1; i>-1; i--)
		(refarr[i][1]==val) ? refarr[i][2]=true : refarr[i][2]=false;
	return refarr;
}
function rmelbyval(refarr,val) {
	var len=refarr.length, i;
	for (i=len-1; i>-1; i--) {
		if (refarr[i][1]==val) {
			refarr.splice(i,1);
			break;
		}
	}
	return refarr;
}
// usato da edinsth.php
function cheltxtbyval(refarr,val,ntxt) {
	var len=refarr.length, i;
	for (i=len-1; i>-1; i--) {
		if (refarr[i][1]==val) {
			refarr[i][0]=ntxt;
			break;
		}
	}
	return refarr;
}
// usato da edinsth.php
function chelvalbyval(refarr,val,nval) {
	var len=refarr.length, i;
	for (i=len-1; i>-1; i--) {
		if (refarr[i][1]==val) {
			refarr[i][1]=nval;
			break;
		}
	}
	return refarr;
}
function seladd(fromselid,toselid,refarr) {
	var fromsel=document.getElementById(fromselid), fslen=fromsel.length, tosel=document.getElementById(toselid), i;
// farlo a scalare invece che a crescere inverte nella select di destinazione l'ordine delle opzioni scelte in quella di origine, perciò lo faccio a crescere
	for (i=0; i<fslen; i++) {
		if (fromsel.options[i].selected) {
			refarr=rmelbyval(refarr,fromsel.options[i].value);
			tosel.add(fromsel.options[i]);
			fslen--;
			i--;
		}
	}
	return refarr;
}
function sortByKey(array, key) {
	return array.sort(function(a, b) {
		var x = a[key].toLowerCase(); var y = b[key].toLowerCase();
		return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	});
}
function selrem(fromselid,toselid,refarr,searchid,regexcbid) {
	var fromsel=document.getElementById(fromselid), fslen=fromsel.length, tosel=document.getElementById(toselid), i;
	for (i=fslen-1; i>-1; i--) {
		if (fromsel.options[i].selected) {
			refarr.push([fromsel.options[i].text,fromsel.options[i].value,true]);
			fromsel.remove(i);
		}
	}
	refarr=sortByKey(refarr,0);
	filtsel(refarr, document.getElementById(searchid).value, toselid, regexcbid, searchid, 'white', '#ff8080');
	return refarr;
}
function selordalf(selid) {
	var sel=document.getElementById(selid), len=sel.length, arr=[], i;
	for (i=0; i<len; i++)
		arr.push([sel.options[i].text,sel.options[i].value,sel.options[i].selected]);
	arr=sortByKey(arr,0);
	sel.length=0;
	for (i=0; i<len; i++)
		sel.add(new Option(arr[i][0],arr[i][1],false,arr[i][2]));
}
function selordinv(selid) {
	var sel=document.getElementById(selid), len=sel.length, arr=[], i;
	for (i=0; i<len; i++)
		arr.push([sel.options[i].text,sel.options[i].value,sel.options[i].selected]);
	arr=arr.reverse();
	sel.length=0;
	for (i=0; i<len; i++)
		sel.add(new Option(arr[i][0],arr[i][1],false,arr[i][2]));
}
function closeinpup() {
	document.getElementById('inpopup').innerHTML='<div id="popupcont">...</div>';
	document.getElementById('popup').style.display='none';
}
function edit(selid,what,mode) {
	var act='', id=0;
	if (mode=='edit')
		act={0:'Editing',1:'edit'};
	else if (mode=='add')
		act={0:'Adding',1:'add'};
	else if (mode=='remove')
		act={0:'Removing',1:'remove'};
	var sel=document.getElementById(selid);
	if ((mode=='edit' || mode=='remove') && sel.selectedIndex<0) {
		alerta('Error','<p>You have to select an entry to '+act[1]+' ;-)</p>');
	} else {
		if (mode=='edit' || mode=='remove')
			id=sel.options[sel.selectedIndex].value;
		document.getElementById('inpopup').innerHTML='<div class="hiftit"><img src="imgs/p.png" style="float:left;width:24px;height:24px">'+act[0]+' «'+what+'» entry<img src="imgs/close.svg" class="imgbut" style="float:right;" onclick="closeinpup()"></div><iframe src="edinsth.php?w='+selid+'&i='+id+'&m='+mode+'" class="hiframe"></iframe>';
		document.getElementById('popup').style.display='table';
	}
}
function swdis(id) {
	var obj=document.getElementById(id);
	if (obj.disabled)
		obj.disabled=false;
	else
		obj.disabled=true;
}
function ckf() {
	// questo qua sotto resetta eventuale filtro su LocalityID in modo che se c'è valore selezionato ma non mostrato viene passato
	filtsel(locselarr, '', 'LocalityID', 'locselre', 'locsearch', 'white', '#ff8080');
	var selids=['ChosenLangs[]','ChosenFinModes[]','ChosenPolicies[]','ChosenTags[]'], len=selids.length, i, sel, ii;
	for (i=0; i<len; i++) {
		sel=document.getElementById(selids[i]).options;
		for (ii=0; ii<sel.length; ii++)
			sel[ii].selected=true;
	}
	document.getElementById('f').submit();
}
//-->
</script>
</head>
<body>

<nav>
<div id="hmenu">
<ul>
<?php echo($menuout); ?>
</ul>
<div class="mtit">Editing «<?php echo($inst['URI']); ?>»</div>
<div id="rightdiv">
<?php if ($account['Level']!='guest') echo('<img src="'.$notifs['imgoff'].'" id="bell" class="rlinks" title="Show notifications" onclick="shidenotifs();">'.N); ?>
<img src="imgs/esci.svg" class="rlinks" title="Logout" onclick="document.location.href='logout.php';">
</div>
</div>
</nav>

<?php if ($account['Level']!='guest') echo($notifs['div']); ?>

<div id="popup">
<div id="inpopup">
<div id="popupcont">
...
</div>
</div>
</div>

<!-- <div id="footer">
</div> -->

<div id="fullscreen">
<div id="middlerow">

<form action="edinstres.php" method="post" id="f" onsubmit="return ckf();">
<input type="hidden" id="id" name="id" value="<?php echo($_GET['id']+0); ?>">
<table class="edtab">
<tr><td class="insthead"><?php echo($inst['URI']); ?></td></tr>
<tr><td>

<?php
if ($account['Level']!='guest') {
	($inst['Noxious']==1) ? $disabled='' : $disabled=' disabled';
	echo('<table class="cbtab">
<tr><td><label for="Visible">Visible:</label></td><td>&nbsp;<input type="checkbox" name="Visible" id="Visible"'.check($inst,'Visible').'></td></tr>
<tr><td><label for="Noxious">Noxious:</label></td><td>&nbsp;<input type="checkbox" name="Noxious" id="Noxious"'.check($inst,'Noxious').' onChange="swdis(\'NoxReason\');"></td></tr>
</table>
<label for="NoxReason"">Reason why it’s noxious:</label>
<textarea id="NoxReason" name="NoxReason" class="tarea" rows="5"'.$disabled.'>'.hspech($inst['NoxReason']).'</textarea>
<div class="ruler"></div>'.N);
}
?>

<div class="tit"><label for="Priority">Priority</label></div>
<input type="number" name="Priority" id="Priority" class="inptxt" value="<?php echo($inst['Priority']); ?>">
<div class="ruler"></div>

<div class="tit"><label for="OurDesc">Description (local language)</label></div>
<textarea name="OurDesc" id="OurDesc" rows="10" class="tarea"><?php echo(hspech($inst['OurDesc'])); ?></textarea>
<div class="ruler"></div>

<div class="tit"><label for="OurDescEN">Description (english)</label></div>
<textarea name="OurDescEN" id="OurDescEN" rows="10" class="tarea"><?php echo(hspech($inst['OurDescEN'])); ?></textarea>

<?php

function selbox($o) {
	global $link, $account;
	($o['multi']) ? $seladdprops=' multiple' : $seladdprops=' name="'.$o['dispselid'].'"';
	echo('<div class="ruler"></div>
<div class="tit"><label for="'.$o['dispselid'].'">'.$o['title'].'</label></div>'.N);
	if ($account['Level']=='guest')
		echo('<div class="desc">'.$o['desc'].'</div>'.N);
	echo('<table class="picktab"><tr>
<td style="width:98%"><input type="text" placeholder="Filter ..." class="search" title="'.$o['searchtit'].'" id="'.$o['searchid'].'" onkeyup="filtsel('.$o['disparr'].', this.value, \''.$o['dispselid'].'\', \''.$o['regexcbid'].'\', \''.$o['searchid'].'\', \'white\', \'#ff8080\')"></td>
<td style="width:1%;padding-left:3px;padding-right:3px;"><label for="'.$o['regexcbid'].'" title="Use regular espression">RegEx:</label></td>
<td style="width:1%"><input type="checkbox" id="'.$o['regexcbid'].'" style="float:right;" title="Use regular espression" onchange="filtsel('.$o['disparr'].', document.getElementById(\''.$o['searchid'].'\').value, \''.$o['dispselid'].'\', \''.$o['regexcbid'].'\', \''.$o['searchid'].'\', \'white\', \'#ff8080\')"></td>
</tr></table>
<table class="picktab">
<tr>
<td style="width:99%;">
<select id="'.$o['dispselid'].'"'.$seladdprops.' size="8" class="mselect" onchange="'.$o['disparr'].'=updselarr(\''.$o['dispselid'].'\','.$o['disparr'].')">'.N);
	$res=mysqli_query($link,$o['dispque'])
		or muoribene(__LINE__.': '.mysqli_error($link),true);
	while ($row=mysqli_fetch_assoc($res)) {
		$selected='';
		if (!$o['multi'] && $row[$o['valcol']]==$o['instselid']) $selected=' selected';
		echo('<option value="'.$row[$o['valcol']].'"'.$selected.'>'.hspech($row[$o['txtcol']]).'</option>'.N);
	}
	echo('</select>
<script type="text/javascript">
<!--
selordalf(\''.$o['dispselid'].'\');
var '.$o['disparr'].'=getselarr(\''.$o['dispselid'].'\');
//-->
</script>
</td>
<td style="width:1%;">'.N);
	if (!array_key_exists('edavail',$o) || $o['edavail'])
		echo('<div class="butdiv"><img src="imgs/edit.svg" class="imgbut" title="Edit first selected entry" onclick="edit(\''.$o['dispselid'].'\',\''.$o['title'].'\',\'edit\')"></div>'.N);
	if (!array_key_exists('addavail',$o) || $o['addavail'])
		echo('<div class="butdiv"><img src="imgs/plus.svg" class="imgbut" title="Add an entry" onclick="edit(\''.$o['dispselid'].'\',\''.$o['title'].'\',\'add\')"></div>'.N);
	if (!array_key_exists('remavail',$o) || $o['remavail'])
		echo('<div class="butdiv"><img src="imgs/minus.svg" class="imgbut" title="Remove first selected entry" onclick="edit(\''.$o['dispselid'].'\',\''.$o['title'].'\',\'remove\')"></div>'.N);
	if (!$o['multi'])
		echo('<div class="butdiv" style="margin-top:6px"><img src="imgs/deselect.svg" class="imgbut" title="Deselect" onclick="deselect(\''.$o['dispselid'].'\')"></div>'.N);
echo('</td>
</tr>'.N);
	if ($o['multi']) {
		echo('<tr>
<td style="width:99%;">
<div class="move">'.$o['dida'].'
<img src="imgs/fresu.svg" class="imgbut" style="float:left;" title="Move selected entries from chosen list to available list" onclick="'.$o['disparr'].'=selrem(\''.$o['chosenselid'].'\', \''.$o['dispselid'].'\', '.$o['disparr'].', \''.$o['searchid'].'\', \''.$o['regexcbid'].'\')">
<img src="imgs/fregiu.svg" class="imgbut" style="float:right;" title="Move selected entries from available list to chosen list" onclick="'.$o['disparr'].'=seladd(\''.$o['dispselid'].'\', \''.$o['chosenselid'].'\', '.$o['disparr'].')">
</div>
</td>
<td style="width:1%;">
</td>
</tr>
<tr>
<td style="width:99%;">
<select id="'.$o['chosenselid'].'" name="'.$o['chosenselid'].'" multiple size="12" class="mselect">'.N);
		$res=mysqli_query($link,$o['chosenque'])
			or muoribene(__LINE__.': '.mysqli_error($link),true);
		while ($row=mysqli_fetch_assoc($res)) {
			echo('<option value="'.$row[$o['chosenvalcol']].'">'.hspech($row[$o['txtcol']]).'</option>'.N);
		}
		echo('</select>
</td>
<td style="width:1%;">
<div class="butdiv"><img src="imgs/fresucim.svg" class="imgbut" title="Move to top" onclick="seltop(\''.$o['chosenselid'].'\')"></div>
<div class="butdiv"><img src="imgs/fresu.svg" class="imgbut" title="Move up" onclick="selup(\''.$o['chosenselid'].'\')"></div>
<div class="butdiv"><img src="imgs/fregiu.svg" class="imgbut" title="Move down" onclick="seldown(\''.$o['chosenselid'].'\')"></div>
<div class="butdiv"><img src="imgs/fregiufon.svg" class="imgbut" title="Move to bottome" onclick="selbot(\''.$o['chosenselid'].'\')"></div>
<div class="butdiv"><img src="imgs/alph.svg" class="imgbut" title="Sort alphabetically" onclick="selordalf(\''.$o['chosenselid'].'\')"></div>
<div class="butdiv"><img src="imgs/invert.svg" class="imgbut" title="Invert sort" onclick="selordinv(\''.$o['chosenselid'].'\')"></div>
</td>
</tr>'.N);
	}
	echo('</table>'.N);
}

selbox(array(
	'desc'=>'If your instance is mainly bound to a particular location (from neighbourhood to country) you can specify it here. You can add up to '.$account['MaxLocalities'].' location[s]. You can edit or remove only the location[s] you added, and only as long as they have not yet been referenced by others. For particular needs don’t hesitate to <a href="mailto:'.$iniarr['ref_email'].'">contact us</a>.',
	'multi'=>false,
	'dispselid'=>'LocalityID',
	'instselid'=>$inst['LocalityID'],
	'title'=>'Location',
	'searchid'=>'locsearch',
	'searchtit'=>'Filter available locations',
	'disparr'=>'locselarr',
	'regexcbid'=>'locselre',
	'dispque'=>'SELECT ID, Name'.$dlang.' FROM Localities ORDER BY NameOrig ASC',
	'valcol'=>'ID',
	'txtcol'=>'Name'.$dlang));

$o=array(
	'desc'=>'Here you can specify the languages which are mostly used on your instance, in order of importance. The list of available languages to choose from should be fairly complete, but if you find out it is not don’t hesitate to <a href="mailto:'.$iniarr['ref_email'].'">contact us</a>. The list of chosen languages is often pre-populated by our crawler, which tries and guess them automatically, but you can always modify it and prevent the crawler from changing it on its next runs by ticking the “Lock this list” checkbox you find under the list.',
	'multi'=>true,
	'dispselid'=>'DispLangs',
	'title'=>'Languages',
	'searchid'=>'langsearch',
	'searchtit'=>'Filter available languages',
	'disparr'=>'langselarr',
	'regexcbid'=>'langselre',
	'dispque'=>'SELECT ID, CONCAT(Name'.$dlang.'," [",Code,"]") AS Txt FROM Languages WHERE ID NOT IN (SELECT OurLangID FROM InstOurLangs WHERE InstID='.$inst['ID'].') ORDER BY Name'.$dlang.' ASC',
	'valcol'=>'ID',
	'txtcol'=>'Txt',
	'dida'=>'Above: available languages; below: chosen languages',
	'chosenselid'=>'ChosenLangs[]',
	'chosenque'=>'SELECT OurLangID, CONCAT(Name'.$dlang.'," [",Code,"]") AS Txt FROM InstOurLangs LEFT JOIN Languages ON Languages.ID=OurLangID WHERE InstID='.$inst['ID'].' ORDER BY Pos ASC',
	'chosenvalcol'=>'OurLangID');
if ($account['Level']=='guest') {
	$o['edavail']=false;
	$o['addavail']=false;
	$o['remavail']=false;
}
selbox($o);

$lockcheck=check($inst,'OurLangsLock');
if ($account['Level']=='guest' && is_null($inst['LastGuestEdit']))
	$lockcheck=' checked';

?>

<table class="cbtab">
<tr><td><label for="OurLangsLock" title="Prevent languages from being overwritten on next crawler run">Lock this list:&nbsp;</label></td><td><input type="checkbox" id="OurLangsLock" name="OurLangsLock" title="Prevent languages from being overwritten on next crawler run"<?php echo($lockcheck); ?>></td></tr>
</table>

<?php

selbox(array(
	'desc'=>'Here you can specify the modalities by which your instance is financed, in order of importance. You can add up to '.$account['MaxFinancing'].' modalities to the list of available modalities. You can edit or remove only the modalities you added, and only as long as they have not yet been referenced by others.<br>For particular needs don’t hesitate to <a href="mailto:'.$iniarr['ref_email'].'">contact us</a>.',
	'multi'=>true,
	'dispselid'=>'DispFinModes',
	'title'=>'Financing modalities',
	'searchid'=>'finsearch',
	'searchtit'=>'Filter available financing modalities',
	'disparr'=>'finselarr',
	'regexcbid'=>'finselre',
	'dispque'=>'SELECT * FROM Financing WHERE ID NOT IN (SELECT FinID FROM InstFinancing WHERE InstID='.$inst['ID'].') ORDER BY Type ASC',
	'valcol'=>'ID',
	'txtcol'=>'Type',
	'dida'=>'Above: available fin. modes; below: chosen fin. modes',
	'chosenselid'=>'ChosenFinModes[]',
	'chosenque'=>'SELECT * FROM InstFinancing LEFT JOIN Financing ON Financing.ID=FinID WHERE InstID='.$inst['ID'].' ORDER BY Pos ASC',
	'chosenvalcol'=>'FinID'));

selbox(array(
	'desc'=>'Here you can specify the policies defining which kind of contents are allowed or not allowed on your instance. You can add up to '.$account['MaxPolicies'].' policies to the list of available policies. You can edit or remove only the policies you added, and only as long as they have not yet been referenced by others.<br>For particular needs don’t hesitate to <a href="mailto:'.$iniarr['ref_email'].'">contact us</a>.',
	'multi'=>true,
	'dispselid'=>'DispPolicies',
	'title'=>'Policies',
	'searchid'=>'polsearch',
	'searchtit'=>'Filter available policies',
	'disparr'=>'polselarr',
	'regexcbid'=>'polselre',
	'dispque'=>'SELECT * FROM Policies WHERE ID NOT IN (SELECT PolID FROM InstPolicies WHERE InstID='.$inst['ID'].') ORDER BY Name ASC',
	'valcol'=>'ID',
	'txtcol'=>'Name',
	'dida'=>'Above: available policies; below: chosen policies',
	'chosenselid'=>'ChosenPolicies[]',
	'chosenque'=>'SELECT * FROM InstPolicies LEFT JOIN Policies ON Policies.ID=PolID WHERE InstID='.$inst['ID'].' ORDER BY Pos ASC',
	'chosenvalcol'=>'PolID'));

selbox(array(
	'desc'=>'Here you can specify the “thematic” categories your instance is dedicated to, if any. If your instance isn’t particularly dedicated to any particular topic, please just associate it with the “General” category. You can add '.$account['MaxTags'].' categories to the list of available categories. You can edit or remove only the categories you added, and only as long as they have not yet been referenced by others. For particular needs don’t hesitate to <a href="mailto:'.$iniarr['ref_email'].'">contact us</a>.',
	'multi'=>true,
	'dispselid'=>'DispTags',
	'title'=>'Categories',
	'searchid'=>'tagsearch',
	'searchtit'=>'Filter available categories',
	'disparr'=>'tagselarr',
	'regexcbid'=>'tagselre',
	'dispque'=>'SELECT * FROM Tags WHERE ID NOT IN (SELECT TagID FROM InstTags WHERE InstID='.$inst['ID'].') ORDER BY Name ASC',
	'valcol'=>'ID',
	'txtcol'=>'Name',
	'dida'=>'Above: available categories; below: chosen categories',
	'chosenselid'=>'ChosenTags[]',
	'chosenque'=>'SELECT * FROM InstTags LEFT JOIN Tags ON Tags.ID=TagID WHERE InstID='.$inst['ID'].' ORDER BY Pos ASC',
	'chosenvalcol'=>'TagID'));

?>

<div class="ruler"></div>
<input type="button" value="Save" class="button" onclick="ckf()">

</td></tr>
</table>
</form>
</div>
</div>

<div id="debug">
<?php echo($dbg); ?>
</div>

</body>
</html>

<?php

mysqli_close($link);

?>
