#!/usr/bin/perl -wT

use CGI qw(:standard);
#use CGI::Carp qw(fatalsToBrowser set_message);

$cgi = new CGI;
print $cgi->header();

print $cgi->start_html(
	-head => [	meta({ -http_equiv => 'pragma',  -content => 'no-cache' }),
	            meta({ -http_equiv => 'expires', -content => '-1' }) ],
	-title => 'Cartographers Guild Member Locations', 
	-meta => { 'keywords' => 'Gartographers,Guild,Member,Locations', 'description' => 'Cartographers Guild member locations' }
	 );

my $world_width = 1200;
my $world_height = 480;
my $world_lat_top = 77.5;
my $world_lat_bot = -56.5;

my $in_lat = $cgi->param("lat");
my $in_lng = $cgi->param("lng");
my $in_usr = $cgi->param("usr");
my $in_usa = $cgi->param("usa");

$in_lat =~ /([0-9\.\-]+)/;
my $lat = $1;

$in_lng =~ /([0-9\.\-]+)/;
my $lng = $1;

$in_usr =~ /([a-zA-Z0-9_\s]+)/;
my $usr = $1;

$in_usa =~ /(.*)/;
my $usa = $1;

if( length( $lat ) > 30 || length( $lng ) > 30 || length( $usr ) > 50 )
{
	$lat = "";
	$lng = "";
	$usr = "";
	$usa = "";
}
else
{
	if( $usa eq "Y" && $lng > 0.0 )
	{
		$lng *= -1.0;
	}
}

$loc_dir = "../MembersMap";
open LOCATIONS, "<$loc_dir/locations.txt";
my @entries = <LOCATIONS>;
close LOCATIONS;

if( length( $lat ) > 0 && length( $lng ) > 0 && length( $usr ) > 0 )
{
	push( @entries, "$lat,$lng,$usr" );
}

my $entry_count = 0;
my @valid_entries;
my $multiple_found = 0;

foreach $entry(@entries)
{
	chomp( $entry );
	if( $entry_count < 1000 && $entry =~ /([0-9\.-]+),([0-9\.-]+),([a-zA-Z0-9_\s]+)/ )
	{
		my $flat = $1;
		my $flng = $2;
		my $eusr = $3;
		
		if( $flat > $world_lat_top )
		{
			$flat = $world_lat_top;
		}
		
		if( $flat < $world_lat_bot )
		{
			$flat = $world_lat_bot;
		}
		
		if( $flng < -180.0 )
		{
			$flng = -180.0;
		}
		
		if( $flng > 180.0 )
		{
			$flng = 180.0;
		}

		if( length( $flat ) <= 30 && length( $flng ) <= 30 && length( $eusr ) <= 50 )
		{
			my $found = 0;
			foreach $valid(@valid_entries)
			{
				chomp( $valid );
				if( $valid =~ /([0-9\.-]+),([0-9\.-]+),([a-zA-Z0-9_\s]+)/ )
				{
					if( $3 eq $eusr )
					{
						$found = 1;
						$multiple_found = 1;
						last;
					}
				}
			}

			if( $found == 0 )
			{
				push( @valid_entries, "$flat,$flng,$eusr" );
				$entry_count++;
			}
		}
	}
}

if( $entry_count == 0 )
{
	push( @valid_entries, "51.453472,-2.590714,Redrobes" );
}

print "\n";
print '<div id="map" style="position: absolute; left: 0px; top: 0px;">'."\n";
print '<img src="http://www.cartographersguild.com/utilities/MembersMap/World.png">'."\n";
print '<a href="http://www.cartographersguild.com/utilities/cgi-bin/WhereInTheUSA.pl">'."\n";
print '<img style="position: absolute; left: 175px; top: 62px;" width="229" height="170" src="http://www.cartographersguild.com/utilities/MembersMap/Clear.png">'."\n";
print '</a>'."\n";
print '<a href="http://www.cartographersguild.com/utilities/cgi-bin/WhereInTheNEurope.pl">'."\n";
print '<img style="position: absolute; left: 563px; top: 54px;" width="102" height="73" src="http://www.cartographersguild.com/utilities/MembersMap/Clear.png">'."\n";
print '</a>'."\n";
print '</div>'."\n";

open LOCATIONS, ">$loc_dir/locations.txt";

foreach $entry(@valid_entries)
{
	chomp( $entry );
	
	if( $entry =~ /([0-9\.\-]+),([0-9\.\-]+),([a-zA-Z0-9_\s]+)/ )
	{
		print LOCATIONS "$entry\n";
	}
}

close LOCATIONS;


for( my $i = (@valid_entries - 1); $i >= 0; $i-- )
{
	$entry = $valid_entries[$i];
	chomp( $entry );
	
	if( $entry =~ /([0-9\.\-]+),([0-9\.\-]+),([a-zA-Z0-9_\s]+)/ )
	{
		my $flat = $1;
		my $flng = $2;
		my $tip = $3;
		
		my $px = int( ($flng + 180.0) / 360.0 * ($world_width - 1) );
		my $py = ($world_height - 1) - int( ($flat - $world_lat_bot) / ($world_lat_top - $world_lat_bot) * ($world_height - 1) );
	
		print '<div style="position: absolute; left: '.$px.'px; top: '.$py.'px; width: 5px; height: 5px; style="border-style: none;">'."\n";
		print '<a href="#" title="'.$3.'" style="border-style: none;">';
		print '<img src="http://www.cartographersguild.com/utilities/MembersMap/Marker.png" style="border-style: none;"';
		print '</a>';
		print '</div>'."\n";
	}
}

print '<div id="form" style="position: absolute; left: 0px; top: 500px;">'."\n";
print '<form action="http://www.cartographersguild.com/utilities/cgi-bin/WhereInTheWorld.pl" method="post" enctype="multipart/form-data">'."\n";
print 'Your CGuild Handle ?: <input type="text" name="usr"><br>'."\n";
print 'Your lattitude ?    : <input type="text" name="lat"><br>'."\n";
print 'Your longditude ?   : <input type="text" name="lng"><br>'."\n";
print 'Tick if you are in USA ?   : <input type="checkbox" name="usa" value="Y"><br>'."\n";
print '<br>'."\n";
print '<input type="submit" name="submit" value="Add Yourself to map"><br>'."\n";
print '</form>'."\n";
print '<br>'."\n";
print 'Lattitude is vertical, i.e. London is about 51.5<br>'."\n";
print 'For southern hemisphere use negative value.<br>'."\n";
print '<br>'."\n";
print 'Longditude is horizontal, i.e. Florida is about -80.0<br>'."\n";
print 'i.e. US and western area is negative, eastern countries positive.<br>'."\n";
print '<br>'."\n";

if( $multiple_found != 1 )
{
	print '<img src="http://www.cartographersguild.com/utilities/MembersMap/PosNeg.png">'."\n";
}

print '</div>'."\n";

if( $multiple_found == 1 )
{
	print '<div id="multiple" style="position: absolute; left: 0px; top: 800px;">'."\n";
	print '<font size="6">Multiple people with same handle found !<br>Try a different handle or mail web master to resolve stolen identity !</font><br>'."\n";
	print '</div>'."\n";
}

print '</body></html>'."\n";

1;

