<?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_equip.php');
	
	//Drop Down Database connections	
	$query2 = "SELECT DISTINCT dept_name FROM departments ORDER BY dept_name" ;
	$db4 = 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>");

	$query4 = "SELECT DISTINCT building FROM departments ORDER BY dept_name" ;
	$sql_result4 = mysql_query($query4, $uclit_dbConn) or die("Couldn't execute query. <p>$query3</p>");
	
	$query3 = "SELECT DISTINCT account FROM data_jacks ORDER BY account" ;
	$db = mysql_select_db('equipment', $equipConn) or die ("Unable to select equipment database.");
	$sql_result3 = mysql_query($query3, $equipConn) or die("Couldn't execute query. <p>$query3</p>");
		
	if(!$_GET['jack_id'])
	{
		$jack_id = '';
		$department = '';
		//$phone_jack = '';
		$data_jack = '';
		$room_jack = '';
		$bldg = '';
		$notes ='';
		$account ='';
	}
	else //Page is being called from edit or delete
	{
		$jack_id = $_GET['jack_id'];
		$uaction = $_GET['uaction'];
		$dbvdjtable = 'data_jacks';
		$sql = "SELECT jack_id, department, data_jack, room_jack, building, notes, account, active FROM data_jacks WHERE jack_id='$jack_id'";
		
		$vdj_result = mysql_query($sql, $equipConn) or die ("Unable to exercute query: <p>$sql</p>");
		
		while($row = mysql_fetch_array($vdj_result))
		{
			$jack_id = $row['jack_id'];
			$department = $row['department'];
			$data_jack = $row['data_jack'];
			$room_jack = $row['room_jack'];
			$bldg = $row['building'];
			$notes = $row['notes'];
			$account = $row['account'];
			$active = $row['active'];
		}
	}

	if($uaction == '')
	{
		$h3 = 'Add a Data Jack';
		$goto = 'process_pdj_add.php';
		$buttonvalue = 'Add Jack';
	}
	elseif ($uaction == 'edit')
	{
		$h3 = "Edit Record[ '$jack_id' ] of Data Jack";
		$goto = 'process_pdjud.php';
		$buttonvalue = 'Update Jack';
	}
	elseif ($uaction == 'delete')
	{
		$h3 = "Delete Current Record[ '$jack_id' ] from Data Jack?";
		$goto = 'process_pdjud.php';
		$buttonvalue = 'Delete Jack';
	}
	

	echo "
<h3>$h3</h3>
<font color='red'>*</font><font size='-1'>- Indicates required fields</font>
<form name='pdj_form' action='$goto' method='post' onSubmit='return validator();'>
  <table width='588' border='0' cellspacing='1' cellpadding='1'>
  <tr>
     <td align=right>Data Jack Number:&nbsp;</td>
     <td><input name='data_jack' type='text' size='25' maxlength='25' value='$data_jack'><font color=red>*</font></td>
  </tr>
  <tr>
     <td align=right>Department:&nbsp;</td>
     <td><select name='department'>
	 <option value='$department' selected>$department</option>";

		while ($row = mysql_fetch_array($sql_result2)) 
		{
			$dept_name = $row["dept_name"];
			echo "<option value='$dept_name'>$dept_name</option>";
		}
	echo "			
        </select></td>
  </tr>
  <tr>
     <td align=right>Room Number:&nbsp;</td>
     <td><input name='room_jack' type='text' size='20' maxlength='20' value='$room_jack'></td>
  </tr>
  <tr>
     <td align=right>Building:&nbsp;</td>
     <td><select name='building'>
	 <option value='$bldg' selected>$bldg</option>";

		while ($frow = mysql_fetch_array($sql_result4)) 
		{
			$bldg = $frow["building"];
			echo "<option value='$bldg'>$bldg</option>";
		}
	echo "			
        </select><font color=red>*</font></td>
  </tr>
  
   <tr>
     <td align=right>Notes:&nbsp;</td>
     <td><input name='notes_jack' type='text' size='20' maxlength='20' value='$notes'></td>
  </tr>
  
  <tr>
     <td align=right>Account:&nbsp;</td>
     <td><select name='account'>
	 <option value='$account' selected>$account</option>";
 		while ($row = mysql_fetch_array($sql_result3)) 
		{
			$account = $row["account"];
			echo "<option value='$account'>$account</option>";
		}
		echo "			
        </select></td>
  </tr>
   <tr>
   <td></td>";
	if ($uaction == 'edit' or $uaction == '')   
	{   
		echo"<td align=left>
		   <INPUT type='checkbox' name='active_sel' value='1' ";  
		if($active == '1')
			echo"CHECKED> Active Data Jack"; 
		else
			echo"> Active Data Jack";
		echo"
 </td>
  </tr>
  <tr>
     <td><input name='jack_id' type='hidden' value='$jack_id'></td>
     <td><input name='uaction' type='hidden' value='$uaction'></td>
     <td>&nbsp;</td>
  </tr>
  <tr>
     <td>&nbsp;</td>
     <td><input type='submit' name='Submit' value='$buttonvalue'></td>
     <td>&nbsp;</td>
  </tr>
  <tr>
	 <td>&nbsp;</td>
     <td colspan='2'><br><a href='http://ul.uc.edu/uclit/protected/itstaff/show_allpdj.php'>List All Data Jacks</a></td>
  </tr>
</table>
</form>";
	}
	else
	{
		echo"	 
  </td>
  </tr>
  <tr>
     <td><input name='jack_id' type='hidden' value='$jack_id'></td>
     <td><input name='uaction' type='hidden' value='$uaction'></td>
     <td>&nbsp;</td>
  </tr>
  <tr>
     <td>&nbsp;</td>
     <td><input type='submit' name='Submit' value='$buttonvalue'></td>
     <td>&nbsp;</td>
  </tr>
  <tr>
	 <td>&nbsp;</td>
     <td colspan='2'><br><a href='http://ul.uc.edu/uclit/protected/itstaff/show_allpdj.php'>List All Data Jacks</a></td>
  </tr>
</table>
</form>";
	}
	include('/var/www/html/uclit/protected/common/css_login/css_foot2.php'); 
?>
<script language='JavaScript'>
function validator()
{	var noErrors;
	var msgError = 'Please complete the following fields\\n';
	var obj = document.pdj_form;
	

	if (obj.data_jack.value != '' && obj.department.value != '') 
	{
		noErrors = true;
		return(noErrors);
	}
	else {
		
		noErrors = false;	

		if (obj.data_jack.value == '') 
		{
			msgError += ''data jack' field.\\n';
			obj.data_jack.focus();
			
		}
		if (obj.department.value == '')
		{
			msgError +=''department' list.\\n';
			if(obj.data_jack.value != '')
				obj.department.focus();
		}
	}
	//Display error message
	if (!noErrors)
	{
		alert(msgError);		
		return (false)
	}
}
</script>