<?php
error_reporting(0);
function zip_string_to_integer($zip){
	return (float)str_replace("-","",trim($zip));
}
function zip_integer_to_string($zip){
	if(strlen($zip)<=5)
		return str_pad($zip,5,'0',STR_PAD_LEFT);
	else
		return substr($zip,0,5)."-".substr($zip,5);
}

function phone_string_to_integer($phone){
	$stripped = str_replace(array("-",")","("," ",'+','.'),"",trim($phone));
	return (float)$stripped;
}
function phone_integer_to_string($phone){
	if(strlen($phone)>7)
		return "(".substr($phone,0,3).") ".substr($phone,3,3)."-".substr($phone,6);
}

function getShortDate($unix_timestamp){
	if(date("j F Y") == date("j F Y",$unix_timestamp)) //If the date is today
		return "Today";
	elseif(date("j F Y",time()+60*60*24) == date("j F Y",$unix_timestamp)) //If the date is tomorrow 
		return "Tomorrow";
	else
		return FALSE;
}
function format_date($unix_timestamp){
	if(date("Y") != date("Y",$unix_timestamp)) //If this date is some other year than this one
		return date("F jS, Y",$unix_timestamp);
	else if(getShortDate($unix_timestamp) != FALSE)
		return getShortDate($unix_timestamp);
	else
		return date("F jS",$unix_timestamp);
}
function format_datetime($unix_timestamp){
	if(date("Y") != date("Y",$unix_timestamp)) //If this date is some other year than this one
		return date("F jS, Y",$unix_timestamp);
	elseif(getShortDate($unix_timestamp) != FALSE)
		return getShortDate($unix_timestamp)." at ".date("g:i a",$unix_timestamp);
	else
		return date("F jS, g:i a",$unix_timestamp);
}


function curl_get_file_contents($URL,$referrer=""){
	$c = curl_init();
	curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($c, CURLOPT_URL, $URL); 
	curl_setopt($c,CURLOPT_FOLLOWLOCATION,true);
	if($referrer!="")
		curl_setopt($c,CURLOPT_REFERER,$referrer);
	else
		curl_setopt($c,CURLOPT_AUTOREFERER,true);
	
	//The rest of this is sneakiness so that websites don't know that this is a script -> Necessary for getting news
	curl_setopt($c,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16");
	curl_setopt($c,CURLOPT_ENCODING,"gzip,deflate");

	$str  = array(
	"Accept-Language: en-us,en;q=0.5",
	"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
	"Keep-Alive: 300",
	"Connection: keep-alive",
	"Accept: text/xml,application/xml,application/xhtml+xml, text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
	"Cache-Control: max-age=0",
	"Pragma: "
	);
       
	curl_setopt($c,CURLOPT_HTTPHEADER, $str);
	curl_setopt($c,CURLOPT_COOKIESESSION,true);
	curl_setopt($c,CURLOPT_FORBID_REUSE,true);
	curl_setopt($c,CURLOPT_FRESH_CONNECT,true);
	curl_setopt($c,CURLOPT_DNS_CACHE_TIMEOUT,1);
	curl_setopt($c, CURLOPT_COOKIEJAR, 'cookie.txt');
	curl_setopt($c, CURLOPT_COOKIEFILE, 'cookie.txt');
	
	$contents = curl_exec($c);
	curl_close($c);

	if ($contents) 
		return $contents;
	return FALSE;
}

function curl_get_header($URL){
	$c = curl_init();
	curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($c, CURLOPT_URL, $URL); 
	
	//The rest of this is sneakiness so that websites don't know that this is a script -> Necessary for getting news
	curl_setopt($c,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16");
	curl_setopt($c,CURLOPT_ENCODING,"gzip,deflate");

	$str  = array(
	"Accept-Language: en-us,en;q=0.5",
	"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
	"Keep-Alive: 300",
	"Connection: keep-alive",
	"Accept: text/xml,application/xml,application/xhtml+xml, text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
	"Cache-Control: max-age=0",
	"Pragma: "
	);
       
	curl_setopt($c,CURLOPT_HTTPHEADER, $str);
	curl_setopt($c,CURLOPT_COOKIESESSION,true);
	curl_setopt($c,CURLOPT_FORBID_REUSE,true);
	curl_setopt($c,CURLOPT_FRESH_CONNECT,true);
	curl_setopt($c,CURLOPT_DNS_CACHE_TIMEOUT,1);
	curl_setopt($c, CURLOPT_COOKIEJAR, 'cookie.txt');
	curl_setopt($c, CURLOPT_COOKIEFILE, 'cookie.txt');
	
	$contents = curl_exec($c);
	$headers = curl_getinfo($c);
	
	curl_close($c);

	return $headers['http_code'];
}

//Change this so it centers on the district instead of the state.
function get_district_google_map($state,$district = -1,$width,$height,$zoom_level = 7){
	?>
	<div id="googlemap<?=$state.abs($district);?>" style="width: <?=$width;?>; height: <?=$height;?>;"></div>
		<script type="text/javascript">		
		
			if (GBrowserIsCompatible()){
				var WMS_URL = 'http://govtrack.us/perl/wms-cd.cgi?';
				
				<?php
				if((int)$district>=0){
					?>
					
					var G_MAP_LAYER_FILLED = createWMSTileLayer(WMS_URL, "cd-filled,district=<?=$state.$district;?>", null, "image/gif", null, null, null, .25);
					var G_MAP_LAYER_OUTLINES = createWMSTileLayer(WMS_URL, "cd-outline,district=<?=$state.$district;?>", null, "image/gif", null, null, null, .66, "Overlay Data Courtesy of GovTrack.us");
					
					<?php
				}
				else{
					?>
					
					var G_MAP_LAYER_FILLED = createWMSTileLayer(WMS_URL, "cd-filled,state=<?=$state;?>", null, "image/gif", null, null, null, .25);
					var G_MAP_LAYER_OUTLINES = createWMSTileLayer(WMS_URL, "cd-outline,state=<?=$state;?>", null, "image/gif", null, null, null, .66, "Overlay Data Courtesy of GovTrack.us");
					
					<?php
				}
				?>
				
				var G_MAP_OVERLAY = createWMSOverlayMapType([G_NORMAL_MAP.getTileLayers()[0], G_MAP_LAYER_FILLED, G_MAP_LAYER_OUTLINES], "Overlay<?=$state.abs($district);?>");
			
				var map = new GMap2(document.getElementById("googlemap<?=$state.abs($district);?>"));
				map.enableContinuousZoom()
				map.removeMapType(G_SATELLITE_MAP);
				map.addMapType(G_MAP_OVERLAY);
				map.addControl(new GLargeMapControl());
				map.addControl(new GScaleControl());
				map.setMapType(G_MAP_OVERLAY);
				
				<?php
				if((int)$district>=0){
				?>
					map.setCenter(new GLatLng(<?=get_district_latlng($state,$district);?>),<?=$zoom_level;?>)
				<?php
				}
				else{
				?>

					
					geocoder = new GClientGeocoder();
					
					geocoder.getLatLng("<?=$state;?>,USA",function(point){
								map.setCenter(point,<?=$zoom_level;?>);
							}
						);
				<?php
				
				}
				?>
				
			}
		</script>
	<?
}


function get_district_latlng($state,$district){
	$state = substr($state,0,2);
	$district = (int)$district;

	//Contains an array: $d['AZ3']='33.8437945,-112.0921315'; where the index is [state][district]
	include("functions_districtlatlng.php");
	
	return $d[$state.$district];
}


/*
	//Testing functions:

	print "<h2>Phone Test</h2>";
	
	$phone[] = '+14165157748';
	$phone[] = '202.222.3434';
	$phone[] = '(999) 888-3434';
	$phone[] = '(454) 345.4355';
	$phone[] = '444-555-6666';
	$phone[] = '455-4554';
	
	foreach($phone as $number)
		print phone_string_to_integer($number)."<br>";
		
	print "<h2>Zip Test</h2>";
	$zip[] = "44444";
	$zip[] = "32423-4444";
	$zip[] = "455551234";
	$zip[] = "4545";
	
	foreach($zip as $zip_test){
		print "Converted to int: ".zip_string_to_integer($zip_test)."<br>";
		print "Converted to string: ".zip_integer_to_string(zip_string_to_integer($zip_test))."<br>";

	}
	
	print "<h2>Lat/Lng centering function</h2>";
	print get_district_latlng("NY","3");
*/
?>