// aimsSelect.js
var aimsSelectPresent=false;
var selectData = new Array();
var selectLeft = new Array();
var selectRight = new Array();
var selectTop = new Array();
var selectBottom = new Array();
var selMaxEnvelope = new Array();

var selectLayer="";
var selectType = "";
var selectCount = 0;
var selectBlurb = "";
var selectEnvelope="";
var highlightedOne = "";
// mode - 0=selection; 1=query
var queryMode=1;
// mode - 1=query; 2=box,point; 3=line,polygon
var selectionMode=1;
var setQueryString="";

// start select box display
function startSelectBox(e) {
	if (checkIfActiveLayerAvailable()) {
		moveLayer("theMap",hspc,vspc);
		getImageXY(e);
		// keep it within the MapImage
		if ((mouseX<iWidth) && (mouseY<iHeight)) {
			if (selectBox) {
				stopSelectBox(e);
			} else {
				x1=mouseX;
				y1=mouseY
				x2=x1+1;
				y2=y1+1;
				zleft=x1;
				ztop=y1;
				zbottom=y1;
				zright=x1
				boxIt(x1,y1,x2,y2);
				/*
				clipLayer("zoomBoxTop",x1,y1,x2,y2);
				clipLayer("zoomBoxLeft",x1,y1,x2,y2);
				clipLayer("zoomBoxRight",x1,y1,x2,y2);
				clipLayer("zoomBoxBottom",x1,y1,x2,y2);
				*/
				selectBox=true;

				//if (isNav4) {
					showLayer("zoomBoxTop");
					showLayer("zoomBoxLeft");
					showLayer("zoomBoxRight");
					showLayer("zoomBoxBottom");
				//} else {
				//	showLayer("zoomBox");
				//}
			}
			highlightedOne="";
		}
		return false;
	}

}
// stop select box display..... select
function stopSelectBox(e) {
	selectBox=false;
        //alert("jeps");
        toolMode=4;
	selectBox=false;
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;


	//if (isNav4) {
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
	//} else {
	//	showLayer("zoomBox");
	//}
	// /*
	if ((zright <zleft+2) && (zbottom < ztop+2)) {

		//getMapXY(mouseX-pixelTolerance,mouseY-pixelTolerance);
		getMapXY(mouseX,mouseY);
		searchTolerance = (xDistance/iWidth) * pixelTolerance;
		tempLeft = mapX-searchTolerance;
		tempTop = mapY-searchTolerance;
		tempRight = mapX+searchTolerance;
		tempBottom = mapY+searchTolerance;

		//select(e);
	} else {
	// */
		pixelX = xDistance / iWidth;
		var theY = iHeight - ztop;
		pixelY = yDistance / iHeight;
		tempTop = pixelY * theY + bottom;
		tempRight = pixelX * zright + left;
		tempLeft = pixelX * zleft + left;
		theY = iHeight - zbottom;
		pixelY = yDistance / iHeight;
		tempBottom = pixelY * theY + bottom;
	}
		window.scrollTo(0,0);

	queryStartRecord=1;
	var theString = writeGetFeatures2(tempLeft,tempBottom,tempRight,tempTop);
	selectEnvelope='maxy="' + forceComma(tempTop) + '" maxx="' + forceComma(tempRight) + '" miny="' + forceComma(tempBottom) + '" minx="' + forceComma(tempLeft) + '"';
	drawSelectBoundary=true;
	showBuffer=false;
	selectionMode=2;
      sendToServer(imsQueryURL,theString,identifyXMLMode);
	return true;

}
// write out XML request to select features
function writeGetFeatures2(west1,south1,east1,north1) {
	if (swapSelectFields) {
		selectFields=selFieldList[ActiveLayerIndex];
		//alert(selectFields);
	}
	var theEnvelope = 'maxy="' + forceComma(north1) + '" maxx="' + forceComma(east1) + '" miny="' + forceComma(south1) + '" minx="' + forceComma(west1 )+ '"';
	var theString = writeEnvelopeXML(ActiveLayer,ActiveLayerType,selectFields,maxFeaturesReturned,queryStartRecord,theEnvelope,useLimitExtent);
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;
}
function writeGetFeatures3() {
	if (swapSelectFields) {
		selectFields=selFieldList[ActiveLayerIndex];
	}
	var theString = writeEnvelopeXML(ActiveLayer,ActiveLayerType,selectFields,maxFeaturesReturned,queryStartRecord,selectEnvelope,useLimitExtent);
	//alert(theString);
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	return theString;
}
// generic envelope select xml write routine
function writeEnvelopeXML(theLayer,theLayerType,theFields,maxReturned,startRec,theEnvelope,hasLimit) {
	var theString = '<?xml version="1.0" encoding="UTF-8"?>\n<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false"';
	theString += ' compact="true" featurelimit="' + maxReturned + '" beginrecord="' + startRec + '">\n';
	theString += '<LAYER id="' + theLayer + '" />';
	theString += '<SPATIALQUERY subfields="' + theFields + '">';
	//theString += '<SPATIALFILTER relation=envelope_intersection >';
	theString += '<SPATIALFILTER relation="area_intersection" >';
	theString += '<ENVELOPE ' + theEnvelope + ' />';
	theString += '</SPATIALFILTER>';
	if (hasLimit) {
		// keep this within the limitExtent
		theString += '<SPATIALFILTER relation="area_intersection">\n';
		theString += '<ENVELOPE maxx="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
		theString += '</SPATIALFILTER>\n';
	}
	theString += '</SPATIALQUERY>';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	return theString;

}
// write out xml request for selection by shape
function writeShapeSelect(theType) {
        if (swapSelectFields) {
		selectFields=selFieldList[ActiveLayerIndex];
                //koulutoimen piirto
                //if(identifyXMLMode == 113)
                  //selectFields=selFieldList[ActiveLayerIndex] + drawselectFields;
                //alert(selectFields);
	}
	var theString = '<?xml version="1.0" encoding="UTF-8"?>\n<ARCXML version="1.1">\n<REQUEST>\n<GET_FEATURES outputmode="xml" envelope="true" geometry="false"';
	if(identifyXMLMode == 113){
          theString += ' featurelimit="100000" beginrecord="1" skipfeatures="true">\n';
        }
        else
          theString += ' featurelimit="' + maxFeaturesReturned + '" beginrecord="1">\n';
	theString += '<LAYER id="' + ActiveLayer + '" />';
	if(identifyXMLMode == 113) {
          theString += '<SPATIALQUERY subfields="' + selectFields + '" where="' + sumfields +'">';
	}
        else
          theString += '<SPATIALQUERY subfields="' + selectFields + '">';
        theString += '<SPATIALFILTER relation="area_intersection" >';
	if ((shapeSelectBuffer) && (shapeBufferDistance>0)) {
		// do a buffer around the shape before selecting
		theString += '<BUFFER distance="' + shapeBufferDistance + '"';
		theString += ' bufferunits="METERS"';
		theString += ' />\n';
	}
	if (theType==1) {
		theString += '<POLYLINE>\n<PATH>\n';
	} else if (theType==2) {
		theString += '<POLYGON>\n<RING>\n';
	} else {
		theString += '<MULTIPOINT>\n';
	}
	if (theType==0) {
		theString += '<POINT x="' + clickPointX[clickCount-1] + '" y="' + clickPointY[clickCount-1] + '" />\n';
	} else {
		for (var i=0;i<clickCount;i++) {
			theString += '<POINT x="' + clickPointX[i] + '" y="' + clickPointY[i] + '" />\n';
		}
	}
	if (theType==0) {
		theString += '</MULTIPOINT>\n';
	} else if (theType==2) {
		theString += '</RING>\n</POLYGON>\n';
	} else {
		theString += '</PATH>\n</POLYLINE>\n';
	}
	theString += '</SPATIALFILTER>';
	if (useLimitExtent) {
		// keep this within the limitExtent
		theString += '<SPATIALFILTER relation="area_intersection">\n';
		theString += '<ENVELOPE maxx="' + forceComma(limitRight) + '" maxy="' + forceComma(limitTop) + '" minx="' + forceComma(limitLeft) + '" miny="' + forceComma(limitBottom) + '" />\n';
		theString += '</SPATIALFILTER>\n';
	}
	theString += '</SPATIALQUERY>';
	theString += '</GET_FEATURES>';
	theString += '</REQUEST>';
	theString += '</ARCXML>';
	selectLayer=ActiveLayer;
	selectType=ActiveLayerType;
	selectCount=0;
	hightlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	//alert("Kysely: \n" + theString);
        return theString;
}
// request a selection using a shape
function sendShapeSelect(theType) {
        queryStartRecord=0;
	selectionMode=3;
	showBuffer=false;
	if (theType==2) {
		if ((clickPointX[0]!=clickPointX[clickCount-1]) && (clickPointY[0]!=clickPointY[clickCount-1])) {
			clickPointX[clickCount]=clickPointX[0];
			clickPointY[clickCount]=clickPointY[0];
			clickCount=clickCount+1;
		}
	}
	showRetrieveData();
	var theString = writeShapeSelect(theType);
	sendToServer(imsQueryURL,theString,105);
}
// Koulutoimen piirtämistä varten by Jani 11.8.2003
function sendShapeSelect2(theType) {
	queryStartRecord=1;
	selectionMode=3;
	showBuffer=false;
	if (theType==2) {
		if ((clickPointX[0]!=clickPointX[clickCount-1]) && (clickPointY[0]!=clickPointY[clickCount-1])) {
			clickPointX[clickCount]=clickPointX[0];
			clickPointY[clickCount]=clickPointY[0];
			clickCount=clickCount+1;
		}
	}
	showRetrieveData();
        //alert(theType);
	var theString = writeShapeSelect(theType);
        //alert(selectXMLMode);
	sendToServer(imsQueryURL,theString,113);
}
// request more records to display
function getMoreData(startRecord) {
	var theString = "";
	queryStartRecord = startRecord;
	if (selectionMode==1) {
		//query
		theString = writeQueryXML(setQueryString);
	} else if (selectionMode==2) {
		theString = writeGetFeatures3();
	} else {
		var theType = clickType-1;
		theString = writeShapeSelect(theType);
	}
	showRetrieveData();
	sendToServer(imsQueryURL,theString,6);
}
// refresh map with hightlighted selection
function showHighlight(selNum) {
		highlightedOne = LayerIDField[ActiveLayerIndex] + " = " + selectPoints[selNum];
		//alert(selectLeft[selNum] + "," + selectRight[selNum]);
		var fWidth = selectRight[selNum] - selectLeft[selNum];
		var fHeight = selectTop[selNum] - selectBottom[selNum];
		var mWMargin = 0;
		var mHMargin = 0;
		if (selectType=="point") {
			mWMargin = fullWidth * selectPointMargin;
			mHMargin = fullHeight * selectPointMargin;
			if (mWMargin > xDistance/2) {
				mWMargin = xDistance/2;
				mHMargin = yDistance/2;
			}
		} else {
			mWMargin = fWidth * selectMargin;
			mHMargin = fHeight * selectMargin;
		}
		saveLastExtent();
		left = selectLeft[selNum] - mWMargin;
		right = selectRight[selNum] + mWMargin;
		top = selectTop[selNum] + mHMargin;
		bottom = selectBottom[selNum] - mHMargin;
		//alert(highlightedOne);
			legendTemp=legendVisible;
			legendVisible=false;
			sendMapXML();

}
// calculate max envelope for returned records
function calcSelectEnvelope() {
	if(selectCount>0) {
		selMaxEnvelope[0] = selectLeft[0];
		selMaxEnvelope[1] = selectBottom[0];
		selMaxEnvelope[2] = selectRight[0];
		selMaxEnvelope[3] = selectTop[0];
		//alert("0 - " + selMaxEnvelope[0] + "," + selMaxEnvelope[1] + "," + selMaxEnvelope[2] + "," + selMaxEnvelope[3]);
		if (selectCount>1) {
			for (var i=1;i<selectCount;i++) {
				if (selectLeft[i]<selMaxEnvelope[0]) selMaxEnvelope[0] = selectLeft[i];
				if (selectBottom[i]<selMaxEnvelope[1]) selMaxEnvelope[1] = selectBottom[i];
				if (selectRight[i]>selMaxEnvelope[2]) selMaxEnvelope[2] = selectRight[i];
				if (selectTop[i]>selMaxEnvelope[3]) selMaxEnvelope[3] = selectTop[i];
				//alert(i + " - " + selMaxEnvelope[0] + "," + selMaxEnvelope[1] + "," + selMaxEnvelope[2] + "," + selMaxEnvelope[3]);
			}
		}
	}
}
function zoomToReturnedRecords() {
	calcSelectEnvelope();
	var fWidth = selMaxEnvelope[2] - selMaxEnvelope[0];
	var fHeight = selMaxEnvelope[3] - selMaxEnvelope[1];
	var mWMargin = 0;
	var mHMargin = 0;
	if ((fWidth==0) && (fHeight==0)) {
		mWMargin = fullWidth * selectPointMargin;
		mHMargin = fullHeight * selectPointMargin;
		if (mWMargin > xDistance/2) {
			mWMargin = xDistance/2;
			mHMargin = yDistance/2;
		}
	} else {
		mWMargin = fWidth * selectMargin;
		mHMargin = fHeight * selectMargin;
	}
	saveLastExtent();
	left = selMaxEnvelope[0] - mWMargin;
	right = selMaxEnvelope[2] + mWMargin;
	top = selMaxEnvelope[3] + mHMargin;
	bottom = selMaxEnvelope[1] - mHMargin;


	//alert(mWMargin);
	//alert(mHMargin);
	if(mWMargin < 150 && mHMargin < 150){	
		//alert("zoom");
		zoomToChoiceMeasure(4000, 3000);
	}	
}
function zoomToLayer() {
	zoomToChoiceMeasure(1615, 1210);
}
function zoomToLayerRecords() {
	//calcSelectEnvelope();
	
	var fWidth = selMaxEnvelope[2] - selMaxEnvelope[0];
	var fHeight = selMaxEnvelope[3] - selMaxEnvelope[1];
	var mWMargin = 0;
	var mHMargin = 0;
	if ((fWidth==0) && (fHeight==0)) {
		mWMargin = fullWidth * selectPointMargin;
		mHMargin = fullHeight * selectPointMargin;
		if (mWMargin > xDistance/2) {
			mWMargin = xDistance/2;
			mHMargin = yDistance/2;
		}
	} else {
		mWMargin = fWidth * selectMargin;
		mHMargin = fHeight * selectMargin;
	}
	saveLastExtent();
	left = selMaxEnvelope[0] - mWMargin;
	right = selMaxEnvelope[2] + mWMargin;
	top = selMaxEnvelope[3] + mHMargin;
	bottom = selMaxEnvelope[1] - mHMargin;

	// <100 m
	//alert("jep");
	if(mWMargin < 285 && mHMargin < 190)
		zoomToChoiceMeasure(1615, 1210); // 300 m
}
// Tätä käytetään aloitus_query.jsp käynnistyksessä by Jani 13.11.2002
function zoomToReturnedRecordsQuery(selLeft,selBottom,selRight,selTop) {
	var fWidth = selRight - selLeft;
	var fHeight = selTop - selBottom;

        var mWMargin = 0;
	var mHMargin = 0;
	if ((fWidth==0) && (fHeight==0)) {
		mWMargin = fullWidth * selectPointMarginQuery;
		mHMargin = fullHeight * selectPointMarginQuery;
		if (mWMargin > xDistance/2) {
			mWMargin = xDistance/2;
			mHMargin = yDistance/2;
		}
	} else {
		mWMargin = fWidth * selectMarginQuery;
		mHMargin = fHeight * selectMarginQuery;
	}
	saveLastExtent();
	startLeft = selLeft - mWMargin;
	startRight = selRight + mWMargin;
	startTop = selTop + mHMargin;
	startBottom = selBottom - mHMargin;

}
// clear current selection
function clearSelection() {
	var theCount = selectCount;
	var theHL = highlightedOne;
	selectCount=0;
	showBuffer=false;
	highlightedOne="";
	selectPoints.length=1;
	selectLeft.length=1;
	selectRight.length=1;
	selectTop.length=1;
	selectBottom.length=1;
	drawSelectBoundary=false;
	if (useTextFrame) {
          //alert();
	} else {
		if (toolMode==20) {
			showLayer("measureBox");
			updateMeasureBox()
			alert("Measure Totals Cleared");
		} else {
			alert("Selection Cleared");
		}
	}
	showGeocode=false;
	clickCount=0;
	totalMeasure=0;
	currentMeasure=0;
        legendTemp=legendVisible;
        legendVisible=false;
        selectBlurb="";
        showLayer("LoadMap");
        var theString = writeXML();
        sendToServer(imsURL,theString,203);

}
// add Draw Selected Features to Map XML request
function addSelectToMap(){
	var selString = "";
	if((selectCount>0) && (showSelectedFeatures)) {
		selString +='<LAYER type="FeatureClass" name="Valittu kohde" visible="true">\n';
		
		selString +='<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';

		if (selectionMode==1) {
                  selString +='<SPATIALQUERY where="' + setQueryString + '"';
			if (useLimitExtent) {
				// keep this within the limitExtent

				selString +='>\n<SPATIALFILTER relation="area_intersection">\n';
				selString +='<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
				selString +='</SPATIALFILTER>\n';
				selString +='</SPATIALQUERY>\n';
			} else {
			selString +='/>\n';
			}

		} else {
			if(globalVaesto){
                          selString +='<SPATIALQUERY where="' + sumfields + '">\n';
                          globalVaesto = false;
                        }
                        else {
                          selString +='<SPATIALQUERY>\n';
			}
                        selString +='<SPATIALFILTER relation="area_intersection" >\n';
			if (selectionMode==2) {
				selString +='<ENVELOPE ' + selectEnvelope + ' />\n';
			} else {
				if (clickType==2) {
					selString +='<POLYLINE>\n<PATH>\n';
				} else if (clickType==3) {
					selString +='<POLYGON>\n<RING>\n';
				} else {
					selString +='<MULTIPOINT>\n';
				}
				for (var i=0;i<clickCount;i++) {
					selString +='<POINT x="' + clickPointX[i] + '" y="' + clickPointY[i] + '" />\n';
				}
				if (clickType==3) {
					//selString +='<POINT x="' + clickPointX[0] + '" y="' + clickPointY[0] + '" />\n';
					selString +='</RING>\n</POLYGON>\n';
				} else if (clickType==2) {
					selString +='</PATH>\n</POLYLINE>\n';
				} else {
					selString +='</MULTIPOINT>\n';
				}
			}
			selString +='</SPATIALFILTER>\n';
			if (useLimitExtent) {
				// keep this within the limitExtent
				selString +='<SPATIALFILTER relation="area_intersection">\n';
				selString +='<ENVELOPE maxx="' + limitRight + '" maxy="' + limitTop + '" minx="' + limitLeft + '" miny="' + limitBottom + '" />\n';
				selString +='</SPATIALFILTER>\n';
			}
			selString +='</SPATIALQUERY>\n';
		}

            // Jos haku on osoitehaku niin halutessa näytetään label
		if(NaytaLabel) {

                        selString +='<GROUPRENDERER>\n';
			selString +='<SIMPLERENDERER>\n';

			if (selectType=="point") {
				selString +='<SIMPLEMARKERSYMBOL color="' + selectColor + '" type="' + selectpointType + '" outline="' + selectoutlinecolor + '" width="' + selectpointWidth + '" overlap="true" antialiasing="true" />\n';
			} else if (selectType=="line") {
				selString +='<SIMPLELINESYMBOL type="'+ selectlineType + '" color="' + selectColor + '" width="' + selectlineWidth +'" />\n';
			} else {
				selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + selectpolygonColor + '" filltype="' + selectpolygonfillType + '" transparency="' + transparentLevel + '" boundarycolor="' + selectpolygonboundaryColor + '" />\n';
			}
			selString +='</SIMPLERENDERER>\n';

                        if (selectType=="point") {
				if(yksi_osoitekentta == true)
                                  selString +='<SIMPLELABELRENDERER field="' + osoitekentta_katu_numero + '" labelpriorities="2,2,1,2,2,2,2,2">\n';
				else
                                  selString +='<SIMPLELABELRENDERER field="' + osoitekentta_katu + ' ' + osoitekentta_numero + '" labelpriorities="2,2,1,2,2,2,2,2">\n';
                                selString +='<CALLOUTMARKERSYMBOL font="' + pointlabelfont + '" fontstyle="' + pointlabelfontstyle + '" fontsize="' + pointlabelfontsize + '" fontcolor="' + pointlabelfontcolor + '" antialiasing="true" backcolor="' + pointlabelfontbackground + '" interval="' + pointlabelfontinterval + '" boundarycolor="' + pointlabelfontboundarycolor + '" transparency="' + pointlabelfonttransparency + '"/>\n';
				selString +='</SIMPLELABELRENDERER>\n';
			}

			selString +='</GROUPRENDERER>\n';
			selString +='</LAYER>\n';

		}
		else {
			selString +='<GROUPRENDERER>\n';
			selString +='<SIMPLERENDERER>\n';
			if (selectType=="point") {
				selString +='<SIMPLEMARKERSYMBOL antialiasing="true" color="' + selectColor + '" type="' + selectpointType + '" outline="' + selectoutlinecolor + '" width="' + selectpointWidth + '" overlap="true" />\n';
			} else if (selectType=="line") {
				selString +='<SIMPLELINESYMBOL antialiasing="true" type="'+ selectlineType + '" color="' + selectColor + '" width="' + selectlineWidth +'" />\n';
			} else {
				selString +='<SIMPLEPOLYGONSYMBOL antialiasing="true" fillcolor="' + selectpolygonColor + '" filltype="' + selectpolygonfillType + '" transparency="' + transparentLevel + '" boundarycolor="' + selectpolygonboundaryColor + '" />\n';
			}
			selString +='</SIMPLERENDERER>\n';
			if (selectType=="point") {
				selString +='<SIMPLELABELRENDERER field="C_OSOITE" labelbufferratio ="1.0"  labelpriorities="2,3,1,4,5,6,7,8" howmanylabels="one_label_per_shape" >\n';
				selString +='<CALLOUTMARKERSYMBOL font="' + pointlabelfont + '" fontstyle="' + pointlabelfontstyle + '" fontsize="' + pointlabelfontsize + '" fontcolor="' + pointlabelfontcolor + '" antialiasing="true" backcolor="' + pointlabelfontbackground + '" interval="' + pointlabelfontinterval + '" boundarycolor="' + pointlabelfontboundarycolor + '" transparency="' + pointlabelfonttransparency + '"/>\n';
				selString +='</SIMPLELABELRENDERER>\n';
			}
                         else if (selectType=="line") {
				selString +='<SIMPLELABELRENDERER field="NIMI" labelbufferratio ="1.0"  linelabelposition="PlaceOnTopHorizontal" howmanylabels="one_label_per_shape" >\n';
				selString +='<TEXTSYMBOL antialiasing="true" font="' + pointlabelfont + '" fontstyle="' + pointlabelfontstyle + '" fontsize="' + pointlabelfontsize + '" fontcolor="' + pointlabelfontcolor + '" blockout="' + pointlabelfontbackground + '" interval="' + pointlabelfontinterval + '" transparency="' + pointlabelfonttransparency + '"/>\n';
                                selString +='</SIMPLELABELRENDERER>\n';
			}
                        else {
                                selString +='<SIMPLELABELRENDERER field="' + selectXMLName + '" labelbufferratio ="1.0" howmanylabels="one_label_per_shape" >\n';
				selString +='<TEXTSYMBOL antialiasing="true" font="' + polygonlabelfont + '" fontstyle="' + polygonlabelfontstyle + '" fontsize="' + polygonlabelfontsize + '" fontcolor="' + polygonlabelfontcolor + '" blockout="' + polygonlabelfontbackground + '" interval="' + polygonlabelfontinterval + '" transparency="' + polygonlabelfonttransparency + '"/>\n';
                                selString +='</SIMPLELABELRENDERER>\n';
                        }
			selString +='</GROUPRENDERER>\n';
			selString +='</LAYER>\n';
		}

	}
	if (selectBlurb!="") {
		// add SelectBlurb to Map XML request
		selString +='<LAYER type="FeatureClass" name="Valittu kohde" visible="true">\n';
		//selString +='<DATASET fromlayer="' + ActiveLayer + '" />\n';
		selString +='<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
		selString +='<QUERY where="' + selectBlurb + '" />\n';
		selString +='<SIMPLERENDERER>\n';
		if (selectType=="point") {
			selString +='<SIMPLEMARKERSYMBOL color="' + selectColor + '" type="' + selectpointType + '" width="' + selectpointWidth + '" />\n';
		} else if (selectType=="line") {
			selString +='<SIMPLELINESYMBOL type="'+ selectlineType + '" color="' + selectColor + '" width="' + selectlineWidth +'" />\n';
		} else {
			selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + selectpolygonColor + '" filltype="' + selectpolygonfillType + '" transparency="' + transparentLevel + '" boundarycolor="' + selectpolygonboundaryColor + '" />\n';
			}
		selString +='</SIMPLERENDERER>\n';
		selString +='</LAYER>\n';
	}

	if (highlightedOne!="") {
		// add Draw Highlighed Feature to Map XML request
		selString +='<LAYER type="FeatureClass" name="Highlighted_Feature" visible="true">\n';
		//selString +='<DATASET fromlayer="' + ActiveLayer + '" />\n';
		selString +='<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '" />\n';
		selString +='<QUERY where="' + highlightedOne + '" />\n';
		selString +='<SIMPLERENDERER>\n';
		if (selectType=="point") {
			selString +='<SIMPLEMARKERSYMBOL color="' + highlightColor + '" type="' + selectpointType + '" width="' + selectpointWidth + '" />\n';
		} else if (selectType=="line") {
			selString +='<SIMPLELINESYMBOL type="'+ selectlineType + '" color="' + highlightColor + '" width="' + selectlineWidth +'" />\n';
		} else {
			selString +='<SIMPLEPOLYGONSYMBOL fillcolor="' + highlightColor + '" filltype="' + selectpolygonfillType + '" transparency="' + transparentLevel + '" boundarycolor="' + selectpolygonboundaryColor + '" />\n';
		}
		selString +='</SIMPLERENDERER>\n';
		selString +='</LAYER>\n';
	}
	return selString;
}
function zoomToChoiceMeasure(xTravel, yTravel) {
	//alert("zoom");
	var tmpLeft = parseInt(left);
	var tmpRight = parseInt(right);
	var tmpTop = parseInt(top);
	var tmpBottom = parseInt(bottom);

	var count_xDistance = parseInt(xDistance);
	var count_yDistance = parseInt(yDistance);

	while(count_xDistance != xTravel) {
		if(count_xDistance > xTravel) {
			tmpLeft += 0.5;
			tmpRight -= 0.5;
		}
		else {
			tmpLeft -= 0.5;
			tmpRight += 0.5;
		}
		count_xDistance = tmpRight - tmpLeft;
	}
	while(count_yDistance != yTravel) {
		if(count_yDistance > yTravel) {
			tmpTop -= 0.5;
			tmpBottom += 0.5;
		}
		else {
			tmpTop += 0.5;
			tmpBottom -= 0.5;
		}
		count_yDistance = tmpTop - tmpBottom;
	}

	left = tmpLeft;
	right = tmpRight;
	top = tmpTop;
	bottom = tmpBottom;
}

//mSki
function zoomToPoint(xIn, yIn) {
	var mWMargin = 0;
	var mHMargin = 0;
	mWMargin = Math.abs(right-left) / 75;
	mHMargin = Math.abs(top-bottom) / 75;
        forceCommaInRequest[activeMapServiceIndex] = false;
	xIn = forceComma(xIn);
	yIn = forceComma(yIn);
	forceCommaInRequest[activeMapServiceIndex] = true;
	left = xIn - mWMargin;
	right = xIn -(-mWMargin);
	top = yIn -(-mHMargin);
	bottom = yIn - mHMargin;
	//alert(mWMargin);
	//alert(mHMargin);
	if(mWMargin < 200 && mHMargin < 300)
		zoomToChoiceMeasure(1600, 1200);
	saveLastExtent();
	sendMapXML();
}
