	var geocoder;
	var ag_res;
	var special_protect;
	var border;
	var map;
	var marker;
	var infowin_toggle = 0;
	var reportHtml;
	
    
    
	function initialize(layerCase) {
		
		if (GBrowserIsCompatible()) {
			//Bring in the KMLs
			ag_res = new GGeoXml("http://www.mcmaps.org/notification/ag_lands.kml");
			special_protect = new GGeoXml("http://www.mcmaps.org/notification/Special_Protection_Areas.kml");
			border = new GGeoXml("http://www.mcmaps.org/notification/Montgomery_County.kmz");

			//Create the Map
			map = new GMap2(document.getElementById("map_canvas"));
			//Scroll Wheel zoom causes UI errors
			//map.enableScrollWheelZoom();
			geocoder = new GClientGeocoder();
            //Add the Map Controls
			map.addControl(new GLargeMapControl());
			map.addControl(new GOverviewMapControl());
			var mapControl = new GMapTypeControl();
			map.addControl(mapControl);
			
			//Add the Overlays
			map.addOverlay(border);
			switch(layerCase) {
				case 'aglands':
					map.addOverlay(ag_res);
					break;
				case 'spa':
					map.addOverlay(special_protect);
					break;
				default:
					map.addOverlay(ag_res);
					map.addOverlay(special_protect);
					break;
			}

			//Zoom to Montgomery County
			map.setCenter(new GLatLng(39.1, -77.2), 10);
			
		}

	}
	
	function toggleLayer(targetlayer, checkbox) {
	    document.getElementById(checkbox).checked ? targetlayer.show() : targetlayer.hide();
	}

	function processInput(addressinput, acctinput, layerCase) {
		// if length(input) == 8 then checkDB
		if (addressinput == "") {
			checkDB(acctinput, layerCase);
		} else {
		    showAddress(addressinput);
		}

	}

	function checkDB(acct, layerCase) {
		//start up ajax requester
		var xhr = null;
		//document.form1.dyn.value = "starting..."
		if (window.XMLHttpRequest) {
			xhr = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xhr.onreadystatechange = function() 
		{
			if (xhr.readyState == 4) 
			{
				if (xhr.status == 200) {
					processInfo(xhr.responseText, layerCase);
				}
				else
					onFail();
			}
		};
		xhr.open("GET", "http://www.mcmaps.org/notification/Parcel_lookup.aspx?acct=" + acct, true);
		xhr.send(null);	
		
	}


	function showAddress(address) {
		if (geocoder) {
			geocoder.getLatLng(address, function(point) {
				if (!point) { alert(address + " not found");} 
				else { 
					
					if (marker != undefined) {
						map.removeOverlay(marker);
					}
					map.setCenter(point, 13); 
					marker = new GMarker(point, {title: address});
					map.addOverlay(marker);

					//Add in a listener to pop open an info box on click
					GEvent.addListener(marker, "click", function() {
						if (infowin_toggle == 0) {
							marker.openInfoWindowHtml(address);
							infowin_toggle = 1;
						} else {
							marker.closeInfoWindow();
							infowin_toggle = 0;
						}
					});

					// A listener for window closing
					GEvent.addListener(marker, "infowindowclose", function() {
						infowin_toggle = 0;
					});
			
					// marker.openInfoWindowHtml(address);
				}
			});
		}
	}
	
	function processInfo (incoming, layerCase) {
	                var response = new Array();
	                if (incoming == "Bad Input") {
	                    showAddress(acctinput);
	                } else { 
	 					//Grab the response and split out into columns:
	 					// 0 - acct, 1 - in_spa, 2 - ag_notify, 3 - lat, 4 - lon
	 					//alert(incoming);
	 					response = incoming.split(",");
					    var in_spa = response[1];
					    var ag_notify = response[2];
					    var lat = response[3];
					    var lon = response[4];


                        //Clear any prexisting points
					    if (marker != undefined) {
						    map.removeOverlay(marker);
					    }
					    //Create a point
					    var pnt = new GLatLng(lat, lon);
					    map.setCenter(pnt, 14);
					    marker = new GMarker(pnt, {title: response[0]});
					    map.addOverlay(marker);
					    
					    //Add a table in the report div detailing status
					    
					    reportHtml = "<table><tr><td>Parcel Acct &#35;</td><td>" + response[0] + "</td></tr>";
					    
					    switch(layerCase) {
						case 'spa':
					    		reportHtml += "<tr><td>Within Special Protection Areas&#63;</td><td>";
							if (in_spa == 1) {
								reportHtml += "YES";
							} else {
					     		   	reportHtml += "NO";
							}
					    		reportHtml += "</td></tr></table>";
							break;
						case 'aglands':
						    reportHtml += "<tr><td>Within, adjacent to or confronting Agricultural Zones&#63;</td><td>";
				                    if (ag_notify == 1) {
	                    				reportHtml += "YES";
						    } else {
						        reportHtml += "NO";
						    }
						    reportHtml += "</td></tr></table>";
						    break;
						default:
					    		reportHtml += "<tr><td>Within Special Protection Areas&#63;</td><td>";
							if (in_spa == 1) {
								reportHtml += "YES";
							} else {
					     		   	reportHtml += "NO";
							}
					    		reportHtml += "</td></tr>";
						    	reportHtml += "<tr><td>Within, adjacent to or confronting Agricultural Zones&#63;</td><td>";
				                    	if (ag_notify == 1) {
	                    					reportHtml += "YES";
						    	} else {
						        	reportHtml += "NO";
						    	}
						    	reportHtml += "</td></tr></table>";
							break;
					    }
	                    
	                    //Create a set of rows
	                    
                        var new_row = document.getElementById('resultstable').insertRow(-1);
	                    var new_acct = new_row.insertCell(0);
	                    var new_result = new_row.insertCell(1);
	                   // var new_ag = new_row.insertCell(2);
	                    new_acct.innerHTML = response[0];
	                    
				switch(layerCase) {
					case 'spa':
						if (in_spa == 1) { new_result.innerHTML = "<center>YES</center>";  } 
						else { new_result.innerHTML = "<center>NO</center>"; }
						break;
					case 'aglands':
						if (ag_notify == 1) { new_result.innerHTML = "<center>YES</center>"; } 
						else { new_result.innerHTML = "<center>NO</center>"; }
						break;
					default:
						if (in_spa == 1) { new_result.innerHTML = "<center>YES</center>";  } 
						else { new_result.innerHTML = "<center>NO</center>"; }

						var new_ag = new_row.insertCell(2);
						if (ag_notify == 1) { new_ag.innerHTML = "<center>YES</center>"; } 
						else { new_ag.innerHTML = "<center>NO</center>"; }
						break;

				}
					    
					    
	                    
	                    //Add in a listener to pop open an info box on click
					    GEvent.addListener(marker, "click", function() {
						    if (infowin_toggle == 0) {
						        marker.openInfoWindowHtml(reportHtml);
							    infowin_toggle = 1;
						    } else {
							    marker.closeInfoWindow();
							    infowin_toggle = 0;
						    }
					    });

					// A listener for window closing
					    GEvent.addListener(marker, "infowindowclose", function() {
						    infowin_toggle = 0;
					    });
                    
	             }     
	}
	
	function onFail() {
	    alert("A technical problem prevented the website from functioning.  Please try again later.");
	}	
	
	function clearInput(target) {
	    target_element = document.getElementById(target);
	    target_element.value = "";
	}