<?
include('auth.php');
include('common.php');

$do_close=0;
if ($_GET['lid']) 	$lid= $_GET['lid'];  else 	$lid = $_POST['lid'];
if ($_GET['pnid']) 	$lrid= $_GET['pnid']; else	$lrid = $_POST['pnid'];	
if ($_GET['mode'])	$mode= $_GET['mode']; else	$mode = $_POST['mode'];	

if (count($_POST) > 0)
{
	$note = secure_user_input($_POST['note']);
	$d = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
	$lgid = $_POST['lgid'];
	
	
	if (!$_POST['lgid'])
	{
		$MSSG= "No Goal selected: a Progress Note must be associated with a Goal";
	}
	else
	{
	
	
		if ($_POST['pnid']) //update
		{
			$SQL="UPDATE progressnotes 
				SET 
				learnergoal_id = '".$lgid."',
				note = '".$note."',
				thedate = '".$d."' WHERE id=".$_POST['pnid'];

			//echo $SQL;
			$do_close=1;

		}
		else //insert new record
		{

		    $SQL="INSERT INTO progressnotes (learnergoal_id,  note, thedate)
				VALUES ('".$_POST['lgid']."','".$note."', '".$d."')";	
			$do_close=1;

		}
		$results = llnb_query($SQL);
	}
}
else 
{

	if ($mode == "edit")
	{
		$SQL="SELECT *, DAY( thedate ) AS theday, MONTH( thedate ) AS themonth, YEAR( thedate ) AS theyear
			FROM progressnotes WHERE id=".$_GET['pnid'];
		$results = llnb_query($SQL);
		$rowprog = llnb_fetch_assoc($results);
	
	}

	$SQL="SELECT * FROM `learnergoals` WHERE learner_id = ".$lid." AND alive=1"; 
	$results = llnb_query($SQL);

	$options.="<select name=\"lgid\" >";

	$i=0;
	while($row = llnb_fetch_assoc($results))
	{
		
		if ($row['id']== $rowprog['learnergoal_id'] )
			$tsel = "selected";
		else
			$tsel ="";
		$options.="<option value=\"".$row['id']."\" ".$tsel.">".$row['goal']."</option>\n";
		$i++;
	}
	$options.="</select>
	<input type=\"hidden\" id=\"goalready\" value=\"1\">
	";

	if ($i==0)
		$options ="No goals avail <input type=\"hidden\" id=\"goalready\" value=\"-1\">";
	
}
?>
<head>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/llnb-style.css">

<script>

function goalReady()
{
	if (self.document.getElementById('goalready').value == -1)
	{
		self.alert("No learnging goal associated with Progress Note");
		return false;
	}
	else	if (self.document.getElementById('goalready').value == 1)
	{
			return true;
	}
	
}
</script>

</head>






<style type="text/css">

body { background-color: #fff; background-image:none; padding: 20px;}
div { font-family: sans-serif; color: #222; }

.btn	{ -moz-border-radius: 7px; -webkit-border-radius: 7px; -khtml-border-radius: 7px; border-radius: 7px; border: 1px #222 solid; background-image: url(img/btn-bg-nor.jpg) ; line-height: 25px; padding: 3px 10px 0; text-decoration: none; color: #222; }
.btn:hover	{ border-color: #33C; color: #33c;}

.btnhidden {border-color: #CCC; background-image: none; font-size: .8em;}

.btncnt	{ width: 160px; text-align:center; display: block; margin-bottom: 10px;}

.btnlrg	{width: 300px; padding-left: 20px;}
.btnsml	{ padding: 1px 10px; font-weight: bold;}
.btnpg	{ padding: 2px 4px 1px; font-size: .75em;margin-right: 4px;}

.lfr	{ float: right; margin-right: 10px;}
.lfl	{ float: left; margin-right: 10px;}
form label { display:block; margin-top: 10px; font-size: .8em; color: #555; }
.session { margin-bottom: 15px; padding-bottom: 10px;}
.talrg	{ width: 460px; height: 60px;}

input.ssminp[type="text"] {width: 40px;}
input.sminp[type="text"] {width: 80px;}
input.btn { height: 30px;}


</style>

<body >

<? 
if ($do_close) 
	echo "<script>parent.location.href='learner_progressnotes.php?lid=".$lid."';</script>"; 

?>

<div style="width: 500px;">
<h2>Add Progress Note for:<br />
      Learner: <?= fetchLearnerName($lid); ?></H2>
<form name="addprogressnote" action="learner_progressnotes_m.php" method="post" onSubmit="return goalReady();">

<div class="session">
<div style="float:left; display:block;  margin-bottom: 15px;">

<?
if ($MSSG)
	echo "<label style=\"display:block; color: red;\">".$MSSG."</label>";

?>


<label style="display:block;">Select Goal for Progress Note:</label>
<?=$options;?>
</div>
<div style="clear:both;"></div>

<div style="float:left; display:block;  margin-bottom: 15px;">

<label style="display:block;">Date:</label>
<select name="month" title="month">
       	<?
       	$mymon = ($_POST['month']) ? stripslashes($_POST['month']) : $rowprog['themonth'];
       	echo buildMonthDropdown($mymon, $_MONTHS_abbr); 
       	?>
</select>
<select name="day" title="date">
       	<?
       	$myday = ($_POST['day']) ? stripslashes($_POST['day']) : $rowprog['theday'];
       	echo buildDayDropdown($myday); 
       	?>
</select>

<select name="year" title="Year">
       	<?
       	$myyear = ($_POST['year']) ? stripslashes($_POST['year']) : $rowprog['theyear'];
       	echo buildYearDropdown($myyear, "2012"); 
       	?>
</select>
</div>

<div style="clear:both;"></div>
<label>Progress notes:</label>
<textarea name="note" cols="" rows="3"  class="talrg"><?=$rowprog['note'];?></textarea>

</div>
<input type="hidden" name="pnid" value="<?=$_GET['pnid'];?>">
<input type="hidden" name="lid" value="<?=$_GET['lid'];?>">
<input type="hidden" name="mode" value="<?=$_GET['mode'];?>">

<p class="clearfix">
<input type="submit"  class="btnlfr_m" value="Save &amp; Close">
<a href="#" onclick="parent.SqueezeBox.close();"class="btn lfr">Cancel</a>
</p>
</form>
</div>