<?php
include("postget.php");
include_once("common.php");

$pagetitle = ucfirst(_TICKETS)." > ".ucfirst(_DEPARTMENTS);

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


if ($_REQUEST[click]==""._EDIT."" OR $_REQUEST[click]==""._ADD_DEPARTMENT."") {
	$errors = '';
	if ($_POST['newname']=='') {$errors.= "<br>".ucfirst(_NAME)." "._NOT_SPECIFIED."";}
	if ($_POST['newemail']==$config[19][1]) {$errors.= "<br>".ucfirst(_EMAIL)." ".strtolower(_MUST_BE_DIFFERENT_TO)." ".$config[19][1];}
	if (!preg_match ('/'.$regex4mail.'/i', $_POST['newemail'])) {$errors.= "<br>".ucfirst(_INVALID_EMAIL)."";}
}



if($errors=='' AND $_REQUEST[click]==""._EDIT."") {
	if($newonlyclients=="on"){$newonlyclients=1;}else{$newonlyclients=0;}
	$query="UPDATE hd_tickets_dep SET
	name='$newname',
	description='$newdescription',
	email='$newemail',
	onlyclients='$newonlyclients',
	server='$newserver',
	port='$newport',
	user='$newuser',
	password='$newpassword'
	WHERE depid ='$id'";

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




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

	$ordermax = query("SELECT max(orderby) FROM hd_tickets_dep",1);
	$neworder = $ordermax[0] + 1;

	if($newonlyclients=="on"){$newonlyclients=1;}else{$newonlyclients=0;}
	$query="INSERT INTO hd_tickets_dep (name,description,email,orderby,onlyclients,server,port,user,password)

	VALUES ('$newname','$newdescription','$newemail','$neworder','$newonlyclients','$newserver','$newport','$newuser','$newpassword')";


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




}else{



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

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

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

	print("
        <div class='row'>
            <div class='span3'></div>
            <div class='span3'><b>".ucfirst(_NAME).":</b></div>
            <div class='span3'></div>

            <div class='span3'></div>
            <div class='span3'><input name='newname' size='20'");
            if($_REQUEST[id]){print(" value='$row[name]'");}else{print(" value='$_POST[newname]'");}
            print("></div>
            <div class='span3'></div>
        </div>
            ");

	print("<div class='row'>
	<div class='span3'></div>  <div class='span3'>".ucfirst(_DESCRIPTION).":</div><div class='span3'></div>
	<div class='span3'></div>  <div class='span3'><input name='newdescription' size='60'");
	if($_REQUEST[id]){print(" value='$row[description]'");}
	print("></div><div class='span3'></div></div>");

	print("<div class='row'>
	<div class='span3'></div>  <div class='span3'><b>".ucfirst(_EMAIL).":</b></div><div class='span3'></div>
	<div class='span3'></div>  <div class='span3'><input name='newemail' size='60'");
	if($_REQUEST[id]){print(" value='$row[email]'");}else{print(" value='$_POST[newemail]'");}
	print("></div><div class='span3'></div></div>");

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

	print("<div class='row'><div class='span12'>&nbsp;</div></div>");
	print("<div class='row'><div class='span12'>&nbsp;</div></div>");

	print("<div class='row'>
	<div class='span3'></div>  <div class='span3'><b>".strtoupper(_POP3_CONFIGURATION_FOR_IMPORTING_DATA)."</b></div><div class='span3'></div></div><div class='row'>
	<div class='span3'></div>  <div class='span3'>".ucfirst(_SERVER).":</div><div class='span3'></div>
	<div class='span3'></div>  <div class='span3'><input name='newserver' size='20'");
	if($_REQUEST[id]){print(" value='$row[server]'");}
	print("></div><div class='span3'></div></div>");

	print("<div class='row'>
	<div class='span3'></div>  <div class='span3'>".ucfirst(_PORT).":</div><div class='span3'></div>
	<div class='span3'></div>  <div class='span3'><input name='newport' size='3'");
	if($_REQUEST[id]){print(" value='$row[port]'");}else{print(" value='110'");}
	print("></div><div class='span3'></div></div>");

	print("<div class='row'>
	<div class='span3'></div>  <div class='span3'>".ucfirst(_USER).":</div><div class='span3'></div>
	<div class='span3'></div>  <div class='span3'><input name='newuser' size='20'");
	if($_REQUEST[id]){print(" value='$row[user]'");}
	print("></div><div class='span3'></div></div>");

	print("<div class='row'>
	<div class='span3'></div>  <div class='span3'>".ucfirst(_PASSWORD).":</div><div class='span3'></div>
	<div class='span3'></div>  <div class='span3'><input type='password' name='newpassword' size='20'");
	if($_REQUEST[id]){print(" value='$row[password]'");}
	print("></div><div class='span3'></div></div>");


	print("<div class='row'>
            <div class='span4'></div>
            <div class='span4'>
            <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_DEPARTMENT."'><br>");
	}
	print("
            </div>
          <div class='span4'></div>
</div>
</form>");
echo '
</div>
</div>
';
	include("foot_template.php");
}
?>