<?php
/**
* @version 1.0.0
* @package RStickets! 1.0.0
* @copyright (C) 2007-2008 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

// DIE()
defined('_JEXEC') or die('Restricted access');

if (rst_is_staff_logged() && $_SESSION['RST_User']['Group']['CanAddTicket'] == 0 && $_SESSION['RST_User']['Group']['CanAddTicketUsers'] == 0 && $_SESSION['RST_User']['Group']['CanAddTicketStaff'] == 0) rst_redirect('index.php?option=com_rstickets&page=my_tickets');

if (!rst_is_logged() && $T_SETTINGS['RStickets_Add_Tickets'] == 'users')
{
	echo '<p style="padding-top: 15px;"><strong>'.ERROR_ADDTICKET.'</strong></p>';
	return;
}
?>
<form method="post" action="<?php echo JRoute::_('index.php?option=com_rstickets&page=add_ticket&action=add_ticket'); ?>" onsubmit="return validate_ticket(this);" enctype="multipart/form-data">
	<table cellpadding="4" cellspacing="0" class="tableGrey wide">
		<thead>
		<tr class="tableHead">
			<td colspan="2"><?php echo LBL_SUBMIT_NEW_TICKET; ?></td>
		</tr>
		</thead>
		<tbody>
		<?php
		if (!isset($_SESSION['RST_User']['UserId']))
		{
		?>
		<tr>
			<td><?php echo LBL_YOUR_EMAIL; ?></td>
			<td><input type="text" name="Email" value="<?php echo isset($_POST['Email']) ? $_POST['Email'] : '' ?>" /></td>
		</tr>
		<tr>
			<td><?php echo LBL_YOUR_FULLNAME; ?></td>
			<td><input type="text" name="Fullname" value="<?php echo isset($_POST['Fullname']) ? $_POST['Fullname'] : '' ?>" /></td>
		</tr>
		<?php
		}
		elseif (rst_is_staff_logged() && ($_SESSION['RST_User']['Group']['CanAddTicketUsers'] == '1' || $_SESSION['RST_User']['Group']['CanAddTicketStaff'] == '1'))
		{
		?>
		<tr>
			<td><?php echo BTN_SEARCH; ?><img src="<?php echo RST_FRONTEND_REL_PATH; ?>/img/loading.gif" alt="" style="width: 13px; height: 13px; display: none" id="LoadingDiv" /></td>
			<td>
			<input type="text" name="" value="" onkeyup="setTimeout('rst_search_users(document.getElementById(\'rst_search_user\').value,\'<?php echo RST_FRONTEND_SCRIPT_PATH;?>\')',750);" id="rst_search_user" />
			<input type="hidden" name="" value="<?php echo time().'000' - 4000; ?>" id="rst_last_search" />
			<input type="hidden" name="" value="" id="rst_last_search_value" />
			</td>
		</tr>
		<tr>
			<td><?php echo LBL_USERNAME; ?></td>
			<td><div id="UserIdDiv">
				<?php
				$users = rst_get_users(0,($_SESSION['RST_User']['Group']['CanAddTicket'] == 1 ? 0 : 1),$_SESSION['RST_User']['Group']['CanAddTicketUsers'],$_SESSION['RST_User']['Group']['CanAddTicketStaff']);
				// rst_get_users($id=0,$remove_self=0,$show_users=1,$show_staff=0)
				if (count($users) > 0)
				{
					echo '<select name="UserId" size="5">';					
					foreach ($users as $user)
						echo '<option '.(isset($_POST['UserId']) && $_POST['UserId'] == $user['UserId'] ? 'selected="selected"' : '').' value="'.$user['UserId'].'">'.$user['Username'].' ('.$user['Fullname'].')</option>';
					echo '</select>';
				}
				else echo '<strong>'.LBL_NO_USERS.'</strong>';
				?>
				</div>
			</td>
		</tr>
		<?php
		}
		?>
		<tr>
			<td><?php echo LBL_DEPARTMENT; ?></td>
			<td><div id="DepartmentIdDiv">
				<?php
				$departments = rst_get_departments();
				if ($T_SETTINGS['Staff_Can_Add_Tickets'] == 'assigned' && count($departments) > 0 && rst_is_staff_logged())
					$departments = $_SESSION['RST_User']['Department'];
				
				if (count($departments) == 0)
					echo '<strong>'.LBL_NO_DEPARTMENTS.'</strong><br/>';
				if (count($departments) == 1)
					echo '<input type="hidden" name="DepartmentId" value="'.$departments[0]['DepartmentId'].'" />'.$departments[0]['DepartmentName'];
				if (count($departments) > 1)
				{
					echo '<select name="DepartmentId" onchange="show_custom_fields(this,\''.LBL_ATTACHMENT_EXT.'\')"><option value="">'.LBL_PLEASE_DEPARTMENT.'</option>';
					foreach ($departments as $department)
						echo '<option '.(isset($_POST['DepartmentId']) && $_POST['DepartmentId'] == $department['DepartmentId'] ? 'selected="selected"' : (isset($_GET['departmentId']) && $_GET['departmentId'] == $department['DepartmentId'] ? 'selected="selected"' : '')).' value="'.$department['DepartmentId'].'">'.$department['DepartmentName'].'</option>';
					echo '</select>';
				}
				?>
				</div>
			</td>
		</tr>
		<?php
		if (count($departments) == 1)
			$_POST['DepartmentId'] = $departments[0]['DepartmentId'];
		
		if (isset($_GET['departmentId']) && intval($_GET['departmentId']) > 0)
			$_POST['DepartmentId'] = $_GET['departmentId'];
		
		echo rst_gen_custom_fields();
		?>
		<tr>
			<td><?php echo LBL_SUBJECT; ?></td>
			<td><input type="text" name="TicketSubject" value="<?php echo isset($_POST['TicketSubject']) ? $_POST['TicketSubject'] : '' ?>" /></td>
		</tr>
		<tr>
			<td><?php echo LBL_MESSAGE; ?></td>
			<td><textarea name="TicketMessage" cols="20" rows="5"><?php echo isset($_POST['TicketMessage']) ? $_POST['TicketMessage'] : '' ?></textarea></td>
		</tr>
		<tr>
			<td><?php echo LBL_PRIORITY; ?></td>
			<td>
				<select name="TicketPriority">
				<?php
				$priority_array = array('low','normal','high');
				foreach ($priority_array as $priority)
					echo '<option '.(isset($_POST['TicketPriority']) && $_POST['TicketPriority'] == $priority ? 'selected="selected"' : (count($departments) == 1 && $department['DepartmentPriority'] == $priority ? 'selected="selected"' : '')).' value="'.$priority.'">'.$priority_array_lang[$priority].'</option>';
				?>
				</select>
			</td>
		</tr>
		<tr id="file_upload" class="<?php
		if (count($departments) > 0)
		foreach ($departments as $department)
		{
			$class = '';
			if ((isset($_POST['DepartmentId']) && $_POST['DepartmentId'] == $department['DepartmentId']) || (isset($_GET['departmentId']) && $_GET['departmentId'] == $department['DepartmentId']))
				switch ($department['FileUpload'])
				{
					case 'yes':default:$class = '';break;
					case 'users':$class = isset($_SESSION['RST_User']['UserId']) ? '' : 'custom_fields';break;
					case 'no':$class = 'custom_fields';break;
				}
			echo $class;
		}
		?>">
			<td><?php echo LBL_ATTACHMENT; ?></td>
			<td><input type="file" name="FileName" /><br/>
			<span id="rst_attachment_ext"><?php
			echo LBL_ATTACHMENT_EXT;
			if (count($departments) > 0 && isset($_POST['DepartmentId']))
				foreach ($departments as $department)
					if ($department['DepartmentId'] == $_POST['DepartmentId'])
						echo (is_array($department['FileUploadExtensions']) && count($department['FileUploadExtensions']) > 0 ? implode(', ',$department['FileUploadExtensions']) : '*');
			?></span></td>
		</tr>
		
		<?php if ($T_SETTINGS['Captcha_Enabled'] == 1 && ((strpos($T_SETTINGS['Captcha_Enabled_For'],'unregistered') !== false && !rst_is_logged()) || (strpos($T_SETTINGS['Captcha_Enabled_For'],'users') !== false && rst_is_customer_logged()) || (strpos($T_SETTINGS['Captcha_Enabled_For'],'staff') !== false && rst_is_staff_logged()))) { ?>
		<tr>
			<td>&nbsp;</td>
			<td><img src="<?php echo JRoute::_('index.php?option=com_rstickets&action=antispam&time='.time().'&Itemid=9999'); ?>" alt="Antispam" /></td>
		</tr>
		<tr>
			<td><?php echo $T_SETTINGS['Captcha_Case_Sensitive'] == 1 ? LBL_ANTISPAM_CASE : LBL_ANTISPAM_ICASE; ?></td>
			<td><input type="text" name="Antispam" value="" /></td>
		</tr>
		<?php } ?>
		
		</tbody>
	</table>
	<p><?php echo ((isset($users) && count($users) > 0 && count($departments) > 0) || (!isset($users) && count($departments) > 0)) ? '<button type="submit" class="submitTicket">'.LBL_SUBMIT_TICKET.'</button>' : '' ?></p>
</form>

<script type="text/javascript">
<!--

var departments_file_upload = new Array();
var departments_file_upload_ext = new Array();
var departments_priority = new Array();

departments_file_upload[''] = 'custom_fields';
<?php
if (count($departments) > 0)
	foreach ($departments as $department)
	{
		$class = '';
		switch ($department['FileUpload'])
		{
			case 'yes':default:$class = '';break;
			case 'users':$class = rst_is_logged() ? '' : 'custom_fields';break;
			case 'no':$class = 'custom_fields';break;
		}
		echo "departments_file_upload[".$department['DepartmentId']."]='".$class."';\n";
	}
?>
departments_file_upload_ext[''] = '*';

<?php
if (count($departments) > 0)
	foreach ($departments as $department)
	{
		$ext = '*';
		if (count($department['FileUploadExtensions']) == 1 && $department['FileUploadExtensions'][0] == '') $ext = '*';
		if (count($department['FileUploadExtensions']) > 0 && $department['FileUploadExtensions'][0] != '') $ext = implode(', ',$department['FileUploadExtensions']);
		echo "departments_file_upload_ext[".$department['DepartmentId']."]='".$ext."';\n
		departments_priority[".$department['DepartmentId']."]='".$department['DepartmentPriority']."';\n";
	}
	
?>

-->
</script>