<?php
define("PAGE_USES_GMAPS",true);

require_once("includes/SQLConnect.php");
include_once("includes/functions.php");

//Like the register_confirm.php page, this page needs better security. Stop people from arbitrarily changing info on people.
$user_ID = @(int)$_GET['ID']; //Trusted variable - shouldn't be

if($user_ID>0){ //If they're landing on the page, get the districts that could be associated with their zip code
	
	//Get their ZIP from DB
	$result = mysql_query("SELECT * FROM Users WHERE ID = ".$user_ID." LIMIT 1");
	$reg_user_data = mysql_fetch_assoc($result);
	
	//Query GovTrack (alternatively, could be done through DB query since govtrack provides this database [it's in the assets folder])
	$result = curl_get_file_contents("http://www.govtrack.us/perl/district-lookup.cgi?zipcode=".zip_integer_to_string($reg_user_data['zip']));
	
	//The following will produce an array ($cdistrict) that has indices that correspond to the state and values that correspond to the district no#
	if(stripos($result,"congressional-district")===FALSE)
		header("Location:register_confirm.php?ID=".$reg_user_data['ID']."&error=It appears that that zip code is not valid.&city=".$reg_user_data['city']."&state=".$reg_user_data['state']);
	else{
	
		$xml = simplexml_load_string($result);
	
		if(stripos($result,"congressional-districts")===FALSE){
			$state = substr(trim($xml->state),0,2);
			$district = (int)$xml->district;
			
			$cdistricts[$state.$district] = $district;	//Not the neatest method of storing in the array, but it's informative
	
		}
		else
			foreach($xml->children() as $xml){
				$state = substr(trim($xml->state),0,2);
				$district = (int)$xml->district;
				
				$cdistricts[$state.$district] = $district;
			}
	}
}
else{ //If the person is setting their district

	$user_ID = (int)$_POST['user_ID'];
	$district_ID = (int)$_POST['district_ID'];
	
	$result = mysql_query("SELECT state,district FROM Districts WHERE ID = ".$district_ID);
	$district = mysql_fetch_assoc($result);
	$email = mysql_result(mysql_query("SELECT email FROM Users WHERE ID = ".$user_ID),0);
	
	if(mysql_query("UPDATE Users SET district_ID = ".$district_ID." WHERE ID = ".$user_ID." LIMIT 1"))
		header("Location:user_login.php?email=".$email."&success=You are now a part of ".$district['state'].$district['district']."! Login below to meet up with other people in your district."); //Success!
	else
		$error = "An error occured while updating your congressional district. Please try again. If the problem persists, please email the administrator (see the contact page).";

}

include_once("includes/header.php");


?>

<h2>Step 3 of 3: Select Your Congressional District</h2>

<?php
if(is_array($cdistricts)){ //Multiple results returned
?>
<p>

	These districts match your zip code. Select your district by clicking "<?=$reg_user_data['state'];?>XX Is My District". You can zoom in on the maps to look more carefully at the boundaries of the districts.

</p>
<?php
}
else{
?>

<p>

	Please ensure that the district below is your congressional district. Select your district by clicking "AAXX Is My District". If it isn't, you might have typed in your zip code incorrectly, in which case you should <a href="register_confirm.php?ID=<?=$user_ID;?>">click here to go back to the previous step</a>.

</p>

<?php

}
?>

<div id="search-results-districts">
<?php

if(isset($cdistricts))
	foreach($cdistricts as $state=>$district){
		$state = substr($state,0,2); //Necessary to get back the state from the index
		
		$result = mysql_query("SELECT * FROM Districts WHERE district=".$district." AND state='".$state."' LIMIT 1");
		$district = mysql_fetch_assoc($result);
		
		$result = mysql_query("SELECT * FROM Representatives WHERE district_ID = ".$district['ID']." AND is_current_flag = 1 AND is_senator_flag = 0");
		$representative = mysql_fetch_assoc($result);
		?>
		
		<div class="search-results-district">
		
			<div class='search-results-representative'>
				
				<form method="POST" action="register_district.php">
					<input type="hidden" class="hidden" name="user_ID" value="<?=$user_ID;?>">
					<input type="hidden" class="hidden" name="district_ID" value="<?=$district['ID'];?>">
					<input type="submit" class="submit" value="<?=$district['state'].$district['district'];?> Is My District">
				</form>

				<?php
				//Check to make sure the file exists on this server, otherwise, fetch copy from govtrack.use
				if((int)curl_get_header('http://'.$_SERVER['SERVER_NAME'].'/assets/photos/'.$representative['gov_track_ID']."-200px.jpeg")==200){
				?>
					<img src="assets/photos/<?=$representative['gov_track_ID'];?>-200px.jpeg" id="photo" width="200" height="244">
				<?php
				}
				else{
				?>
					<img src="http://www.govtrack.us/data/photos/<?=$representative['gov_track_ID'];?>-200px.jpeg" id="photo" width="200" height="244">		
				<?php
				}
				?>
				<h3>
					<?=$representative['first_name'].' '.$representative['last_name'];?> (<?=$representative['party_abbreviated'];?>)
				</h3>
			</div>
			
			<div class='search-results-map'>
				<?php
					if(!is_array($cdistricts)) //If there's only one map, use the default zoom level
						get_district_google_map($district['state'],$district['district'],"100%","355px");
					else //If there are multiple maps, zoom it in one closer to make it more clear
						get_district_google_map($district['state'],$district['district'],"100%","355px",8);
				?>
			</div>
			
			
		</div>
		<?php
	}
?>
</div>

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