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

$pagetitle = lang("_TICKETS");

///////////////////////////////////////// verifica que el modulo est� activo para los invitados
if (substr($config[64][1],0,1)=='0' AND substr($config[65][1],0,1)=='0') {
	no_access();
}

///////////////////////////////////////// verifica que el modulo est� activo para los invitados si se llama desde contact
if (substr($config[65][1],0,1)=='0' AND $fromcontact){
	no_access();
}



//$thisclient=1;

//////////////////////////////////////////////////////////////// si se envi� el formulario, cargar los errores
if ($_REQUEST[replyclick]==""._SEND."") {

	////////////////////////////////////// CAPTCHA
	if ($config[79][1]==1) {
		session_start();
		include_once 'securimage/securimage.php';
		$securimage = new Securimage();
	}
	////////////////////////////////////// FIN CAPTCHA

	$estiloerror = "for='inputError'";
	$errors = '';
	if ($_POST['firstname']=='') {$errors.= "<br>".ucfirst(_FIRST_NAME)." "._NOT_SPECIFIED.""; $estilofirstname=$estiloerror;}
	if ($_POST['lastname']=='') {$errors.= "<br>".ucfirst(_LAST_NAME)." "._NOT_SPECIFIED.""; $estilolastname=$estiloerror;}
	//	if ($_POST['email']=='') {$errors.= "<br>".ucfirst(_EMAIL)." "._NOT_SPECIFIED."";}
	if (!preg_match ('/'.$regex4mail.'/i', $_POST['email'])) {$errors.= "<br>".ucfirst(_INVALID_EMAIL).""; $estiloemail=$estiloerror;}
	if ($_POST['title']=='') {$errors.= "<br>".ucfirst(_SUBJECT)." "._NOT_SPECIFIED.""; $estilotitle=$estiloerror;}
	if ($_POST['description']=='') {$errors.= "<br>".ucfirst(_DESCRIPTION)." "._NOT_SPECIFIED.""; $estilodescription=$estiloerror;}
	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('Email',$_POST['email'])) {$errors.= antispam('Email',$_POST['email']);}
	if (antispam('Domain',$_POST['email'])) {$errors.= antispam('Domain',$_POST['email']);}
	if (antispam('Subject',$_POST['title'])) {$errors.= antispam('Subject',$_POST['title']);}
	if (antispam('Body',$_POST['description'])) {$errors.= antispam('Body',$_POST['description']);}

	////////////////////////////////////// CAPTCHA
	if ($config[79][1]==1) {
		if ($securimage->check($_POST['captcha_code']) == false) {$errors.= "<br>".ucfirst(_SECURITY_CODE)." "._WRONG.""; $estilocaptcha=$estiloerror;}
	}
	////////////////////////////////////// FIN CAPTCHA

}


//////////////////////////////////////////////////////////////// si no hay errores y se presion� SEND
if($errors=='' AND $_REQUEST[replyclick]==""._SEND."") {

	//////////////////////////////////////////////////////// subir el adjunto
	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;
	}

	openticket(
	'0',
	$_POST['title'],
	$_POST['description'],
	$_POST['status'],
	$_POST['priority'],
	$_POST['product'],
	$_POST['department'],
	$attachment,
	'0',
	$_POST['firstname']." ".$_POST['lastname'],
	$_POST['email']
	);

	messagetop(strtoupper(_TICKET_SENT),_WE_VE_SENT_AN_EMAIL_WITH_INFORMATION_ON_THIS_TICKET,"ticketsview.php?reference=".$reference,1); 


}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",
	theme : "simple"
});
</script>
<?php

	//	if($_SESSION['hd_accesslvl']==1) {
	//		include("menu_client.php");
	//	}

//	if (substr($config[73][1],0,1)=='1') {
//		include("menu_guest.php");
//	}

	/////////////////////////////////////////////////// VER TICKET EXISTENTE
	echo '
<div class="container">
	<div class="row">

		<table width="98%" class="table table-bordered">
			<tr>
				<td align="center" width="100%">
';
	echo "".ucfirst(_SEE_EXISTING_TICKET)."";
echo '
				</td>
			</tr>
';	
echo '
			<tr valign="top">
				<td>

';

	print("
	<form method='GET' name='viewguestticket' action='ticketsview.php?reference=".$_GET['reference']."' style='margin-bottom:0;'>
 	<b>".ucfirst(_REFERENCE_TICKET).": </b> 	
	<input name='reference' size='20'>
	<input type=submit name=guestclick value='".ucfirst(_VIEW)."'>
	</form>
	");


echo '
				</td>
			</tr>
		</table>
	</div>
</div>
';
	////////////////////////////////////////////////////////////////////////////

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

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


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

	echo "".ucfirst(_OPEN_NEW_TICKET)."";

	if ($fromcontact) {
		$imagenAmostrar='contact_.png';
	}else{
		$imagenAmostrar='ticket_.png';
	}

echo '
		</td>
		</tr>
';	
echo '
			<tr valign="top">
				<td>

';


	print("<form method='POST' enctype='multipart/form-data' class='form-horizontal'> ");
	echo '<div class="control-group">';
	print("
		<label class='control-label'>".ucfirst(_DEPARTMENT)."</label>
		<div class='controls'>
					<select name='department'>");
		$query="SELECT depid,name FROM hd_tickets_dep WHERE onlyclients='0'";
		$Deptos=mysql_query($query,$db_link);
		while($depart=mysql_fetch_array($Deptos)) {
			print ("<option value='".$depart[depid]."' ".sele($_POST['department'],$depart[depid]).">".$depart[name]."</option>");
		};
		print("</select>
		</div>");

	echo '</div><div class="control-group">';

	print("
		<label class='control-label'>".ucfirst(_PRIORITY)."</label>
		<div class='controls'>
			<select name='priority'>
				<option value='low' ".sele($_POST['priority'],'low').">".ucfirst(_LOW)."</option>
				<option value='medium' ".sele($_POST['priority'],'medium').">".ucfirst(_MEDIUM)."</option>
				<option value='high' ".sele($_POST['priority'],'high').">".ucfirst(_HIGH)."</option>
			</select>
			<input type='hidden' name='status' value='1'>
			<input type='hidden' name='product' value='0'>
		</div>
		
		</div><div class='control-group'>

		<label class='control-label' $estilofirstname>".ucfirst(_FIRST_NAME)."</label>
		<div class='controls'>
			<input name='firstname' value='".$_POST['firstname']."' $estiloname>
		</div>
		
		</div><div class='control-group'>

		<label class='control-label' $estilolastname>".ucfirst(_LAST_NAME)."</label>
		<div class='controls'>
			<input name='lastname' value='".$_POST['lastname']."'>
		</div>
		
		</div><div class='control-group'>

		<label class='control-label' $estiloemail>".ucfirst(_EMAIL)."</label>
			<div class='controls'>
			<input class='input-xlarge' name='email' value='".$_POST['email']."'>
		</div>
		
		</div><div class='control-group'>

		<label class='control-label' $estilotitle>".ucfirst(_SUBJECT)."</label>
			<div class='controls'>
			<input class='input-xxlarge' name='title' value='".$_POST['title']."'>
		</div>
		
		</div><div class='control-group'>

		<label class='control-label' $estilodescription>".ucfirst(_DESCRIPTION)."</label>
			<div class='controls'>
					<TEXTAREA class='input-xxlarge' cols=80 rows=8 name=description>".$_POST['description']."</TEXTAREA>
			</div>
		
		</div><div class='control-group'>

		<label class='control-label'>".ucfirst(_ATTACHMENT)."</label>
		<div class='controls'>
			<spam class='help-block'>".ucfirst(_ALLOWED_FILE_TYPES).": ".$config[9][1]."</spam>
			<input type='file' name='attachment'>
		</div>
		");


	////////////////////////////////////// CAPTCHA
	if ($config[79][1]==1) {
		print("
		
		<div class='control-group'>

		<label class='control-label' $estilocaptcha>".ucfirst(_TYPE_THE_FOLLOWING_CODE)."
		<img id='captcha' src='securimage/securimage_show.php' alt='CAPTCHA Image' />
		</label>
				<div class='controls'>
				<input type='text' name='captcha_code' size='20' maxlength='20' />
				<a href='#' onclick=\"document.getElementById('captcha').src = 'securimage/securimage_show.php?' + Math.random(); return false\">".ucfirst(_RELOAD_IMAGE)."</a>
				</div>
		");
	}
	////////////////////////////////////// FIN DEL CAPTCHA

	print("
		</div><div class='control-group'>
		
	<input type=submit name=replyclick value='"._SEND."'>
	</div>
	</form>
	");

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