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

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

if ($_REQUEST[click]==""._EDIT."" OR $_REQUEST[click]==""._ADD_CANNED_RESPONSE."") {
	$errors = '';
	if ($_POST['newname']=='') {$errors.= "<br>".ucfirst(_NAME)." "._NOT_SPECIFIED."";}
	if ($_POST['newdescription']=='') {$errors.= "<br>".ucfirst(_DESCRIPTION)." "._NOT_SPECIFIED."";}
}



if($errors=='' AND $_REQUEST[click]==""._EDIT."") {
	$query="UPDATE hd_tickets_canned SET 
	name='$newname', 
	description='$newdescription', 
	dept='$newdept', 
	langs='$currentlang' 
	WHERE id ='$id'";

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

	
	
	
}else if($errors=='' AND $_REQUEST[click]==""._ADD_CANNED_RESPONSE."") {
	
	$ordermax = query("SELECT max(orderby) FROM hd_tickets_canned WHERE langs LIKE '%".$currentlang."%'",1);
	$neworder = $ordermax[0] + 1;

	$query="INSERT INTO hd_tickets_canned (name,description,dept,orderby,langs) 
	VALUES ('$newname','$newdescription','$newdept','$neworder','$currentlang')";
	
	
	if(!mysql_query($query,$db_link)){
		messagetop("ERROR","DEBUG: ".$query."<br>MYSQL ERROR: ".mysql_error(),'canned.php',0); 
	}else{
		go2('canned.php');
	}

	
	
	
}else{
	
	
	
	if($_REQUEST[id]) {
		$query="SELECT * FROM hd_tickets_canned 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 language="javascript" type="text/javascript">
tinyMCE.init({
	mode : "textareas",
	theme : "advanced"
});
</script>
<?php


	echo '
<div class="container">
	<div class="row">
';
	print(messagetopinthispage(_THIS_ITEM_WILL_BE_CREATED_ONLY_FOR_THE_CURRENT_LANGUAGE." (".langname($currentlang).")","",2));

	print errors($errors); $errors = '';

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

	print("<tr>
	<td align='right'><b>".ucfirst(_NAME).":</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(_DESCRIPTION).":</b></td>
	<td>
	<TEXTAREA cols=60 rows=6 name='newdescription'>");
	if($_REQUEST[id]){print ($row[description]);}else{print($_POST[newdescription]);}
	print("</TEXTAREA>");
	print("</td></tr>");
	
	print("<tr>
	<td align='right'><b>".ucfirst(_DEPARTMENT).":</b></td>
	<td>
	<select name='newdept'>");
			$query="SELECT depid,name FROM hd_tickets_dep ORDER BY orderby";
			$depa=mysql_query($query,$db_link);
			print ("<option value='0' ".sele($row[dept],0).">".ucfirst(_ALL)."</option>");
			while($deparow=mysql_fetch_array($depa)) {
				print ("<option value='".$deparow[depid]."' ".sele($row[dept],$deparow[depid]).">".$deparow[name]."</option>");
			};
			print("
	</select>
	</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_CANNED_RESPONSE."'><br>");
	}
	print("</form>");
echo '
</div>
</div>
';
	include("foot_template.php");
}
?>