<?PHP
session_start();
ob_start();

$newby=11;
require("includes/.htconfig.php");
require("includes/dbcon.php");
define("EONUMSHOW","12");

$activeQuery ="select * from enrollee where enrollee_id=".$enrollee_id;
$enrollee = mysql_getRow($activeQuery);
$address=mysql_getRow("select * from address where address_id=".$enrollee["address_id"]);
$baseLat=$address["lat"];
$baseLon=$address["lng"];

if( !empty($_GET) )
{
	$insert_EnrolleeEo_sql="insert into enrollee_eo set 
							enrollee_id =".$enrollee_id.", 
							eo_id=".$_REQUEST['eoid'];	
	if (mysql_query($insert_EnrolleeEo_sql))
	{	 
	  $update_enrollee_sql="update enrollee SET activFlag=1";
	  $update_enrollee_sql .= " WHERE enrollee_id=".$enrollee_id;	
	  if(mysql_query($update_enrollee_sql))
  	  header("location:getDocs.php");
    else
      echo reporterror("updating the active flag",$update_enrollee_sql);
  }
    else
      echo reporterror("inserting the enrollee-officer relationship",$insert_EnrolleeEo_sql);
}

ob_end_flush();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <link href="./style/design.css" rel="stylesheet" type="text/css" />

    <style type="text/css">
      <!--
.style1 {font-size: 0.8em}
-->
    </style>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Enrollment Officer Selection Page</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?=GOOGLEKEY?>"
      type="text/javascript">
    </script>
  </head>
  <body onunload="GUnload()">
    <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td align="center" valign="top">

          <?php
include_once("includes/header.php");
?>
          <?php
if( $enrollee['activFlag']== 0 )
{
?>
          <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0">
            <?PHP
   if (strlen($err_msg)>0)
   {
   ?>
            <tr>
              <td height="30" align="center" valign="top" colspan="8" class="ErrorMessage">
                <strong>
                  <?=$err_msg?>
                </strong>
              </td>
            </tr>
            <?PHP
   }
   ?>
            <tr>
              <td width="30%" valign="top">
                <div class="bodySubHeader">
                  <span class="bsht03">Step 5: Enrollment Officer Selection</span>
                </div>
              </td>
            </tr>
            <tr>
              <td width="30%" valign="top">
                Now, you need to choose an enrollment officer.
                The markers on the map below represent the <?=EONUMSHOW?> officers nearest you.
                Dragging your mouse over a marker will reveal the name and address.
                Clicking on a marker will choose that officer.
              </td>
            </tr>
          </table>
          <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0" class="DarkLightBox">
            <tr>
              <td width="30%" valign="top">
                <div id="map" style="width:800px;height:600px"></div>
              </td>
            </tr>
          </table>
  <script type="text/javascript">
  //<![CDATA[
  var map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(<?=$baseLat?>,<?=$baseLon?>), 13);
	
	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	var baseIcon = new GIcon(G_DEFAULT_ICON);
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
  //]]>

  index = 0;
  // Creates a marker whose info window displays the letter corresponding
  // to the given index.
  function createMarker(point, index1, adId,officer_id)
  {
    // Create a lettered icon for this point using our icon class
    var letter = String.fromCharCode("A".charCodeAt(0) + index1 );
    index = parseInt(index) + 1;
    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

    // Set up our GMarkerOptions object
    markerOptions = { icon:letteredIcon };
    var marker = new GMarker(point, markerOptions);

    GEvent.addListener(marker, "mouseover", function()
    {
      marker.openInfoWindowHtml(adId);
    });
    GEvent.addListener(marker,"mouseout",function()
    {
      marker.closeInfoWindow();
    });
    GEvent.addListener(marker,"click",function()
    {
      window.location="https://<?=$_SERVER["SERVER_NAME"]?>/<?=$script?>?eoid="+officer_id;
    });
    return marker;
    }
    <?PHP
		$eo_query="select eo.officer_id,eo.fisrt_name,eo.middle_name,eo.last_name,a.*,sqrt((a.lng=$baseLon)*(a.lng=$baseLon)+(a.lat-$baseLat)*(a.lat-$baseLat)) as distance from enrollment_officer eo,address a where a.address_id=eo.address_id order by distance asc limit ".EONUMSHOW;
    $eos=mysql_query($eo_query);
    if (! $eos) echo reporterror("finding officers",$eo_query);
    $j=0;
		while($eo = mysql_fetch_array($eos))
		{
			$officer_id=$eo['officer_id'];
			$name=$eo['fisrt_name']." ".$eo['middle_name']." ".$eo['last_name'];			
			$gender=$EnrolOfficerValue['gender'];
			 
			$street_number=$eo['street_number'];
			$street_line_01=$eo['street_line_01'];
			$street_line_02=$eo['street_line_02'];
			$street_line_03=$eo['street_line_03'];
			$zipCode=$eo['zipCode'];
			$lat=$eo['lat'];
			$lon=$eo['lng'];
			$building_numer=$eo['building_numer'];
			$building_name=$eo['building_name'];
			$apartment_numer=$eo['apartment_numer'];
			$room_numer=$eo['room_numer'];
			
			$country_name=mysql_getOne("select country_name from country where country_id=".$eo["country_id"]);
			$state_name=mysql_getOne("select state_name from state where state_id=".$eo["state_id"]);
      $city_name=mysql_getOne("select city_name from city where city_id=".$eo["city_id"]);

      $address=$street_line_01;
      if (strlen($street_line_02) > 0) $address.="<BR>$street_line_02</BR>";
      if (strlen($street_line_03) > 0) $address.="<BR>$street_line_03</BR>";
      $address.="<BR>$city_name, $state_name  $zipCode";
      $address.="<BR>$country_name</BR>";

    ?>
    var point = new GLatLng(<?=$lat?>,<?=$lon?>);
    var address="<B><?=$name?></B><BR><?=$address?>";
    map.addOverlay(createMarker(point,<?=$j?>, address,<?=$officer_id?>));
    <?PHP
      $j++;
    }     ##End of eo loop
    ?>
  </script>
<?php
}
else    ##activFlag == 0
{
	$msg="You are already activated";
?>
          <table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td height="30" align="center" valign="top" class="ErrorMessage">
                <strong>
                  <?=$msg?>
                </strong>
              </td>
            </tr>
          </table>
          <?php
}
?>
          <?php
include_once("includes/footer.php");
?>
        </td>
      </tr>
    </table>
  </body>
</html>
