
function updateMapControls(response)
{
  try
  {
	if(response.value[0])
	{		
		imgMap = new Image();
		imgMap.src = response.value[0].toLocaleString();
		imgMap.onload = function() { document.getElementById(MAPCONTROL + "_map").src = imgMap.src; updateMapState(response.value[8]); LayerShow(gMC.mMapImageName); ChangeToMain(); };
		
		imgScale = new Image();
		imgScale.src = response.value[1].toLocaleString();
		imgScale.onload = function() { document.getElementById(MAPCONTROL + "_scalebar").src = imgScale.src; };			

		imgLegend = new Image();
		imgLegend.src = response.value[2].toLocaleString();
		imgLegend.onload = function() { document.getElementById("img_legend").src = imgLegend.src; };

		imgReference = new Image();
		imgReference.src = response.value[3].toLocaleString();
		imgReference.onload = function() { document.getElementById("img_reference").src = imgReference.src; };
		
		document.getElementById(MAPCONTROL + "_txtHiddenRange").value = response.value[4] + "|" + response.value[5] + "|" + response.value[6] + "|" + response.value[7];
		document.getElementById(MAPCONTROL + "_txtHiddenScale").value = response.value[8];
		
		document.getElementById(MAPCONTROL + "_map_state").value = response.value[9];						
	} else {
		ChangeToMain();
	}
  }
  catch(er)
  {
	ChangeToMain();
  }		
}

function updateMapState(scale)
{
	MapRange=DoGetMapRange(); 

	DoShowMapScale(scale);		
		
	MapRange=DoGetMapRange();
	gMC.SetMapRange(MapRange);	
}

function zoomExtent(left, top, right, bottom)
{		
	ChangeToLoading();
	
	var mapState = "";
	
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
			
	AjaxMap.MapRequests.zoomExtent(Math.min(left,right), Math.min(top,bottom), Math.max(left,right), Math.max(top,bottom), mapState, zoomExtent_CallBack);
}

function zoomExtent_CallBack(response)
{	
	if (response == null)
	{
		ChangeToMain();	
		
		return;		
	}
	
	if (response.error != null)
	{
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null)
	{	
		ChangeToMain();	
		
		return;
	}	
	
	updateMapControls(response);		
}

function zoomScale(scale)
{		
	ChangeToLoading();
	
	var mapState = "";
		
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
			
	AjaxMap.MapRequests.zoomScale(scale, mapState, zoomScale_CallBack);
}
function zoomScale_CallBack(response)
{	
	if (response == null)
	{
		ChangeToMain();	
		
		return;		
	}
	
	if (response.error != null)
	{
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null)
	{	
		ChangeToMain();	
		
		return;
	}
	
	updateMapControls(response);
}

function zoomReference(imgX, imgY)
{		
	ChangeToLoading();
	
	var mapState = "";
		
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
			
	AjaxMap.MapRequests.zoomReference(imgX, imgY, mapState, zoomReference_CallBack);
}
function zoomReference_CallBack(response)
{	
	if (response == null)
	{
		ChangeToMain();	
		
		return;		
	}
	
	if (response.error != null)
	{
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null)
	{	
		ChangeToMain();	
		
		return;
	}	
			
	updateMapControls(response);	
}

function queryLayersByPoint(x, y)
{
	ChangeToLoading();
	
	var mapState = "";
		
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
	
	var layersCodeList = "";
				
	for (i=0; i < themeButtons.length; i++)
	{
		var button = themeButtons[i];
						
		if (button.mySelected && button.myQuery)
		{
			layersCodeList += button.myLayerCode + " ";
		}
	}
	
	if (layersCodeList=="")
	{
		alert('Active um ou mais temas da lista!');
		
		ChangeToMain();
	} else {			
		DoSearchPOIByPoint(x, y, SEARCH_BUFFER, layersCodeList," ", 1,"pnl_Lista");
	}
}
//function queryLayersByPoint_CallBack(response)
//{	
//	ChangeToMain();
//	
//	if (response == null)
//	{		
//		return;		
//	}
//	
//	if (response.error != null)
//	{			
//		return;
//	}	
//			
//	var idList="";
//	
//	if (response.value != null)
//	{
//		updateMapControls(response);
//					
//		idList=response.value[10];
//						
//		map_queryLayerByPoint(idList);		
//	}	
//}



function queryLayersByPolygon(polyCoords)
{
	ChangeToLoading();
	
	var mapState = "";
		
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
	
	var layersCodeList = "";
				
	for (i=0; i < themeButtons.length; i++)
	{
		var button = themeButtons[i];
						
		if (button.mySelected && button.myQuery)
		{
			layersCodeList += button.myLayerCode + " ";
		}
	}
		
	if (layersCodeList=="")
	{
		alert('Active um ou mais temas da lista!');
		
		ChangeToMain();
	} else {			
	    DoSearchPOIByPolygon(polyCoords, layersCodeList, " ", 1, true, "pnl_Lista");
		//AjaxMap.MapRequests.queryLayersByPolygon(polyCoords, layersList, mapState, queryLayersByPolygon_CallBack);
	}
}
//function queryLayersByPolygon_CallBack(response)
//{	
//	ChangeToMain();
//	
//	if (response == null)
//	{		
//		return;		
//	}
//	
//	if (response.error != null)
//	{			
//		return;
//	}	
//			
//	var idList="";
//	
//	if (response.value != null)
//	{
//		updateMapControls(response);
//					
//		idList=response.value[10];
//						
//		map_queryLayerByPoint(idList);		
//	}	
//}



/*
function queryLayersByPolygon(polyCoords)
{
	ChangeToLoading();
	
	var mapState = "";
		
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
	
	var layersList = "";
				
	for (i=0; i < themeButtons.length; i++)
	{
		var button = themeButtons[i];
						
		if (button.mySelected && button.myQuery)
		{
			layersList += button.myLayerName + " ";
		}
	}
	
	if (layersList=="")
	{
		alert('Active um ou mais temas da lista!');
		
		ChangeToMain();
	} else {			
		AjaxMap.MapRequests.queryLayersByPolygon(polyCoords, layersList, mapState, queryLayersByPolygon_CallBack);
	}
}
function queryLayersByPolygon_CallBack(response)
{	
	if (response == null)
	{
		ChangeToMain();	
		
		return;		
	}
	
	if (response.error != null)
	{
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null)
	{	
		ChangeToMain();	
		
		return;
	}	
		
			
	var idList="";
	
	if (response.value != null)
	{			
		updateMapControls(response);
					
		idList=response.value[10];
												
		map_queryLayerByPoint(idList);
	}		
}
*/

function SetLayerVisibility(layerName, visible)
{
	ChangeToLoading();
		
	var mapState = "";
	
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
		
	AjaxMap.MapRequests.SetLayerVisibility(layerName, visible, mapState,  SetLayerVisibility_CallBack);
}
function SetLayerVisibility_CallBack(response)
{
	if (response == null)
	{
		ChangeToMain();	
		
		return;		
	}
	
	if (response.error != null)
	{
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null)
	{	
		ChangeToMain();	
		
		return;
	}	
	
	updateMapControls(response);			
}

function SetLayersListVisibility(layersList, visible)
{
	ChangeToLoading();
		
	var mapState = "";
		
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
		
	AjaxMap.MapRequests.SetLayersListVisibility(layersList, visible, mapState,  SetLayersListVisibility_CallBack);
}
function SetLayersListVisibility_CallBack(response)
{
	if (response == null)
	{
		ChangeToMain();	
		
		return;		
	}
	
	if (response.error != null)
	{
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null)
	{	
		ChangeToMain();	
		
		return;
	}	
	
	updateMapControls(response);			
}

function SwitchLayersListVisibility(layersListOn, layersListOff)
{
	ChangeToLoading();
		
	var mapState = "";
	
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
		
	AjaxMap.MapRequests.SwitchLayersListVisibility(layersListOn, layersListOff, mapState,  SwitchLayersListVisibility_CallBack);
}
function SwitchLayersListVisibility_CallBack(response)
{			
	if (response == null)
	{
		ChangeToMain();	
		
		return;		
	}
	
	if (response.error != null)
	{
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null)
	{	
		ChangeToMain();	
		
		return;
	}	
			
	updateMapControls(response);
}

function clearFeatures()
{
	ChangeToLoading();
		
	var mapState = "";
		
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
		
	AjaxMap.MapRequests.clearFeatures(mapState, clearFeatures_CallBack);
}
function clearFeatures_CallBack(response)
{
	if (response == null)
	{
		ChangeToMain();	
		
		return;		
	}
	
	if (response.error != null)
	{
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null)
	{	
		ChangeToMain();	
		
		return;
	}		
			
	updateMapControls(response);
}

function addFeature(feature, clear, extent)
{
	ChangeToLoading();
		
	var mapState = "";
		
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
	
	AjaxMap.MapRequests.addFeature("SELECTION_POINT", feature, clear, extent, mapState, addFeature_CallBack);
}
function addFeature_CallBack(response)
{
	if (response == null)
	{
		ChangeToMain();	
		
		return;		
	}
	
	if (response.error != null)
	{
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null)
	{	
		ChangeToMain();	
		
		return;
	}		
			
	updateMapControls(response);
}


function DoZoomLoc(x, y, draw, buffer)
{
	if (gMC.IsBusy)
	{
		return false;
	}

	ChangeToLoading();
		
	var mapState = "";
	
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
	
	var MapCenteredRange = new Array();
	
	MapCenteredRange[0] = x - buffer;
	MapCenteredRange[1] = y - buffer;
	MapCenteredRange[2] = x + buffer;
	MapCenteredRange[3] = y + buffer;
	
	var extent = MapCenteredRange[0] + ";" + MapCenteredRange[1] + ";" + MapCenteredRange[2] + ";" + MapCenteredRange[3];
	
	var geom = x + ";" + y;	
	var feature = geom + "|";
	
    if (draw)
	{
	    AjaxMap.MapRequests.addFeature("SELECTION_POINT", feature, true, extent, mapState, DoZoomLoc_CallBack);
	} else {
	    AjaxMap.MapRequests.zoomExtent(MapCenteredRange[0], MapCenteredRange[1], MapCenteredRange[2], MapCenteredRange[3], mapState, DoZoomLoc_CallBack);
	}
}
function DoZoomLoc_CallBack(response)
{
	if (response == null){		
		ChangeToMain();	
		
		return;
	}

	if (response.error != null){
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null){		
		ChangeToMain();	
		
		return;
	}	
	
	updateMapControls(response);
}

function DoZoomStreet(id)
{
	if (gMC.IsBusy)
	{
		return false;
	}

	ChangeToLoading();
		
	var mapState = "";
	
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
		
	AjaxMap.MapRequests.DoSelectStreetLine(id,true,ZOOM_BUFFER_STREET, mapState, DoZoomStreet_CallBack);
		
	//AjaxMap.MapRequests.selectByAttribute("vias", "id", id, true, ZOOM_BUFFER, mapState,  DoZoomStreet_CallBack);
}
function DoZoomStreet_CallBack(response)
{
	if (response == null)
	{
		ChangeToMain();	
		
		return;		
	}
	
	if (response.error != null)
	{
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null)
	{	
		ChangeToMain();	
		
		return;
	}		
	
	updateMapControls(response);
}

function DoZoomStreetLines(listID)
{
	if (gMC.IsBusy)
	{
		return false;
	}

	ChangeToLoading();
		
	var mapState = "";
	
	if( document.getElementById(MAPCONTROL + "_map_state") != null)
		mapState = document.getElementById(MAPCONTROL + "_map_state").value;
		
	AjaxMap.MapRequests.DoSelectStreetLines(listID,true,ZOOM_BUFFER_STREET, mapState, DoZoomStreetLines_CallBack);
		
	//AjaxMap.MapRequests.selectByAttribute("vias", "id", id, true, ZOOM_BUFFER, mapState,  DoZoomStreet_CallBack);
}
function DoZoomStreetLines_CallBack(response)
{
	if (response == null)
	{
		ChangeToMain();	
		
		return;		
	}
	
	if (response.error != null)
	{
		alert(response.error);
		
		ChangeToMain();	
		
		return;
	}	
	
	if (response.value == null)
	{	
		ChangeToMain();	
		
		return;
	}		
	
	updateMapControls(response);
}


function map_queryLayerByPoint(idList)
{
	MapaInteractivo.forms.searchPanel.DoSearchPOIByIdList(idList, "pnl_Lista", DoSearchPOI_CallBack)
}