<?php
include("postget.php");
include_once("common.php");
$pagetitle = lang("_TICKETS");
include("ticketsfunc.php");

if ($_REQUEST['clientid']<>0 AND $_REQUEST['email']==''){
	checklevel(1,$_REQUEST['clientid']);
	$thisclient = 1;
}

if ($_REQUEST[option]=="delete" AND $_SESSION['hd_accesslvl']==4) {
	$query="DELETE FROM hd_tickets WHERE reference='$_REQUEST[reference]'";
	if(!mysql_query($query,$db_link)){
		messagetop(_ERROR,mysql_error(),'ticketsview.php?reference='.$_REQUEST[reference].'&clientid='.$_SESSION['hd_clientid'].'',0); 
	}else{
		messagetop(_THE_CHANGES_WHERE_MADE_SUCCESSFULLY,'','main.php',1); 
	}

}

if ($_REQUEST[replyclick]==""._REPLY."") {
	$errors = '';
	if ($_POST['newreply']=='') {$errors.= "<br>".ucfirst(_ANSWER).": "._NOT_SPECIFIED."";}

	if ($_FILES['attachment']['name']<>'') {
		$extension = explode('.',$_FILES['attachment']['name']) ;
		$extension = array_pop($extension) ;
		if (substr_count($config[9][1],$extension)==0) {$errors.= "<br>".ucfirst(_INVALID_FILE_TYPE_IN_ATTACHMENT)."";}
	}

	if (antispam('Body',$_POST['newreply'])) {$errors.= antispam('Body',$_POST['newreply']);}
}

if ($_POST['currentstatus']<>'') {
	$query="UPDATE hd_tickets SET status='".$_POST['currentstatus']."' WHERE ticketid='$parent'";
	if(!mysql_query($query,$db_link)){
		messagetop(_ERROR,"DEBUG: ".$query."<br>ERROR: ".mysql_error(),'tickets.php',0);
	}
}

if ($_POST['product']<>'') {
	$query="UPDATE hd_tickets SET product='".$_POST['product']."' WHERE ticketid='$parent'";
	if(!mysql_query($query,$db_link)){
		messagetop(_ERROR,"DEBUG: ".$query."<br>ERROR: ".mysql_error(),'tickets.php',0);
	}
}

if ($_POST['newdepartment']<>'') {
	$query="UPDATE hd_tickets SET depid='".$_POST['newdepartment']."' WHERE ticketid='$parent'";
	if(!mysql_query($query,$db_link)){
		messagetop(_ERROR,"DEBUG: ".$query."<br>ERROR: ".mysql_error(),'tickets.php',0);
	}
}

if ($_POST['internalnote']<>'') {
	$query="UPDATE hd_tickets SET internalnote='".$_POST['internalnote']."' WHERE ticketid='$parent'";
	if(!mysql_query($query,$db_link)){
		messagetop(_ERROR,"DEBUG: ".$query."<br>ERROR: ".mysql_error(),'tickets.php',0);
	}
}


if ($_POST['newreplynativo']<>'') {
	$_POST['newreply'] = $_POST['newreply']." ".translate(nl2br($_POST['newreplynativo']),$currentlang,$_SESSION['preflang']);
	$_POST['newreplynativo'] = '';
}


if ($_POST['canned']<>'') {
	$resprow = query("SELECT id,name,description FROM hd_tickets_canned WHERE id=".$_POST['canned'],1);
	$_POST['newreply'] = $_POST['newreply']." ".$resprow['description'];
}

if ($_POST['faq']<>'') {
	$resprow = query("SELECT id,question,answer,category,subcategory FROM hd_faq WHERE id=".$_POST['faq'],1);
	$respuesta = "
	<b>".strtoupper(lang('_QUESTION',$_SESSION[preflang])).":</b> ".$resprow[question]."
	<br><b>".strtoupper(lang('_ANSWER',$_SESSION[preflang])).":</b><br>".$resprow[answer]."
	<br>";

	if ($resprow[subcategory]==0) {
		$linkref = $serverurl."/faq.php?c=".$resprow[category]."&i=".$resprow[id];
	}else{
		$linkref = $serverurl."/faq.php?s=".$resprow[subcategory]."&i=".$resprow[id];
	}

	$respuesta .= "<b>".lang('_REFERENCE',$_SESSION[preflang]).":</b> <a href='".$linkref."' target='_blank'>".$linkref."</a>";

	$_POST['newreply'] = $_POST['newreply']." ".$respuesta;
}


if ($_POST['manual']<>'') {
	$resprow = query("SELECT id,question,answer,category,subcategory FROM hd_manuals_pages WHERE id=".$_POST['manual'],1);
	$respuesta = "
	<b>".strtoupper(lang('_QUESTION',$_SESSION[preflang])).":</b> ".$resprow[question]."
	<br><b>".strtoupper(lang('_ANSWER',$_SESSION[preflang])).":</b><br>".$resprow[answer]."
	<br>";

	if ($resprow[subcategory]==0) {
		$linkref = $serverurl."/manuals.php?c=".$resprow[category]."&i=".$resprow[id];
	}else{
		$linkref = $serverurl."/manuals.php?s=".$resprow[subcategory]."&i=".$resprow[id];
	}

	$respuesta .= "<b>".lang('_REFERENCE',$_SESSION[preflang]).":</b> <a href='".$linkref."' target='_blank'>".$linkref."</a>";
	
	$respuesta = replacellaves($respuesta);

	$_POST['newreply'] = $_POST['newreply']." ".$respuesta;
}




if($errors=='' AND $_REQUEST[replyclick]==""._REPLY."") {
	if ($_FILES['attachment']['name']) 	{
		$newfilename = time() . '_' . $_FILES['attachment']['name'];
		$attachdir = 'attachments/';
		if(!@copy ($_FILES['attachment']['tmp_name'], $attachdir . $newfilename)) {
			$errors= error_get_last();
			echo "COPY ERROR: ".$errors['type'];
			echo "<br />\n".$errors['message']; exit;
		}
		$attachment = $newfilename;
	}

	$query="UPDATE hd_tickets SET status='".$_POST['newstatus']."' WHERE ticketid='$parent'";
	if(!mysql_query($query,$db_link)){
		messagetop(_ERROR,"DEBUG: ".$query."<br>ERROR: ".mysql_error(),'tickets.php',0);
	}

	if ($_SESSION['hd_accesslvl']>1) {
		if($followup=="on"){$followup=1;}else{$followup=0;}
		$query="UPDATE hd_tickets SET followup='".$followup."' WHERE ticketid='$parent'";
		if(!mysql_query($query,$db_link)){
			messagetop(_ERROR,"DEBUG: ".$query."<br>ERROR: ".mysql_error(),'tickets.php',0);
		}
	}

	$now=time()+$config[31][1]*3600;
	$query="UPDATE hd_tickets SET lastreply='".$now."' WHERE ticketid='$parent'";
	if(!mysql_query($query,$db_link)){
		messagetop(_ERROR,"DEBUG: ".$query."<br>ERROR: ".mysql_error(),'tickets.php',0);
	}

	$query="UPDATE hd_tickets SET lastreplier='".$_SESSION['hd_idc']."' WHERE ticketid='$parent'";
	if(!mysql_query($query,$db_link)){
		messagetop(_ERROR,"DEBUG: ".$query."<br>ERROR: ".mysql_error(),'tickets.php',0);
	}


	addreply(
	$_POST['parent'],
	$_SESSION['hd_clientid'],
	$_POST['newreply'],
	$attachment
	);

	if ($_SESSION['hd_accesslvl']>1) {
		go2("ticketsview.php?reference=".$_REQUEST['reference']."&clientid=".$_REQUEST['clientid']."");
	}else{
		go2("ticketsview.php?reference=".$_REQUEST['reference']);
	}


}else{

	if (!is_numeric($_REQUEST['reference'])) {
		messagetop(_ERROR,ucfirst(_REFERENCE_TICKET_INVALID),'index.php',0);
	}

	if (isset($_SESSION['hd_accesslvl'])) {
		include("head_template.php");
	}else{
		include("head_template_guest.php");
	}
	
	?>
<script language="javascript" type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
	mode : "textareas",
	relative_urls : false,
	remove_script_host : false,
	theme : "simple",
	editor_deselector : "mceNoEditor", // class="mceNoEditor" will not have tinyMCE
	editor_selector : "mceEditor" // class="mceEditor" will.
});
</script>
<?php

	$now=time()+$config[31][1]*3600;
	echo '
<div class="container">
<div class="row">


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

	echo "".ucfirst(_TICKET)." #".$_REQUEST['reference'];

	if ($_SESSION['hd_accesslvl']==4) {
		print("&nbsp;&nbsp;&nbsp;&nbsp;
		<a href='ticketsview.php?reference=".$_REQUEST['reference']."&clientid=".$_SESSION['hd_clientid']."&option=delete'><img class='sprites del' src='images/transparent.gif' border='0' title='"._DELETE."'></a>
		");
	}
	
	
echo '
		</td>
		</tr>
';	
echo '
			<tr valign="top">
				<td>

';

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

	$tickets = query("SELECT * FROM hd_tickets WHERE hd_tickets.reference=".$_REQUEST['reference'],1);

	if (mysql_affected_rows()==0) {
		messagetop(_ERROR,ucfirst(_REFERENCE_TICKET_INVALID),'index.php',0);
		echo '
		</td>
		</tr>
		</table>
</div>
</div>
';
		exit;
	}

	$query="SELECT * FROM hd_tickets_replies WHERE parent=".$tickets['ticketid']." ORDER BY date ASC";
	$output=mysql_query($query,$db_link);

	echo '<table class="table table-bordered">';

		print("<thead><tr>
		<th>".ucfirst(_LAST_REPLY)."</th> 
		<th>".ucfirst(_DEPARTMENT)."</th> 
		<th>".ucfirst(_SUBJECT)."</th>
		<th>".ucfirst(_SUBMITTED_BY)."</th>
		<th>".ucfirst(_PRIORITY)."</th> 
		<th colspan='2'>".ucfirst(_STATUS)."</th>
		");
	
		print("</tr></thead><tbody>");

		print("<tr>");
			print("<td class='frame-title' align='center'>");
			if ($tickets[lastreply]==0) {
				print(TimeAgo($tickets[date],$now));
			}else{
				print(TimeAgo($tickets[lastreply],$now));
			}
			print("</td>");

			$depto = query('SELECT name,depid FROM hd_tickets_dep WHERE hd_tickets_dep.depid = '.$tickets[depid], 1);
			print("<td class='frame-title' align='center'>$depto[0]</td>");
			print("<td align='left'>$tickets[title]");
			print("</td>");
			
			
			if ($tickets['clientid']==0) {
				print("<td align='center' style='border: 6px solid red;'>");
				print("
					<b>".ucfirst(_NAME).":</b> ".$tickets['name']." ["._GUEST."] 
					<br><b>".ucfirst(_EMAIL).":</b> ".$tickets['email'] );
				if ($_SESSION['hd_accesslvl']>1) {
					print("&nbsp;&nbsp;&nbsp;&nbsp;
							<a href='markasspam.php?type=Email&text=".$tickets['email']."'
							target='_blank' 
							onClick=\"window.open(this.href, this.target, 'width=400, height=200, left=200, top=200, scrollbars=1'); 
							return false;\">
							<b>(".strtolower(_MARK_AS_SPAM).")</b></a>");
				}
				print("</td>");
			}else{
				print("<td align='center'>");
				print("<b>".ucfirst(_CLIENT).":</b> ".$tickets['clientid']);
				print("</td>");
			}
			
			
			
			
			switch ($tickets[priority]) {
				case 'low':
					print("<td align='center' valign='middle' bgcolor='gray' class='sizeextra'>".lang('_'.$tickets[priority])."</td>");
					break;
				case 'medium':
					print("<td align='center' valign='middle' bgcolor='green' class='sizeextra'>".lang('_'.$tickets[priority])."</td>");
					break;
				case 'high':
					print("<td align='center' valign='middle' bgcolor='red' class='sizeextra'>".lang('_'.$tickets[priority])."</td>");
					break;
			}


			if ($tickets[followup]=='1') {
				$img_followup="<img class='sprites followup' src='images/transparent.gif' title='".ucfirst(_FOLLOW_UP)."'>&nbsp;&nbsp;&nbsp;";
			}else{
				$img_followup='';
			}
		
		
			if ($tickets[status]<>'5' OR $tickets[followup]=='1') {
				print("<form method='POST' enctype='multipart/form-data'> ");
			}
		
			if ($_SESSION['hd_accesslvl']>1){
					$disabledstatus = "";
			}else{
					$disabledstatus = "disabled";
			}
		

			if ($tickets[status]=='1') {
				$color='#ff4200';
			}elseif ($tickets[status]=='2') {
				$color='yellow';
			}elseif ($tickets[status]=='3') {
				$color='#4ad31e';
			}elseif ($tickets[status]=='4') {
				$color='orange';
			}elseif ($tickets[status]=='5') {
				$color='gray';
			}
	
			$estadoactual = "<select name='currentstatus' onChange='this.form.submit()' $disabledstatus class='sizeextra' 
			style='background-color: ".$color."; border-color: ".$color.";'>
			<option value='1' ".sele($tickets[status],'1').">".strtoupper(_NEW)."</option>
			<option value='2' ".sele($tickets[status],'2').">".strtoupper(_PENDING)."</option>
			<option value='3' ".sele($tickets[status],'3').">".strtoupper(_ANSWERED)."</option>
			<option value='4' ".sele($tickets[status],'4').">".strtoupper(_CUSTOMER_REPLY)."</option>
			<option value='5' ".sele($tickets[status],'5').">".strtoupper(_CLOSED)."</option>
					</select> ";

			print("<td width='1' nowrap class='frame-title' align='center' bgcolor='".$color."'>$img_followup</td>");
			print("<td width='1' nowrap class='frame-title' align='center' bgcolor='".$color."'><b>".$estadoactual."</b></td>");




		print("</tr>");

		print("</tbody>
	<table>");

	echo '<table class="table table-striped">';
		print("<tr>");

			if ($config[110][1] == 1) {
				if(is_numeric($tickets[user])){
					$estilofondo = "background-image: url(themes/$theme/40.png);";
//					$staff_client_img = "<img src='images/customer_icon.png' title='"._MESSAGE_WRITTEN_BY_THE_CLIENT."' alt='"._MESSAGE_WRITTEN_BY_THE_CLIENT."'><br>";
					$staff_client_img = "";
				}else{
					$estilofondo = "";
//					$staff_client_img = "<img src='images/staff_icon.png' title='"._MESSAGE_WRITTEN_BY_THE_STAFF."' alt='"._MESSAGE_WRITTEN_BY_THE_STAFF."'><br>";
					$staff_client_img = "";
				}
			}else{
				if(is_numeric($tickets[user])){
					$estilofondo = "background-image: url(themes/$theme/40.png);";
					$staff_client_img = "<b>".lang('_MESSAGE_WRITTEN_BY_THE_CLIENT')."</b><br><br>";
				}else{
					$estilofondo = "";
					$staff_client_img = "<b>".lang('_MESSAGE_WRITTEN_BY_THE_STAFF')."</b><br><br>";
				}
			}
			if ($tickets[attachment]) {
				$extension = explode('.',$tickets[attachment]) ;
				$extension = array_pop($extension) ;
				if ($extension=='jpg' OR $extension=='gif' OR $extension=='png') {
					$imagen =  "attachments/".$tickets[attachment]."" ;
				}elseif (file_exists("images/fileformats/".$extension.".png")){
					$imagen =  "images/fileformats/".$extension.".png" ;
				}else{
					$imagen =  "images/fileformats/other.png" ;
				}
				print("
					<td width='1%' align='left' valign='top' style='border: 2px solid $color; $estilofondo'>".$staff_client_img."".date($df.' H:i:s',$tickets[date])."</td> 
					<td colspan='2' align='left' valign='top' width='90%' style='border: 2px solid $color; $estilofondo'><font size='2'>".stripslashes($tickets[message])."</font></td>
					<td align='right' width='100' style='border: 2px solid $color; $estilofondo'>
					<a href='ticketsviewattach.php?attachment=$tickets[attachment]&ext=$extension' target='_blank' onClick=\"window.open(this.href, this.target, 'width=600,height=700'); return false;\">
					<img src='".$imagen."' width='100' border='1' title='"._SEE_ATTACHMENT."'>
					</a>
					</td>");
			}else{
				print("
					<td width='1%' align='left' valign='top' style='border: 2px solid $color; $estilofondo'>".$staff_client_img."".date($df.' H:i:s',$tickets[date])."</td> 
					<td align='left' valign='top' colspan='3' style='border: 2px solid $color; $estilofondo'><font size='2'>".stripslashes($tickets[message])."</font></td>");
			}
		print("</tr>");

		if ($_SESSION['hd_accesslvl']>1 AND $currentlang <> $_SESSION['preflang'] AND $thisclient == 1 AND $config[109][1] == 1) {
			print("<tr><td></td><td align='center'>");
			$mitexto = "<div align='left'>
			<strong>".lang('_SUBJECT').":</strong> 
			".translate($tickets[title],$_SESSION['preflang'],$currentlang)."</br>
			<strong>".lang('_MESSAGE').":</strong> 
			".translate($tickets[message],$_SESSION['preflang'],$currentlang)."
			</div>";
			print(messagetopinthispage(lang('_TRANSLATION')." (".langname($_SESSION['preflang'])." <img class='sprites right_arrow' src='images/transparent.gif'> ".langname($currentlang).")",$mitexto,3));
			print("</td></tr>");
		}


		$tiempoanterior = $tickets[date];

		while($replies=mysql_fetch_array($output)) {

			if ($config[80][1]==1){
				print("<TR>");
				print("<td align='left' colspan='4'>
			<img class='sprites clock' src='images/transparent.gif' border='0' title='' align='absmiddle'>
			".TimeAgo($tiempoanterior,$replies[date])."</td></tr>");
				$tiempoanterior = $replies[date];
			}

			print("<TR>");
			if(is_numeric($replies[user])){
				$estilofondo = "style = 'background-image: url(themes/$theme/40.png)'";
				if ($config[110][1] == 1) {
					$staff_client_img = "<img src='images/customer_icon.png' title='"._MESSAGE_WRITTEN_BY_THE_CLIENT."' alt='"._MESSAGE_WRITTEN_BY_THE_CLIENT."'><br>";
				}else{
					$staff_client_img = "<b>".lang('_MESSAGE_WRITTEN_BY_THE_CLIENT')."</b><br><br>";
				}
				
				if ($replies[attachment]) {
					$extension = explode('.',$replies[attachment]) ;
					$extension = array_pop($extension) ;
					if ($extension=='jpg' OR $extension=='gif' OR $extension=='png') {
						$imagen =  "attachments/".$replies[attachment]."" ;
					}elseif (file_exists("images/fileformats/".$extension.".png")){
						$imagen =  "images/fileformats/".$extension.".png" ;
					}else{
						$imagen =  "images/fileformats/other.png" ;
					}
					print("
				<td width='1%' align='left' valign='top' $estilofondo>".$staff_client_img."".date($df.' H:i:s',$replies[date])."</td> 
				<td align='left' valign='top' colspan='2' width='90%' $estilofondo><font size='2'>".stripslashes($replies[message])."</font></td>
				<td align='right' width='100' $estilofondo>
				<a href='ticketsviewattach.php?attachment=$replies[attachment]&ext=$extension' target='_blank' onClick=\"window.open(this.href, this.target, 'width=600,height=700'); return false;\">
				<img src='".$imagen."' width='100' border='1' title='"._SEE_ATTACHMENT."'>
				</a>
				</td>
				</tr>");
				}else{
					print("
				<td align='left' valign='top' $estilofondo>".$staff_client_img."".date($df.' H:i:s',$replies[date])."</td> 
				<td align='left' valign='top' colspan='3' $estilofondo><font size='2'>".stripslashes($replies[message])."</font></td>
				</tr>");
				}
			}else{
				
				if (!is_numeric($replies[user]) AND is_numeric($_SESSION['hd_idc'])) {
					if ($replies[isread] == NULL) {
						$readtext = time();
					}else{
						$readtext = $replies[isread]."|".time();
					}
					$query="UPDATE hd_tickets_replies SET isread = '".$readtext."' WHERE id='".$replies[id]."'";
					if(!mysql_query($query,$db_link)){
						messagetop(_ERROR,"DEBUG: ".$query."<br>ERROR: ".mysql_error(),'tickets.php',0);
					}
				}
				if ($replies[date] > 1300821000) {
					if ($replies[isread] == NULL) {
						$leido = "<img class='sprites unread btip' src='images/transparent.gif' title='".lang('_NOT_READ_BY_THE_CLIENT')."' alt='".lang('_NOT_READ_BY_THE_CLIENT')."'>";					
					}else{
						$lectura = explode("|",$replies[isread]);
						$cuantos = count($lectura);
						$conta = 0;
						if (isset($lectura2)) {unset($lectura2);}
						foreach ($lectura as $key => $value) {
							$conta ++;
							if ($cuantos < 7) {
								$fechaformateada = date($df,$value)." - ".date("G:i:s",$value)." (".TimeAgo($value,time()).")";
								$lectura2[$conta-1] = "<br>".$fechaformateada;
							}else{
								if ($conta < 4) {
									$fechaformateada = date($df,$value)." - ".date("G:i:s",$value)." (".TimeAgo($value,time()).")";
									$lectura2[$conta-1] = "<br>".$fechaformateada;
								}
								if ($conta == 4) {
									$lectura2[$conta-1] = "<br>...... (".($cuantos-6)." +)";
								}
								if ($conta > ($cuantos-3)) {
									$fechaformateada = date($df,$value)." - ".date("G:i:s",$value)." (".TimeAgo($value,time()).")";
									$lectura2[$conta-1] = "<br>".$fechaformateada;
								}
							}
						}
						$fechaformateada = implode($lectura2);
						
						$leido = "<img class='sprites read btip' src='images/transparent.gif' title='<strong>".lang('_READ_BY_THE_CLIENT').":</strong> <br>".$fechaformateada."' alt='".lang('_READ_BY_THE_CLIENT')."'>";					
					}
				}else{
					$leido = "";
				}
				$estilofondo = "";
				if ($config[110][1] == 1) {
					$staff_client_img = "<img src='images/staff_icon.png' title='"._MESSAGE_WRITTEN_BY_THE_STAFF."' alt='"._MESSAGE_WRITTEN_BY_THE_CLIENT."'><br>";
				}else{
					$staff_client_img = "<b>".lang('_MESSAGE_WRITTEN_BY_THE_STAFF')."</b><br><br>";
				}
				if ($replies[attachment]) {
					$extension = explode('.',$replies[attachment]) ;
					$extension = array_pop($extension) ;
					if ($extension=='jpg' OR $extension=='gif' OR $extension=='png') {
						$imagen =  "attachments/".$replies[attachment]."" ;
					}elseif (file_exists("images/fileformats/".$extension.".png")){
						$imagen =  "images/fileformats/".$extension.".png" ;
					}else{
						$imagen =  "images/fileformats/other.png" ;
					}
					print("
				<td width='1%' align='left' valign='top' $estilofondo>".$staff_client_img."".date($df.' H:i:s',$replies[date])."</td> 
				<td colspan='2' align='left' valign='top' width='90%' $estilofondo>".$leido."<font size='2'>".stripslashes($replies[message])."</font></td>
				<td align='right' width='100' $estilofondo>
				<a href='ticketsviewattach.php?attachment=$replies[attachment]&ext=$extension' target='_blank' onClick=\"window.open(this.href, this.target, 'width=600,height=700'); return false;\">
				<img src='".$imagen."' width='100' border='1' title='"._SEE_ATTACHMENT."'>
				</a>
				</td>
				</tr>");
				}else{
					print("
				<td align='left' valign='top' $estilofondo>".$staff_client_img."".date($df.' H:i:s',$replies[date])."</td> 
				<td align='left' valign='top' colspan='3' $estilofondo>".$leido."<font size='2'>".stripslashes($replies[message])."</font></td>
				</tr>");
				}

			}

			if ($_SESSION['hd_accesslvl']>1 AND $currentlang <> $_SESSION['preflang'] AND $thisclient == 1 AND $config[109][1] == 1) {
				print("<tr><td></td><td align='center'>");
				$mitexto = "<div align='left'>
				".translate($replies[message],$_SESSION['preflang'],$currentlang)."
				</div>";
				print(messagetopinthispage(lang('_TRANSLATION')." (".langname($_SESSION['preflang'])." <img class='sprites right_arrow' src='images/transparent.gif'> ".langname($currentlang).")",$mitexto,3));
				print("</td></tr>");
			}


		}

	print("</table>");



	print("<hr>");


	if ($_SESSION['hd_accesslvl']>1) {
		$textareacolumns = 160;
	}else{
		$textareacolumns = 160;
	}
	print("<table border='0'>");

		if ($tickets[status]<>'5' OR $tickets[followup]=='1') {
			$text4reply = "";
			if ($_SESSION['hd_accesslvl']>1 AND $currentlang <> $_SESSION['preflang'] AND $thisclient == 1 AND $config[109][1] == 1) {
			$textoconvariables = lang('_WRITE_IN_X_AND_TRANSLATE_INTO_Y');
			$textoconvariables = str_replace('%x%',langname($currentlang),$textoconvariables);
			$textoconvariables = str_replace('%y%',"<i>\"".langname($_SESSION['preflang'])."\"</i>",$textoconvariables);
			$text4reply = " "._IN." <i>\"".langname($_SESSION['preflang'])."\"</i>";
			print("
				<tr>
					<td valign='top' align='center' colspan='3'>
					<br><b><font size='+1'>".$textoconvariables.":</font></b><br>
					<TEXTAREA class='mceNoEditor input-xxlarge' cols='".$textareacolumns."' rows='8' name='newreplynativo' onChange='this.form.submit()'>".$_POST['newreplynativo']."</TEXTAREA>
					<br><span class='btnformyellow'>".lang('_TRANSLATE')."</span>
					</td>
				</tr>");
			}

			print("
			<tr>
				<td valign='top' align='center' colspan='3'>
				<br><b><font size='+1'>".ucfirst(_REPLY)."".$text4reply.":</font></b><br>
				<TEXTAREA class='mceEditor input-xxlarge' cols='".$textareacolumns."' rows='12' name='newreply'>".$_POST['newreply']."</TEXTAREA>
				</td>
			</tr>
			<tr>
				
				<td valign='top' align='left'><br>
					<table border='0'>");

						if ($_SESSION['hd_accesslvl']>1) {
							print("<tr><td align='left'>".ucfirst(_INTERNAL_NOTE)." ("._NOT_VISIBLE_TO_THE_CLIENT."): <br>
							<TEXTAREA class='mceNoEditor input-xxlarge' cols='60' rows='3' name='internalnote' onChange='this.form.submit()'>".$tickets['internalnote']."</TEXTAREA>");
							print("
							</td></tr>");

							print("<tr><td align='left'>".ucfirst(_REASSIGN_TO).": <br>
							<select name='newdepartment' onChange='this.form.submit()'>");
							$query="SELECT depid,name FROM hd_tickets_dep";
							$Deptos=mysql_query($query,$db_link);
							while($depart=mysql_fetch_array($Deptos)) {
								print ("<option value='".$depart[depid]."' ".sele($depto[1],$depart[depid]).">".$depart[name]."</option>");
							};
							print("
							</select>
							</td></tr>
							");

							print("<tr><td align='left'>".ucfirst(_CANNED_RESPONSES)." (".ucfirst($depto[0])."): <br>
							<select name='canned' onChange='this.form.submit()'>");
							if (isset($_SESSION['preflang'])) {
								$donde = "AND langs='".$_SESSION['preflang']."'";
							}else{
								$donde = "";
							}
							$query="SELECT id,name,langs FROM hd_tickets_canned WHERE (dept=0 OR dept=$depto[1]) $donde ORDER BY orderby";
							$canned=mysql_query($query,$db_link);
							print ("<option value=''>"._SELECT_AN_OPTION."</option>");
							while($cannedrow=mysql_fetch_array($canned)) {
								print ("<option value='".$cannedrow[id]."'>[".$cannedrow[langs]."] ".substr($cannedrow[name],0,40)."</option>");
							};
							print("
							</select>
							</td></tr>
							");
							print("<tr><td align='left'>".ucfirst(_INSERT_FROM_FAQS).":<br>
							<select name='faq' onChange='this.form.submit()'>");
							if (isset($_SESSION['preflang'])) {
								$donde = "WHERE hd_faq.langs='".$_SESSION['preflang']."'";
							}else{
								$donde = "";
							}
							$query="SELECT hd_faq.id AS id, hd_faq.question AS question, hd_faq.langs AS langs, hd_faq_category.name AS category, hd_faq_subcategory.name AS subcategory
							FROM hd_faq 
							LEFT JOIN hd_faq_category ON hd_faq.category = hd_faq_category.id 
							LEFT JOIN hd_faq_subcategory ON hd_faq.subcategory = hd_faq_subcategory.id 
							 $donde 
							ORDER BY category,subcategory,question";
							$faq=mysql_query($query,$db_link);
							print ("<option value=''>"._SELECT_AN_OPTION."</option>");
							while($faqrow=mysql_fetch_array($faq)) {
								$grupo1 = "[".$faqrow[langs]."] ".strtoupper($faqrow[category])." > ".strtoupper($faqrow[subcategory])."";
								if (substr($grupo1,-2)=='> ') {$grupo1=substr($grupo1,0,-2);}
								if ($grupo1 <> $grupo2) {
									if (isset($grupo2)) {
										print ("</optgroup>");
									}
									print ("<optgroup label='".$grupo1."'>");
								}
								print ("<option value='".$faqrow[id]."'>".substr($faqrow[question],0,40)."</option>");
								$grupo2 = "";
									
								$grupo2 = "[".$faqrow[langs]."] ".strtoupper($faqrow[category])." > ".strtoupper($faqrow[subcategory])."";
								if (substr($grupo2,-2)=='> ') {$grupo2=substr($grupo2,0,-2);}
								
							};
							print ("</optgroup>");
							
							print("
							</select>
							</td></tr>
							");

							print("<tr><td align='left'>".ucfirst(_INSERT_FROM_MANUALS).": <br>
							<select name='manual' onChange='this.form.submit()'>");
							if (isset($_SESSION['preflang'])) {
								$donde = "WHERE hd_manuals_pages.langs='".$_SESSION['preflang']."'";
							}else{
								$donde = "";
							}
							$query="SELECT hd_manuals_pages.id AS id, hd_manuals_pages.question AS question, hd_manuals_pages.langs AS langs, hd_manuals_manuals.name AS manual, hd_manuals_chapters.name AS chapter
							FROM hd_manuals_pages 
							LEFT JOIN hd_manuals_manuals ON hd_manuals_pages.category = hd_manuals_manuals.id 
							LEFT JOIN hd_manuals_chapters ON hd_manuals_pages.subcategory = hd_manuals_chapters.id 
							$donde  
							ORDER BY manual,chapter,question";
							$manual=mysql_query($query,$db_link);
							print ("<option value=''>"._SELECT_AN_OPTION."</option>");

							while($manualrow=mysql_fetch_array($manual)) {
								$grupo1 = "[".$manualrow[langs]."] ".strtoupper($manualrow[manual])." > ".strtoupper($manualrow[chapter])."";
								if (substr($grupo1,-2)=='> ') {$grupo1=substr($grupo1,0,-2);}
								if ($grupo1 <> $grupo2) {
									if (isset($grupo2)) {
										print ("</optgroup>");
									}
									print ("<optgroup label='".$grupo1."'>");
								}
								print ("<option value='".$manualrow[id]."'>".substr($manualrow[question],0,40)."</option>");
								$grupo2 = "";
									
								$grupo2 = "[".$manualrow[langs]."] ".strtoupper($manualrow[manual])." > ".strtoupper($manualrow[chapter])."";
								if (substr($grupo2,-2)=='> ') {$grupo2=substr($grupo2,0,-2);}
							};
							print ("</optgroup>");

							print("
							</select>
							</td></tr>
							");
						}
					print("</table>");
			
				print("</td>
				<td width='15'>&nbsp;</td>
				<td valign='top' align='center'>");
					print("
					<br>
					".ucfirst(_ATTACHMENT)."<br>(".ucfirst(_ALLOWED_FILE_TYPES).": ".$config[9][1]."):<br>
                                        <input type='file' name='attachment'>
					<br>
					".ucfirst(_WHEN_I_REPLY_THE_STATE_OF_THE_TICKET_WILL_BECOMES).":<br>");

					if ($_SESSION['hd_accesslvl']==1) {
						print("<select name='newstatus'>
									<option value='4' style='color:orange; font-size:12px; font-weight:bold;' selected>".ucfirst(_CUSTOMER_REPLY)."</option>
									<option value='5' style='color:gray; font-size:12px; font-weight:bold;'>".ucfirst(_CLOSED)."</option>
								</select> ");
					}elseif ($_SESSION['hd_accesslvl']>1){
						print("<select name='newstatus'>
									<option value='3' style='color:green; font-size:12px; font-weight:bold;' selected>".ucfirst(_ANSWERED)."</option>
									<option value='2' style='color:#CCCC33; font-size:12px; font-weight:bold;'>".ucfirst(_PENDING)."</option>
									<option value='5' style='color:gray; font-size:12px; font-weight:bold;'>".ucfirst(_CLOSED)."</option>
								</select> ");
					}else{
						print("<select name='newstatus'>
									<option value='4' style='color:orange; font-size:12px; font-weight:bold;' selected>".ucfirst(_CUSTOMER_REPLY)."</option>
									<option value='5' style='color:gray; font-size:12px; font-weight:bold;'>".ucfirst(_CLOSED)."</option>
								</select> ");
					}
					print("<br>");

					if ($_SESSION['hd_accesslvl']>1){
						print("<input type='checkbox' name='followup'");
						if ($tickets[followup]==1){print(" checked");}
						print("><img class='sprites followup' src='images/transparent.gif' title='".ucfirst(_FOLLOW_UP)."'> ".ucfirst(_FOLLOW_UP)."");
					}

					print("<br><br>
					<input type=hidden name=parent value='".$tickets['ticketid']."'>
					<input type=hidden name=reference value='".$tickets['reference']."'>
					<input type=hidden name=email value='".$tickets['email']."'>
					<input type=hidden name=name value='".$tickets['name']."'>
					<input type=hidden name=depto value='".$depto[0]."'>
					<input type=hidden name=clientid value='".$_SESSION['hd_clientid']."'>
					<input type=submit name=replyclick value='"._REPLY."'>
					</form>");

					print("<br><br>
					<table border='0' cellpadding='0' cellspacing='0' align='center'><tr>");
						redbutton("ticketsclose.php?reference=".$tickets['reference']."&ticketid=".$tickets['ticketid']."",_CLICK_HERE_IF_THIS_CASE_IS_CONSIDERED_TO_BE_RESOLVED);
					print("</tr></table>");

					if (!isset($_SESSION['hd_accesslvl'])){
						print("<br><br><table border='0' cellpadding='0' cellspacing='0' align='center'><tr>");
						graybutton("index.php",_RETURN);
						print("</tr></table>");
					}

				print("</td>
			</tr>");
		}

	print("</table>");


echo '
		</td>
		</tr>
		</table>
</div>
</div>
';
	
	include("foot_template.php");
}
?>