    //<![CDATA[

    var map = null;
    var geocoder = null;

    function load() {
      if (GBrowserIsCompatible()) 
	  {
	  
		  // Create our "tiny" marker icon
		  var blueIcon = new GIcon(G_DEFAULT_ICON);
		  blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png"; 
		  blueIcon.iconSize = new GSize(32, 32);
		  
		  // Set up our GMarkerOptions object
		  publicAgency = { icon:redIcon };
	  
		  // Create our "tiny" marker icon
		  var redIcon = new GIcon(G_DEFAULT_ICON);
		  redIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png"; 
		  redIcon.iconSize = new GSize(32, 32);
		  
		  // Set up our GMarkerOptions object
		  privateAgency = { icon:blueIcon };

		   // Create our "tiny" marker icon
		  var freeIcon = new GIcon(G_DEFAULT_ICON);
		  freeIcon.image = "http://labs.google.com/ridefinder/images/mm_20_gray.png"; 
		  freeIcon.iconSize = new GSize(12, 20);
		  
		  // Set up our GMarkerOptions object
		  freeAgency = { icon:freeIcon };
		  
		map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(37.685994,-97.327881), 4);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        geocoder = new GClientGeocoder();
		var randomnumber=Math.floor(Math.random()*100000);
		
		GDownloadUrl("fcc-xml.php?rand="+randomnumber, function(data, responseCode) {  
			var xml = GXml.parse(data);  
			var markers = xml.documentElement.getElementsByTagName("marker");  
			for (var i = 0; i < markers.length; i++) {    
				var point = new GLatLng(parseFloat(markers[i].getAttribute("latitude")),parseFloat(markers[i].getAttribute("longitude")));
				
				//all agencies have name displayed
				var html1 = "<strong>"+markers[i].getAttribute("name")+"</strong><br/>";
				
				//member agencies have address displayed
				var html2 = markers[i].getAttribute("address")+"<br/>"+markers[i].getAttribute("city")+", "+markers[i].getAttribute("state")+" ";
				if(markers[i].getAttribute("zipcode") > 0) 
				  html2 = html2 + markers[i].getAttribute("zipcode") +"<br/>";
				else
				  html2 = html2 + "<br/>";
				
				//all agencies have zoom to link displayed
				var html3 = ""; //"<ul><li type='square'><a href='javascript:zoomto("+markers[i].getAttribute("latitude")+","+markers[i].getAttribute("longitude")+")'>Zoom to agency</a>";


				//var html4 = "<li type='square'><a href='javascript:subscribe();'>Subscription Warning</a><!--<img border=0 src=\"icon001.gif\"/></a>-->";
				var html4 = "<br/>&middot; <a href='moreinfo.php?agencyid="+markers[i].getAttribute("id")+"'>Get more information about agency</a>";
				var html5 = "<br/>&middot; <a href='agency.php'>Subscribe if you are this Agency</a>";
				
				//register.php?mid="+markers[i].getAttribute("id")+"'>Subscribe if you are Agency</a><!--<img border=0 src=\"icon003.gif\"/></a>-->";
				
				var html51 = "<br/>&middot; <a href='moreinfo.php?agencyid="+markers[i].getAttribute("id")+"'>Interested in becoming a foster parent?</a>";
				var html6 = "<br/>&middot; <a target='_blank' href='http://www.fostercarecentral.org'>Visit fostercarecentral.org!</a>";
				
				var agencyGSearch = markers[i].getAttribute("name")+",+"+markers[i].getAttribute("address")+",+"+markers[i].getAttribute("city")+",+"+markers[i].getAttribute("state")+"+"+markers[i].getAttribute("zipcode");
				
				var html7 = "<br/>&middot; <a target='_blank' href='http://www.google.com/search?q="+agencyGSearch+"'>Search for agency on Google</a>";
				
				//Arkansas+Department+of+Human+Services+-+Polk+County+Mena%2C+AR+71953
				
				//name for ALL
				var myhtml = html1; 
				
				//address for PAID
				if(markers[i].getAttribute("payment")==1 && markers[i].getAttribute("profile")) myhtml = myhtml + html2; 
				
				if(markers[i].getAttribute("payment")==0) myhtml = myhtml + ""; //"<br/><em>This agency is not a member.</em><br/>";
				if(markers[i].getAttribute("payment")==1) myhtml = myhtml + ""; //"<br/><strong style='color:green;'>Agency is a member.</em><br/>";
				
				//zoom to for ALL
				myhtml = myhtml + html3; 
				
				//more info button
				if(markers[i].getAttribute("payment")==1 && markers[i].getAttribute("profile")) myhtml = myhtml + html4; 
				
				//if NOT PAID
				if(markers[i].getAttribute("payment")==0) myhtml = myhtml + html5; 
				
				//if PAID and ACCEPTING LEADS
				//if(markers[i].getAttribute("payment")==1) myhtml = myhtml + html51; 
				
				myhtml = myhtml + html6;

				if(markers[i].getAttribute("payment")==0) myhtml = myhtml + html7 + "<p/>"; //always display link to fostercarecentral.org
				
				if(markers[i].getAttribute("payment")==1)
					var moreinfohtml = myhtml;
				else
					var moreinfohtml = html1+html6;
				//map.addOverlay(new GMarker(point));  
				map.addOverlay(createMarker(myhtml,markers[i].getAttribute("agencytype"),moreinfohtml,point));
			}
		});

		GEvent.addListener(map, "infowindowclose", function() {    
			//document.getElementById("moreinfoinner").innerHTML = ""; 
		});  	
	
		if(window.location.search.length<1) 
			map.openInfoWindow(map.getCenter(), "<div style='margin:10px;text-align:left;width:300px;font-family:sans-serif;'><strong>Welcome to FosterCareMaps.org!</strong>	<ul><li type='square'><strong>Agencies</strong>, <a href='agency.php'>click here</a> to add yourself to the map.</li><li type='square'><strong>Counties</strong>, <a href='county.php'>click here</a> to advertise your high need areas.</li><li type='square'><strong>Foster/ Adoptive parents</strong>, use the search box above to find an agency to meet your needs!</li></ul></div>");      
		else
			advertiseAddress(queryString("premium"));
			
		}
    }

	function details(html) {
		document.getElementById("moreinfoinner").innerHTML = '<p style="margin: 0 10px 10px 10px;">' + html + '</p>';
	}
	
	function subscribe() {
		alert('We are sorry; the page you have requested is not available because this foster agency has not signed up yet to be a part of the fostercaremap.org web site. ');
	}
	
    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 11);
              //alert(point);
			  //var marker = new GMarker(point);
              //map.addOverlay(marker);
              //marker.openInfoWindowHtml(address);
              //map.removeOverlay(marker);
              //var lat = point.coordinates[0];
			  //var lng = point.coordinates[1];
              map.openInfoWindowHtml(point,"<strong>"+address+"</strong><br/>Close this window to begin<br/>exploring agencies nearby.");
	      processAjax('search_text.php?point='+point);			  
            }
          }
        );
      }
    }

    function advertiseAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 11);
              //alert(point);
			  //var marker = new GMarker(point);
              //map.addOverlay(marker);
              //marker.openInfoWindowHtml(address);
              //map.removeOverlay(marker);
              //var lat = point.coordinates[0];
			  //var lng = point.coordinates[1];
			  var randomnumber=Math.floor(Math.random()*100000);
              map.openInfoWindowHtml(point,"<div id='countyinfowindow' style='height:250px;width:350px;overflow-y:auto;'>Loading...<br/><a href=\"javascript:processAjax3('advertise_countyinfo.php?address="+address+"&random="+randomnumber+"');\">click here to reload</a></div>");
			  //var start = new Date().getTime();
			  //while (new Date().getTime() < start + 100);
			  processAjax2('advertise_count.php?clicked='+address+'&random='+randomnumber);	
			  processAjax3('advertise_countyinfo.php?address='+address+'&random='+randomnumber);
			  processAjax('search_text.php?point='+point);	
			  processAjax3('advertise_countyinfo.php?address='+address+'&random='+randomnumber);
			  }
          }
        );
      }
    }

	function createMarker(myhtml, agencytype, morehtml, point) {  
		if(agencytype=="public") var marker = new GMarker(point, publicAgency);  
		if(agencytype=="private") var marker = new GMarker(point, privateAgency);  
		if(agencytype=="") var marker = new GMarker(point, freeAgency);
		GEvent.addListener(marker, "click", function() {    
			map.openInfoWindowHtml(point, myhtml); 
			details(morehtml);
		});  
		return marker;
	}

    function zoomto(lat,lng) {
      var point = new GLatLng(lat,lng);
      map.setCenter(point, 13);
	  processAjax('search_text.php?lat='+lat+'&lng='+lng);
    }
    
	function search() {
		alert(document.searchform.search.value);
	}

	function processAjax(url) { 
		 

		if (window.XMLHttpRequest) { // Non-IE browsers 
		  req = new XMLHttpRequest(); 
		  req.onreadystatechange = targetDiv; 
		  try { 
			req.open("GET", url, true); 
		  } catch (e) { 
			alert(e); 
		  } 
		  req.send(null); 
		} else if (window.ActiveXObject) { // IE 
		  req = new ActiveXObject("Microsoft.XMLHTTP"); 
		  if (req) { 
			req.onreadystatechange = targetDiv; 
			req.open("GET", url, true); 
			req.send(); 

		  } 
		} 
	  return false; 
	} 

	function processAjax2(url) { 
		 

		if (window.XMLHttpRequest) { // Non-IE browsers 
		  req2 = new XMLHttpRequest(); 
		  //req.onreadystatechange = targetDiv; 
		  try { 
			req2.open("GET", url, true); 
		  } catch (e) { 
			alert(e); 
		  } 
		  req2.send(null); 
		} else if (window.ActiveXObject) { // IE 
		  req2 = new ActiveXObject("Microsoft.XMLHTTP"); 
		  if (req2) { 
			//req.onreadystatechange = targetDiv; 
			req2.open("GET", url, true); 
			req2.send(); 

		  } 
		} 
	  return false; 
	} 

	function processAjax3(url) { 
		 

		if (window.XMLHttpRequest) { // Non-IE browsers 
		  req3 = new XMLHttpRequest(); 
		  req3.onreadystatechange = targetDiv2; 
		  try { 
			req3.open("GET", url, true); 
		  } catch (e) { 
			alert(e); 
		  } 
		  req3.send(null); 
		} else if (window.ActiveXObject) { // IE 
		  req3 = new ActiveXObject("Microsoft.XMLHTTP"); 
		  if (req3) { 
			req3.onreadystatechange = targetDiv2; 
			req3.open("GET", url, true); 
			req3.send(); 

		  } 
		} 
	  return false; 
	} 
	
	function targetDiv() { 
		if (req.readyState == 4) { // Complete 
			  if (req.status == 200) { // OK response 
				  document.getElementById("agencyidinner").innerHTML = req.responseText; 
			  } else { 
				alert("Problem: " + req.statusText); 
			  } 
		} 
	} 

	function targetDiv2() { 
		if (req3.readyState == 4) { // Complete 
			  if (req3.status == 200) { // OK response 
				  document.getElementById("countyinfowindow").innerHTML = req3.responseText; 
			  } else { 
				alert("Problem: " + req3.statusText); 
			  } 
		} 
	} 

	function queryString(key){
		var page = new PageQuery(window.location.search);
		return unescape(page.getValue(key));
	}
	
	function PageQuery(q) {
if(q.length > 1) this.q = q.substring(1, q.length);
else this.q = null;
this.keyValuePairs = new Array();
if(q) {
for(var i=0; i < this.q.split("&").length; i++) {
this.keyValuePairs[i] = this.q.split("&")[i];
}
}
this.getKeyValuePairs = function() { return this.keyValuePairs; }
this.getValue = function(s) {
for(var j=0; j < this.keyValuePairs.length; j++) {
if(this.keyValuePairs[j].split("=")[0] == s)
return this.keyValuePairs[j].split("=")[1];
}
return false;
}
this.getParameters = function() {
var a = new Array(this.getLength());
for(var j=0; j < this.keyValuePairs.length; j++) {
a[j] = this.keyValuePairs[j].split("=")[0];
}
return a;
}
this.getLength = function() { return this.keyValuePairs.length; }
}
    //]]>

