<?Php
/*
* @author Istvan Franko
* @copyright 2014
*/

session_start();

if (!isset($_SESSION['cart'])) 
{
  $_SESSION['cart']=array(); 
}

if (!isset($_SESSION['loggedin'])) 
{
  $_SESSION['loggedin'] = '0'; 
}
?>

<html>

<?php include 'head.php'; ?>

<div id="aside">
	<!--Link for navigation-->
	<a href="product-list.php"><p style="text-align:center">Back to list</a></p>	

	<fieldset style="width:85%"><legend>HELP</legend>
	<p>If any questions or enquiries come to mind, then here these can be asked here.</p> 
	<p>We will try to answer in the shortest time possible to the sender's e-mail address.</p>
	</fieldset><br>
</div>

<div id="content">

<?php


 // define variables and set to empty values
$nameErr = $emailErr = $subjectErr = "";
$name = $subject = $comments = $email = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	$validations = 0; //reference variable for validations


   if (isset($Comments))
	{
	$comments = test_input($_POST["comments"]);
	}

   
  if (empty($_POST["name"]))
    {$nameErr = "(Name is required)";
	$validations++;
	}
  else
    {$name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name))
      {
      $nameErr = "Only letters and white space allowed";
	  $validations++;
      }
	}
	
  if (empty($_POST["subject"]))
    {$subjectErr = "(Subject is required)";
	$validations++;
	}
  else
    {$subject = test_input($_POST["subject"]);}
	
  if (empty($_POST["email"]))
    {$emailErr = "(Email is required)";
	$validations++;
	}
  else
    {$email = test_input($_POST["email"]);
	// check if e-mail address syntax is valid
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
      {
      $emailErr = "Invalid email format";
	  $validations++;
      }
	}
}

function test_input($data)
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

//if the required amounts was right the $validations must be 0
if (isset($validations))
{
	if ($validations == 0)
	{
		$con = mysql_connect("localhost","*****","*****");
		if (!$con)
		{
		die('Could not connect: ' . mysql_error());
		}

		mysql_select_db("university", $con);
		
		//replace single quotes from text field to insert to sql
		$email2 = str_replace("'", "''", $email);
		$subject2 = str_replace("'", "''", $subject);
		$comments2 = str_replace("'", "''", $comments);

		//construct a valid SQL statement that will be send to the database server.
		$sql="INSERT INTO feedbacks (FeedbackName, FeedbackSubject, FeedbackComment, FeedbackEmail, FeedbackState) 
		VALUES 
		('$name', '$subject2', '$comments2', '$email2', 'Waiting')";

		//display the SQL we will sent to server -  for debugging only
		//echo $sql;

		// run the SQL query on the server (i.e. send the SQL to the database server)

		if (!mysql_query($sql,$con))
		{
		die ('Error: ' . mysql_error());
		}

		echo "<h1>Feedback recorded</h1><br>";
		echo "<p>We have received your message. We try to answer as soon as possible. </p>";

		//close the connection to the database.
		mysql_close($con); 
	}
}

?>

<!--HTML form for feedbacks inputs-->
<br>
<fieldset style="width:60%"><legend>FEEDBACK</legend>

<p><span class="error">* required fields</span></p>

<form  method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Name: <span class="error">*</span><span class="error"> <?php echo $nameErr;?> </span><br>
<input type="text" name="name" size="50" value="<?php echo $name; ?>"><br><br>

Subject: <span class="error">*</span><span class="error"> <?php echo $subjectErr;?> </span><br>
<input type="text" name="subject" size="70" value="<?php echo $subject; ?>"><br><br>

Comments: <br><textarea name="comments" rows="10" cols="60"><?php echo $comments; ?></textarea><br><br>

E-mail: <span class="error">*</span><span class="error"> <?php echo $emailErr;?> </span><br>
<input type="text" name="email" size="50" value="<?php echo $email; ?>"><br><br>

<input type="submit" name="Submit" value="Send"><br>
</form></fieldset>


</div>

<div id="footer">
<p align="center">Copyright &#169; Istvan Franko, 2014</p>
</div>


</div>
</body>
</html>