<?php
require_once("includes/SQLConnect.php");

//This should be modified so that it only lets you change things when the confirmed_flag = 0 to begin with
//This is critical for security, otherwise people can modify arbitrary accounts

//SECURITY PROBLEM
$user_ID = (int)$_GET['ID'];

if($user_ID>0){ //Landing off of the email confirmation message
	if(mysql_affected_rows(mysql_query("UPDATE Users SET confirmed_flag = 1 WHERE ID = ".$user_ID." LIMIT 1"))>0)
		$success = "Welcome to the November5 District-by-District Organizing Tool";
}
else{
	include_once("includes/functions.php");
	$user_ID = (int)$_POST['user_ID'];
	
	$city = ucfirst($_POST['city']);
	$state = strtoupper(substr($_POST['state'],0,2));
	$zip = zip_string_to_integer($_POST['zip']);
	
	if(mysql_query("UPDATE Users SET city = '".mysql_real_escape_string($city)."',state = '".mysql_real_escape_string($state)."',zip = ".$zip." WHERE ID = ".$user_ID." LIMIT 1"))
		header("Location:register_district.php?ID=".$user_ID."&success=Please select your congressional district.");
	else
		$error = "There was an error updating your account details. Please check to make sure you correctly filled in the boxes below.";
}

include_once("includes/header.php");
?>


<form method="POST" action="register_confirm.php">
	<h2>Step 2 of 3: Please Tell Us A Bit About Your Location</h2>
	
	<p>
	
		Please enter your city, state and zip so that we can help you find your way to the right congressional district community.
	
	</p>

	<input type="hidden" class="hidden" name="user_ID" value="<?=$user_ID;?>">

	<label>Your City:</label>
	<input type="text" name="city" size="50" value="<?php if(isset($_GET['city'])){echo $_GET['city'];}?>">
	
	<label>State:</label>
	<select name="state" id="state">
		<option value='AL' class='AL'>Alabama</option>
		<option value='AK' class='AK'>Alaska</option>
		<option value='AZ' class='AZ'>Arizona</option>
		<option value='AR' class='AR'>Arkansas</option>
		<option value='CA' class='CA'>California</option>
		<option value='CO' class='CO'>Colorado</option>
		<option value='CT' class='CT'>Connecticut</option>
		<option value='DC' class='DC'>District of Columbia</option>
		<option value='DE' class='DE'>Delaware</option>
		<option value='FL' class='FL'>Florida</option>
		<option value='GA' class='GA'>Georgia</option>
		<option value='HI' class='HI'>Hawaii</option>
		<option value='ID' class='ID'>Idaho</option>
		<option value='IL' class='IL'>Illinois</option>
		<option value='IN' class='IN'>Indiana</option>
		<option value='IA' class='IA'>Iowa</option>
		<option value='KS' class='KS'>Kansas</option>
		<option value='KY' class='KY'>Kentucky</option>
		<option value='LA' class='LA'>Louisiana</option>
		<option value='ME' class='ME'>Maine</option>
		<option value='MD' class='MD'>Maryland</option>
		<option value='MA' class='MA'>Massachusetts</option>
		<option value='MI' class='MI'>Michigan</option>
		<option value='MN' class='MN'>Minnesota</option>
		<option value='MS' class='MS'>Mississippi</option>
		<option value='MO' class='MO'>Missouri</option>
		<option value='MT' class='MT'>Montana</option>
		<option value='NE' class='NE'>Nebraska</option>
		<option value='NV' class='NV'>Nevada</option>
		<option value='NH' class='NH'>New Hampshire</option>
		<option value='NJ' class='NJ'>New Jersey</option>
		<option value='NM' class='NM'>New Mexico</option>
		<option value='NY' class='NY'>New York</option>
		<option value='NC' class='NC'>North Carolina</option>
		<option value='ND' class='ND'>North Dakota</option>
		<option value='OH' class='OH'>Ohio</option>
		<option value='OK' class='OK'>Oklahoma</option>
		<option value='OR' class='OR'>Oregon</option>
		<option value='PA' class='PA'>Pennsylvania</option>
		<option value='RI' class='RI'>Rhode Island</option>
		<option value='SC' class='SC'>South Carolina</option>
		<option value='SD' class='SD'>South Dakota</option>
		<option value='TN' class='TN'>Tennessee</option>
		<option value='TX' class='TX'>Texas</option>
		<option value='UT' class='UT'>Utah</option>
		<option value='VT' class='VT'>Vermont</option>
		<option value='VA' class='VA'>Virginia</option>
		<option value='WA' class='WA'>Washington</option>
		<option value='WV' class='WV'>West Virginia</option>
		<option value='DC' class='DC'>Washington D.C.</option>
		<option value='WI' class='WI'>Wisconsin</option>
		<option value='WY' class='WY'>Wyoming</option>
	</select>	

		<?php
		if(isset($_GET['state'])){
			$state = substr($_GET['state'],0,2);
		?>		
			<script type="text/javascript">
				$("#state option.<?=$state;?>").attr({selected:'selected'});
			</script>
		<?php
		}
		?>

	<label>Zip (5 or 9 Digits):</label>
	<input type="text" name="zip" size="10" value="<?php if(isset($_GET['zip'])){echo $_GET['zip'];}?>">
	
	<input type="submit" class="submit" value="Find My District">
	
</form>



<?php
include_once("includes/footer.php");
?>