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

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

if ($_REQUEST[click]==""._EDIT."" OR $_REQUEST[click]==""._ADD_NOTE."") {
	$errors = '';
//	if ($_POST['newtitle']=='') {$errors.= "<br>".ucfirst(_TITLE)." "._NOT_SPECIFIED."";}
	if ($_POST['newdescription']=='') {$errors.= "<br>".ucfirst(_DESCRIPTION)." "._NOT_SPECIFIED."";}
	if ($_POST['category']=='' AND $_POST['subcategory']=='') {$errors.= "<br>".ucfirst(_CATEGORY)." ".strtolower(_OR)." ".ucfirst(_SUB_CATEGORY)." "._NOT_SPECIFIED."";}
	if ($_POST['newonlyclients']=='') {$errors.= "<br>".ucfirst(_ACCESS)." "._NOT_SPECIFIED."";}
}



if($errors=='' AND $_REQUEST[click]==""._EDIT."") {
	$neweditor = $_SESSION['hd_idc'];
	$now=time()+$config[31][1]*3600;

	if ($newtitle=='') {$newtitle = strip_tags(substr($newdescription,0,40));}

	//	if($subcategory!=0){$category=0;}
	$query="UPDATE hd_notes SET
	title='$newtitle',
	description='$newdescription',
	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(),'notes_admin.php',0);
	}else{
		messagetop(_THE_CHANGES_WHERE_MADE_SUCCESSFULLY,'','notes_admin.php',1);
	}




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

	if ($_POST['subcategory']) {
		$ordermax = query("SELECT max(position) FROM hd_notes WHERE subcategory=".$_POST['subcategory'],1);
	}else{
		$ordermax = query("SELECT max(position) FROM hd_notes WHERE category=".$_POST['category'],1);
	}

	$neworder = $ordermax[0] + 1;

	$neweditor = $_SESSION['hd_idc'];
	$now=time()+$config[31][1]*3600;
	if ($newtitle=='') {$newtitle = strip_tags(substr($newdescription,0,40));}

	$query="INSERT INTO hd_notes (
	title,
	description,
	editor,
	editeddate,
	category,
	subcategory,
	position,
	onlyclients
	)

	VALUES (
	'$newtitle',
	'$newdescription',
	'$neweditor',
	'$now',
	'$category',
	'$subcategory',
	'$neworder',
	'$newonlyclients'
	)";


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




}else{


	if($_REQUEST[id]) 	{
		$query="SELECT * FROM hd_notes 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 table-striped">
';

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

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

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

	print("<tr>
	<td align='right'><b>".ucfirst(_DESCRIPTION).": </b></td>
	<td>
	<TEXTAREA cols=127 rows=16 name=newdescription>");
	if($_REQUEST[id]){print(stripslashes($row[description]));}else{print(stripslashes($_POST[newdescription]));}
	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_notes_category 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_notes_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_notes_subcategory WHERE category='".$_POST[category]."' ORDER BY position";
		}else{
			$query="SELECT id, name FROM hd_notes_subcategory WHERE category='".$row[category]."' 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'><b>".ucfirst(_ACCESS).": </b></td>
		<td>");


	if ($_SESSION[hd_accesslvl]==2 OR $_SESSION[hd_accesslvl]==4) {
                echo '<label class="radio">';
		print("<input type='radio' name='newonlyclients' value='2'");
		if($_REQUEST[id]){if ($row[onlyclients]==2){print(" checked");};}elseif ($_SESSION[hd_accesslvl]==2){print(" checked");}
		print("><span class='metro-radio'>".ucfirst(_SUPPORT_STAFF)."</span> &nbsp;&nbsp;");
                echo '</label>';
	}
	if ($_SESSION[hd_accesslvl]==4) {
                echo '<label class="radio">';
		print("<input type='radio' name='newonlyclients' value='4'");
		if($_REQUEST[id]){if ($row[onlyclients]==4){print(" checked");};}
		print("><span class='metro-radio'>".ucfirst(_ADMINISTRATOR)."</span> &nbsp;&nbsp;");
                echo '</label>';
	}


	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_NOTE."'><br>");
	}
	print("</form>");
echo '
		</td>
		</tr>
		</table>
</div>
</div>
';
	include("foot_template.php");
}
?>

