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


if(trim($_SESSION['pName'])== "" || trim($_SESSION['pID'])=="")
{
	echo "<script>";
	echo "location.replace('parentLogin.php');";
	echo "</script>";

}

$sy=0;
$errmsg = "";
$school=0;
$stdntID=0;
$tchrID=0;
$gradeID=0;

/*$byear = Year(Date)
$bmonth = Month(Date)
$bday = Day(Date)*/

$byear = date('Y');
$bmonth = date('m');
$bday = date('d');
$toDate =$byear+ 1;
$frDate = $toDate-20;

if(trim($_GET["id"])!="")
{
	$stdntID=$_GET["id"];
}
else if(trim($_POST["stdntID"])!="")
{
	$stdntID=$_POST["stdntID"];
}
$action=$_POST["action"];
$studentfName=$_POST["studentfName"];
$studentlName=$_POST["studentlName"];
$emailAdd=$_POST["emailAdd"];
$guardianName=$_POST["guardianName"];
$contactNo=$_POST["contactNo"];
$allergy=$_POST['stAllergy'];
$allergd=$_POST['stAllergy_desc'];

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

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) == "UPDATE")
{

	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($tchrID == 0)
		$errmsg = $errmsg."Select Teacher<br/>";
	if($gradeID == 0)
		$errmsg = $errmsg."Select a Grade<br/>";

	if(trim($errmsg)=="")
	{
		
		$dBirth = $byear."-".$bmonth."-".$bday ." 00:00:00";
		$strInput = "school_id=".$school.",";
		$strInput = $strInput."student_fname='".$studentfName."',";
		$strInput = $strInput."student_lname='".$studentlName."',";
		$strInput = $strInput."date_birth='".$dBirth."',";
		$strInput = $strInput."teacher=".$tchrID.",";
		$strInput = $strInput."grade=".$gradeID.",";
		$strInput = $strInput."sy=".$sy.",";
		$strInput = $strInput."parent_guardian='".$guardianName."',";
		$strInput = $strInput."contact_no='".$contactNo."',";
		$strInput = $strInput."alergy='".$allergy."',";
		$strInput = $strInput."alergy_desc='".$allergd."',";
		$strInput = $strInput."email='".$emailAdd."'";

		$strSql = @mysql_query("update tr_student set ".$strInput." where student_id=".$stdntID);
		
		$errmsg = "You have successfully updated your student.";
	}
}
else if($school==0)
{
	
	$strSql1 =@mysql_query("select * from tr_student where student_id=".$stdntID);
	$cntr=@mysql_num_rows($strSql1);
	$rsu=@mysql_fetch_array($strSql1);
	if($cntr>0)
	{
		$selp=@mysql_query("select * from tr_parent where parent_id='".$rsu['parent_id']."'");
		$rsp=@mysql_fetch_array($selp);
		
		$school =$rsu["school_id"];
		$studentfName = $rsu["student_fname"];
		$studentlName=$rsu["student_lname"];
		$emailAdd=$rsu["email"];
		$guardianName=$rsu["parent_guardian"];
		if($rsu["contact_no"]!="")
			$contactNo=$rsu["contact_no"];
		else
			$contactNo=$rsp["contact_no"];

		if(trim($rsu["date_birth"])!="")
		{
			$dob=explode("-",$rsu["date_birth"]);
			$byear=$dob[0];
			$bmonth=$dob[1];
			$bday=$dob[2];
		}
		if(trim($rsu["teacher"])!="")
			$tchrID = $rsu["teacher"];
		if(trim($rsu["grade"])!="")
			$gradeID =$rsu["grade"];
		
		$allergy=$rsu['alergy'];
		$allergd=$rsu['alergy_desc'];
	}
}
?>
<tr>
	<td>
		<table width="100%" cellpadding="0" cellspacing="0" border="0" height='300px'>
			<tr>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td  class="fs14 onlines blue" align='center'><b><u>EDIT STUDENT</u></b></td>
			</tr>
			<tr>
				<td>
					<table width="100%" cellpadding="1" cellspacing="1" border="0">
						<tr><td colspan="2"><hr /></td></tr>
				
						<?php
						if(strtoupper(substr(trim($errmsg),0,8)) == "YOU HAVE")
						{
							echo "<tr valign='top' align='center'><td colspan='4'>".$errmsg."</td></tr>";
						?>
							<meta http-equiv="Refresh" content="3;url=parentAccount.php"><br/><br/><br/><br/><br/>
						<?php
						}
						else if($_POST['eaction']=='CANCEL')
						{
							echo '<script>';
							echo 'location.replace("parentAccount.php");';
							echo '</script>';
						?>
						
						<?php
						}
						
						else
						{
							?>
						<tr>
							<td>
								<form action="edit_student.php?id=<?=$stdntID?>" method="post" name="frm">
									<table width="80%" border="0" cellpadding="0" cellspacing="1" class="fs12" align='center'>
										<tr>
											<td height="10" width="20%"></td><input type="hidden" name="stdntID" value="<?=$stdntID?>" />
											<td width="30%" class="errmsg"><?=$errmsg?></td>
											<td height="10" width="15%"></td>
											<td width="35%" class="fs10" valign="bottom"><i>* required fields</i></td>
										</tr>
										<tr>
											<td align="right" nowrap><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" onChange="javascript:{if(this.value!='') submit();}">
													<?php
													$strSql2 =@mysql_query("select school_id,school_name from tr_schools where status=1");
													$cnt1=@mysql_num_rows($strSql2);
													if($cnt1>0)
													{
														while($rs1=@mysql_fetch_array($strSql2))
														{

															if($school==$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" nowrap><b>* </b>LAST NAME:&nbsp;</td>
											<td><input class="formInput" type="text" name="studentlName" value="<?=$studentlName?>" /></td>
											<td align="right" nowrap>BIRTH DATE:&nbsp;</td>
											<td>
												<select name="byear" class="formYear">
													<?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">
													<?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">
											<?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" nowrap><b>* </b>GUARDIAN NAME:&nbsp;</td>
										<td><input class="formInput" type="text" name="guardianName" value="<?=$guardianName?>" /></td>
										<td align="right" nowrap>TEACHER:&nbsp;</td>
										<td>
											<select name="tchrID" class="formSelect">
											<option value='0'>--Select--</option>
											<?php
											
											$strSql3 = @mysql_query("select teacher_id,teacher_name from tr_teachers where status=1 and school_id=".$school);
											$cntr2=@mysql_num_rows($strSql3);
											if($cntr2>0)
											{
												while($rs3=@mysql_fetch_array($strSql3))
												{
													if($tchrID==$rs3['teacher_id'])
														echo '<option value='.$rs3['teacher_id'].' selected>'.$rs3['teacher_name'].'</option>';
													else
														echo '<option value='.$rs3['teacher_id'].'>'.$rs3['teacher_name'].'</option>';
												}
											}
												?>
											</select>
										</td>
									</tr>
									<tr>
										<td align="right" nowrap><b>* </b>CONTACT NO.:&nbsp;</td>
										<td><input class="formInput" type="text" name="contactNo" value="<?=$contactNo?>" /></td>
										<td align="right" nowrap>GRADE:&nbsp;</td>
										<td>
											<select name="gradeID" class="formSelect">
										
											<?php
											$strSqlg =@mysql_query("select grade_id,grade_name from ma_grades where status=1 order by orderBy");
											$cntg=@mysql_num_rows($strSqlg);
											if($cntg>0)
											{
												echo "<option value='0'>--Select--</option>";
												while($rsg=@mysql_fetch_array($strSqlg))
												{
													
													if($gradeID==$rsg['grade_id'])
														echo '<option value='.$rsg['grade_id'].' selected>'.$rsg['grade_name'].'</option>';
													else
														echo '<option value='.$rsg['grade_id'].'>'.$rsg['grade_name'].'</option>';
												}
											}
											 ?>
										
											</select>
										</td>
									</tr>
									<tr>
										<td align="right" nowrap>EMAIL ADDRESS:&nbsp;</td>
										<td><input class="formInput" type="text" name="emailAdd" value="<?=$emailAdd?>" /></td>
										<td align="right" nowrap>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 align="right">Allergies?</td>
									<td>
									<?php
									if($allergy=="1")
										$chm="checked";
									else
										$chm="";
									?>
									<input type="checkbox" id="stAllergy" name="stAllergy" value='1' <?=$chm?> /> 
									<input type="text" id="stAllergy_desc" name="stAllergy_desc" value="<?=$allergd?>"/>
									</td>
								</tr>
									<tr>
										<td colspan="3"></td>
										<td><input class="fs12" type="submit" name="action" value="UPDATE" /><input type='submit' name='eaction' value='CANCEL'/></td>
									</tr>
								</table>
 							</form>
						</td>
					</tr>
			<?php
				}
			?>
			<tr><td colspan="2"><hr /></td></tr>
			</table>
		</td>
	</tr>
</table>
</td>
</tr>
<?php
include("include/footer.php");
?>