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


$do_close=0;

if ($_GET['tid']||$_GET['coordid']||$_GET['eid']||$_GET['vid'])
{
	//tutor
	if ($_GET['tid']) 	$id= $_GET['tid']; 
	//coord
	if ($_GET['coordid']) 	$id= $_GET['coordid']; 
	//exec dir
	if ($_GET['eid']) 	$id= $_GET['eid'];  
	//volunteer
	if ($_GET['vid']) 	$id= $_GET['vid']; 
	
}
else //simply propogate
	$id = $_POST['id'];

if ($_GET['vsid']) 	$vsid= $_GET['vsid'];  else 	$vsid = $_POST['vsid'];
if ($_GET['utype']) $utype= $_GET['utype'];  else 	$utype = $_POST['utype'];



if (count($_POST) > 0)
{
	$notes = secure_user_input($_POST['notes']);
	$d = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
	$t = $_POST['hours'].":".$_POST['minutes'].":00";
	
	if ($_POST['vsid']) //update
	{
		$SQL="UPDATE volunteersession 
			SET 
			user_id= '".$_POST['id']."',
			user_type= '".$_POST['utype']."',
			notes = '".$notes."',
			thedate = '".$d."',
			duration = '".$t."'
			WHERE id=".$_POST['vsid'];
	}
	else //insert new record
	{
		$SQL="INSERT INTO volunteersession (user_id, user_type, notes, thedate, duration )
		VALUES ('".$_POST['id']."', '".$_POST['utype']."', '".$notes."', '".$d."', '".$t."')";
	}
	$results = llnb_query($SQL);
	
	$do_close=1;
	
	
	//GET comes from form ? url string
	if ($_GET['frompg'] == "tutor_volunteer_timelist")
		$pgreturn= "tutor_volunteer_timelist.php?tid=".$_POST['id'];
	else	if ($_GET['frompg'] == "coordinator_timelist")
		$pgreturn= "coordinator_timelist.php?coordid=".$_POST['id'];
	else	if ($_GET['frompg'] == "volunteer_list" || $_GET['frompg'] == "volunteer_timelist")
		$pgreturn= "volunteer_timelist.php?vid=".$_POST['id'];
	else if ($_GET['frompg'] == "executive_director_timelist")
		$pgreturn = "executive_director_timelist.php?eid=".$_POST['id'];		
	else
		$a=1;
		
}
else 
{
	if ($_GET['mode']=="edit")
	{
		$SQL="SELECT * , 
			DAY( thedate ) AS theday, MONTH( thedate ) AS themonth, YEAR( thedate ) AS theyear,
			MINUTE( duration ) AS theminutes, HOUR( duration ) AS thehours
			FROM `volunteersession` 
			WHERE id=".$_GET['vsid'];
			//echo $SQL;
			
		$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">

<link href="scripts/SqueezeBox.css" rel="stylesheet" type="text/css" />
<script src="scripts/mootools.js" type="text/javascript" ></script>
<script src="scripts/SqueezeBox.js" type="text/javascript"></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: 500px; height: 60px;}

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


</style>

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




if ($utype == 1) 
{	
	$personname = fetchExecDirName($id);
}
else if ($utype == 2) 
{
	$personname = fetchTutorName($id);
}
else if ($utype == 3) 
{
	$personname = fetchVolName($id);

}
else if ($utype == 4) 
{
	$personname = fetchCoordName($id);

}
else if ($utype == 5) 
{
	$personname = "Executive Director";

}



?>

<div style="width: 500px;">
<h2>Add Volunteer Time for:<br />
    <?= $personname; ?></h2>
   
<form name="addtime" action="volunteer_add_time_m.php?frompg=<?=$_GET['frompg'];?>" method="post">

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


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

<div style="float:left;display:block;">

<label style="display:block;">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 buildYearDropdown($myyear, date('Y')); 
       	?>
</select>
</div>


<div class=" input plusleft">
<label>Amount:</label>
<select name="hours" title="amounthr">
<?
$myhrs = ($_POST['hours']) ? stripslashes($_POST['hours']) : $row['thehours'];
echo buildHourDropdown($myhrs);
?>
</select>

<select name="minutes" title="amountmin">
<? 
$mymin = ($_POST['minutes']) ? stripslashes($_POST['minutes']) : $row['theminutes'];
echo buildMinDropdown($mymin);
?>
</select>

</div>
</div>
<div style="clear:both;"></div>
<input type="hidden" name="lid" value="<?=$_GET['lid'];?>">



<label>Session notes:</label>
<textarea name="notes" cols="" rows="3"  class="talrg">
<?= ($_POST['notes']) ? stripslashes($_POST['notes']) : $row['notes']; ?>
</textarea>

</div>

<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>
<input type="hidden" value="<?=$vsid;?>" name="vsid">
<input type="hidden" value="<?=$id;?>" name="id">
<input type="hidden" value="<?=$utype;?>" name="utype">


</form>
</div>