<?php
include("postget.php");
include_once("common.php");
checklevel(2,$_REQUEST['clientid']);


//////////////////////////////////////////////////////////////// si se envi� el formulario, cargar los errores
if ($_REQUEST[click]==""._EDIT."" OR $_REQUEST[click]==""._ADD_SUB_CATEGORY."") {
	$errors = '';
	if ($_POST['newname']=='') {$errors.= "<br>".ucfirst(_SUB_CATEGORY)." "._NOT_SPECIFIED."";}
	if ($_POST['category']=='') {$errors.= "<br>".ucfirst(_CATEGORY)." "._NOT_SPECIFIED."";}
}



if($errors=='' AND $_REQUEST[click]==""._EDIT."") {
	//	if($newonlyclients=="on"){$newonlyclients=1;}else{$newonlyclients=0;}
	$query="UPDATE hd_faq_subcategory SET
	name='$newname', 
	category='$category' 
	WHERE id ='$id'";

	if(!mysql_query($query,$db_link)){
		messagetop("ERROR","<b>DEBUG:</b> ".$query."<br><b>MYSQL ERROR:</b> ".mysql_error(),'faq_admin.php',0); 
	}else{
		messagetop(_THE_CHANGES_WHERE_MADE_SUCCESSFULLY,'','faq_admin.php',1);
	}




}else if($errors=='' AND $_REQUEST[click]==""._ADD_SUB_CATEGORY."") {


	$ordermax = query("SELECT max(position) FROM hd_faq_subcategory WHERE langs LIKE '%".$currentlang."%'",1);
	$neworder = $ordermax[0] + 1;

	//	if($newonlyclients=="on"){$newonlyclients=1;}else{$newonlyclients=0;}
	$query="INSERT INTO hd_faq_subcategory (name, category, position, langs)
	
	VALUES ('$newname','$category','$neworder','$currentlang')";


	if(!mysql_query($query,$db_link)){
		messagetop("ERROR","DEBUG: ".$query."<br>MYSQL ERROR: ".mysql_error(),'faq_admin.php',0); 
	}else{
		messagetop(_THE_CHANGES_WHERE_MADE_SUCCESSFULLY,'','faq_admin.php',1);
	}




}else{



	if($_REQUEST[id]) 	{
		$query="SELECT * FROM hd_faq_subcategory WHERE id='$_REQUEST[id]'";
		$output=mysql_query($query,$db_link);
		$row=mysql_fetch_array($output);
	}
	include("head_template.php");
	echo '
<div class="container">
	<div class="row">


<table width="98%" class="table table-bordered">
		<tr>
		<td align="center" width="100%">
';

	print(ucfirst(_FAQS)." > ".ucfirst(_SUB_CATEGORY));

echo '
		</td>
		</tr>
';	
echo '
			<tr valign="top">
				<td>

';

	print(messagetopinthispage(_THIS_ITEM_WILL_BE_CREATED_ONLY_FOR_THE_CURRENT_LANGUAGE." (".langname($currentlang).")","",2));
	//	viewhelp(48);
	print errors($errors); $errors = '';

	print("<form method='POST'><table>");

	print("<tr>
	<td align='right'><b>".ucfirst(_SUB_CATEGORY).":</b></td>
	<td><input name='newname' size='60'");
	if($_REQUEST[id]){print(" value='$row[name]'");}else{print(" value='$_POST[newname]'");}
	print("></td></tr>");

	print("<tr>
	<td align='right'><b>".ucfirst(_CATEGORY).":</b></td>
	<td>
	<select name='category'>");
	$query="SELECT id, name FROM hd_faq_category WHERE langs LIKE '%".$currentlang."%' ORDER BY position";
	$cats=mysql_query($query,$db_link);
	print ("<option value=''>** ".ucfirst(_SELECT_AN_OPTION)." **</option>");
	while($categ=mysql_fetch_array($cats)) {
		if ($_POST['category']) {
			print ("<option value='".$categ[id]."' ".sele($_POST['category'],$categ[id]).">".$categ[name]."</option>");
		}else{
			print ("<option value='".$categ[id]."' ".sele($row[category],$categ[id]).">".$categ[name]."</option>");
		}
	};
	print("</select></td></tr>");

	//	print ("<tr>
	//	<td align='right'>".ucfirst(_ONLY_ALLOW_REGISTERED_CLIENTS_TO_OPEN_TICKETS_IN_THIS_DEPARTMENT).":</td>
	//	<td><input type='checkbox' name='newonlyclients'");
	//	if($_REQUEST[id]){if ($row[onlyclients]==1){print(" checked");};}
	//	print("></td></tr>");


	print("</table><br><input type='hidden' name='id' value='$id'>");
	if($id){
		print("<input type='submit' name='click' value='"._EDIT."'><br>");
	}else{
		print("<input type='submit' name='click' value='"._ADD_SUB_CATEGORY."'><br>");
	}
	print("</form>");
echo '
		</td>
		</tr>
		</table>
</div>
</div>
';
	include("foot_template.php");
}
?>