<?php

$hasError = false;

if (isset($_POST['action']) && $_POST['action'] == "submit_form") {

	// get config
	require_once($_SERVER['DOCUMENT_ROOT'] . "/config/config.inc.php");

	// set path
	//ini_set("include_path", PEAR_INCLUDES_DIR);

	// get custom wrapper
	require_once("captcha.inc.php");

	// instatnitate the class
	//$cap = new Captcha_Manager();

	// session is not started.
	if (!isset($_SESSION)) {
		
		// start the session
		session_start();
	}

	if (PhpCaptcha::Validate($_POST['spam_test'])) {

		// SET THE INCLUDE PATH
		ini_set('include_path', INCLUDES_DIR);

		//grab, concatenate and escape the age group
		$age_group = mysql_escape_string(implode(",", $_POST['age_group']));

		// run through the data & escape
		foreach($_POST as $key => $value)
		{
			// escape the element
			if(!is_array($_POST[$key])){$_POST[$key] = mysql_escape_string($value);}
		}

		// get the mysql class
		require_once("mysql.class.php");
		
		// instantiate 
		$mysql = new MYSQL_Wrapper(DB_USERNAME, DB_PASSWORD, DB_SERVER, 'twest2');

		// if user doesn't want to subscribe to newsletter
		$receive_newsletter = 0;
		if (isset($_POST['receive_newsletter']))
			$receive_newsletter = 1;
	
		// construct the query
		$query = "
		insert into win_million 
		(title, first_name, last_name, partner, street_1, street_2, suburb, state, postcode, country, phone_home, phone_work, phone_mobile, email, marital_status, age_group, income, last_holiday, own_home, submitted_ts, referrer, ip_address, receive_newsletter) 
		values 
		(
			'{$_POST['title']}',
			'{$_POST['first_name']}',
			'{$_POST['last_name']}',
			'{$_POST['partner']}',
			'{$_POST['street_1']}',
			'{$_POST['street_2']}',
			'{$_POST['suburb']}',
			'{$_POST['state']}',
			'{$_POST['postcode']}',
			'{$_POST['country']}',
			'{$_POST['phone_home']}',
			'{$_POST['phone_work']}',
			'{$_POST['phone_mobile']}',
			'{$_POST['email']}',
			'{$_POST['marital_status']}',
			'{$age_group}',
			'{$_POST['income']}',
			'{$_POST['last_holiday']}',
			'{$_POST['own_home']}',
			NOW(),
			'{$_SERVER['HTTP_REFERER']}',
			'{$_SERVER['REMOTE_ADDR']}',
			{$receive_newsletter}
		)";

		//print $query;die;

		// run the query
		if ($mysql->insert($query)) {

			require_once("functions.php");

			send_email($_POST['email'], $_POST['first_name'], $_POST['receive_newsletter']);
			header("Location: thankyou.php");
		
		} else {
			// show success
			$outcome_msg = "<script>window.alert('There was an issue while submitting your entry, please try again.');</script>";
		}

	} else {
		$hasError = true;
	}
}

function check_field ($fieldname, $fieldvalue, $str) {
	
	if (isset($_POST[$fieldname])) {
		if (strpos($_POST[$fieldname], $fieldvalue) !== false)
			return $str;
	}
}

function check_checkbox ($fieldname, $fieldvalue) {

	if (isset($_POST[$fieldname])) {
		if (in_array($fieldvalue, $_POST[$fieldname]))
			return "checked";
	}
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
	<title>Win a Million With Trendwest</title>
	<meta name="Keywords" content="">
	<meta name="Description" content="">
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	<link href="style.css" type="text/css" rel="stylesheet">
	<script SRC="/scripts/utilities.js" LANGUAGE="Javascript1.2" type="text/javascript"></script>
	<script SRC="validate.js" LANGUAGE="Javascript1.2" type="text/javascript"></script>
	<script language="JavaScript" type="text/javascript">
	<!--
		// a function to set the tooltip params and return the string
		function show_ttip_string(title, content, obj) {

				  obj.T_BORDERCOLOR='#6D99B6'; 
				  obj.T_SHADOWCOLOR='#cdcdcd';
				  obj.T_SHADOWWIDTH=3; 
				  obj.T_LEFT = true;
				  obj.T_BGCOLOR='#ffffff';
				  obj.T_DELAY=10;
				  obj.T_WIDTH=300;
				  obj.T_PADDING = 5;

				  // build the content
				  return content;
		}
	//-->
	</script>
</head>

<body style="margin:0px;">
	<form method="post" action="index.php" onSubmit="return formcheck()">
		<input type="hidden" name="action" />
		<div id="banner">
			<img src="competition_banner.jpg" width="269" height="842" alt="Dare to Dream Trendwest - Grand prize $50,000 cash, Second prize $15,000 holiday, Third prize $8,500 Home Entertainment System"/>
			<p>Prize values are in Australian dollars.</p>
			<p>NSW Permit No. TPL06/06280, ACT TP06/02345, VIC Permit No. 06/1822 & SA Lic No. T06/2365</p>
		</div>
		<div id="content">
			<p>All questions must be answered. No purchase necessary to enter contest or redeem prizes.</p>			

			<p>This contest is open to residents of Australia, New Zealand, Fiji, USA &amp; Canada over the age of 18 years. <strong>There is a limit of one entry per household</strong>.			
			
				<div class="section">
					<div class="row">
						<div class="label">Title:</div>
						<div class="field">
							<select name="title">
								<option <?php print check_field("title", "Mr", "selected") ?>>Mr</option>
								<option <?php print check_field("title", "Mrs", "selected") ?>>Mrs</option>
								<option <?php print check_field("title", "Ms", "selected") ?>>Ms</option>
								<option <?php print check_field("title", "Miss", "selected") ?>>Miss</option>
								<option <?php print check_field("title", "Dr", "selected") ?>>Dr</option>
							</select>
						</div>
					</div>
					
					<div class="row">
						<div class="label">First name:</div>
						<div class="field"><input name="first_name" type="text"style="width:200px;" value="<?php print @$_POST['first_name']; ?>" /></div>
					</div>

					<div class="row">
						<div class="label">Last name:</div>
						<div class="field"><input name="last_name" type="text"style="width:200px;" value="<?php print @$_POST['last_name']; ?>" /></div>
					</div>

					<div class="row">
						<div class="label">Partner:</div>
						<div class="field"><input name="partner" type="text"style="width:200px;" value="<?php print @$_POST['partner']; ?>" /></div>
					</div>
				</div>

				<div class="section">
					<div class="row">
						<div class="label" style="height:50px;">Address:</div>
						<div class="field">
							<input name="street_1" type="text"style="width:200px;" value="<?php print @$_POST['street_1']; ?>" /><br/>
							<input name="street_2" type="text"style="width:200px;" value="<?php print @$_POST['street_2']; ?>" />	
						</div>
					</div>

					<div class="row">
						<div class="label">Suburb/city:</div>
						<div class="field"><input name="suburb" type="text"style="width:200px;" value="<?php print @$_POST['suburb']; ?>" /></div>
					</div>

					<div class="row">
						<div class="label">State:</div>
						<div class="field"><input type="text" name="state">
							<!--<select name="state">							
								<option <?php print check_field("state", "ACT", "selected") ?>>ACT</option>
								<option <?php print check_field("state", "NSW", "selected") ?>>NSW</option>
								<option <?php print check_field("state", "NT", "selected") ?>>NT</option>
								<option <?php print check_field("state", "QLD", "selected") ?>>QLD</option>
								<option <?php print check_field("state", "TAS", "selected") ?>>TAS</option>
								<option <?php print check_field("state", "WA", "selected") ?>>WA</option>							
							</select>-->				
						</div>
					</div>
					
					<div class="row">
						<div class="label">Postcode:</div>
						<div class="field"><input name="postcode" type="text" size="6" value="<?php print @$_POST['postcode']; ?>" /></div>
					</div>

					<div class="row">
						<div class="label">Country</div>
						<div class="field">
							<!-- country codes from ISO 3166 -->
							<select name='country'>
							<option value='AUS'>Australia</option>
							<option value='NZL'>New Zealand</option>
							<option value='FJI'>Fiji</option>
							<option value='USA'>USA</option>
							<option value='CAN'>Canada</option>
							</select>
						</div>
					</div>

					<div class="row">
						<div class="label">Phone (H):</div>
						<div class="field"><input name="phone_home" type="text"style="width:200px;" value="<?php print @$_POST['phone_home']; ?>" /></div>
					</div>

					<div class="row">
						<div class="label">Phone (W):</div>
						<div class="field"><input name="phone_work" type="text"style="width:200px;" value="<?php print @$_POST['phone_work']; ?>" /></div>
					</div>

					<div class="row">
						<div class="label">Mobile phone:</div>
						<div class="field"><input name="phone_mobile" type="text"style="width:200px;" value="<?php print @$_POST['phone_mobile']; ?>" /></div>
					</div>

					<div class="row">
						<div class="label">Email address:</div>
						<div class="field"><input name="email" type="text"style="width:200px;" value="<?php print @$_POST['email']; ?>" /></div>
					</div>
				</div>

				<div class="section">
					<div class="row">
						<div class="label">I am currently:</div>
						<div class="field">
							<input type="radio" name="marital_status" value="single" <?php print check_field("marital_status", "single", "checked") ?> />Single 
							<input type="radio" name="marital_status" value="married" <?php print check_field("marital_status", "married", "checked") ?> />Married 
							<input type="radio" name="marital_status" value="other" <?php print check_field("marital_status", "other", "checked") ?> />Other
						</div>
					</div><br />

					<div class="row">
						<div class="label">
							My/my partner's age group(s):<br/>
							<span class="explanation">(two categories if necessary)</span>
						</div>
						<div class="field">
							<input type="checkbox" id="age_group[]" name="age_group[]" value="18to24" <?php print check_checkbox("age_group", "18to24") ?> />18-24 
							<input type="checkbox" id="age_group[]" name="age_group[]" value="25to72" <?php print check_checkbox("age_group", "25to72") ?> />25-72 
							<input type="checkbox" id="age_group[]" name="age_group[]" value="73plus" <?php print check_checkbox("age_group", "73plus") ?> />73+
						</div>
					</div><br />

					<div class="row">					
						<div class="label" style="height:120px;">My and my partner's combined household income:<br />
						<span class="explanation">(check one category)</span></div>
						<div class="field">                
							<input type="radio" name="income" value="under40" <?php print check_field("income", "under40", "checked") ?> />Under $40,000<br/>
							<input type="radio" name="income" value="40to44" <?php print check_field("income", "40to44", "checked") ?> />$40,000 - $44,000<br/>
							<input type="radio" name="income" value="45to54" <?php print check_field("income", "45to54", "checked") ?> />$45,000 - $54,000<br/>
							<input type="radio" name="income" value="55over" <?php print check_field("income", "55over", "checked") ?> />$55,000 +
						</div>
					</div>

					<div class="row">					
						<div class="label" style="height:120px;">When did you last take a holiday?</div>
						<div class="field">                
							<input type="radio" name="last_holiday" value="under6" <?php print check_field("last_holiday", "under6", "checked") ?> />Less than 6 months<br/>
							<input type="radio" name="last_holiday" value="6to12" <?php print check_field("last_holiday", "6to12", "checked") ?> />6 - 12 months<br/>
							<input type="radio" name="last_holiday" value="overyear" <?php print check_field("last_holiday", "overyear", "checked") ?> />More than 1 year<br/>
							<input type="radio" name="last_holiday" value="toolong" <?php print check_field("last_holiday", "toolong", "checked") ?> />Too long to recall!
						</div>
					</div>

					<div class="row">
						<div class="label">Own, rent or mortgage home?</div>
						<div class="field">              
							<input type="radio" name="own_home" value="yes" <?php print check_field("own_home", "yes", "checked") ?> />Own 
							<input type="radio" name="own_home" value="no_rent" <?php print check_field("own_home", "no_rent", "checked") ?> />Rent
							<input type="radio" name="own_home" value="mortgage" <?php print check_field("own_home", "mortgage", "checked") ?> />Mortgage
						</div>
					</div>
				</div>

				<div class="section">
					<div class="row">
						<div style="float:left;"><input type="checkbox" id="receive_newsletter" name="receive_newsletter" value="true" <?php print check_field("receive_newsletter", "true", "checked") ?> checked /></div>
						<label for="receive_newsletter" style="float:left; margin-top:4px;">Yes, I would like to register for Trendwest Holiday's E-newsletter to receive great holiday offers all year round</label>
					</div>
				</div>

				<div class="section" style="margin-top:70px;">
					<div class="row">
						<div style="margin-bottom:10px;"><a name="spam_test"></a>
							<strong>Verification measure:</strong><br/>
							Please enter the text in the image below.
						</div>
						<div class="field" style="text-align:center;">
							<input name="spam_test" type="text" style="width:202px;" /><br/>
							<img src="/includes/captcha_img_eliot.php" name="spam_img" alt="Please enter the text in the field above." style="border:1px solid #CCC;"/>
						</div>
						<div>This question helps ensure that each competition entry is submitted by a person manually. Too difficult to read? Request a <a href="javascript:void(document.spam_img.src='/includes/captcha_img_eliot.php');">new image</a>.<!-- Please remember that there is a limit of one entry per household. --></div>
					</div>
				</div>

			<p style="text-align:center;"><input type='button' onclick="javascript:displayWindow('details_participation.html','details',480,390,'scrollbars=yes')" value='Details of participation'>&nbsp;<input type="submit" name="Submit" value="Submit entry" onclick="return validate_form(this.form);" /></p>
		</div>
	</form>
	<script SRC="/scripts/tooltip.js" LANGUAGE="Javascript1.2" type="text/javascript"></script>
	<?php if ($hasError) { ?>
		<script language="JavaScript">
		<!--
			alert('The verification measure text entered does not match the image displayed.');
			document.forms[0].spam_test.focus();
		//-->
		</script>
	<?php } ?>
</body>

</html>