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

$do_close=0;

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

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



if (count($_POST) > 0)
{
	$orgname = secure_user_input($_POST['orgname']);
	$orgcontact = secure_user_input($_POST['orgcontact']);
	$address = secure_user_input($_POST['address']);
	$unit = secure_user_input($_POST['unit']);
	$pobox = secure_user_input($_POST['pobox']);
	$city = secure_user_input($_POST['city']);
	$prov = secure_user_input($_POST['prov']);
	$postalcode = secure_user_input($_POST['postalcode']);
	$phone = $_POST['ph1'].$_POST['ph2'].$_POST['ph3'];
	$curr = secure_user_input($_POST['current']); if (!$curr) $curr=0;
	$resp = secure_user_input($_POST['resp']);
	$startdate = $_POST['yearstart']."-".$_POST['monthstart']."-".$_POST['daystart'];
	$enddate = $_POST['yearend']."-".$_POST['monthend']."-".$_POST['dayend'];

	if ($_POST['void']) //update
	{
		$SQL="UPDATE volunteerorg 
			SET 
			user_id=    '".$_POST['id']."',
			user_type=   '".$_POST['utype']."',
			orgname= 		'".$orgname."',
			orgcontact=	 '".$orgcontact."',
			address = 	'".$address."',
			unit= 		'".$unit."',
			pobox=		 '".$pobox."',
			city=		 '".$city."',
			prov=		 '".$prov."',
			postalcode=		 '".$postalcode."',
			phone= 		'".$phone."',
			startdate= 		'".$startdate."',
			enddate= 		'".$enddate."',
			current=		 '".$curr."',
			resp= 		'".$resp."'
				WHERE id=".$_POST['void'];
	}
	else //insert new record
	{
		$SQL="INSERT INTO volunteerorg (
			user_id, 
			user_type,
			orgname,
			orgcontact,
			address ,
			unit	,
			pobox	,
			city	,
			prov	,
			postalcode,
			phone,
			startdate,
			enddate	,
			current,
			resp
				) 	

		VALUES 
		(
		'".$_POST['id']."',
		 '".$_POST['utype']."',
			'".$orgname."',
			 '".$orgcontact."',
			'".$address."',
			'".$unit."',
			 '".$pobox."',
			 '".$city."',
			 '".$prov."',
			 '".$postalcode."',
			'".$phone."',
			'".$startdate."',
			'".$enddate."',
			 '".$curr."',
			'".$resp."'
		)";
		
	}
	$results = llnb_query($SQL);
	$do_close=1;
	
	
	//echo $SQL." ****************";
	
	if ($_GET['frompg'] == "tutor_volunteer_org")
		$pgreturn= "tutor_volunteerinfo.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'] == "volunteer_volunteer_org" )
		$pgreturn= "volunteer_volunteerinfo.php?vid=".$_POST['id'];		
	else
		$a=1;
		
}
else 
{
	
	
	if ($_GET['mode']=="edit")
	{
		$SQL="SELECT * , 
			DAY( startdate ) AS thedays, MONTH( startdate ) AS themonths, YEAR( startdate ) AS theyears,
			DAY( enddate ) AS thedaye, MONTH( enddate ) AS themonthe, YEAR( enddate ) AS theyeare
			FROM `volunteerorg` 
			WHERE id=".$_GET['void'];
			//echo $SQL;
			
		$results = llnb_query($SQL);
		$row = llnb_fetch_assoc($results);
		
		//checkboxes
		$iscurrent = $row['current'];
		if ($iscurrent)
			$currchecked= "checked";
		
		//assemble phone number
		$phnums = phonenumberToPieces($row['phone']);
		$ph1 = $phnums[0]; $ph2 = $phnums[1]; $ph3 = $phnums[2]; 
		
		//create date pieces from DB
		$datepieces = explode("-", $row['startdate']);
		$startmonth = $datepieces[1];
		$startday = $datepieces[2];
		$startyear = $datepieces[0];

		$datepieces = explode("-", $row['enddate']);
		$endmonth = $datepieces[1];
		$endday = $datepieces[2];
		$endyear = $datepieces[0];
		$prov=$row['prov'];
	}

}
?><head>
	<link rel="stylesheet" href="css/style.css">
	<link rel="stylesheet" href="css/llnb-style.css">
</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 10px; 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; }

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


.btnbox {padding-top: 10px;}


</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);

}




?>

<body onLoad="document.forms.addtime.orgname.focus()">

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

      <div class="input">
        <label>Organization Name:</label>
        <input name="orgname" type="text"   value="<?= ($_POST['orgname']) ? stripslashes($_POST['orgname']) : $row['orgname']; ?>" class="midinp">
      </div>
      <div style="clear:both;"></div>
      <div class="input">
        <label>Contact(s) Name:</label>
        <input name="orgcontact" type="text"   value="<?= ($_POST['orgcontact']) ? stripslashes($_POST['orgcontact']) : $row['orgcontact']; ?>" class="midinp">
      </div>
      <div style="clear:both;"></div>
      <div class="input">
        <label>Street Address:</label>
        <input name="address" type="text"   value="<?= ($_POST['address']) ? stripslashes($_POST['address']) : $row['address']; ?>" class="midinp">
      </div>
      <div class="input">
        <label>Unit.#:</label>
        <input name="unit" type="text"    value="<?= ($_POST['unit']) ? stripslashes($_POST['unit']) : $row['unit']; ?>" class="ssminp">
      </div>
      <div class="input">
        <label>PO Box:</label>
        <input name="pobox" type="text"   value="<?= ($_POST['pobox']) ? stripslashes($_POST['pobox']) : $row['pobox']; ?>" class="sminp">
      </div>
      <div style="clear:both;"></div>
      <div class="input">
        <label>City/Town:</label>
        <input name="city" type="text" value="<?= ($_POST['city']) ? stripslashes($_POST['city']) : $row['city']; ?>"  >
      </div>
      <div class="input">
        <label>Province:</label>
          <select name="prov" class="typevolunteer" title="prov">
			<?
			echo  buildProvDropdown($prov);
			?>
          </select>
      </div>
      <div class="input">
        <label>Postal Code:</label>
        <input name="postalcode" type="text" value="<?= ($_POST['postalcode']) ? stripslashes($_POST['postalcode']) : $row['postalcode']; ?>" class="sminp">
      </div>
      <div style="clear:both;"></div>

  <div class="input">
          <label> Phone:</label>
          <input class="ssminp" name="ph1" MAXLENGTH="3" value="<?= ($_POST['ph1']) ? stripslashes($_POST['ph1']) : $ph1; ?>" type="text">
          <input class="ssminp" name="ph2" MAXLENGTH="3" value="<?= ($_POST['ph2']) ? stripslashes($_POST['ph2']) : $ph2; ?>" type="text">
          <input class="smminp" name="ph3" MAXLENGTH="4" value="<?= ($_POST['ph3']) ? stripslashes($_POST['ph3']) : $ph3; ?>" type="text">
        </div>
      <div style="clear:both;"></div>

        <div style="width:550px;">
        <div class="input">
        <label style="display:block;">Start Date:</label>
		<select name="monthstart" title="month">
		<?
				$mymon = ($_POST['monthstart']) ? stripslashes($_POST['monthstart']) : $row['themonths'];
				echo buildMonthDropdown($mymon, $_MONTHS_abbr); 
				?>
		</select>
	<select name="daystart" title="date">
			<?
			$myday = ($_POST['daystart']) ? stripslashes($_POST['daystart']) : $row['thedays'];
			echo buildDayDropdown($myday); 
			?>
	</select>
	<select name="yearstart" title="Year">
			<?
			$myyear = ($_POST['yearstart']) ? stripslashes($_POST['yearstart']) : $row['theyears'];
			echo buildYearDropdown($myyear, date('Y')); 
			?>
	</select>
        </div>
        <div class="input plusleft">
        <label style="display:block;">&nbsp;</label>
		<label class="check">
		<input name="current" type="checkbox" value="1" <?=$currchecked;?>>Current</label>
        </div>
        
        <div class="input plusleft">
        <label style="display:block;">End Date:</label>
	<select name="monthend" title="month">
				<?
				$mymon = ($_POST['monthend']) ? stripslashes($_POST['monthend']) : $row['themonthe'];
				echo buildMonthDropdown($mymon, $_MONTHS_abbr); 
				?>
		</select>
	<select name="dayend" title="date">
			<?
			$myday = ($_POST['dayend']) ? stripslashes($_POST['dayend']) : $row['thedaye'];
			echo buildDayDropdown($myday); 
			?>
	</select>
        
	<select name="yearend" title="Year">
			<?
			$myyear = ($_POST['yearend']) ? stripslashes($_POST['yearend']) : $row['theyeare'];
			echo buildYearDropdown($myyear, date('Y')); 
			?>
	</select>
        </div>


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

        <div class="input">
            <label>Responsibilities:</label>
            <textarea name="resp" cols="" rows="3"  class="talrg"><?= ($_POST['resp']) ? stripslashes($_POST['resp']) : $row['resp']; ?></textarea>
        </div>
        <div style="clear:both;"></div>
        <div class="btnbox">
<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="<?=$void;?>" name="void">
<input type="hidden" value="<?=$id;?>" name="id">
<input type="hidden" value="<?=$utype;?>" name="utype">
</form></div>
</div>
</body>