function testinput(e)
{
	var keynum;
	
	if(window.event)
	{
		keynum = e.keyCode;
	}
	else
	{
		keynum = e.which;
	}
	
	if(keynum == 13)
	{
		searchLocations();
	}
}



    var map;
    var geocoder;
  //sets fancy markers
    var iconBlue = new GIcon(); 
    iconBlue.image = '/images/mm_20_blue.png';
    iconBlue.shadow = '/images/mm_20_shadow.png';
    iconBlue.iconSize = new GSize(12, 20);
    iconBlue.shadowSize = new GSize(22, 20);
    iconBlue.iconAnchor = new GPoint(6, 20);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

    var iconRed = new GIcon(); 
    iconRed.image = '/images/mm_20_red.png';
    iconRed.shadow = '/images/mm_20_shadow.png';
    iconRed.iconSize = new GSize(12, 20);
    iconRed.shadowSize = new GSize(22, 20);
    iconRed.iconAnchor = new GPoint(6, 20);
    iconRed.infoWindowAnchor = new GPoint(5, 1);

var iconYellow = new GIcon(); 
    iconYellow.image = '/images/mm_20_yellow.png';
    iconYellow.shadow = '/images/mm_20_shadow.png';
    iconYellow.iconSize = new GSize(12, 20);
    iconYellow.shadowSize = new GSize(22, 20);
    iconYellow.iconAnchor = new GPoint(6, 20);
    iconYellow.infoWindowAnchor = new GPoint(5, 1);
    
var iconGreen = new GIcon(); 
    iconGreen.image = '/images/mm_20_green.png';
    iconGreen.shadow = '/images/mm_20_shadow.png';
    iconGreen.iconSize = new GSize(12, 20);
    iconGreen.shadowSize = new GSize(22, 20);
    iconGreen.iconAnchor = new GPoint(6, 20);
    iconGreen.infoWindowAnchor = new GPoint(5, 1);

var iconPurple = new GIcon(); 
    iconPurple.image = '/images/mm_20_purple.png';
    iconPurple.shadow = '/images/mm_20_shadow.png';
    iconPurple.iconSize = new GSize(12, 20);
    iconPurple.shadowSize = new GSize(22, 20);
    iconPurple.iconAnchor = new GPoint(6, 20);
    iconPurple.infoWindowAnchor = new GPoint(5, 1);

var customIcons = [];
    customIcons["northwest"] = iconBlue;
    customIcons["midwest"] = iconRed;
    customIcons["southeast"] = iconGreen;
    customIcons["northeast"] = iconPurple;
    customIcons["southwest"] = iconYellow;
    
   function load() {
      if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.setCenter(new GLatLng(39, -96), 4);

        GDownloadUrl("/marker.xml", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {
            var name = markers[i].getAttribute("name");
            var address = markers[i].getAttribute("address");
            var type = markers[i].getAttribute("region");
            var org_id = markers[i].getAttribute("org_id");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            var programs = markers[i].childNodes;
            var marker = createMarker(point, name, address, type, org_id, programs);
            map.addOverlay(marker);
          }
        });
      }
    }
    
     function loadOrganization(lat, lng) {
      if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(lat, lng), 14);
                    var point = new GLatLng(lat, lng);
         var marker = new GMarker(point);
        map.addOverlay(marker);
          }
        }

   function searchLocations() {
     var address = document.getElementById('addressInput').value;
     geocoder.getLatLng(address, function(latlng) {
       if (!latlng) {
         alert(address + ' not found');
       } else {
         searchLocationsNear(latlng);
       }
     });
   }

   function searchLocationsNear(center) {
     var radius = document.getElementById('radiusSelect').value;
     var searchUrl = '/includes/gmaps.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
     GDownloadUrl(searchUrl, function(data) {
     //alert(data);
       var xml = GXml.parse(data);
       var guide_organizations = xml.documentElement.getElementsByTagName('marker');
       map.clearOverlays();

       var results = document.getElementById('search-results-body');
       results.innerHTML = "";
       if (guide_organizations.length == 0) {
			results.innerHTML = "<td></td><td colspan='3'><strong>No Results Found</strong></td>";
			document.getElementById("search-results-div").style.display = "block";
			document.getElementById("search-results-count").innerHTML = '0';
         map.setCenter(new GLatLng(40, -100), 4);
         return;
       }

       var bounds = new GLatLngBounds();
       // look through the organizations and create markers and results tables //
       var counter = 0;
       
	   for (var i = 0; i < guide_organizations.length; i++)
	   {
		   var name = guide_organizations[i].getAttribute('name');
		   var org_id = guide_organizations[i].getAttribute('org_id');
		   var address = guide_organizations[i].getAttribute('address');
		   var distance = parseFloat(guide_organizations[i].getAttribute('distance'));
		   var point = new GLatLng(parseFloat(guide_organizations[i].getAttribute('lat')),
								   parseFloat(guide_organizations[i].getAttribute('lng')));
		   var type = guide_organizations[i].getAttribute('region');
		   var programs = guide_organizations[i].childNodes;
	
		   var marker = createMarker(point, name, address, type, org_id, programs);
		   map.addOverlay(marker);
		   
if(programs.length > 0)
{
		  for(var x = 0; x < programs.length; x++)
		  {
			//var prog_title	= programs[i].getAttribute("prog_title");
			//var prog_id		= programs[i].getAttribute("pid");
			//var prog_score	= programs[i].getAttribute("score");
			//var prog_year	= programs[i].getAttribute("year");
			
			var result = createResultsEntry(marker, name, address, distance, programs[x], (counter++ % 2));
			results.appendChild(result);
		  }
		  
}
		   //var sidebarEntry = createSidebarEntry(marker, name, address, distance);
		   //sidebar.appendChild(sidebarEntry);
		   bounds.extend(point);
	   }
       document.getElementById("search-results-div").style.display = "block";
       document.getElementById("search-results-count").innerHTML = counter;
       map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
     });
   }

  
   function createMarker(point, name, address, type, org_id, programs) {
      var marker = new GMarker(point, customIcons[type]);
      var html = "<strong>" + name + "</strong> <br/>" + address;
      
      if(programs.length > 0)
      {
      	  html += "<br /><br/><strong>Programs:</strong><ul style='margin-top: 0;'>";
		  for(var i = 0; i < programs.length; i++)
		  {
			var prog_title	= programs[i].getAttribute("prog_title");
			var prog_id		= programs[i].getAttribute("pid");
			var prog_score	= programs[i].getAttribute("score");
			var prog_year	= programs[i].getAttribute("year");
			
			html += "<li><a href='/guide/"+prog_id+"/overview/' title='"+prog_title+"'>"+prog_title+"</a></li>";
		  }
		  html += "</ul>";
	  }
	  GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }

    function createSidebarEntry(marker, name, address, distance) {
      var tr = document.createElement('tr');
      var html = '<td><b>' + name + '</b> (' + distance.toFixed(1) + ' miles)<br/>' + address + '</td>';
      tr.innerHTML = html;
      tr.style.cursor = 'pointer';
      tr.style.marginBottom = '5px'; 
      GEvent.addDomListener(tr, 'click', function() {
        GEvent.trigger(marker, 'click');
      });
      GEvent.addDomListener(tr, 'mouseover', function() {
        tr.style.backgroundColor = '#eee';
      });
      GEvent.addDomListener(tr, 'mouseout', function() {
        tr.style.backgroundColor = '#fff';
      });
      return tr;
    }


/* Make this:

<tr class="=odd or even">
	<td>
		<strong><a href="/guide/{$program.program_id}/overview/" title="{$program.prog_title|escape}">{$program.name|escape} - {$program.prog_title|escape}</a></strong><br/>
		{$program.city}, {$program.state|upper}
		{if $program.type}<br /><strong>{$program.year} Samaritan Award {$program.type}</strong>{/if}
	</td>
	<td class="centered">{$program.batch_year}</td>
	<td class="centered"><strong>{$rating[$score]}</strong></td>
</tr>

*/
function createResultsEntry(marker, name, address, distance, program, oddoreven)
{
	// set up some basic variables
	var prog_title	= program.getAttribute("prog_title");
	var prog_id		= program.getAttribute("pid");
	
	var tr = document.createElement('tr');
	var locat = document.createElement('td');
	var first = document.createElement('td');
	var secon = document.createElement('td');
	var third = document.createElement('td');
	
	if(oddoreven == 1)	tr.className = "even";	else	tr.className = "odd";
	
	var html = '<strong><a href="/guide/' + prog_id + '/overview/" title="' + escape(prog_title) + '">' + name + ' - ' + prog_title + '</a></strong><br/>' + address + ' (' + distance.toFixed(1) + ' miles away)';
	locat.innerHTML = '<a href="#map"><img src="/images/locate.jpg" height="17" width="17" title="Locate" Alt="Locate"></a>';
	first.innerHTML = html;
	secon.innerHTML = program.getAttribute("year");
	third.innerHTML = "<strong>" + program.getAttribute("score") + "</strong>";
	secon.className = "centered";
	third.className = "centered";
	tr.appendChild(locat);
	tr.appendChild(first);
	tr.appendChild(secon);
	tr.appendChild(third);
	
	
	locat.style.cursor = 'pointer';
	locat.style.marginBottom = '5px'; 
	GEvent.addDomListener(locat, 'click', function() {
	GEvent.trigger(marker, 'click');
	});
	/*GEvent.addDomListener(locat, 'mouseover', function() {
	locat.style.backgroundColor = '#eee';
	});
	GEvent.addDomListener(locat, 'mouseout', function() {
	locat.style.backgroundColor = '#fff';
	});
	*/
	/*
	tr.style.cursor = 'pointer';
	tr.style.marginBottom = '5px'; 
	GEvent.addDomListener(tr, 'click', function() {
	GEvent.trigger(marker, 'click');
	});
	GEvent.addDomListener(tr, 'mouseover', function() {
	tr.style.backgroundColor = '#eee';
	});
	GEvent.addDomListener(tr, 'mouseout', function() {
	tr.style.backgroundColor = '#fff';
	});
	*/
	return tr;
}
