<?php
include("postget.php");
include_once("common.php");
$pagetitle =_FAQS;

checklevel(2,$_REQUEST['clientid']);

//////////////////////////////////////////////////////////////// si se envi� el formulario, cargar los errores
if ($_REQUEST[click]==""._EDIT."" OR $_REQUEST[click]==""._ADD_FAQ."") {
	$errors = '';
	if ($_POST['newquestion']=='') {$errors.= "<br>".ucfirst(_QUESTION)." "._NOT_SPECIFIED."";}
	if ($_POST['newanswer']=='') {$errors.= "<br>".ucfirst(_ANSWER)." "._NOT_SPECIFIED."";}
	if ($_POST['category']=='' AND $_POST['subcategory']=='') {$errors.= "<br>".ucfirst(_CATEGORY)." ".strtolower(_OR)." ".ucfirst(_SUB_CATEGORY)." "._NOT_SPECIFIED."";}
}



if($errors=='' AND $_REQUEST[click]==""._EDIT."") {
	if($newonlyclients=="on"){$newonlyclients=1;}else{$newonlyclients=0;}
	$neweditor = $_SESSION['hd_idc'];
	$now=time()+$config[31][1]*3600;
	
	//	if($subcategory!=0){$category=0;}
	$query="UPDATE hd_faq SET
	question='$newquestion', 
	answer='$newanswer', 
	editor='$neweditor', 
	editeddate='$now', 
	category='$category', 
	subcategory='$subcategory', 
	onlyclients='$newonlyclients' 
	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_FAQ."") {

	if ($_POST['subcategory']) {
		$ordermax = query("SELECT max(position) FROM hd_faq WHERE langs LIKE '%".$currentlang."%' AND subcategory=".$_POST['subcategory'],1);
	}else{
		$ordermax = query("SELECT max(position) FROM hd_faq WHERE langs LIKE '%".$currentlang."%' AND category=".$_POST['category'],1);
	}

	$neworder = $ordermax[0] + 1;

	if($newonlyclients=="on"){$newonlyclients=1;}else{$newonlyclients=0;}
	
	$neweditor = $_SESSION['hd_idc'];
	$now=time()+$config[31][1]*3600;
	
	$query="INSERT INTO hd_faq (
	question, 
	answer, 
	editor, 
	editeddate, 
	category, 
	subcategory, 
	position, 
	onlyclients, 
	langs
	)
	
	VALUES (
	'$newquestion', 
	'$newanswer', 
	'$neweditor', 
	'$now', 
	'$category', 
	'$subcategory', 
	'$neworder',
	'$newonlyclients', 
	'$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 WHERE id='$_REQUEST[id]'";
		$output=mysql_query($query,$db_link);
		$row=mysql_fetch_array($output);
	}
	include("head_template.php");
	
	
?>
<script language="javascript" type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({




	// General options
	mode : "textareas",
	theme : "advanced",
	plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

	// produce BR en lugar de P
	//	forced_root_block : false,
	//	force_br_newlines : true,
	//	force_p_newlines : false,



	// Theme options
	theme_advanced_buttons1 : "undo,redo,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
	theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true,
	
//	extended_valid_elements : "input[id|name|type|size|value],textarea[cols|rows]",
	extended_valid_elements : "input[id|name|type|size|value]",
	

	// Example content CSS (should be your site CSS)
	//	content_css : "css/example.css",

	// Drop lists for link/image/media/template dialogs
	//	template_external_list_url : "js/template_list.js",
	external_link_list_url : "<?php echo $config[1][1]?>/website_linkslist.php",
	external_image_list_url : "<?php echo $config[1][1]?>/website_imagelist.php",
	//	media_external_list_url : "js/media_list.js",

	relative_urls : false,
	remove_script_host : true,
	document_base_url : "<?php echo $config[1][1]?>/website/"

	// Replace values for the template plugin
	//	template_replace_values : {
	//		username : "Some User",
	//		staffid : "991234"
	//	}
});
</script>
<?php

	echo '
<div class="container">
	<div class="row">


<table class="table">
';


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

	//////////////////////////////////////////////////////////////////////////// agregar faq 
	if ($_REQUEST['manualid']) {
		$resprow = query("SELECT id,question,answer FROM hd_manuals_pages WHERE id=".$_REQUEST['manualid'],1);
		$NEWquestion = $resprow[question];
		$NEWanswer = $resprow[answer];
	}else{
		$NEWquestion = $_REQUEST[newquestion];
		$NEWanswer = $_REQUEST[newanswer];
	}
	/////////////////////////////////////////////////////////////////////////////////////////	
	
	print("<form method='POST'><table>");
	
	print("<tr>
	<td align='right'><b>".ucfirst(_QUESTION).":</b></td>
	<td><input name='newquestion' size='130'");
	if($_REQUEST[id]){print(" value='$row[question]'");}else{print(" value='$NEWquestion'");}
	print("></td></tr>");

	print("<tr>
	<td align='right'><b>".ucfirst(_ANSWER).":</b></td>
	<td>
	<TEXTAREA cols=127 rows=16 name=newanswer>");
	if($_REQUEST[id]){print(stripslashes($row[answer]));}else{print(stripslashes($NEWanswer));}
	print("</TEXTAREA>");
	print("</td></tr>");




	print("<tr>
	<td align='right'><b>".ucfirst(_CATEGORY).":</b></td>
	<td>
	<select name='category' onChange='this.form.submit()'>");
	$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>");
	//	if ($_POST[category]){
	//		$subcategory = query("SELECT * FROM hd_faq_subcategory WHERE category='$_POST[category]'",1);
	//	}
	print("</td></tr>");



	if ($_POST[category] OR $_REQUEST[id]){
		print("<tr>
	<td align='right'><b>".ucfirst(_SUB_CATEGORY).":</b></td>
	<td>
	<select name='subcategory'>");
		if ($_POST['category']) {
			$query="SELECT id, name FROM hd_faq_subcategory WHERE category='".$_POST[category]."' AND langs LIKE '%".$currentlang."%' ORDER BY position";
		}else{
			$query="SELECT id, name FROM hd_faq_subcategory WHERE category='".$row[category]."' AND langs LIKE '%".$currentlang."%' ORDER BY position";
		}
		$subcats=mysql_query($query,$db_link);
		print ("<option value=''>** ".ucfirst(_NONE)." **</option>");
		while($subcateg=mysql_fetch_array($subcats)) {
			if ($_POST['subcategory']) {
				print ("<option value='".$subcateg[id]."' ".sele($_POST['subcategory'],$subcateg[id]).">".$subcateg[name]."</option>");
			}else{
				print ("<option value='".$subcateg[id]."' ".sele($row[subcategory],$subcateg[id]).">".$subcateg[name]."</option>");
			}
		};
		print("</select>");
		print("</td></tr>");
	}



	print ("<tr>
		<td align='right'></td>
		<td><input type='checkbox' name='newonlyclients'");
	if($_REQUEST[id]){if ($row[onlyclients]==1){print(" checked");};}
	print(">".ucfirst(_VISIBLE_ONLY_TO_REGISTERED_CLIENTS)."</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_FAQ."'><br>");
	}
	print("</form>");
echo '
		</td>
		</tr>
		</table>
</div>
</div>
';
	include("foot_template.php");
}
?>