<?
session_start();
ob_start();
?>
<html>
<head>
<script language="JavaScript"> 
function onInsert()
{ 
		if(form1.txtscience.value =="")
		{
			alert("You not fill scientific name!");
			form1.txtscience.focus();
			return false;
		}
		else
		{
			return true; 
		}	 
} 

function onUpdate() 
{ 
		if(form2.txtscience2.value =="")
		{
			alert("You not fill Sampling port!");
			form2.txtscience2.focus();
			return false;
		}
		else
		{
			return true;
		} 
} 
</script> 

<title>Fish landing data management system</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
#wrapper {
		border: 1px solid black;
		background-color: #E0E0EF;
		color:#FFFFFF;
		padding:10px;
        height:800px;
        width:1024px;
		text-align:center;
    }
.stylewhite {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 18px;
	font-weight: bold;
	color: #FFFFFF;
}
.stylewhitesmall {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-weight: bold;
	color: #FFFFFF;
}
.stylegreen {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-weight: bold;
	color: #00661F;
}
.styleblack {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-weight: bold;
	color: #000000;
}
.styleblackitalic {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-style:italic;
	color: #000000;
}
.stylewhiteitalic {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-style:italic;
	color: #FFFFFF;
}

.styleblacklarge {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 18px;
	font-weight: bold;
	color: #000000;
}
a:link {
	color: #FFFFFF;
	text-decoration: none;
}
a:visited {
	color: #FFFFFF;
	text-decoration: none;
}
a:hover {
	color: #FF0000;
	text-decoration: none;
}
a:active {
	color: #FFFFFF;
	text-decoration: none;
}
.style1 {color: #FF0000}
.styleblacklargered {	font-family: Arial, Helvetica, sans-serif;
	font-size: 18px;
	font-weight: bold;
	color:#FF0000;
}
-->
</style>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<!-- ImageReady Slices (Untitled-1) -->
<table width="1024" height="799" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#F3F3F3" id="Table_01">
<tr>
		<td height="50" colspan="2" bgcolor="#ADCE4C" class="stylewhite">&nbsp;Fish Landing Data Management System</td>
  </tr>
	<tr class="stylewhitesmall">
	  <td height="20" colspan="2" bgcolor="#006666"><span class="styleblack">&nbsp;</span><span class="styleblack">&nbsp;</span><a href="main.php">Home</a><span class="style1">&nbsp;</span> | <a href="usr.php">User</a> |<span class="style1"> <a href="fishingzone.php">Fishing Area |</a> <a href="district.php">State/Province </a></span>|<a href="samplingport.php"> Landing Site </a>| <a href="fgmethod.php">Fishing Gear and Method</a> | <a href="vessel.php">Fishing Vessel</a> | <a href="species.php">Fish Species</a> | <a href="catchdata.php">Catch Data </a> | <a href="biology.php">Biology Data</a> | <a href="report.php"> </a><a href="report.php">Report</a> |<span class="style1"> <a href="logout.php">Logout</a></span>
	    <div align="right"></div></td></tr>
	<tr>
	  <td width="517" height="705" align="right" valign="top" bgcolor="#CCCCCC">
	    <div align="center" class="styleblack">
	      <?
if($_POST[submit])
{
	$inspid = trim($_POST["hdspid"]);
	$inssp = trim($_POST["txtscience"]);
	$insfam = trim($_POST["txtfamily"]);
	$inslocal = trim($_POST["txtlocal"]);
	$inscommon = trim($_POST["txtcommon"]);
	$insgrade = trim($_POST["txtgrade"]);
	

	require_once("sp_function.inc.php");
	InsertSp($inspid,$insfam,$inssp,$inslocal,$inscommon,$insgrade);
	
	if($_FILES["fileField2"]["name"] != "")
		{
		if(($_FILES["fileField2"]["type"] == "image/gif") && ($_FILES["fileField2"]["type"] == "image/pjpeg") && $_FILES["fileField"]["type"] != "image/jpeg" && ($_FILES["fileField2"]["size"] < 20000))
		{
							//echo "File type=".$_FILES["fileField2"]["type"];
						  		require("usr_function.php");
								$msg = $_FILES["fileField2"]["type"]." File type: JPG or GIF only";
								ErrMsg($msg);
						  	}
						  	else
						  	{		
								
								
								if($_FILES["fileField2"]["type"] == "image/gif")
								{
								$dnow = date("YmdHis");
							
								$newname = $dnow.".gif";
								}
								elseif($_FILES["fileField2"]["type"] == "image/pjpeg" || $_FILES["fileField2"]["type"] == "image/jpeg")
								{
								$dnow = date("YmdHis");
							
								$newname = $dnow.".jpg";
								}
								//Create Folder--------------------------
								if (!file_exists("species/"))
								{
									mkdir("species/");
								} 
								//---------------------------------------

								$save_file = "species/".$newname;
								//echo "New Name=".$newname;
								require_once("sp_function.inc.php");
								InsertSpPic($inspid,$newname);
								$tlog = "Add species picture file ".$newname;
								require_once("usr_function.php");
								addLog($_SESSION['myusername'],$tlog);
						
								if(!copy($_FILES["fileField2"]["tmp_name"],$save_file))
								{
								echo "Upload: " . $_FILES["fileField2"]["name"] . "<br>";
    							echo "Type: " . $_FILES["fileField2"]["type"] . "<br>";
    							echo "Size: " . ($_FILES["fileField2"]["size"] / 1024) . " Kb<br>";
    							echo "Temp file: " . $_FILES["fileField2"]["tmp_name"] . "<br>";
				  				}
							}
						}
	
	
	
	$tlog = "Add Species ".$inssp."(".$inspid.")";
	require_once("usr_function.php");
	addLog($_SESSION['myusername'],$tlog);
}
if($_POST[submit2])
{
	$edtspid = trim($_POST["hdspid2"]);
	$edtsp = trim($_POST["txtscience2"]);
	$edtfam = trim($_POST["txtfamily2"]);
	$edtlocal = trim($_POST["txtlocal2"]);
	$edtcommon = trim($_POST["txtcommon2"]);
	$edtgrade = trim($_POST["txtgrade2"]);
	
	require_once("sp_function.inc.php");
	EditSp($edtspid,$edtfam,$edtsp,$edtlocal,$edtcommon,$edtgrade);
	
	if($_FILES["fileField"]["name"] != "")
	{
		//echo "File".$_FILES["fileField"]["name"];
		require_once("sp_function.inc.php");
		DeleteSpFile($edtspid);
		if(($_FILES["fileField"]["type"] != "image/gif") && ($_FILES["fileField"]["type"] != "image/pjpeg") && $_FILES["fileField"]["type"] != "image/jpeg")
			{
				require_once("usr_function.php");
				$msg = $_FILES["fileField"]["type"]."File type: JPG or GIF only";
				ErrMsg($msg);
			}
		else
			{			
								//echo "File type=".$_FILES["fileField"]["type"];
								if($_FILES["fileField"]["type"] == "image/gif")
								{
								$dnow = date("YmdHis");
								$newname = $dnow.".gif";
								}
								elseif($_FILES["fileField"]["type"] == "image/pjpeg" || $_FILES["fileField"]["type"] == "image/jpeg")
								{
								$dnow = date("YmdHis");
								$newname = $dnow.".jpg";
								}
								//echo $newname;
								//$fname = rename($_FILES["file"]["name"],$newname);
								//Create Folder--------------------------
								if (!file_exists("species/"))
								{
									mkdir("species/");
								} 
								//---------------------------------------

								$save_file = "species/".$newname;
								require_once("sp_function.inc.php");
								InsertSpPic($edtspid,$newname);
								$tlog = "Edit species picture file ".$newname;
								require_once("usr_function.php");
								addLog($_SESSION['myusername'],$tlog);
								
								if(!copy($_FILES["fileField"]["tmp_name"],$save_file))
																
								{
								echo "Upload: " . $_FILES["fileField"]["name"] . "<br>";
    							echo "Type: " . $_FILES["fileField"]["type"] . "<br>";
    							echo "Size: " . ($_FILES["fileField"]["size"] / 1024) . " Kb<br>";
    							echo "Temp file: " . $_FILES["fileField"]["tmp_name"] . "<br>";
				  				}
							}
						}
							
						
							
	
	$tlog = "Edit Species ".$edtsp."(".$edtspid.")";
	require_once("usr_function.php");
	addLog($_SESSION['myusername'],$tlog);
}
if($_POST[submit3])
{
	$delid = $_POST["hdspid3"];
	require_once("sp_function.inc.php");
	DeleteSp($delid);
	$tlog = "Delete Species ".$delid;
	require_once("usr_function.php");
	addLog($_SESSION['myusername'],$tlog);
}
if($_POST[delimg])
{

	$edtsp = $_POST["hdspid2"];
	require_once("sp_function.inc.php");
	DeleteSpFile($edtsp);
	$tlog = "Delete Species File ".$edtsp;
	require_once("usr_function.php");
	addLog($_SESSION['myusername'],$tlog);
	//$encodeship = base64_encode($edtshipname);
	//header("vessel.php?edtship=$encodeship");
}	

?>
	      <?
//foreach (range('a', 'z') as $i)
  //  echo "<a href='species?alpha=$i'>"." $i "."</a>"."|";     
	?>
        </div>
	    <table width="80%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
          <tr class="stylewhitesmall">
            <td width="70%" bgcolor="#000000"><div align="center"><span class="stylewhite">Fish Species</span></div></td>
            <td width="15%" bgcolor="#000000"><div align="center">Edit</div></td>
            <td width="15%" bgcolor="#000000"><div align="center">Delete</div></td>
          </tr>
          <div align="center"> <span class="stylegreen">
            <?
	  include "connect.inc.php";
	   $sqlgetsp = "select spid,family,scientific,local,common,grade from fishspecies order by scientific ";
	 /*
	 if(!isset($alpha))
	  {
	 $sqlgetsp = "select spid,family,scientific,local,common,grade from fishspecies where scientific like 'A%' or scientific like 'a%'order by scientific ";
	  }
	  else
	  {
	  $ua = strtoupper($_GET["alpha"]);
	  $la = strtolower($_GET["alpha"]);
	  $sqlgetsp = "select spid,family,scientific,local,common,grade from fishspecies where scientific like '$ua%' or scientific like '$la%'order by scientific ";
	  }
	  */
	 //echo $sqlgetsp;
	  $qrygetsp = mysql_query($sqlgetsp);
	  $numgetsp = mysql_num_rows($qrygetsp);
	   for($i=1;$i<=$numgetsp;$i++)
		{
			$resgetsp = mysql_fetch_array($qrygetsp);
			$dbspid =  $resgetsp["spid"];
			$dbsp = $resgetsp["scientific"];
			$dbfam = $resgetsp["family"];
			$dblocal = $resgetsp["local"];
			$dbcommon = $resgetsp["common"];
			$dbgrade = $resgetsp["grade"];
			$encodespid = base64_encode($dbspid);
			?>
            </span>
              <tr class="styleblack">
                <td bgcolor="#006666"><span class="stylewhiteitalic">
				<? 
				if($dblocal !="")
				{
				echo $dbsp."(".$dblocal.")";
				}
				else
				{
				echo $dbsp;
				} 
				?>
				</span></td>
                <td bgcolor="#006666"><div align="center"><a href="species.php?edtsp=<? echo $encodespid; ?>"><img src="images/document_edit.png" width="20" height="20" border="0"></a></div></td>
                <td bgcolor="#006666"><div align="center"><a href="species.php?delsp=<? echo $encodespid; ?>"><img src="images/close.png" width="20" height="20" border="0"></a></div></td>
              </tr>
              <span class="stylegreen">
                <?
        }
      ?>
            </span></div>
	      <tr>
            <td colspan="3" bgcolor="#000000"><div align="right"><span class="styleblack"><span class="stylewhitesmall"><a href="species.php"><img src="images/add.png" width="24" height="24" border="0"></a></span>&nbsp; </span></div></td>
          </tr>
        </table>	    
      <p>&nbsp;</p></td>
      <td width="501" height="705" valign="top" bgcolor="#CCCCCC"><?
	  if(!isset($_GET["edtsp"]) && !isset($_GET["delsp"]))
	  {
	  ?>
        <br>
        <form action="species.php" method="post" enctype="multipart/form-data" name="form1" id="form1" onSubmit="return onInsert();">
          <table width="90%" height="131" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td height="19" colspan="2" background="images/index_01.gif"><div align="center"><span class="stylewhite">Add Fish Species</span></div>
                  <div align="right"></div></td>
            </tr>
            <tr>
              <td height="14" colspan="2" bgcolor="#ADCE4C" class="style1"><label>
                <div align="left">&nbsp;<span class="styleblack"><? 
			  	$sqlgetid = "select max(spid) as spmax from fishspecies";
				//echo $sqlgetid;
				$qrygetid = mysql_query($sqlgetid);
				$resultgetid = mysql_fetch_array($qrygetid);
				$maxId = $resultgetid["spmax"];
				$spid = $maxId+1;
				//echo $spid;
			  ?>
                      <input type="hidden" name="hdspid" id="hdspid" value="<? echo $spid; ?>">
                  </span></div>
                </label></td>
            </tr>

            <tr>
              <td width="29%" height="12" valign="top" bgcolor="#ADCE4C" class="style1"><div align="right"><span class="stylegreen">Family&nbsp; </span></div></td>
              <td width="71%" height="12" bgcolor="#ADCE4C"><label>
              <input name="txtfamily" type="text" class="styleblack" id="txtfamily3" size="20" maxlength="20">
              </label></td>
            </tr>
            <tr>
              <td height="6" valign="top" bgcolor="#ADCE4C" class="style1"><div align="right"><span class="stylegreen">Scientific Name</span>&nbsp; </div></td>
              <td height="0" bgcolor="#ADCE4C"><label>                <input name="txtscience" type="text" class="styleblackitalic" id="txtscience" size="30" maxlength="30">
              </label></td>
            </tr>
            <tr>
              <td height="7" valign="top" bgcolor="#ADCE4C" class="style1"><div align="right"><span class="stylegreen">Common Name&nbsp; </span></div></td>
              <td height="0" bgcolor="#ADCE4C">              <input name="txtcommon" type="text" class="styleblack" id="txtcommon3" size="30" maxlength="30"></td>
            </tr>
            <tr>
              <td height="13" valign="top" bgcolor="#ADCE4C" class="style1"><div align="right"><span class="stylegreen">Local  Name&nbsp; </span></div></td>
              <td height="0" bgcolor="#ADCE4C"><input name="txtlocal" type="text" class="styleblack" id="txtlocal4" size="30" maxlength="30"></td>
            </tr>
            <tr>
              <td height="7" valign="top" bgcolor="#ADCE4C" class="stylegreen"><div align="right">Picture&nbsp;</div></td>
              <td height="7" bgcolor="#ADCE4C"><input name="fileField2" type="file" class="styleblack" id="fileField2"></td>
            </tr>
            <tr>
              <td height="34" colspan="2" bgcolor="#ADCE4C"><label>
                  <div align="center">
                    <input name="submit" type="submit" class="styleblack" id="submit" value="Insert" />
                    <input name="Reset" type="reset" class="styleblack" id="button" value="Cancel">
                  </div>
                </label></td>
            </tr>
            <tr>
              <td height="19" colspan="2" bgcolor="#6B716F"><div align="right"></div></td>
            </tr>
          </table>
        </form>
        <?
		}
		elseif(isset($_GET["edtsp"]))
		{
		$edtspid = base64_decode($_GET["edtsp"]);
		$sqlgetdesc = "select spid,family,scientific,local,common,grade,sppic from fishspecies where spid = $edtspid";
		//echo $sqlgetdesc;
		$qrygetdesc = mysql_query($sqlgetdesc);
		$resgetdesc = mysql_fetch_array($qrygetdesc);
		//$editport = $resgetdesc["portname"];
		//$editprovince = $resgetdesc["province"];
		$editspid =  $resgetdesc["spid"];
		$editdbsp = $resgetdesc["scientific"];
		$editdbfam = $resgetdesc["family"];
		$editdblocal = $resgetdesc["local"];
		$editdbcommon = $resgetdesc["common"];
		$editdbgrade = $resgetdesc["grade"];
		$editsppic = $resgetdesc["sppic"];
		$encodespid = base64_encode($editspid);
        ?>
       <br>
        <form action="species.php?edtsp=<? echo $encodespid;?>" method="post" enctype="multipart/form-data" name="form2" id="form2" onSubmit="return onUpdate()">
          <table width="90%" height="131" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td height="19" colspan="2" background="images/index_01.gif"><div align="center"><span class="stylewhite">Edit Fish Species</span></div>
                  <div align="right"></div></td>
            </tr>
            <tr>
              <td height="14" colspan="2" align="center" bgcolor="#ADCE4C" class="style1"><label> <br>
              <?php
				
				$spImage = $editsppic;
				$defaultpath = "species/noimage.jpg";
				$picpath = "species/".$spImage;
				if($spImage =="")
				{
				//echo $defaultpath;
				echo "<img src='$defaultpath' width='300' height='150'/>";
				}
				else
				{
				//echo $picpath;
					if(file_exists($picpath))
					{
					echo "<img src='$picpath' width='300' height='150'/>";
					echo "<input name='delimg' type='submit' class='styleblack' id='delimg' value='delete'>";
					//echo "<a href='vessel.php?delpicship=$encodeship'>"."Delete"."</a>";
					}
					else
					{
					echo "file not found";
					echo "<img src='$defaultpath' width='300' height='150'/>";
					}
				}
				
				?>
              </label><br></td>
            </tr>

            <tr>
              <td width="28%" height="12" valign="top" bgcolor="#ADCE4C" class="style1"><div align="right"><span class="stylegreen"><span class="styleblack">
                <input type="hidden" name="hdspid2" id="hdspid2" value="<? echo $edtspid; ?>"></span>Scientific Name</span>&nbsp; </div></td>
              <td width="72%" height="12" bgcolor="#ADCE4C"><label><span class="stylewhite">
                <input name="txtscience2" type="text" class="styleblack" id="txtscience2" value="<? echo $editdbsp; ?>" size="30" maxlength="30">
              </span></label></td>
            </tr>
            <tr>
              <td height="6" valign="top" bgcolor="#ADCE4C" class="style1"><div align="right"><span class="stylegreen">Family</span>&nbsp; </div></td>
              <td height="0" bgcolor="#ADCE4C"><span class="stylewhite"><a href="species.php?delsp=<? echo $edtsp; ?>">
                <input name="txtfamily2" type="text" class="styleblack" id="txtfamily2" size="20" maxlength="20" value="<? echo $editdbfam; ?>">
              </a></span></td>
            </tr>
            <tr>
              <td height="7" valign="top" bgcolor="#ADCE4C" class="style1"><div align="right"><span class="stylegreen">Local  Name&nbsp; </span></div></td>
              <td height="0" bgcolor="#ADCE4C"><input name="txtlocal2" type="text" class="styleblack" id="txtlocal2" value="<? echo $editdblocal; ?>" size="30" maxlength="30"></td>
            </tr>
            <tr>
              <td height="13" valign="top" bgcolor="#ADCE4C" class="style1"><div align="right"><span class="stylegreen">Common  Name&nbsp; </span></div></td>
              <td height="0" bgcolor="#ADCE4C"><input name="txtcommon2" type="text" class="styleblack" id="txtcommon2" value="<? echo $editdbcommon; ?>" size="30" maxlength="30"></td>
            </tr>
            <tr>
              <td height="7" valign="top" bgcolor="#ADCE4C" class="stylegreen"><div align="right">Picture&nbsp; </div></td>
              <td height="7" bgcolor="#ADCE4C"><span class="styleblack">
                <input name="fileField" type="file" class="styleblack" id="fileField">
              </span></td>
            </tr>
            <tr>
              <td height="34" colspan="2" bgcolor="#ADCE4C"><label>
                  <div align="center">
                    <input name="submit2" type="submit" class="styleblack" id="submit2" value="Update" />
                    <input name="button2" type="button" class="styleblack" id="button2" value="Cancel" onClick="window.location='species.php';">
                  </div>
                </label></td>
            </tr>
            <tr>
              <td height="19" colspan="2" bgcolor="#6B716F"><div align="right"></div></td>
            </tr>
          </table>
        </form>
        <?
		}
		if(isset($_GET["delsp"]))
		{
		$delspid = base64_decode($_GET["delsp"]);
		$sqlgetdesc2 = "select spid,family,scientific,local,common,grade from fishspecies where spid = $delspid";
		//echo $sqlgetdesc2;
		$qrygetdesc2 = mysql_query($sqlgetdesc2);
		$resgetdesc2 = mysql_fetch_array($qrygetdesc2);
		$editsp2 = $resgetdesc2["scientific"];
		
		?>
       <br>
        <form action="species.php" method="post" name="form3" id="form3">
          <table width="82%" height="86" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td width="100%" height="19" background="images/index_01.gif"><div align="center"><span class="stylewhite">Delete Fish Species</span></div>
                <div align="right"></div></td>
            </tr>
            <tr>
              <td height="25" bgcolor="#ADCE4C" class="style1"><label>
                  <br>
                <div align="center" class="stylewhite"><span class="styleblack">
                  <input type="hidden" name="hdspid3" id="hdspid3" value="<? echo $delspid; ?>">
                </span>&nbsp; Are you sure to delete <br>
                 Species '<span class="styleblacklargered"><? echo $editsp2; ?></span>'?</div></td>
            </tr>
            <tr>
              <td height="34" bgcolor="#ADCE4C"><label>                <div align="center">
                    <input name="submit3" type="submit" class="styleblack" id="submit3" value="Confirm" />
                  <input name="button22" type="button" class="styleblack" id="button22" value="Cancel" onClick="window.location='species.php';">
                  </div>
                </label></td>
            </tr>
            <tr>
              <td height="19" bgcolor="#6B716F"><div align="right"></div></td>
            </tr>
          </table>
        </form>
        <?
        }
		?></td>
	</tr>
	<tr>
	  <td colspan="2" bgcolor="#006666"><div align="right"><span class="styleblack">SEAFDEC 2012</span></div></td>
  </tr>
</table>
<!-- End ImageReady Slices -->
</body>
</html>