function processResults(response, map){
	map.removeAllShapes();
	
	response = decompress(response);
	
	var maxLat = -95, minLat = 95;
	var maxLng = -190, minLng = 190;

	if(response.origin){
		var circle = new MQA.CircleOverlay();
		circle.fillColor = "#e0e0e0";
		circle.fillColorAlpha = .4;
		circle.setShapePoints([response.origin.latLng.lat, response.origin.latLng.lng]);
		circle.radiusUnits = response.options.units;
		circle.radius = response.options.radius;
		map.addShape(circle);
		
		var originPOI = new MQA.Poi(response.origin.latLng);	
		originPOI.setInfoTitleHTML('Center Point of the Radius');
		originPOI.setInfoContentHTML('Center Point of the Radius');
		map.addShape(originPOI);
	}
	
	if(response.boundingBox){
		var ulLat = response.boundingBox.ul.lat;
		var ulLng = response.boundingBox.ul.lng;
		var lrLat = response.boundingBox.lr.lat;
		var lrLng = response.boundingBox.lr.lng;
		
		var rectangle = new MQA.RectangleOverlay();	
		rectangle.fillColor = "#e0e0e0";
		rectangle.fillColorAlpha = .4;
		rectangle.setShapePoints([ulLat, ulLng, lrLat, lrLng]);
		map.addShape(rectangle);
	}
	
	if(response.polygon){
		var poly = new MQA.PolygonOverlay();
		poly.fillColor = "#e0e0e0";
		poly.fillColorAlpha = .4;
		poly.setShapePoints(response.polygon);
		map.addShape(poly);
	}
	
	if(response.line){
		var line = new MQA.LineOverlay();	
		line.fillColor = "#e0e0e0";
		line.fillColorAlpha = .4;
		line.setShapePoints(response.line);
		map.addShape(line);
	}


	if(response && response.info && parseInt(response.info.statuscode)==0 && response.searchResults){
		for(var i=0; i < response.searchResults.length; i++){
			if(response.searchResults[i]){
				var searchResult = response.searchResults[i];
				if(searchResult.shapePoints){
					for(var j=0; j < searchResult.shapePoints.length; j+=2){
						if(searchResult.shapePoints[j] > maxLat)
							maxLat = searchResult.shapePoints[j];
						if(searchResult.shapePoints[j] < minLat)
							minLat = searchResult.shapePoints[j];
						if(searchResult.shapePoints[j+1] > maxLng)
							maxLng = searchResult.shapePoints[j+1];
						if(searchResult.shapePoints[j+1] < minLng)
							minLng = searchResult.shapePoints[j+1];
					}
				}
				var shape = this.constructSearchResult(searchResult);
				if(shape) map.addShape(shape);
			}
		}
		
		var rectLL = new MQA.RectLL(new MQA.LatLng(maxLat, minLng), new MQA.LatLng(minLat, maxLng));
		
		map.zoomToRect(rectLL);
	}
};

function processResultsWithNoShape(response, map){
	map.removeAllShapes();
	
	response = decompress(response);

	var maxLat = -95, minLat = 95;
	var maxLng = -190, minLng = 190;

	if(response && response.info && parseInt(response.info.statuscode)==0 && response.searchResults){
		for(var i=0; i < response.searchResults.length; i++){
			if(response.searchResults[i]){
				var searchResult = response.searchResults[i];
				if(searchResult.shapePoints){
					for(var j=0; j < searchResult.shapePoints.length; j+=2){
						if(searchResult.shapePoints[j] > maxLat)
							maxLat = searchResult.shapePoints[j];
						if(searchResult.shapePoints[j] < minLat)
							minLat = searchResult.shapePoints[j];
						if(searchResult.shapePoints[j+1] > maxLng)
							maxLng = searchResult.shapePoints[j+1];
						if(searchResult.shapePoints[j+1] < minLng)
							minLng = searchResult.shapePoints[j+1];
					}
				}
				var shape = this.constructSearchResult(searchResult);
				if(shape) map.addShape(shape);
			}
		}
	}
};

function constructSearchResult(searchResult){
	if(searchResult.shapePoints){
		
	
		if(searchResult.shapePoints.length == 2){
			var p = new MQA.Poi({lat:searchResult.shapePoints[0], lng:searchResult.shapePoints[1]});
			p.setRolloverContent(searchResult.name);
			p.setInfoTitleHTML(p.getRolloverContent());
			
			if(searchResult.poiImageUrl){
				var img = new Image();
				img.onload = function(){
					var icon = new MQA.Icon(this.src, this.height, this.width);
					p.setIcon(icon);
				}
				img.src = searchResult.poiImageUrl;

			}

			if(searchResult.fields){				
				var content = "";
				content += "<nobr>Address: " + searchResult.fields['Address'] + "</nobr><br/>";
				content += "<nobr>City: " + searchResult.fields['City'] + "</nobr><br/>";
				content += "<nobr>State: " + searchResult.fields['State'] + "</nobr><br/>";
				content += "<nobr>ZIP: " + searchResult.fields['ZIP'] + "</nobr><br/>";
				content += "<nobr>Country: " + searchResult.fields['Country'] + "</nobr><br/>";
				content += "<nobr>Phone: " + searchResult.fields['Phone'] + "</nobr><br/>";
				p.setInfoContentHTML(content);
			}
			else {
				p.setInfoContentHTML(' ');
			}
			
			return p;
		}
		else if(searchResult.shapePoints.length > 2){
			if(searchResult.shapePoints[0]  == searchResult.shapePoints[searchResult.shapePoints.length-2]
				&& searchResult.shapePoints[1]  == searchResult.shapePoints[searchResult.shapePoints.length-1]){
				var polygon = new MQA.PolygonOverlay();
				polygon.shapePoints = searchResult.shapePoints;
				polygon.color = "#e00000";
				polygon.colorAlpha = .8;
				polygon.fillColorAlpha = .4;
				polygon.fillColor = "#e00000";
				return polygon;
			}
			else {
				var line = new MQA.LineOverlay();
				line.shapePoints = searchResult.shapePoints;
				line.color = "#00e000";
				return line;
			}
		}
	}
	return null;
};


function decompress(results) {
	if(!results)
		return results;
		
	else if(!results.options)
		return results;
		
	else if(!results.options.shapeFormat)
		return results;	
	
    else if(!(results.options.shapeFormat=='cmp' || results.options.shapeFormat=='cmp6'))
		return results;
		
	var encoded;	
	if(results.polygon){	
		encoded = results.polygon;		
		results.polygon = decompressPoint(encoded, results.options.shapeFormat);
	}
	
	if(results.line){	
		encoded = results.line;		
		results.line = decompressPoint(encoded, results.options.shapeFormat);
	}
		
	if(results.searchResults){
		for(var i = 0; i < results.searchResults.length; i++){		
			encoded = results.searchResults[i].shapePoints;
			if(encoded){
				results.searchResults[i].shapePoints = decompressPoint(encoded, results.options.shapeFormat);
			}
		}
	}	

    return results;
};
  
function decompressPoint(encoded, shapeFormat) {

      var len = encoded.length, index=0,lat=0,lng = 0, array = [];
      try{
        while (index < len) {
          var b, shift = 0, result = 0;
          do {
            b = encoded.charCodeAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
          } while (b >= 0x20);
          var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
          
          lat += dlat;
          shift = 0;
          result = 0;
          do {
            b = encoded.charCodeAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
          } while (b >= 0x20);
            var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
  
          lng += dlng;
          if(shapeFormat=='cmp'){
            array.push(lat * 1e-5);
            array.push(lng * 1e-5);
          }else{
            array.push(lat * 1e-6);
            array.push(lng * 1e-6);
          }
        }
      } catch(ex) {
		alert(ex);
	  
      //error in encoding.
      }
	  
    return array;
};
