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

$errmsg = "";
$action=$_POST["action"];
$startDate=$_POST["startDate"];
$endDate=$_POST["endDate"];
$incMon=$_POST["incMon"];
$incTue=$_POST["incTue"];
$incWed=$_POST["incWed"];
$incThu=$_POST["incThu"];
$incFri=$_POST["incFri"];
$schoolID=$_GET["schlID"];
$dateCnt = 0;

if(strtoupper($action)== "CREATE")
{
	if($startDate=="")
		$errmsg = $errmsg."From Date field is empty.<br/>";
	if($endDate=="")
		$errmsg = $errmsg."To Date field is empty.<br/>";

	$sDate=explode("/",$startDate);
	$ssDate=$sDate[2]."-".$sDate[1]."-".$sDate[0];
	
	$eDate=explode("/",$endDate);
	$eeDate=$eDate[2]."-".$eDate[1]."-".$eDate[0];



	$date1 = strtotime($ssDate);
	$date2 = strtotime($eeDate);

	if($date1 > $date2)
	{
		$errmsg = $errmsg."To Date field cannot be before From Date.<br/>";
	}
	
	if(trim($errmsg)=="")
	{
		//do until date1 > date2
		do
		{
					
			$addMe = false;
		
			$wdate1=date('D', $date1);

			if($incMon==$wdate1)
				$addMe="true";
			else if($incMon==$wdate1)
				$addMe="true";
			else if($incTue==$wdate1)
				$addMe="true";
			else if($incWed==$wdate1)
				$addMe="true";
			else if($incThu==$wdate1)
				$addMe="true";
			else if($incFri==$wdate1)
				$addMe="true";

			if($addMe == true)
			{
				$dateaa=date("Y-m-d",$date1);
				$strSql = @mysql_query("insert into tr_school_dates (school_id, orderDate,estatus) VALUES (".$schoolID.",'".$dateaa."','1')");

				$dateCnt = $dateCnt + 1;
			}
			$date1=strtotime("+1 day",$date1);
			//);
		
		}while($date1 <= $date2);
		$errmsg = $errmsg."You have created ".$dateCnt." new dates for this school.";
	}
}
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr><td></td><td align="center" class="fs14 black"><u><b>Create New Dates</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>
				<form action="schoolDateNew.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"></td></tr>
				<tr><td align="right">FROM DATE:&nbsp;&nbsp;</td>
				<td><input class="formInput" type="text" name="startDate" value="<?=$startDate?>" /><a href="javascript:setDate('startDate');" onMouseOver="window.status='Date Picker';return true;" onMouseOut="window.status='';return true;"><img src="../cal.gif" width=16 height=16 border=0></a></td>
				<td align="right">TO DATE:&nbsp;</td>
				<td><input class="formInput" type="text" name="endDate" value="<?=$endDate?>" /><a href="javascript:setDate('endDate');" onMouseOver="window.status='Date Picker';return true;" onMouseOut="window.status='';return true;"><img src="../cal.gif" width=16 height=16 border=0></a>
				</td></tr>
				<tr><td></td><td></td></tr>
                <tr><td align="right">DAYS TO INCLUDE:</td><td></td></tr>
                <tr><td></td><td><input type="checkbox" name="incMon" value="Mon" checked /> Monday</td></tr>
                <tr><td></td><td><input type="checkbox" name="incTue" value="Tue" checked /> Tuesday</td></tr>
                <tr><td></td><td><input type="checkbox" name="incWed" value="Wed" checked /> Wednesday</td></tr>
                <tr><td></td><td><input type="checkbox" name="incThu" value="Thu" checked /> Thursday</td></tr>
                <tr><td></td><td><input type="checkbox" name="incFri" value="Fri" checked /> Friday</td></tr>
				<tr><td colspan="3"></td><td><input class="fs12" type="submit" name="action" value="CREATE" /></td></tr>
				</form>
				<tr><td colspan="4"><hr /></td></tr>
			</table></td></tr>
</table>
<?php
include("footer.php");
?>