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

$errmsg = "";
$schooldID=0;

if(trim($_GET['id'])!="")
	$schoolID=$_GET['id'];
elseif(trim($_POST['schoolID'])!="")
	$schoolID=$_POST['schoolID'];

$action=$_POST['action'];
$schoolName=$_POST['schoolName'];
$sState=$_POST['sState'];
$address=$_POST['address'];
$country=$_POST['country'];
$city=$_POST['city'];
$isnacks=$_POST['snak'];
$idrinks=$_POST['drink'];

if(strtoupper($action) == "UPDATE")
{
	if(trim($schoolName)=="")
		$errmsg = "School Name field is empty";
	

	if(trim(strtoupper($sState))=="AA")
	{
		if(trim($errmsg)!="")
			$errmsg = $errmsg."<br/>Select a State";
		else
			$errmsg = "Select a State";
		
	} 

	if(trim($errmsg)=="")
	{
		$strSql = mysql_query("update tr_schools set school_name='".$schoolName."',address='".$address."',city='".$city."',state='".$sState."',country='".$country."' where school_id='".$schoolID."'");
		
		$sel=@mysql_query("select * from tr_snak_drink where status='1' and school_id='".$schoolID."'");
		$rec=@mysql_num_rows($sel);
		if($rec>0)
		{
			$upd=@mysql_query("update tr_snak_drink set snacks='".$isnacks."',drinks='".$idrinks."' where school_id='".$schoolID."'");
		}
		else
		{
			$ins=@mysql_query("insert into tr_snak_drink values('','".$isnacks."','".$idrinks."','".$schoolID."','1')");
		}

		$errmsg = "You have successfully updated your school.";
	}

}
else
{

	$strSql3 = mysql_query("select * from tr_schools where school_id='".$schoolID."'");
	$scnt=mysql_num_rows($strSql3);
	if($scnt>0)
	{
		$rs3=@mysql_fetch_array($strSql3);

		$selrec=@mysql_query("select * from tr_snak_drink where status='1' and school_id='".$schoolID."'");
		$rowrec=@mysql_fetch_array($selrec);

		$schoolID = $rs3['school_id'];
		$schoolName = trim($rs3['school_name']);
		$sState = trim($rs3['state']);
		$address = trim($rs3['address']);
		$country = trim($rs3['country']);
		$city = trim($rs3['city']);

		$snacks=$rowrec['snacks'];
		$drink=$rowrec['drinks'];
	}
}
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr><td></td><td align="center" class="fs14 black"><u><b>Update School <?=$schoolName?></b></u></td></tr>
	<tr><td width="200" class="fs12" valign="top"><?php include("sidemenu.php");?></td>
		<td width="700" class="fs12">
			<table width="680" cellpadding="1" cellspacing="1" border="0">
			<tr><td colspan="4"><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=schools.php"><br/><br/><br/><br/><br/>
		<?php
		}
		else
		{
		?>
				<form action="schoolUpdate.php?id=<?=$schoolID?>" method="POST" name="frm">
				<input type="hidden" name="schoolID" value="<%=schoolID%>" />
				<tr><td height="10" width="17%"></td><td width="33%" class="fs10"><i>* required fields</i></td><td height="10" width="15%"></td><td width="35%"></td></tr>
				<tr><td align="right"><b>* </b>SCHOOL NAME:&nbsp;</td><td><input class="formInput" type="text" name="schoolName" value="<?=$schoolName?>" /></td><td align="right">STATE:&nbsp;</td>
					<td><select name="sState" class="formSelect">
				<?php
					$strSql = mysql_query("select state_code,state_name from ma_states where status=1 and country_id=1");
					$strcnt=@mysql_num_rows($strSql);
					if($strcnt>0)
					{
						echo "<option value='AA'>-- Select --</option>";

						while($rs=@mysql_fetch_array($strSql))
						{
							if(strtoupper(trim($sState)) == trim($rs['state_code']))							
								echo "<option value='".trim($rs['state_code'])."' selected >".$rs['state_name']."</option>";
							else
								echo "<option value='".trim($rs['state_code'])."' >".$rs['state_name']."</option>";
						}
					}
					?>
						</select></td></tr>
				<tr><td align="right">ADDRESS:&nbsp;</td><td><input class="formInput" type="text" name="address" value="<?=$address?>"/></td><td align="right">COUNTRY:&nbsp;</td>
					<td><select name="country" class="formSelect">
				<?php
					$strSql2 = mysql_query("select country_code,country_name from ma_country where status=1");
					$strcnt2=@mysql_num_rows($strSql2);
					if($strcnt2>0)
					{
						while($rs2=@mysql_fetch_array($strSql2))
						{
							if(strtoupper(trim($sState)) == trim($rs2['country_code']))
								echo "<option value='".$rs2['country_code']."' selected >".$rs2['country_name']."</option>";
							else
								echo "<option value='".$rs2['country_code']."' >".$rs2['country_name']."</option>";							
						}
					}
					?>
						</select></td></tr>
				<tr><td align="right">CITY:&nbsp;</td><td><input class="formInput" type="text" name="city" value="<?=$city?>"/></td><td colspan="2"></td></tr>
				<?php
				if($snacks=='y')
				{
					$chk1="checked";
					$chk2="";
				}
				else if($snacks=='n')
				{
					$chk2="checked";
					$chk1="";
				}

				 if($drink=='y')
				{
					$chk3="checked";
					$chk4="";
				}
				else if($drink=='n')
				{
					$chk4="checked";
					$chk3="";
				}
				?>
				<tr><td align="right">SNACKS:</td><td><input type='radio' name='snak' value='y' <?=$chk1?>/>On&nbsp;&nbsp;<input type='radio' name='snak' value='n' <?=$chk2?>/>Off&nbsp;&nbsp;</td><td align="right">DRINKS:</td><td><input type='radio' name='drink' value='y' <?=$chk3?>/>On&nbsp;&nbsp;<input type='radio' name='drink' value='n' <?=$chk4?>/>Off&nbsp;&nbsp;</td></tr>
				<tr valign="top"><td></td><td class="fs12 errmsg" colspan="2"><?=$errmsg?></td><td><input class="fs12" type="submit" name="action" value="UPDATE" /></td></tr>
				</form>
		<?php
		}
		?>
				<tr><td colspan="4"><hr /></td></tr>
			</table></td></tr>
</table>
<?php
include("footer.php");
?>
