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


$update_success=false;
$insert_success=false;

if (count($_POST) > 0) //form has been submitted by user
{
	
	//assemble phone numbers
	$homephone = $_POST['phone1'].$_POST['phone2'].$_POST['phone3'];
	$altphone = $_POST['cell1'].$_POST['cell2'].$_POST['cell3'];
	$workphone = $_POST['work1'].$_POST['work2'].$_POST['work3'];

	//prep the data regardless
	$firstname= secure_user_input($_POST['firstname']);
	$lastname= secure_user_input($_POST['lastname']);
	$address= secure_user_input($_POST['address']);
	$apt= secure_user_input($_POST['apt']);
	$pobox= secure_user_input($_POST['pobox']);
	$city= secure_user_input($_POST['city']);
	$prov= secure_user_input($_POST['prov']);
	$postalcode= secure_user_input($_POST['postalcode']);
	$email= secure_user_input($_POST['email']);
	$password= secure_user_input($_POST['password']);
	$council= secure_user_input($_POST['cid']);


	if (!$firstname || !$email || !$password || !$lastname)
	{
		$mssg= "*** REQUIRED FIELDS MISSING ***";
		
	}
	else
	{
		if ($_GET['mode'] == 'new' && !$_POST['coordid'])
		{
			$SQL = "INSERT INTO coordinator
			(`email`, 		
			`firstname`,		
			`lastname`,
			`password`,
			`council_id`,
			`active` ,
			`address` ,		
			`apt` ,		
			`pobox` 	,	
			`city` ,		
			`prov` 	,	
			`postalcode`, 		
			`homephone` ,		
			`altphone`, 		
			`workphone` 		
			)			
			 VALUES 
			('".$email."',
			 '".$firstname."',
			 '".$lastname."',
			 '".$password."',
			 '".$council."',
			 '1',
			 '".$address."',
			 '".$apt."',
			 '".$pobox."',
			 '".$city."',
			 '".$prov."',
			 '".$postalcode."',
			 '".$homephone."',
			 '".$altphone."',
			 '".$workphone."'
			)";						
		
			if (!llnb_query($SQL))
				echo "ERROR on INSERT SQL";
			//echo $SQL;
			$insert_success=true;
			
			$currentid = mysql_insert_id();
			$currentdCOORDID = $currentid; //make a copy for bleow
			//add a status of active to status history
			$SQL="INSERT INTO status
			(user_id, thestatus,usertype,thedate )
						VALUES 
			('".$currentid."', 1, '4', CURDATE() )";
			$results = llnb_query($SQL);
			
			header("Location: ./council_coordinator_list.php?cid=".$_POST['cid']);
		
		}
		else if ($_POST['coordid'] && $_GET['mode'] == 'edit' )
		{
			$SQL = "UPDATE coordinator SET
				`email`= 		         '".$email."', 
				`firstname`=		    '".$firstname."',
				`lastname`=		    '".$lastname."',
				`password`=		    '".$password."',
				`active`=		    '1',
				`address`=		    '".$address."',
				`apt`=			    '".$apt."',
				`pobox`=		    '".$pobox."',
				`city`=			    '".$city."',
				`prov`=		    '".$prov."',
				`postalcode`= 		    '".$postalcode."',
				`homephone`=		    '".$homephone."',
				`altphone`= 		    '".$altphone."',
				`workphone`=		    '".$workphone."'
				WHERE id=".$_POST['coordid'];

			//echo $SQL;
			$results = llnb_query($SQL);
			$update_success = true;
			header("Location: ./coordinator_overview.php?coordid=".$_POST['coordid']);

		}
	}

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

	//assemble phone numbers
	$phnums = phonenumberToPieces($row['homephone']);
	$ph1 = $phnums[0]; $ph2 = $phnums[1]; $ph3 = $phnums[2]; 
	$phnums = phonenumberToPieces($row['altphone']);
	$c1 = $phnums[0]; $c2 = $phnums[1]; $c3 = $phnums[2]; 
	$phnums = phonenumberToPieces($row['workphone']);
	$w1 = $phnums[0]; $w2 = $phnums[1]; $w3 = $phnums[2]; 
		

	//prep the data regardless
	$firstname= secure_user_input($row['firstname']);
	$lastname= secure_user_input($row['lastname']);
	$address= secure_user_input($row['address']);
	$apt= secure_user_input($row['apt']);
	$pobox= secure_user_input($row['pobox']);
	$city= secure_user_input($row['city']);
	$prov= secure_user_input($row['prov']);
	$postalcode= secure_user_input($row['postalcode']);
	$email= secure_user_input($row['email']);
	$password= secure_user_input($row['password']);

}
else //default values
{

	//assemble phone numbers
	$ph1= "506";
	$c1 = "506";
	$w1 = "506";
}
?>


<!doctype html>
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->

<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>


</head>
<body onLoad="document.forms.coordinatorpersonalinfo.firstname.focus()">
<div id="wrap"> 
  
  <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>
    
    <?
		//coords can ONLY see their council
		if ($_SESSION['ulevel'] == $_COORD || $_SESSION['ulevel'] == $_EXECDIR)
		{	$force_council = "?cid=".$_GET['cid'];
			
			if ($_SESSION['ulevel'] == $_EXECDIR)
				$subpage="";
			else
				$subpage="council_";
	
		}
	     echo "<a href=\"".$subpage."coordinator_list.php".$force_council."\" class=\"btn lfl\">Coordinator List</a>";
	    
	    
    ?>
    
    <?
    
    if ($_GET['mode'] != "new")
    {
    	 echo "<a href=\"coordinator_overview.php?coordid=".$_GET['coordid']."\" class=\"btn lfl\">Coordinator Overview</a></nav>";
    }
    ?>    
    </nav>
  </header>
  <div role="main" id="main" class="clearfix">
    <div id="location">
          <?
	     
	     if ($_GET['mode'] != "new")
	     {
	 		echo "
	 		    <div id=\"location\">
	 			 <H2><span>Coordinator:</span> ".$row['firstname']." ".$row['lastname']."</H2>
	 		    </div>";
	     }
    ?>
    </div>
    <!-- end of location -->
    <div>
    <H2>Coordinator Information</H2>
 



    <div style="color: red;"><?=$mssg;?></div>
    
    
    <form class="midform" name="coordinatorpersonalinfo" action="coordinator_personalinfo.php?mode=<?=$_GET['mode']; ?>&cid=<?= $_GET['cid'];?>" method="POST">
    

    <div style="clear:both;"></div>
      <div class="input">
        <label class="mandatory">First Name:</label>
        <input name="firstname" type="text" value="<?= ($_POST['firstname']) ? stripslashes($_POST['firstname']) : $row['firstname'];?>" >
      </div>
      
      <div class="input">
        <label class="mandatory">Last Name:</label>
        <input name="lastname" type="text" value="<?= ($_POST['lastname']) ? stripslashes($_POST['lastname']) : $row['lastname'];?>" >
      </div>
      
      <div style="clear:both;"></div>
      <div class="input">
        <label class="mandatory">Email Address: *</label>
        <input name="email" type="text" value="<?= ($_POST['email']) ? stripslashes($_POST['email']) : $row['email'];?>" class="midinp">
      </div>
       <div class="input">
	         <label class="mandatory">Password: *</label>
        <input name="password" type="password" value="<?= ($_POST['password']) ? stripslashes($_POST['password']) : $row['password'];?>" >
      </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>Apt.#:</label>
        <input name="apt" type="text" value="<?= ($_POST['apt']) ? stripslashes($_POST['apt']) : $row['apt'];?>" 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>Home Phone:</label>
        <input name="phone1" maxlength="3" type="text" value="<?= ($_POST['phone1']) ? stripslashes($_POST['phone1']) : $ph1;?>" class="ssminp" onkeyup="checkLen(this,this.value)">
        <input name="phone2" maxlength="3" type="text" value="<?= ($_POST['phone2']) ? stripslashes($_POST['phone2']) : $ph2;?>" class="ssminp" onkeyup="checkLen(this,this.value)">
        <input name="phone3" maxlength="4" type="text" value="<?= ($_POST['phone3']) ? stripslashes($_POST['phone3']) : $ph3;?>" class="sminp" onkeyup="checkLen(this,this.value)">
      </div>
       
      
      <div style="clear:both;"></div>

      <div class="input">
        <label>Alternate Phone:</label>
        <input name="cell1" maxlength="3" type="text" value="<?= ($_POST['cell1']) ? stripslashes($_POST['cell1']) : $c1;?>" class="ssminp" onkeyup="checkLen(this,this.value)">
        <input name="cell2" maxlength="3" type="text" value="<?= ($_POST['cell2']) ? stripslashes($_POST['cell2']) : $c2;?>" class="ssminp" onkeyup="checkLen(this,this.value)">
        <input name="cell3" maxlength="4" type="text" value="<?= ($_POST['cell3']) ? stripslashes($_POST['cell3']) : $c3;?>" class="sminp" onkeyup="checkLen(this,this.value)">
      </div>
      <div style="clear:both;"></div>

      <div class="input">
        <label>Work Phone:</label>
        <input name="work1" MAXLENGTH="3" type="text" value="<?= ($_POST['work1']) ? stripslashes($_POST['work1']) : $w1;?>" class="ssminp" onkeyup="checkLen(this,this.value)">
        <input name="work2" MAXLENGTH="3" type="text" value="<?= ($_POST['work2']) ? stripslashes($_POST['work2']) : $w2;?>" class="ssminp" onkeyup="checkLen(this,this.value)">
        <input name="work3" MAXLENGTH="4" type="text" value="<?= ($_POST['work3']) ? stripslashes($_POST['work3']) : $w3;?>" class="sminp" onkeyup="checkLen(this,this.value)">
      </div>

		   <?
		   		//coords can ONLY see their council
		   		if ($_SESSION['ulevel'] == $_COORD || $_SESSION['ulevel'] == $_EXECDIR)
		   		{	$force_council = "?cid=".$_GET['cid'];
		   			$subpage="council_";
		   		}
		   	
		   		if (($_GET['mode'] != "new") )
		   	         	 $cancel= "<a href=\"coordinator_overview.php?cid=".$_GET['cid']."&coordid=".$_GET['coordid']."\" class=\"btn lfr\">Cancel</a>";
		   	     else 
		   	     {
		   	     	if ( $_SESSION['ulevel'] == $_EXECDIR)
		   	     		$cancel= "<a href=\"coordinator_list.php".$force_council."\" class=\"btn lfr\">Cancel</a>";
		   	     	else
		   	     		$cancel= "<a href=\"".$subpage."coordinator_list.php".$force_council."\" class=\"btn lfr\">Cancel</a>";
		   	     }
	        ?>
            
	      <div style="clear:both;"></div>
  
    <p class="clearfix"><input type="submit" class="btn lfr" value="Save &amp; Close">
      <?=$cancel;?></p>
      
      <input type="hidden" name="cid" value="<?=$_GET['cid']; ?>">
      <input type="hidden" name="coordid" value="<?=$_GET['coordid']; ?>">

 
 </form>
  </div>
</div>
<!-- end of main -->
    <div style="clear:both;"></div>
<footer>
  <?php include('footer.php'); ?>
</footer>
</div>
<!-- end of wrap --> 


</body>
</html>