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

//propagate the data we need
if ($_GET['lid']) 	$lid= $_GET['lid'];  else 	$lid = $_POST['lid'];
if ($_GET['lrid']) 	$lrid= $_GET['lrid']; else	$lrid = $_POST['lrid'];	
if ($_GET['mode'])	$mode= $_GET['mode']; else	$mode = $_POST['mode'];		


$do_close=0;

if (count($_POST) > 0)
{
	
	$resid = $_POST['resource'];
	$d = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
	$other=secure_user_input($_POST['other']);
	
	if ($mode == "new")
	{
	
		$SQL="INSERT INTO learner_resource 
			(
			learner_id, 
			res_id,
			start, 
			other
			)
			VALUES (
			'".$lid."',
			'".$resid."',
			'".$d."',
			'".$other."'
			)";
	}
	else
	{
		$SQL="UPDATE learner_resource  SET
				learner_id= '".$lid."',
				res_id= '".$resid."',
				start =	  '".$d."',
				other =	  '".$other."'
				WHERE id=".$lrid;
	}
	$results = llnb_query($SQL);

	//echo $SQL;	
	$do_close=1;
}
else if ($_GET['lrid'])
{

	//fetch the resource
	$SQL="SELECT *, DAY(start) as theday, MONTH(start) as themonth, YEAR(start) as theyear from learner_resource
		WHERE id=".$_GET['lrid']." ";
	$results = llnb_query($SQL);
	$row = llnb_fetch_assoc($results);
}
?>
<head>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/llnb-style.css">

<script language="javascript">
  function displayOther()
  {
 	document.getElementById("other").style.display = '';
  }
</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;}

li { margin-bottom: 2px;}

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_resources.php?lid=".$lid."';</script>"; 

?>

<div style="width: 660px;">
<h2>Add Resource for:<br />
  Learner <?= fetchLearnerName($lid); ?></h2>

<form name="addresource" action="learner_resourcelist_m.php" method="post">

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

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


<select name="year" title="Year">
       	<?
       	$myyear = ($_POST['year']) ? stripslashes($_POST['year']) : $row['theyear'];
       	echo buildFiveYearDropdown($myyear, date('Y')); 
       	?>
</select>

</div>

<div style="clear:both;"></div>

    <div class="input" style="width: 660px;">
        <label>List of Resources:</label>

	<?= buildResourceList($row['res_id'], $row['other']); ?>
     <div style="clear:both;"></div>    
</div>

<p><input type="submit"  class="btnlfr_m" value="Save &amp; Close">
<a href="#" onclick="parent.SqueezeBox.close();"class="btn lfr">Cancel</a>
</p>
<input type="hidden" name="lrid" value="<?=$_GET['lrid'];?>">
<input type="hidden" name="lid" value="<?=$_GET['lid'];?>">
<input type="hidden" name="mode" value="<?=$_GET['mode'];?>">

</form>
</div>