<?php
if (!isset($_SESSION['FirstName'])) {
	session_start();
}
    include ('includes/db1config.php');
    include ('../includes/mysql_connect.php');
	
	
function getCoordinates($address){
 
$address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern
 
$url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address";
 
$response = file_get_contents($url);
 
$json = json_decode($response,TRUE); //generate array object from the response from the web
 
return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);
 
 

 
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<?php include('title.php'); ?>
<meta name="description" content="<?php include('seodescr.php'); ?>" />
<meta name="keywords" content="<?php include('seokeywords.php'); ?>" />

<link href="css/main_style.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/print_style.css" rel="stylesheet" type="text/css" media="print" />

<link rel="stylesheet" type="text/css" href="css/cssverticalmenu.css" />


<!-- <script type="text/javascript" src="js/cssverticalmenu.js">
/***********************************************
* CSS Vertical List Menu- by JavaScript Kit (www.javascriptkit.com)
* Menu interface credits: http://www.dynamicdrive.com/style/csslibrary/item/glossy-vertical-menu/ 
* This notice must stay intact for usage
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and 100s more
***********************************************/ 
</script> -->


</head>


<body>

<div id="wrapper">

<!-- start header -->
<?php include('includes/header.inc.php'); ?>
<!-- end header-->


<!-- start page -->
<div id="page-container">
<div id="page-bg-top">


<!-- start content -->
<div id="content">
<div id="main-content">
   
		<div class="post">
		  <div class="entry">
          
          <br />
<h1>address test</h1>

<div id="single-admin-button4">
<ul>
<a href="index.php"><li>BACK TO HOME ADMIN</li></a>
</ul>
</div>

<?php 
if($_SESSION['adminkey']=="security"){

	//Point class, storage of lat/long-pairs
		class Point {
			public $lat;
			public $long;
			function Point($lat, $long) {
				$this->lat = $lat;
				$this->long = $long;
			}
		}
//$your_address = "3868 Edith LN Circle Pines MN 55014";
$your_address = "3868 Edith LN Ham Lake MN 55014";

$encoded_address = urlencode($your_address);
echo "encoded address: $encoded_address<br><br>";
/*API REQUEST:
https://api.smartystreets.com/street-address?auth-id=2c4314ad-a55e-4c9d-ae9f-9833c1add8b0&auth-token=6LPPJmvGB7Vi9Cp4ozdz&street=3868+Edith+LN&city=Circle+Pines&state=MN&zip=55014&candidates=10
*/

/* API RESULT:
[{"input_index":0,"candidate_index":0,"delivery_line_1":"3868 Edith Ln","last_line":"Circle Pines MN 55014-3405","delivery_point_barcode":"550143405689","components":{"primary_number":"3868","street_name":"Edith","street_suffix":"Ln","city_name":"Circle Pines","state_abbreviation":"MN","zipcode":"55014","plus4_code":"3405","delivery_point":"68","delivery_point_check_digit":"9"},"metadata":{"record_type":"S","zip_type":"Standard","county_fips":"27003","county_name":"Anoka","carrier_route":"C006","congressional_district":"06","rdi":"Residential","elot_sequence":"0463","elot_sort":"A","latitude":45.14498,"longitude":-93.17285,"precision":"Zip9","time_zone":"Central","utc_offset":-6,"dst":true},"analysis":{"dpv_match_code":"Y","dpv_footnotes":"AABB","dpv_cmra":"N","dpv_vacant":"N","active":"Y"}}]
*/
/*
curl -v "https://api.smartystreets.com/street-address?
auth-id=YOUR+AUTH-ID+HERE&auth-token=YOUR+AUTH-TOKEN+HERE&
street=1600+amphitheatre+pkwy&city=mountain+view&state=CA&candidates=10"

curl -v "https://api.smartystreets.com/street-address?
auth-id=2c4314ad-a55e-4c9d-ae9f-9833c1add8b0&auth-token=6LPPJmvGB7Vi9Cp4ozdz&
$encoded_address&candidates=10"
*/	
$your_location = getCoordinates($your_address);


//$your_location = str_replace (",", " ", $your_coords);
 echo "Your address lng & lat: $your_location";
 $your_coords=explode(',',$your_location);
 echo "<br><br>";
	#########################################################
	$qry = "select * from delivery_zones order by zone_number";
	$rt=mysql_query($qry);
	while($nt=mysql_fetch_array($rt)){
		//$pointstring is the list of polygon points
		$pointstring = $nt['coordinates'];
		//$pointstring = '0,0 100,0 100,100 0,100 0,0';	//for test
	}
	    //echo "pointstring: $pointstring<br>";
		$polygon_points=explode(' ',$pointstring); //$polygon_points is the long/lat pairs pulled from pointstring


	###########################################################
	
		//the Point in Polygon function
		function pointInPolygon($p, $polygon) {
			//if you operates with (hundred)thousands of points
			set_time_limit(60);
			$c = 0;
			$p1 = $polygon[0];
			$n = count($polygon);

			for ($i=1; $i<=$n; $i++) {
				$p2 = $polygon[$i % $n];
				if ($p->long > min($p1->long, $p2->long)
					&& $p->long <= max($p1->long, $p2->long)
					&& $p->lat <= max($p1->lat, $p2->lat)
					&& $p1->long != $p2->long) {
						$xinters = ($p->long - $p1->long) * ($p2->lat - $p1->lat) / ($p2->long - $p1->long) + $p1->lat;
						if ($p1->lat == $p2->lat || $p->lat <= $xinters) {
							$c++;
						}
				}
				$p1 = $p2;
			}
			// if the number of edges we passed through is even, then it's not in the poly.
			return $c%2!=0;
		}
		foreach($polygon_points as $key => $point) {
			echo "<br>point " . ($key) . " ($point): <br>";
			$lonlat=explode(',',$point);			
			//$longcoord[$key] = $point[0];
			//$latcoord[$key] = $point[1];
			$polygon[$key] = new Point($lonlat[1],$lonlat[0]);

		}
		
		/*	//to use test polygon coordinates
		$polygon = array(
		new Point(45.0,-94.0), 
		new Point(46,-94.0),
		new Point(46,-90.0),
		new Point(45.0,-90.0)
		);
		*/
		
		 //print_r($pointstring);
		 
		 //echo "polygon: "; print_r($polygon);
		 
		function test($lat, $long) {
			global $polygon;		
			$ll=$lat.','.$long;
			echo (pointInPolygon(new Point($lat,$long), $polygon)) ? $ll .' is inside polygon<br>' : $ll.' is outside<br>';
		}
echo "<br><br>";
		//test(2, 2);
		//test(1, 1);
		//test(1.5333, 2.3434);
		//test(400, -100);
		//test(1.01, 1.01);
		test($your_coords[0],$your_coords[1]); echo $ll;
	
	//end test 2
	
} else {
	echo '<font color="red"><div align="center">You are not authorized to view this page. <br>Please <a href="index.php"><font color="blue">login here</font></a>.</div></font>';
}
?>
            
            
            </div><!--end location buttons-->
            
            <div style="clear: both;"></div>
       
       </div><!-- end main-content-->

<br />


</div>


</div>

</div>
	<!-- end content -->
    <!-- start sidebar -->
<?php include('includes/admin-sidebar.inc.php'); ?>
<!--sidebar-->

	
<div style="clear: both;">&nbsp;</div>
<div id="page-bg-btm">
</div><!-- end page-bg-btm div -->
</div><!-- end page-bg-top div -->
</div><!-- end page-container -->



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

<!-- end page -->


<!--footer-->



</body>
</html>
