<?php
session_start();
include("header.php");

if(trim($_SESSION['accountName'])=="" &&  trim($_SESSION['accountID'])=="" && trim($_SESSION['pgid'])!="i3g3e3nious7")
{
	echo '<script>';
	echo 'location.replace("index.php");';
	echo '</script>';
}
else
{

$sy=0;
$errmsg = "";
$schoolID=0;
$tchrID=0;
$gradeID=0;
$yearRef=0;
$byear = date('Y');
$bmonth = 1;
$bday = 1;
$toDate = $byear + 1;
$frDate = $toDate - 20;
$action=$_POST["action"];
$studentfName=$_POST["studentfName"];
$studentlName=$_POST["studentlName"];
$school=$_POST["school"];
$emailAdd=$_POST["emailAdd"];
$guardianName=$_POST["guardianName"]; 
$contactNo=$_POST["contactNo"];
if(trim($_POST["sy"])!="")
	$sy=$_POST["sy"];
if(trim($_GET["schlID"])!="")
	$schoolID=$_GET["schlID"];

if(trim($_POST["byear"])!="")
	$byear = $_POST["byear"];
if(trim($_POST["bmonth"])!="")
	$bmonth = $_POST["bmonth"];
if(trim($_POST["bday"])!="")
	$bday = $_POST["bday"];
if(trim($_POST["tchrID"])!="")
	$tchrID = $_POST["tchrID"];

if(trim($_POST["gradeID"])!="")
	$gradeID = $_POST["gradeID"];

if(strtoupper($action)=="CREATE")
{
	if(trim($studentfName)=="")
		$errmsg = $errmsg."Student First Name field is empty.<br/>";
	if(trim($studentlName)=="")
		$errmsg = $errmsg."Student Last Name field is empty.<br/>";
	if(trim($guardianName)=="")
		$errmsg = $errmsg."Guardian Name field is empty.<br/>";
	if(trim($contactNo)=="")
		$errmsg = $errmsg."Contact Number field is empty.<br/>";
	if($tchrID == 0)
		$errmsg = $errmsg."Select Teacher name.<br/>";
	if($gradeID == 0)
		$errmsg = $errmsg."Select a Grade<br/>";

	if(trim($errmsg)=="")
	{
		$dBirth = $byear."-".$bmonth."-".$bday ." 00:00:00";
		$strSql =@mysql_query("insert into tr_student(school_id,student_fname,student_lname,date_birth,teacher,grade,parent_guardian,contact_no,email,sy) values(".$schoolID.",'".$studentfName."','".$studentlName."','".$dBirth."',".$tchrID.",".$gradeID.",'".$guardianName."','".$contactNo."','".$emailAdd."',".$sy.")");
		
		$errmsg = "You have successfully created your student.";
	}
}
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr><td></td><td align="center" class="fs14 black"><u><b>Create New Student</b></u></td></tr>
	<tr><td width="200" class="fs12" valign="top">
	<?php
	include("sidemenu.php");
	?>
	</td>
		<td width="700" class="fs12" valign="top">
			<table width="680" cellpadding="1" cellspacing="1" border="0">
				<tr><td colspan="4"><hr /></td></tr>
			<?php
			
			$strSqli = @mysql_query("SELECT syID FROM ma_schoolyear order by syID desc limit 1");
			$cnti=@mysql_num_rows($strSqli);
			$rsi=@mysql_fetch_array($strSqli);
			if($cnti>0)
				$yearRef=$rsi["syID"];
			
			if(strtoupper(substr(trim($errmsg),1,8))== "YOU HAVE")
			{
				echo "<tr valign='top' align='center'><td colspan='4'>".$errmsg."</td></tr>";
			?>
				<meta http-equiv="Refresh" content="3;url=students.php?schlID=<?=$schoolID?>"><br/><br/><br/><br/><br>
			<?php
			}
			else 
			{
			?>
				<form action="studentNew.php?schlID=<?=$schoolID?>" method="post" name="frm">
				<tr><td height="10" width="20%"></td><td width="30%" class="errmsg"><?=$errmsg?></td><td height="10" width="15%"></td><td valign="bottom" width="35%" class="fs10"><i>* required fields</i></td></tr>
				<tr><td align="right"><b>* </b>FIRST NAME:&nbsp;</td><td><input class="formInput" type="text" name="studentfName" value="<?=$studentfName?>" /></td><td align="right">SCHOOL:&nbsp;</td>
					<td><select name="school" class="formSelect">
					<?php
						$strSqls =@mysql_query("select school_id,school_name from tr_schools where status=1 and school_id=".$schoolID);
						$cnts=@mysql_num_rows($strSqls);
						if($cnts>0)
						{
							echo "<option value='0'>--Select--</option>";
							while($rs1=@mysql_fetch_array($strSqls))
							{
								
								if($schoolID==$rs1['school_id'])
									echo '<option value='.$rs1['school_id'].' selected>'.$rs1['school_name'].'</option>';
								else
									echo '<option value='.$rs1['school_id'].'>'.$rs1['school_name'].'</option>';
							}
							
						}
						
						 ?>
					</select></td></tr>
				<tr><td align="right"><b>* </b>LAST NAME:&nbsp;</td><td><input class="formInput" type="text" name="studentlName" value="<?=$studentlName?>" /></td><td align="right">BIRTH DATE:&nbsp;</td>
					<td><select name="byear" class="formYear"><option value=''>-Y-</option>
							<?php
							for($i=$frDate;$i<=$toDate;$i++)
							{
								if($byear==$i)
									echo '<option value='.$i.' selected>'.$i.'</option>';
								else
									echo '<option value='.$i.' >'.$i.'</option>';
							}

							
							?>
							</select>&nbsp;
						<select name="bmonth" class="formDDMM">
						<option value=''>-M-</option>
							<?php
						   for($i=1;$i<=12;$i++)
							{
								if($bmonth==$i)
									echo '<option value='.$i.' selected>'.$i.'</option>';
								else
									echo '<option value='.$i.' >'.$i.'</option>';
							}
							?>
					</select>&nbsp;
						<select name="bday" class="formDDMM">
						<option value=''>-D-</option>
						<?php
						for($i=1;$i<=31;$i++)
						{
							if($bday==$i)
								echo '<option value='.$i.' selected>'.$i.'</option>';
							else
								echo '<option value='.$i.' >'.$i.'</option>';
						}
						?>
					</select></td></tr>
				<tr><td align="right"><b>* </b>GUARDIAN NAME:&nbsp;</td><td><input class="formInput" type="text" name="guardianName" value="<?=$guardianName?>" /></td><td align="right">TEACHER:&nbsp;</td>
					<td><select name="tchrID" class="formSelect">
					<?php
					$strSql2 =@mysql_query("select teacher_id,teacher_name from tr_teachers where status=1 and school_id=".$schoolID." and sy=".$yearRef);
					$cntr2=@mysql_num_rows($strSql2);
					
					echo "<option value='0'>--Select--</option>";
					if($cntr2>0)
					{
						while($rs2=@mysql_fetch_array($strSql2))
						{
							
							if($tchrID==$rs2['teacher_id'])
								echo '<option value='.$rs2['teacher_id'].' selected>'.$rs2['teacher_name'].'</option>';
							else
								echo '<option value='.$rs2['teacher_id'].'>'.$rs2['teacher_name'].'</option>';
						}
					}
					?>
				</select></td></tr>
				<tr><td align="right"><b>* </b>CONTACT NO.:&nbsp;</td><td><input class="formInput" type="text" name="contactNo" value="<?=$contactNo?>" /></td><td align="right">GRADE:&nbsp;</td>
					<td><select name="gradeID" class="formSelect">
					<?php
						$strSqlg =@mysql_query("select grade_id,grade_name from ma_grades where status=1");
						$cntg=@mysql_num_rows($strSqlg);
						if($cntg>0)
						{
							echo "<option value='0'>--Select--</option>";
							while($rsg=@mysql_fetch_array($strSqlg))
							{
								
								if($gradeID==$rs2['grade_id'])
									echo '<option value='.$rsg['grade_id'].'>'.$rsg['grade_name'].'</option>';
								else
									echo '<option value='.$rsg['grade_id'].'>'.$rsg['grade_name'].'</option>';
							}
						}
						?>
					</select></td></tr>
				<tr><td align="right">EMAIL ADDRESS:&nbsp;</td><td><input class="formInput" type="text" name="emailAdd" value="<?=$emailAdd?>" /></td>
					<td align="right">SCHOOL YR:&nbsp;</td>
					<td><select name="sy" class="formYear">
					<?php
					$strSqly =@mysql_query("select syID,syName from ma_schoolyear where sstatus=1 order by syID desc");
					$cntry=@mysql_num_rows($strSqly);
					if($cntry>0)
					{
						while($rsy=@mysql_fetch_array($strSqly))
						{
							
							if($sy==$rsy['syID'])
								echo '<option value='.$rsy['syID'].' selected>'.$rsy['syName'].'</option>';
							else
								echo '<option value='.$rsy['syID'].'>'.$rsy['syName'].'</option>';
						}
					}
							
					?></select></td></tr>
				<tr><td colspan="3"></td><td><input class="fs12" type="submit" name="action" value="CREATE" /></td></tr>
				</form>
		<?php
		}
		?>
				<tr><td colspan="4"><hr /></td></tr>
			</table></td></tr>
</table>
<?php
include("footer.php");
}
?>