<?php 
$your_email ='info@wisconsinbiathlon.com, wibiathlon@gmail.com';// <<=== update to your email address

session_start();
$errors = '';
$name = '';
$visitor_email = '';
$user_message = '';

if(isset($_POST['submit']))
{
	
	$name = $_POST['name'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$age = $_POST['age'];
	$visitor_email = $_POST['email'];
	$biathlon = $_POST['biathlon'];
	$ski = $_POST['ski'];
	$shoot = $_POST['shoot'];
	$user_message = $_POST['message'];
	///------------Do Validations-------------
	if(empty($name)||empty($visitor_email))
	{
		$errors .= "\n Name and Email are required fields. ";	
	}
	if(IsInjected($visitor_email))
	{
		$errors .= "\n Bad email value!";
	}
	if(empty($_SESSION['6_letters_code'] ) ||
	  strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
	{
	//Note: the captcha code is compared case insensitively.
	//if you want case sensitive match, update the check above to
	// strcmp()
		$errors .= "\n The captcha code does not match!";
	}
	
	if(empty($errors))
	{
		//send the email
		$to = $your_email;
		$subject="MAILING LIST";
		$from = $your_email;
		$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
		
		$body = "".
		"Name: $name\n".
		"City: $city\n".
		"State: $state\n".
		"Age: $age\n".
		"Email: $visitor_email \n".
		"Biathlon Experience: $biathlon \n".
		"Ski Experience: $ski \n".
		"Shooting Experience: $shoot \n".
		"Message: \n ".
		"$user_message\n".
		"IP: $ip\n";	
		
		$headers = "From: $from \r\n";
		$headers .= "Reply-To: $visitor_email \r\n";
		
		mail($to, $subject, $body,$headers);
		
		header('Location: thank_you.php');
	}
}

// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<title>Wisconsin Biathlon Association - Wisconsin Biathlon Mailing List</title>
<meta name="description" content="The Wisconsin Biathlon Association is a non-profit organization dedicated to educating, promoting, increasing participation in biathlon, the Winter Olympic sport combining cross-country skiing and rifle marksmanship within the state of Wisconsin." />
<meta name="keywords" content="biathlon, wisconsin, illinois, iowa, skiing, ski, cross country ski, xc ski, shooting, olympic, winter, summer, rifle, marksmanship" />
<link href="_css/styles.css" type="text/css" rel="stylesheet" />

<script language="javascript" src="_js/gen_validatorv31.js" type="text/javascript"></script>	
</head>

<body>
<div id="wrapper">

<div id="content">

<div id="header">
</div>
<!--end header-->

<div id="main">

<div id="leftCol">
<?php include("_includes/navigation.html"); ?>
</div><!--end leftCol-->

<div id="mainCol"> 

<h1>Wisconsin Biathlon Mailing List</h1>
<br />

<p>To keep up to date on the latest club news &amp; events, join the Wisconsin Biathlon email list by completing the form below. For all other inquiries, please visit the <a href="contact.php">contact page</a>.</p>

<br />

<fieldset>
<legend>Email List Signup</legend>

<div id='contact_form_errorloc' class='err'>
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
</div>

<form method="POST" name="contact_form" 
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> 
<p>
<label for='name'>Name: </label><br>
<input type="text" name="name" value='<?php echo htmlentities($name) ?>'>
</p>
<p>
<label for='city'>City: </label><br>
<input type="text" name="city" value='<?php echo htmlentities($city) ?>'>
</p>
<p>
<label for='state'>State: </label><br>
<input type="text" name="state" value='<?php echo htmlentities($state) ?>'>
</p>
<p>
<label for='age'>Age: </label><br>
<input type="text" name="age" value='<?php echo htmlentities($age) ?>'>
</p>
<p>
<label for='email'>Email: </label><br>
<input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'>
</p>

<p>I have biathlon experience:
  <label>
  <input name="biathlonExperience" type="radio" value="yes" />
</label> 
Yes 

<label>
<input name="biathlonExperince" type="radio" value="no" />
</label> 
No
</p>

<p>I have xc skiing experience:
  <label>
  <input name="skiExperience" type="radio" value="yes" />
</label> 
Yes 

<label>
<input name="skiExperience" type="radio" value="no" />
</label> 
No
</p>

<p>I have shooting experience:
  <label>
  <input name="shootingExperience" type="radio" value="yes" />
</label> 
Yes 

<label>
<input name="shootingExperience" type="radio" value="no" />
</label> 
No
</p>

<p>
<label for='message'>Message:</label> <br>
<textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea>
</p>
<p>
<img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br /><br />
<label for='message'>Enter the code shown above. <a href='javascript: refreshCaptcha();'>Refresh</a> code.</label><br>
<input id="6_letters_code" name="6_letters_code" type="text"><br>
</p>
<input type="submit" value="Submit" name='submit'>
</form>
 
<br />

</div><!--end mainCol-->

</div><!--centerCol-->

</div>

<div id="footer"> 
<p>All contents &copy;2010 Wisconsin Biathlon Association. All rights reserved.</p>
</div><!--end footer-->

</div><!--end wrapper-->

<script language="javascript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator  = new Validator("contact_form");
//remove the following two lines if you like error message box popups
frmvalidator.EnableOnPageErrorDisplaySingleBox();
frmvalidator.EnableMsgsTogether();

frmvalidator.addValidation("name","req","Please provide your name"); 
frmvalidator.addValidation("email","req","Please provide your email"); 
frmvalidator.addValidation("email","email","Please enter a valid email address"); 
</script>
<script language='JavaScript' type='text/javascript'>
function refreshCaptcha()
{
	var img = document.images['captchaimg'];
	img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>
</body>
</html>