<?php
    // Start the session
    session_start();
    
    //returns the name of the array's index
    function rowName($index, $name){
            return "ID".$index."_".$name;
    }
    
    
    //after the submit 
    if(isset($_POST['ID1_objective']) && isset($_POST['ID1_mesure']) && isset($_POST['ID1_result_comment']) && 
        isset($_POST['ID1_weighting']) && isset($_POST['ID1_note'])  && isset($_SESSION['idSession'])){
           
           $storage = array();
           $objective = array();
           
           //taking the current values
           $str = file_get_contents($_SESSION['idSession'].".json");
           $storage = json_decode($str, true);
           
           //index will increment until we reached all the objectives added by the user
           $count = 1;
           
           while($_POST[rowName($count,"objective")] != null){
               
               
               $objectiveStorage = array();
               
               $objectiveStorage['Objective'] = $_POST[rowName($count,"objective")];
               
               $objectiveStorage['Mesure'] = $_POST[rowName($count,"mesure")];
               
               $objectiveStorage['Result and Comment'] = $_POST[rowName($count,"result_comment")];
               
               $objectiveStorage['Weighting'] = $_POST[rowName($count,"weighting")];
               
               $objectiveStorage['Note'] = $_POST[rowName($count,"note")];
               
               $count++;
               
               array_push($objective,$objectiveStorage);
           }
           
           $storage['Objective'] =  $objective;
           
           //store elements in a json file
           $fp = fopen($_SESSION['idSession'].".json", 'w');
           fwrite($fp, json_encode($storage));
           fclose($fp);
           
           header('location: form_personal_contribution.php');
    }
    
?>

<!doctype html>
<html class="fixed">
	<head>
	<head>
		<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript" src="js/script_form_objective.js"></script>
	</head>
	</head>
	<body>
    	<section class="body-sign">
            
    		<div class="center-sign">
    			<div id="wrapper">
    				<form action="form_objective.php" method="post" id="sign-up_area" role="form">
    					<div id="entry1" class="clonedInput">
    						<h2 id="reference" name="reference" class="heading-reference">Entry #1</h2>
    						<fieldset>
    							<!-- Text input-->
    							<div class="form-group">
    								<label class="label_fn control-label" for="ID1_objective">Objectifs :</label>
    								<input id="ID1_objective" name="ID1_objective" type="text" placeholder="" class="form-control" >
    							</div>
    
    							<!-- Text input-->
    							<div class="form-group">
    								<label class="label_fn control-label" for="ID1_mesure">Mesure :</label>
    								<input id="ID1_mesure" name="ID1_mesure" type="text" placeholder="" class="form-control" >
    							</div>
    
    							<!-- Text input-->
    							<div class="form-group">
    								<label class="label_fn control-label" for="ID1_result_comment">Résultats et commentaires:</label>
    								<input id="ID1_result_comment" name="ID1_result_comment" type="text" placeholder="" class="form-control" >
    							</div>
    
    							<!-- Text input-->
    							<div class="form-group">
    								<label class="label_fn control-label" for="ID1_weighting">Pondération:</label>
    								<input id="ID1_weighting" name="ID1_weighting" type="text" placeholder="" class="form-control" >
    							</div>
    
    							<!-- Text input-->
    							<div class="form-group">
    								<label class="label_fn control-label" for="ID1_note">Note:</label>
    								<input id="ID1_note" name="ID1_note" type="text" placeholder="" class="form-control" >
    							</div>
    							<br>
    						</fieldset>
            			</div><!-- end #entry1 -->
						<!-- Button (Double) -->
						<p>
						    <button type="button" id="btnAdd" name="btnAdd" class="btn btn-info">add section</button>
							<button type="button" id="btnDel" name="btnDel" class="btn btn-danger">remove section above</button>
						</p>
						<br>
						<!-- Button -->
						<p>
							<button id="submit_button" name="submit_button" class="btn btn-primary">Submit</button>
						</p>
    				</div>
    		    </form>
            </div>
        </div>
    </section>
    </body>
</html>