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

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


$update_success=false;
$insert_success=false;

if (count($_POST) > 0) //form has been submitted by user
{
	//prep the data regardless
	$name= secure_user_input($_POST['name']);
	$code= secure_user_input($_POST['code']);
	$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['phone1'].$_POST['phone2'].$_POST['phone3'];

	//checkbox
	if ($_POST['mailsame'] == 1)
		$mailsame = "1";
	else
		$mailsame = "0";
	
	$address2= secure_user_input($_POST['address2']);
	$unit2= secure_user_input($_POST['unit2']);
	$pobox2= secure_user_input($_POST['pobox2']);
	$city2= secure_user_input($_POST['city2']);
	$prov2= secure_user_input($_POST['prov2']);
	$postalcode2= secure_user_input($_POST['postalcode2']);


	if (!$name || !$code )
		$mssg= "*** REQUIRED FIELDS MISSING ***";
	else
	{
		if ($_GET['mode'] == 'new' && !$_POST['cid'])
		{
			$SQL = "INSERT INTO council
			(`code`, 
			`name`,		
			`active` ,
			`address` ,		
			`unit` ,		
			`pobox` 	,	
			`city` ,		
			`prov` 	,	
			`postalcode`, 	
			`mailsame`, 	
			`phone` ,	
			`address2` ,		
			`unit2` ,		
			`pobox2` 	,	
			`city2` ,		
			`prov2` 	,	
			`postalcode2` 		
			)			
			 VALUES 
			('".$code."',
			 '".$name."',
			 '1',
			 '".$address."',
			 '".$unit."',
			 '".$pobox."',
			 '".$city."',
			 '".$prov."',
			 '".$postalcode."',
			 '".$mailsame."',
			 '".$phone."',
			 '".$address2."',
			 '".$unit2."',
			 '".$pobox2."',
			 '".$city2."',
			 '".$prov2."',
			 '".$postalcode2."'
	 
			)";						
		
			if (!llnb_query($SQL))
				echo "ERROR on INSERT SQL";
			//echo $SQL;
			$insert_success=true;
			header("Location: ./council_overview.php?cid=".$cid);
		
		}
		else if ($_POST['cid'] && $_GET['mode'] == 'edit' )
		{
			$SQL = "UPDATE council SET
				`name`=		    '".$name."',
				`code`=		    '".$code."',
				`active`=		    '1',
				`address`=		    '".$address."',
				`unit`=			    '".$unit."',
				`pobox`=		    '".$pobox."',
				`city`=			    '".$city."',
				`prov`=		    '".$prov."',
				`postalcode`= 		    '".$postalcode."',
				`mailsame`= 		    '".$mailsame."',
				`phone`=		    '".$phone."',
				`address2`=		    '".$address2."',
				`unit2`=			    '".$unit2."',
				`pobox2`=		    '".$pobox2."',
				`city2`=			    '".$city2."',
				`prov2`=		    '".$prov2."',
				`postalcode2`= 		    '".$postalcode2."'
				WHERE id=".$_POST['cid'];

			//echo $SQL;
			$results = llnb_query($SQL);
			$update_success = true;
			header("Location: ./council_overview.php?cid=".$cid);

		}
	}

}
else if (isset($_GET['cid']))
{
	$SQL="SELECT * FROM council WHERE id=".$_GET['cid'];
	$results = llnb_query($SQL);
	$row = llnb_fetch_assoc($results);

	//assemble phone numbers
	$phnums = phonenumberToPieces($row['phone']);
	$ph1 = $phnums[0]; $ph2 = $phnums[1]; $ph3 = $phnums[2]; 

	if ($row['mailsame'] == 1)
		$mailsame="checked";
	else
		$mailsame="";

	//prep the data regardless
	$name= secure_user_input($row['firstname']);
	$code= secure_user_input($row['code']);
	$address= secure_user_input($row['address']);
	$unit= secure_user_input($row['unit']);
	$pobox= secure_user_input($row['pobox']);
	$city= secure_user_input($row['city']);
	$prov= secure_user_input($row['prov']);
	$postalcode= secure_user_input($row['postalcode']);
	$address2= secure_user_input($row['address2']);
	$unit2= secure_user_input($row['unit2']);
	$pobox2= secure_user_input($row['pobox2']);
	$city2= secure_user_input($row['city2']);
	$prov2= secure_user_input($row['prov2']);
	$postalcode2= secure_user_input($row['postalcode2']);	
}
else //default values
{

	//assemble phone numbers
	$ph1= "506";

}

?>

<!doctype html>
<head>
<meta charset="utf-8">

<!-- Use the .htaccess and remove these lines to avoid edge case issues.
       More info: h5bp.com/i/378 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>LLNB System</title>
<meta name="description" content="">

<!-- Mobile viewport optimized: h5bp.com/viewport -->
<meta name="viewport" content="width=device-width, maximum-scale=1.0">

<!-- Place favicon.ico and apple-touch-icon.png in the root directory: mathiasbynens.be/notes/touch-icons -->
<link rel="SHORTCUT ICON" href="favicon.ico">

<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/llnb-style.css">

<!-- More ideas for your <head> here: h5bp.com/d/head-Tips -->

<script src="js/libs/modernizr-2.5.3.min.js"></script>

<script language="javascript">
  function showhide()
  {
	 if (document.getElementById("addtwo").style.display == 'none')
	 {
	 	document.getElementById("addtwo").style.display = '';
	 	document.getElementById("unittwo").style.display = '';
	 	document.getElementById("poboxtwo").style.display = '';
	 	document.getElementById("citytwo").style.display = '';
	 	document.getElementById("provtwo").style.display = '';
	 	document.getElementById("pcodetwo").style.display = '';

	 }
	 else
	 {
	 	document.getElementById("addtwo").style.display = 'none';
	 	document.getElementById("unittwo").style.display = 'none';
	 	document.getElementById("poboxtwo").style.display = 'none';
	 	document.getElementById("citytwo").style.display = 'none';
	 	document.getElementById("provtwo").style.display = 'none';
	 	document.getElementById("pcodetwo").style.display = 'none';
	 }
	  
  }
</script>

</head>
<body onLoad="document.forms.council.name.focus()">
<div id="wrap"> 
  <!-- Prompt IE 6 users to install Chrome Frame. Remove this if you support IE 6.
       chromium.org/developers/how-tos/chrome-frame-getting-started --> 
  <!--[if lt IE 7]><p class=chromeframe>Your browser is <em>ancient!</em> <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience this site.</p><![endif]-->
  <header><a href="index.php" class="btn lfr" style="position:relative; top: 15px; right: 0;">Logout</a>
	<h1>Laubach Literacy - <?=uType();?></h1> 
	<h3><?=fetchCouncilName($_SESSION['council_id']);?></h3>
	
    <nav id="menu" class="clearfix navmenu">
    <a href="home.php" class="btn lfl">Home</a>
  <?
 	    	if ($_SESSION['ulevel'] == $_EXECDIR)
		 	echo "<a href=\"council_list.php\" class=\"btn lfl\">Council List</a>";
  ?>  
    
    <?
    if ($_GET['mode'] != "new")
    {
    	  echo "<a href=\"council_overview.php?cid=".$cid."\" class=\"btn lfl\">Council Overview</a></nav>";
    }
    ?> 
    </header>
  <div role="main" id="main" class="clearfix">
    <div id="location">
	<?
	   if ($_GET['mode'] != "new")
		{
			echo "
			    <div id=\"location\">
				 <H2><span>Council:</span> ".$row['name']."</H2>
			    </div>";
		}
    ?>   

    </div>
    <!-- end of location -->
    <div>
    <H2>Council Information</H2>
          <div style="color: red;"><?=$mssg;?></div>

	 <form class="midform" name="council" action="council_personalinfo.php?mode=<?=$_GET['mode']; ?>&cid=<?= $_GET['cid'];?>" method="POST">

      <div class="input">
        <label class="mandatory">Council Name: *</label>
        <input name="name" type="text" value="<?= ($_POST['name']) ? stripslashes($_POST['name']) : $row['name'];?>" class="midinp">
      </div>
      <div class="input">
        <label class="mandatory">Council Code: *</label>
        <input name="code" type="text" value="<?= ($_POST['code']) ? stripslashes($_POST['code']) : $row['code'];?>" maxlength=4 <? if ($_GET['mode'] == "edit") echo "readonly" ;?> class="sminp">
      </div>
      <div style="clear:both;"></div>

      <div class="input">
        <label>Office Phone:</label>
        <input name="phone1" MAXLENGTH="3" type="text" value="<?= ($_POST['phone1']) ? stripslashes($_POST['phone1']) : $ph1;?>" class="ssminp">
        <input name="phone2" MAXLENGTH="3" type="text"  value="<?= ($_POST['phone2']) ? stripslashes($_POST['phone2']) : $ph2;?>"  class="ssminp">
        <input name="phone3" MAXLENGTH="4" type="text"   value="<?= ($_POST['phone3']) ? stripslashes($_POST['phone3']) : $ph3;?>" class="smminp">
      </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="sminp">
      </div>
      <div class="input">
        <label>PO Box:</label>
        <input name="pobox" MAXLENGTH="7" 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 for="sameasabove">
			<input name="mailsame" onClick='showhide();' type="checkbox" value="1" <?=$mailsame;?>>Mailing address same as above.
			</label>  
			</div>
      <div style="clear:both;"></div>

	<?
	if ($mailsame)
		$style = "style=\"display: none;\"";
	?>
      <div class="input" <?=$style;?> id="addtwo">
        <label>Mailing Address:</label>
        <input name="address2" type="text"   value="<?= ($_POST['address2']) ? stripslashes($_POST['address2']) : $row['address2'];?>"  class="midinp">
      </div>
      <div class="input" <?=$style;?>  id="unittwo">
        <label>Unit#:</label>
        <input name="unit2" type="text" value="<?= ($_POST['unit2']) ? stripslashes($_POST['unit2']) : $row['unit2'];?>"  class="sminp">
      </div>
      <div class="input" <?=$style;?>   id="poboxtwo">
        <label>PO Box:</label>
        <input name="pobox2" type="text" value="<?= ($_POST['pobox2']) ? stripslashes($_POST['pobox2']) : $row['pobox2'];?>"  class="sminp">
      </div>
      <div style="clear:both;"></div>

      <div class="input" <?=$style;?>    id="citytwo">
        <label>City/Town:</label>
        <input name="city2" type="text" value="<?= ($_POST['city2']) ? stripslashes($_POST['city2']) : $row['city2'];?>"  >
      </div>
      <div class="input" <?=$style;?> id="provtwo">
        <label>Province:</label>
            <select name="prov2" class="typevolunteer" title="prov">

			<?
			echo  buildProvDropdown($prov2);
			?>
          </select>      
        
      </div>
      <div class="input" <?=$style;?> id="pcodetwo">
        <label>Postal Code:</label>
        <input name="postalcode2" type="text" value="<?= ($_POST['postalcode2']) ? stripslashes($_POST['postalcode2']) : $row['postalcode2'];?>"   class="sminp">
      </div>
      <div style="clear:both;"></div>
      
            
      <p class="clearfix">
      <input type="submit" class="btn lfr" value="Save &amp; Close">
      
      <?
      	if ($_GET['mode'] != "edit")
      		echo "<a href=\"council_list.php\" class=\"btn lfr\">Cancel</a></p>";
      	else
      	    echo 	"<a href=\"council_overview.php?cid=".$cid."\" class=\"btn lfr\">Cancel</a></p>";
      
      ?>
      
      <input type="hidden" name="cid" value="<?=$_GET['cid'];?>">
    </form>
  </div>
</div>
<!-- end of main -->
<aside>
</aside>
    <div style="clear:both;"></div>
<footer>
  <?php include('footer.php'); ?>
</footer>
</div>
<!-- end of wrap --> 



</body>
</html>