<?php
	include('/var/www/html/uclit/protected/common/login/check.php');
	include('/var/www/html/uclit/protected/common/css_login/css_head2.php');
	include('/var/www/html/uclit/protected/connections/db_help.php');
	
	$query = "SELECT help_type FROM help_catagories ORDER BY help_type" ;

	$db = mysql_select_db('helpcalls', $helpConn) or die("Couldn't select helpcalls database ");
	$sql_result = mysql_query($query, $helpConn) or die("Couldn't execute query. $query");
		
	$query_per = "SELECT personnel_id, last_name, first_name FROM personnel ORDER BY last_name" ; 	
	$db = mysql_select_db('uclit_db', $uclit_dbConn) or die("Couldn't select database. uclit_db");		
	$sql_per = mysql_query($query_per, $uclit_dbConn) or die("Couldn't execute query. $query_per");

	if (!$sql_result || !sql_per) 
	{
		echo "<font color='red'><p>Couldn't get list!</p></font>";
	}
	else 
	{
?>						
		<h1>Phone Ticket</h1>
				<FORM METHOD='post' ACTION='add_phone_ticket.php' name='ticket_form' onSubmit='return validator();'>
				<TABLE BORDER='1' width='90%'>
					<TR>
						<TD VALIGN='top'>
							<P><STRONG>&nbsp;&nbsp; Help Catagory:</STRONG><BR>
								<select name='help_catagory'>
									<option selected value='0'>Select.....</option> 
<?php								
								while ($row = mysql_fetch_array($sql_result)) 
								{
									$help_type = $row["help_type"];
									echo "<option>$help_type</option>";
								}
?>
							</select><font color='#ff0000' size='2'>*</font>
							<!--<INPUT TYPE='text' NAME='help_catagory' SIZE=25 MAXLENGTH=15></p>--></P>
					</TD>
					<TD VALIGN='top'>
						<P><STRONG>&nbsp;&nbsp; User's Name:</STRONG><BR>
							<select name='per' onChange='CheckDisabled()'>
								<option value='0' selected>Select...</option> 
								<option value=''>Student/Other</option> 
<?php					
								$ln = "";
								$fn = "";
								while ($row = mysql_fetch_array($sql_per)) 
								{
									$per_id = $row["personnel_id"];
									$ln = $row["last_name"];
									$fn = $row["first_name"];
									echo "<option value='$per_id'>$ln, $fn</option>";
								}
?>
					</select><font color='#ff0000' size='3'>*</font>
					<INPUT TYPE='hidden' NAME='username' value='$fn $ln'>
					</TD>

				</TR>
				<tr>
					<TD VALIGN='top'>
						<P><STRONG>&nbsp;&nbsp; Department:</STRONG><BR>
							<select name='help_dept'>
								<option selected value='0'>Select...</option>
<?php
		$query2 = "SELECT dept_name FROM departments ORDER BY dept_name";
		$db = mysql_select_db('uclit_db', $uclit_dbConn) or die("Couldn't select database. uclit_db");		
		$sql_result2 = mysql_query($query2, $uclit_dbConn) or die("Couldn't execute query. <p>$query2</p>");
	
								while ($row = mysql_fetch_array($sql_result2))
								{
									$dept_name = $row["dept_name"];
									echo "<option>$dept_name</option>";
								}
								
?>
					</select><font color='#ff0000' size='3'>*</font>
					</td>
					<td>
							<p><STRONG>&nbsp;&nbsp; Phone Number:</STRONG><BR><font color='#ff0000' size='3'> </font> 
					          <INPUT TYPE='text' NAME='help_phone' SIZE='25' MAXLENGTH='25'></p>
					</TD>
					
				</tr>
				<TR >
					<TD VALIGN='top' COLSPAN='2' >
						<P><STRONG>&nbsp;&nbsp; Help Call Description:</STRONG><BR>
							<TEXTAREA NAME='help_desc' COLS='45' ROWS='5' WRAP='virtual'></TEXTAREA><font color='#ff0000' size='3'>*</font></P>
						</TD>
				</TR>	
				<TR >
					<TD VALIGN='top' COLSPAN='2' >
						<p><STRONG>&nbsp;&nbsp; Resolution:</STRONG><BR>
						<TEXTAREA NAME='help_res' COLS='45' ROWS='5' WRAP='virtual' value=''></TEXTAREA><font color='#ff0000' size='3'>*</font>
						&nbsp;&nbsp;<input name='completebox' type='checkbox' value='false'>Completed</p>
						</TD>
				</TR>	
				<tr>
					<td> E-mail(If Student/Other selected): </td>
					<td><input name='email' type='text' size='50' disabled></td>	
				</tr>
				<tr>
						<td><INPUT TYPE='button' VALUE='Submit Phone Ticket' onClick='CheckBeforeSumbit()'></td>
						<td><font color='#ff0000' size='3'>* fields required</font></td>
				</tr>
			
			</TABLE>
		</FORM>
<?php 
	}
	include ('/var/www/html/uclit/protected/common/css_login/css_foot2.php');
?>
<script type="text/javascript">
function CheckBeforeSumbit()
{

	var x = document.ticket_form;
	if(x.per.value == "0" || x.help_catagory.value == "0" || x.help_dept.value == "0" || x.help_desc.value == "")
	{
		if(x.completebox.checked)
		{
			if(x.help_res.value == "")
			{
				window.alert("Please select for the following items:\nResolution");
			}
		}
		else
		{
			window.alert("Please select for the following items:\nUser\nHelp Catagory\nDepartment\nHelp Description");
		}
		
	}
	else
	{
		if(x.completebox.checked)
		{
			x.completebox.value = "true";
		}	
		if(x.email.value == "")
		{
			if(x.per.value == "")
				window.alert("Student/Other needs to provide email\n in order to have email services for this helpcall");			
			else
			{
				x.submit();
				
			}
		}
		else
		{//check for correctness of the email
			var email = x.email.value;
			var reg1 = new RegExp("[0-9a-zA-Z]+@[a-zA-Z]+");
			if(reg1.test(email))
			{
				//window.alert("Good email");
				x.submit();
				
			}
			else
			{
				//bad email or empty email
				window.alert("Please enter a valid email address");
			}
		}
	}
}
function CheckDisabled()
{
	var x = document.ticket_form;
	if(x.per.value == "")
	{
		x.email.value = "";
		x.email.disabled = false;
	}
	else
	{
		x.email.value = "";
		x.email.disabled = true;
	}
}
</script>