
//Handles address search
function AddressSearchHandler(geocoder, geocodeCallbackHandler, smartServiceCallbackHandler, invokeSmartServiceFunction){
  this.geocoder = geocoder;
  this.geocodeCallbackHandler = geocodeCallbackHandler;
  this.smartServiceCallbackHandler = smartServiceCallbackHandler;
  this.invokeSmartServiceFunction = invokeSmartServiceFunction;
}
AddressSearchHandler.prototype.doSearch = function(address, crop){
  	this.smartServiceCallbackHandler.clear();
    var callbackHandler = this.geocodeCallbackHandler;
    this.geocoder.getLatLng(address, function(point){callbackHandler.callback(point);});
    this.invokeSmartServiceFunction("/yieldreport/servlet/gmap/iprSearch?random="+Math.random()+"&method=search&zip_code_input="+address+"&crop="+crop, this.smartServiceCallbackHandler);
}

/**
 * This is just a placeholder object, a mockup of Google's Geocoder, which does nothing.
 * It is intended to be used by the AddressSearchHandler as long as the fix for
 * TeamTrack issue BUG0129699 is in place.  Because of this issue, we are not geocoding
 * the user's entered zip code, but instead for now are geocoding the zip code of the
 * first county returned by the AJAX XML.
 */
function InactiveGeocoder(){}
InactiveGeocoder.prototype.getLatLng = function(address, callbackFunction){}

/**
 * Handles creating a Google Map marker when the marker's info text and the marker's
 * location are both separately returned asynchronously and in arbitrary order.
 * If no search results are returned, then this instance can be invalidated and reset
 * for continued reuse.
 */
function MarkerFuture(gmap){
  this.gmap = gmap;  
  this.point = null;
  this.setPointCalled = false;
  
  this.xmlDocument = null;
  this.setXmlDocumentCalled = false;  
}
MarkerFuture.prototype.setPoint = function(point){
  this.setPointCalled = true;
  this.point = point;
  this.createPoint();
}
MarkerFuture.prototype.setXmlDocument = function(xmlDocument){
  	this.setXmlDocumentCalled = true;
  	this.xmlDocument = xmlDocument;  
  	this.createPoint();
}
MarkerFuture.prototype.createPoint = function(){
	//alert("Enter createPoint()\nPoint: "+this.point+"\nDocument: "+this.xmlDocument);  
  	if (this.setXmlDocumentCalled && this.setPointCalled) {
  		this.setXmlDocumentCalled = false;
  		this.setPointCalled = false;
    	this.plotPoint();
      	this.centerAndZoom();
  	}
}
MarkerFuture.prototype.centerAndZoom = function()
{
	var bounds = this.getBoundsForData();	
	var center = this.point;
	
	// If both null, center on the United States
	if (center == null && bounds == null) {
		this.setCenter(UNITED_STATES_LAT_LONG, UNITED_STATES_ZOOM_LEVEL);
	}
	// If both not null, center on zip and zoom out
	// until the data bounding box is visible
	else
	if (center != null && bounds != null)
	{
		var zoomLevel = DEFAULT_ZOOM_LEVEL;		
		this.gmap.setCenter(center, zoomLevel);				
		
		var mapBounds = this.gmap.getBounds()
		
		while (! mapBounds.containsBounds(bounds)) {
			zoomLevel = zoomLevel - 1;
			this.gmap.setCenter(center, zoomLevel);
			mapBounds = this.gmap.getBounds();
		}
	}
	// Center on zip if it is not null.
	else
	if (center != null) {
		this.setCenter(center, DEFAULT_ZOOM_LEVEL);	
	}
	// Center on center of data bounding box
	else {
		var zoomLevel = this.gmap.getBoundsZoomLevel(bounds);
		if (zoomLevel > 14) {
			zoomLevel = 14;
		}
		this.setCenter(bounds.getCenter(), zoomLevel);
	}
}
MarkerFuture.prototype.getBoundsForData = function()
{	
	if (this.setCenterCalled) {
		return null;
	}
	//alert("Enter setZoom()");
	var CountiesElement = this.xmlDocument.getElementsByTagName('Counties')[0];
	if (CountiesElement == null) {
		return null;
	}
	
	var ViewAreaElement = CountiesElement.getElementsByTagName('ViewArea')[0];
	if (ViewAreaElement == null) {
		return null;
	}
	
	var minLat = null;
	var minLng = null;
	var maxLat = null;
	var maxLng = null;
	
	var MinLatitudeElement = ViewAreaElement.getElementsByTagName('MinLatitude')[0];
	if (MinLatitudeElement == null) {
		return null;
	} else {
		minLat = MinLatitudeElement.firstChild.data;
	}
	
	var MinLongitudeElement = ViewAreaElement.getElementsByTagName('MinLongitude')[0];
	if (MinLongitudeElement == null) {
		return null;
	} else {
		minLng = MinLongitudeElement.firstChild.data;
	}
	
	var MaxLatitudeElement = ViewAreaElement.getElementsByTagName('MaxLatitude')[0];
	if (MaxLatitudeElement == null) {
		return null;
	} else {
		maxLat = MaxLatitudeElement.firstChild.data;
	}
	
	var MaxLongitudeElement = ViewAreaElement.getElementsByTagName('MaxLongitude')[0];
	if (MaxLongitudeElement == null) {
		return null;
	} else {
		maxLng = MaxLongitudeElement.firstChild.data;
	}
	
	
	var sw = new GLatLng(minLat, minLng);
	var ne = new GLatLng(maxLat, maxLng);
	var bounds = new GLatLngBounds(sw, ne);
	
	//var str = "";
	//str += "Min: " + minLat + "," + minLng;
	//str += "\n" + "Max: " + maxLat + "," + maxLng;
	//alert(str);
	
	return bounds;
}
MarkerFuture.prototype.setCenter = function(center, zoomLevel) {
	this.gmap.setCenter(center, zoomLevel);
}
MarkerFuture.prototype.plotPoint = function()
{
	//alert("Enter plotPoint()");
	
	//alert("featured grower names array");
	var featuredGrowerNamesArray = this.getFeaturedGrowerNamesArray();
	if (featuredGrowerNamesArray == null) {
		return;
	}

	//alert("county element array");
	var countyElementArray = this.getCountyElementArray();
	if (countyElementArray == null || countyElementArray.length == 0) {
		return;
	}

	//alert("for county element array items");
	for (var i=0; i<countyElementArray.length; i++)
	{
		var growerElementArray = this.getGrowerElementArray(countyElementArray.item(i));
		if (growerElementArray == null) {
			continue;
		}

		for (var j=0; j<growerElementArray.length; j++) 
		{
			var GrowerElement = growerElementArray.item(j);
			var plotElementArray = this.getPlotElementArray(GrowerElement);
			if (plotElementArray == null || plotElementArray.length == 0) {
				continue;
			}
			
			for (var m=0; m<plotElementArray.length; m++) {
				var marker = this.getMarker(GrowerElement, plotElementArray[m], featuredGrowerNamesArray);
			}
		}		
	}
}
MarkerFuture.prototype.getMarker = function(GrowerElement, PlotElement, featuredGrowerNamesArray) 
{
	var latlng = this.getLatLng(PlotElement);

	var marker = new GIcon(G_DEFAULT_ICON);
    marker.image = this.getMarkerImage(GrowerElement, featuredGrowerNamesArray);
    marker.iconSize = new GSize(24, 38);
    //marker.shadow = this.getMarkerShadowImage();
    //marker.infoWindowAnchor = new GPoint(16, 1);

    var markerOptions = { icon:marker };

    marker = new GMarker(latlng, markerOptions);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(
       		MarkerFuture.prototype.getBubbleText(GrowerElement, PlotElement)
        );
    });
        
    this.gmap.addOverlay(marker);   
}
MarkerFuture.prototype.getMarkerImage = function(GrowerElement, featuredGrowerNamesArray)
{
	var cooperator = GrowerElement.getElementsByTagName('Cooperator')[0].firstChild.data;
	var idx = -1;
	for (var i=0; idx == -1 && i<featuredGrowerNamesArray.length; i++) {
		if (cooperator == featuredGrowerNamesArray[i]) {
			idx = i;
		}
	}
	
	return getFeaturedMarkerImage(idx);
}

MarkerFuture.prototype.getLatLng = function(PlotElement)
{
	return new GLatLng(
		 PlotElement.getElementsByTagName('Latitude')[0].firstChild.data
		,PlotElement.getElementsByTagName('Longitude')[0].firstChild.data
	);		
}
MarkerFuture.prototype.getBubbleText = function(GrowerElement, PlotElement)
{
	var html = "";
	
	// Cooperator
	var cooperator = GrowerElement.getElementsByTagName('Cooperator')[0].firstChild.data;
	if (cooperator == null || cooperator.length == 0) {
		cooperator = "Name: N/A";
	} else {
		cooperator = "<b>" + cooperator + "</b>";
	}
	
	// City Description
	var cityDescription = GrowerElement.getElementsByTagName('CityDescription')[0].firstChild.data;
	if (cityDescription == null || cityDescription.length == 0) {
		cityDescription = "City: N/A";
	} 
	
	// Latitude
	var latitude = PlotElement.getElementsByTagName('Latitude')[0].firstChild.data;
	if (latitude == null || latitude.length == 0) {
		latitude = "Latitude: N/A";
	} else {
		latitude = "Latitude: " + latitude;
	}
	
	// Longitude
	var longitude = PlotElement.getElementsByTagName('Longitude')[0].firstChild.data;
	if (longitude == null || longitude.length == 0) {
		longitude = "Longitude: N/A";
	} else {
		longitude = "Longitude: " + longitude;
	}
	
	// URL
	var url = PlotElement.getElementsByTagName('URL')[0].firstChild.data;
	if (url == null || url.length == 0) {
		url = "Plot Report: N/A";
	} else {
		url = "<a style=\"color:blue;\" href=\""+url+"\">View Report</a>";
	}
	
	html = 
	      cooperator 
		+ "<br />" 
		+ cityDescription
		+ "<br />"
		+ url
		//+ "<br />"
		//+ latitude
		//+ "<br />"
		//+ longitude
	;
		
	return html;
}
MarkerFuture.prototype.getPlotElementArray = function(GrowerElement){
	var PlotArray = GrowerElement.getElementsByTagName('Plot');
	if (PlotArray == null || PlotArray.length == 0) {
		//alert("PlotArray is null");
		return null;
	}

	var plotElementArray = new Array();
	var plotElmmentArrayIdx = 0;
	
	for (var j=0; j<PlotArray.length; j++) 
	{
		var PlotElement = PlotArray.item(j);
		var hasLatLong = PlotElement.getAttribute('hasLatLong');
		if (hasLatLong == "true") {
			plotElementArray[plotElmmentArrayIdx] = PlotElement;
			plotElmmentArrayIdx++;
		}
	} 

	return 	plotElementArray;
}
MarkerFuture.prototype.getGrowerElementArray = function(CountyElement){
	var GrowerArray = CountyElement.getElementsByTagName('Grower');
	if (GrowerArray == null || GrowerArray.length == 0) {
		//alert("GrowerArray == null");
		return null;
	}
	return GrowerArray;
}
MarkerFuture.prototype.getCountyElementArray = function(){
	var CountiesElement = this.xmlDocument.getElementsByTagName('Counties')[0];
	if (CountiesElement == null) {
		return null;
	}	
	return CountiesElement.getElementsByTagName('County');
}
MarkerFuture.prototype.getFeaturedGrowerNamesArray = function(){
	var FeaturedGrowersElement = this.xmlDocument.getElementsByTagName('FeaturedGrowers')[0];	
	if (FeaturedGrowersElement == null) {
		return null;
	}
	var GrowerArray = FeaturedGrowersElement.getElementsByTagName('Grower');
	if (GrowerArray == null || GrowerArray.length == 0) {
		return null;
	}
	var growerNamesArray = new Array();
	var arrayIndex = 0;
	for (var i=0; i<GrowerArray.length; i++)
	{
		var GrowerElement = GrowerArray.item(i);
		
		var numberOfPlotsWithLatLong = 0;
		var numOfPlotsWithLatLongElement = GrowerElement.getElementsByTagName('NumberOfPlotsWithLatLong')[0];
 		if (numOfPlotsWithLatLongElement != null && numOfPlotsWithLatLongElement.firstChild != null)
 		{
 			numberOfPlotsWithLatLong
 				=  parseInt(numOfPlotsWithLatLongElement.firstChild.data);
       		if (isNaN(numberOfPlotsWithLatLong)) {
       			numberOfPlotsWithLatLong = 0;
       		}
       	}
      	
      	if (numberOfPlotsWithLatLong > 0) {
	  		growerNamesArray[arrayIndex] = GrowerElement.getElementsByTagName('Cooperator')[0].firstChild.data;			
	  		arrayIndex++;
	  	}
	}
	/*
	var str = "";
	for (var i=0; i<growerNamesArray.length; i++) {
		str = str + growerNamesArray[i] + ", ";
	}
	alert(str);	
	*/
	return growerNamesArray;
}


//Handles Google zipcode callback, recenters map
function GeocodeCallbackHandler(gmap, markerFuture){
  this.gmap = gmap;
  this.markerFuture = markerFuture;
}
GeocodeCallbackHandler.prototype.callback = function(point){	
  this.gmap.setCenter(point, DEFAULT_ZOOM_LEVEL);
  this.markerFuture.setPoint(point);
}

//Handles smart service callback
function SmartServiceCallbackHandler(xmlHTTP, xmlHandler){
  this.xmlHTTP = xmlHTTP;
  this.xmlHandler = xmlHandler;
}
SmartServiceCallbackHandler.prototype.clear = function(){
	if (this.xmlHandler.clear) {
		this.xmlHandler.clear();
	}
}
SmartServiceCallbackHandler.prototype.callback = function(){
  if (this.xmlHTTP.readyState == 4) {
    var xmlDocument = this.xmlHTTP.responseXML;
    this.xmlHandler.handleXML(xmlDocument);
  }
}

// Interface of all XMLHandlers
function XMLHandler(){
}
XMLHandler.prototype.handleXML = function(xmlDocument) {
}
XMLHandler.prototype.clear = function() {
}

//Composite XMLHandler contains an array of XMLHandlers
function CompositeXMLHandler(xmlHandlerArray){
  this.xmlHandlerArray = xmlHandlerArray;
}
CompositeXMLHandler.prototype = new XMLHandler();
CompositeXMLHandler.prototype.handleXML = function(xmlDocument) {
  for (var i=0; i<this.xmlHandlerArray.length; i++){
    var xmlHandler = this.xmlHandlerArray[i];
    xmlHandler.handleXML(xmlDocument);
  }
}
CompositeXMLHandler.prototype.clear = function() {
  for (var i=0; i<this.xmlHandlerArray.length; i++){
    var xmlHandler = this.xmlHandlerArray[i];
    if (xmlHandler.clear) {
    	xmlHandler.clear();
    }
  }
}

function MapUpdater(gmap, markerFuture){
  this.gmap = gmap;
  this.markerFuture = markerFuture;
}
MapUpdater.prototype = new XMLHandler();
MapUpdater.prototype.clear = function() {
	this.gmap.clearOverlays();
}
MapUpdater.prototype.handleXML = function(xmlDocument) {

	this.markerFuture.setXmlDocument(xmlDocument);
	
//  var countyElement = xmlDocument.getElementsByTagName('County')[0];
//  if (countyElement){
//
//    var nameElement = countyElement.getElementsByTagName('Name')[0];
//    var plotCountElement = countyElement.getElementsByTagName('NumberOfPlots')[0];
//    var urlElement = countyElement.getElementsByTagName('URL')[0];
//
//    var name = nameElement.firstChild.data;
//    var count = plotCountElement.firstChild.data;
//    var url = urlElement.firstChild.data;
//
//    var markerText = name+"<br><a href=\"" + url + "\"> View all reports</a><br>There are <b>" + count + " Individual Plot<br/>Reports</b> in this area.";
//    this.markerFuture.setCount(count);
//    this.markerFuture.setBubbleText(markerText);
//  } else {
//    this.markerFuture.invalidate();
//    this.markerFuture.setCount(0);
//   this.markerFuture.setBubbleText("dummyText");
//  }
}

//Makes call to Google to Geocode zipcode returned in XML
function MapGeocoder(geocoder, geocodeCallbackHandler){
  this.geocoder = geocoder;
  this.geocodeCallbackHandler = geocodeCallbackHandler;
}
MapGeocoder.prototype = new XMLHandler();
MapGeocoder.prototype.clear = function() {
}
MapGeocoder.prototype.handleXML = function(xmlDocument) {
  var zipCodeElement = xmlDocument.getElementsByTagName('ZipCodeToMap')[0];
  if (zipCodeElement){
    var zipCode = zipCodeElement.firstChild.data;
    var callbackHandler = this.geocodeCallbackHandler;
    this.geocoder.getLatLng(zipCode, function(point){callbackHandler.callback(point);});
  }
}

//Makes call to Google to Geocode zipcode returned in XML
function UserInputUpdaterXMLHandler(cropOption, zipCodeTextfield){
  this.cropOption = cropOption;
  this.zipCodeTextfield = zipCodeTextfield;
}
UserInputUpdaterXMLHandler.prototype = new XMLHandler();
UserInputUpdaterXMLHandler.prototype.clear = function() {
}
UserInputUpdaterXMLHandler.prototype.handleXML = function(xmlDocument) {
  var zipCodeElement = xmlDocument.getElementsByTagName('ZipCode')[0];
  var cropElement = xmlDocument.getElementsByTagName('Crop')[0];
  if (zipCodeElement && cropElement){

    var zip = zipCodeElement.firstChild.data;
    var crop = cropElement.firstChild.data;

    this.zipCodeTextfield.value = zip;

    var options = this.cropOption.options;
    for (var i=0; i<options.length; i++){
      if (options[i].value == crop){
        options[i].selected = true;
      }
	  }
  }
}

//Shows and Hides various DIVs based on whether or not the user has Crop and Zip Code cookies set
function ShowOrHideDivXMLHandler(yieldDataWrapperDiv, yieldDataZipWrapperDiv, yieldDataContentDiv, yieldDataContentUnknownDiv){
  this.yieldDataWrapperDiv = yieldDataWrapperDiv;
  this.yieldDataZipWrapperDiv = yieldDataZipWrapperDiv;
  this.yieldDataContentDiv = yieldDataContentDiv;
  this.yieldDataContentUnknownDiv = yieldDataContentUnknownDiv;
}
ShowOrHideDivXMLHandler.prototype = new XMLHandler();
ShowOrHideDivXMLHandler.prototype.clear = function() {
}
ShowOrHideDivXMLHandler.prototype.handleXML = function(xmlDocument) {
  if (this.areCookiesPresent(xmlDocument)){
    this.handleCookiesPresent();
  } else {
    this.handleCookiesNotPresent();
  }
}
ShowOrHideDivXMLHandler.prototype.handleCookiesNotPresent = function() {
  this.yieldDataWrapperDiv.id = "yield_data_unknown_wrapper";
  this.yieldDataZipWrapperDiv.style.display = "none";
  this.yieldDataContentDiv.style.display = "none";
  this.yieldDataContentUnknownDiv.style.display = "block";
}
ShowOrHideDivXMLHandler.prototype.handleCookiesPresent = function() {
  this.yieldDataWrapperDiv.id = "yield_data_wrapper";
  this.yieldDataZipWrapperDiv.style.display = "block";
  this.yieldDataContentDiv.style.display = "block";
  this.yieldDataContentUnknownDiv.style.display = "none";
}
ShowOrHideDivXMLHandler.prototype.areCookiesPresent = function(xmlDocument) {
  var zipCodeElement = xmlDocument.getElementsByTagName('ZipCode')[0];
  var cropElement = xmlDocument.getElementsByTagName('Crop')[0];
  if (zipCodeElement && cropElement){
    return true;
  }
  return false;
}

function SkinUpdater(skin, favicon) {
	this.skin = skin;
	this.favicon = favicon;
}
SkinUpdater.prototype = new XMLHandler();
SkinUpdater.prototype.clear = function() {
}
SkinUpdater.prototype.handleXML = function(xmlDocument) {
	var crop = "corn";
	
	var cropElement = xmlDocument.getElementsByTagName('CropDisplay')[0];
	if (cropElement != null) {
		crop = cropElement.firstChild.data;
	}
  
  	if (crop.toLowerCase() == "corn") {
  		if (this.skin != null) {
	  		this.skin.href=CSS_PATH+"skin_dekalb.css";
  		}
  		if (this.favicon != null) {
  			this.favicon.href=IMAGE_PATH+"favicon_dekalb.ico";
  		}
 	}
  	else
  	if (crop.toLowerCase() == "soybean") {
  		if (this.skin != null) {
	  		this.skin.href=CSS_PATH+"skin_asgrow.css";
  		}
  		if (this.favicon != null) {
  			this.favicon.href=IMAGE_PATH+"favicon_asgrow.ico";
  		}
 	}
  	else
  	if (crop.toLowerCase() == "silage") {
  		if (this.skin != null) {
	  		this.skin.href=CSS_PATH+"skin_dekalb.css";
  		}
  		if (this.favicon != null) {
  			this.favicon.href=IMAGE_PATH+"favicon_dekalb.ico";
  		}
  	}
  	else {
  		if (this.skin != null) {
	  		this.skin.href=CSS_PATH+"skin_specialty.css";
  		}
  		if (this.favicon != null) {
  			this.favicon.href=IMAGE_PATH+"favicon_dekalb.ico";
  		}
  	}
}

//Updates the info pane to the right of the Google Map
function InfoPaneUpdater(elementToUpdate){
  this.elementToUpdate = elementToUpdate;
}
InfoPaneUpdater.prototype = new XMLHandler();
InfoPaneUpdater.prototype.clear= function() {
	var html = "";
	//html=html+"<h3>Searching....</h3>";
	//html=html+"<p>Please wait while the search is being performed.</p>";
	this.elementToUpdate.innerHTML = html;
}
InfoPaneUpdater.prototype.handleXML = function(xmlDocument) {

  var registeredTrademark = "<sup>&#174;</sup>";
  
  // Try to get the zip code
  var zipCode = null;
  var zipCodeElement = xmlDocument.getElementsByTagName('ZipCode')[0];  
  if (zipCodeElement == null) {
  	return;
  } else {
  	zipCode = zipCodeElement.firstChild.data;
  }
  
  // Try to get the crop
  var crop = null;
  var cropElement = xmlDocument.getElementsByTagName('CropDisplay')[0];
  if (cropElement == null) {
  	return;
  } else {
    crop = cropElement.firstChild.data;
  }
  
  // Try to get the brand  
  var brand = "";
  if (crop.toLowerCase() == "soybean") {
  	brand = "Asgrow" + registeredTrademark;
  }
  else
  if (crop.toLowerCase() == "silage") {
  	brand = "DEKALB" + registeredTrademark;
  }
  else
  if (crop.toLowerCase() == "sorghum") {
  	brand = "Asgrow" + registeredTrademark + " and DEKALB" + registeredTrademark;
  }
  else {
  	brand = "DEKALB" + registeredTrademark;
  }

  var html="<h3>Local "+brand+" "+crop+" Yield Data</h3>";


  var countyArray = xmlDocument.getElementsByTagName('County');
  if (countyArray.length <= 0)
  {
  	//html=html + "<p>There are <strong> 0 Individual Plot Reports</strong> available near <strong>"+zipCode+"</strong>.";
    html=html + "<p>There are no plots in this area.";
  }
  else
  {
    var totalNumberOfPlots = 0;
	for (var i=0; i<countyArray.length; i++)
	{
	      var countyElement = countyArray.item(i);
	      var numberElement = countyElement.getElementsByTagName('NumberOfPlots')[0];
	      if (numberElement.firstChild){
	        var anInt = parseInt(numberElement.firstChild.data);
	        if (anInt != Math.NaN) {
	        	totalNumberOfPlots += anInt;
	        }
	      }
	}
	var firstCountyUrl = "";
	{
		var countyElement = countyArray.item(0);
		var urlElement = countyElement.getElementsByTagName('URL')[0];
		if (urlElement){
	        firstCountyUrl = urlElement.firstChild.data;
	      }
	}
	html=html + "<p>There ";
	html=html + ((totalNumberOfPlots > 1) ? "are" : "is");
	html=html + " <strong>";
	html=html + totalNumberOfPlots;
	html=html + " Individual Plot Report";
	html=html + ((totalNumberOfPlots > 1) ? "s" : "");
	html=html + "</strong> available near <strong>";
	html=html + zipCode;
	html=html + "</strong>.  <a href=\"";
	html=html + firstCountyUrl;
	html=html + "\" class=\"arrow\">View <strong>all reports</strong> for this area</a></p>";


    html=html + "<div id=\"featured\">Featured Individual Plot Reports near you:</div>";
    html=html + "<div class=\"clear\"></div>";
    var featuredGrowers = xmlDocument.getElementsByTagName('FeaturedGrowers')[0];
    var GrowerArray = null;
    if (featuredGrowers != null) {
        GrowerArray = featuredGrowers.getElementsByTagName('Grower');
    }
    html=html+"<ul>";
    var featuredGrowersWithLatLong = 0;
    for (var i=0; GrowerArray != null && i<GrowerArray.length && i<3; i++)
    {
      var GrowerElement = GrowerArray.item(i);
      var cityElement = GrowerElement.getElementsByTagName('CityDescription')[0];
      var coopElement = GrowerElement.getElementsByTagName('Cooperator')[0];
      var numPlotsElement = GrowerElement.getElementsByTagName('NumberOfPlots')[0];
      var numOfPlotsWithLatLongElement = GrowerElement.getElementsByTagName('NumberOfPlotsWithLatLong')[0];      
      var urlElement = GrowerElement.getElementsByTagName('URL')[0];
      var cityDesc = "";
      var cooperator = "";
      var numberOfPlots = "";
      var numberOfPlotsWithLatLong = 0;
      var url = "";
      if (cityElement.firstChild){
        cityDesc = cityElement.firstChild.data;
      }
      if (coopElement.firstChild){
        cooperator = coopElement.firstChild.data;
      }
      if (numPlotsElement.firstChild){
        numberOfPlots = numPlotsElement.firstChild.data;
      }
      if (numOfPlotsWithLatLongElement.firstChild){
        numberOfPlotsWithLatLong = 
       		parseInt(numOfPlotsWithLatLongElement.firstChild.data);
       	if (isNaN(numberOfPlotsWithLatLong)) {
       		numberOfPlotsWithLatLong = 0;
       	}
      }
      if (urlElement){
        url = urlElement.firstChild.data;
      }
      
      var id = "";
      if (numberOfPlotsWithLatLong > 0) {      	
      	featuredGrowersWithLatLong++;
      	//alert("numberOfPlotsWithLatLong: " + numberOfPlotsWithLatLong + "\n" + "featuredGrowersWithLatLong: " + featuredGrowersWithLatLong);
      	id = getFeaturedId(featuredGrowersWithLatLong);      	
      } 
            
	  html=html + "<li id=\""+id+"\"><strong>" + cooperator + "</strong> (" + numberOfPlots + " plot"+((numberOfPlots > 1) ? "s" : "")+") - ";
      html=html + "<a href=\""+ url +"\">View Report"+((numberOfPlots > 1) ? "s" : "")+"</a><br /> ";
      html=html + cityDesc + "</li>";
    }
    html=html+"</ul>";


    html=html + "<div id=\"additional_reports\">";
    html=html + "<div id=\"ap_column-1\"><ul>";

    var countiesIndex=0;
    if (countyArray.length > 1){

      for (; countiesIndex<countyArray.length && countiesIndex<=4; countiesIndex++){

        if (countiesIndex==4){
          //end first column, start second column
          html=html + "</ul></div><div id=\"ap_column-2\"><ul>";
        }

        var countyElement = countyArray.item(countiesIndex);
        var nameElement = countyElement.getElementsByTagName('Name')[0];
        var stateElement = countyElement.getElementsByTagName('StateName')[0];
        var numberElement = countyElement.getElementsByTagName('NumberOfPlots')[0];
        var urlElement = countyElement.getElementsByTagName('URL')[0];
        var name = "";
        var state = "";
        var numberOfPlots = "";
        var url = "";
        if (nameElement.firstChild){
          name = nameElement.firstChild.data;
        }
        if (stateElement.firstChild){
          state = stateElement.firstChild.data;
        }
        if (numberElement.firstChild){
          numberOfPlots = numberElement.firstChild.data;
        }
        if (urlElement){
          url = urlElement.firstChild.data;
        }
        html=html + "<li><a href=\""+url+"\">"+name+"</a> ("+numberOfPlots+" plot"+((numberOfPlots > 1) ? "s" : "")+")</li>";
      }
    }

    var allCountiesURLElement = xmlDocument.getElementsByTagName('AllCountiesURL')[0];
    if (allCountiesURLElement.firstChild){
      if (countiesIndex==4){
        //end first column, start second column
        html=html + "</ul></div><div id=\"ap_column-2\"><ul>";
      }
      var allCountiesURL = allCountiesURLElement.firstChild.data;
      html=html + "<li><a href=\""+allCountiesURL+"\">View <strong>all reports</strong> for this area</a></li>";
    }

    html=html + "</ul></div>";//end col 1 or 2
    html=html + "</div>"; //end additional_reports
  }
  this.elementToUpdate.innerHTML = html;
}
function getFeaturedId(idx) {
	var id = "";
	if (idx == 1) {
        id = "a";
    }
    else
    if (idx == 2) {
        id = "b";
    }
    else
    if (idx == 3) {
        id = "c";
    }
    else {
    	id = "";
	}
	
    return id;
}
function getFeaturedMarkerImage(idx) {
    var image = IMAGE_PATH;
    
    if (idx == 0) {
        image=image+MARKER_A;
    }
    else
    if (idx == 1) {
        image=image+MARKER_B;
    }
    else
    if (idx == 2) {
        image=image+MARKER_C;
    }
    else {
    	image=image+MARKER_BLANK;
	}
	
    return image;
}
function submitenter(myfield,e,buttonId)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13) {
        $(buttonId).click();
        return false;
    }
    else {
       return true;
    }
}