<?php
# ************** LICENCE ****************
/*
	Copyright (c) PORTANEO.

	This file is part of POSH (Portaneo Open Source Homepage) http://sourceforge.net/projects/posh/.

	POSH is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version

	POSH is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Posh.  If not, see <http://www.gnu.org/licenses/>.
*/
# ***************************************
# POSH Module management - add a new directory script
#
# !! be careful, this file must be saved under uft8 format, and display an e accentuated here : é
# ***************************************

require_once("includes_api.php");
require_once("authentication.php");

header ('Content-Type: text/javascript; charset=utf-8');
$catSelected =  isset($_GET["dir"])?$_GET["dir"]:-1;
$result = "var categories = [";

$DB->getResults($xml_getCategories);
    
if ($DB->nbResults()>0)
{
    while ($row = $DB->fetch(0))
    {
         $result .=  "{'category':".$row['id'].",'name':'".$row['name']."','lang':'".$row['lang']."'},";
    }

    $result =  substr($result,0, - 1);
    
    $result .= "];";
    echo $result;
    $DB->freeResults();
}

?>
<?php
if( isset($_GET["c"]) ) {
?>
	function selectpCategory() {
		var indexCat = document.getElementById('portaneo_category');
		document.getElementById("pcategory").value = indexCat.options[indexCat.selectedIndex].value;
	}
	
	function buildSelectOptionCategory() {
		var catSelect = "<select id='portaneo_category' name='portaneo_category' onchange='selectpCategory()' >";
		var sel = "";
	    for(var i=0; i<categories.length; i++) {
	        sel = "";
	        if(categories[i].category == <?php echo $catSelected; ?> ) {
	            sel = " selected ";
				if(document.getElementById("pcategory"))
					document.getElementById("pcategory").value = categories[i].category;
			}
	        catSelect += "<option "+sel+" label='"+categories[i].name+"' value='"+categories[i].category+"'>"+categories[i].name+" ("+categories[i].lang+") </option>";
	    }
	    catSelect += "</select>";
		if(document.getElementById("pcatDiv"))
			document.getElementById("pcatDiv").innerHTML = catSelect;
	}
	buildSelectOptionCategory();

<?php
} else {
?>
	function buildSelectOptionCategory() {
	    var catSelect = "<select id='portaneo_category' name='portaneo_category'>";
	    var sel = "";
	    for(var i=0; i<categories.length; i++) {
	        sel = "";
	        if(categories[i].category == <?php echo $catSelected; ?> )
	            sel = " selected ";
	        catSelect += "<option "+sel+" label='"+categories[i].name+"' value='"+categories[i].category+"'>"+categories[i].name+" ("+categories[i].lang+") </option>";
	    }
	    catSelect += "</select>";
	    document.getElementById("pcategory").innerHTML = catSelect;
	}
	buildSelectOptionCategory();
<?php
}
?>