//***********************************************************************
// Name:	GKAIMS.js
//-----------------------------------------------------------------------
// Parametrar och funktioner till ARCIms för VisaKarta
//-----------------------------------------------------------------------
//	Version	Date	Dev	History
//-----------------------------------------------------------------------
//	1.0	040719	JJ	Creation
//	1.1	050201	JJ	hela länet
//	2.0	100503	JJ	sweref99
//***********************************************************************
//
//***********************************************************************
//	parametrar
//***********************************************************************
//	machine name
var hostName = document.location.host;
//
//	IMS service
//var imsURL = 'http://gis.gavle.se/servlet/com.esri.esrimap.Esrimap?ServiceName=befostat_gav';
//var qryURL = 'http://gis.gavle.se/servlet/com.esri.esrimap.Esrimap?ServiceName=befostat_gav&CustomService=Query';
var imsURL = 'http://gis.gavle.se/servlet/com.esri.esrimap.Esrimap?ServiceName=CX_KARTA_SW99';
var qryURL = 'http://gis.gavle.se/servlet/com.esri.esrimap.Esrimap?ServiceName=CX_KARTA_SW99&CustomService=Query';

var serviceType = "I"	// I = ArcIMS image service, M = ArcMap server 
			// Anpassad kod för ArcMap server resp ArcIMS iage server finns i funktionerna drawCircle och writePolyCoord 

var applURL = "http://gis.gavle.se/VisaKarta";

//	parametrar för att hantera informationstyper
var mataLayerPresent = true;
var aimsNavigationPresent=true;
var aimsLayersPresent=true;

//	startextent
//var startLeft = 1569202;
//var startRight = 1580644;
//var startTop = 6735044;
//var startBottom = 6723602;
var startLeft = 41023;
var startRight = 228421;
var startTop = 6918606;
var startBottom = 6672604;

//	Aktuella kartextents. . . dynamiskt uppdaterade
var left = startLeft;				
var right = startRight;
var top = startTop;
var bottom = startBottom;

//	hela kartan
//var fullLeft = 1427000;			//1550000
//var fullRight = 1617000;		//1597000
//var fullTop = 6917000;			//6778000
//var fullBottom = 6673000;		//6683000
var fullLeft = 41023;			//1550000
var fullRight = 228421;		//1597000
var fullTop = 6918606;			//6778000
var fullBottom = 6672604;		//6683000

var fullWidth = Math.abs(fullRight - fullLeft);
var fullHeight = Math.abs(fullTop - fullBottom);

//	gränsvärden
//var limitLeft = 1427000;		//1550000
//var limitRight = 1617000;		//1597000
//var limitTop = 6917000;			//6778000
//var limitBottom = 6673000;		//6683000
var limitLeft = 41023;		//1550000
var limitRight = 228421;			//1597000
var limitTop = 6918606;			//6778000
var limitBottom = 6672604;		//6683000

var imageLimitLeft = limitLeft;
var imageLimitTop = limitTop;
var imageLimitRight = limitRight;
var imageLimitBottom = limitBottom;

//var minYin = 6683000;
//var maxYin = 6778000;
//var minXin = 1550000;
//var maxXin = 1597000;
var minYin = 6672604;
var maxYin = 6918606;	
var minXin = 41023;	
var maxXin = 228421;

//	kartbildsstorlek i pixlar
var iWidth = 300;					//U
var iHeight = 300;					//U

var enforceFullExtent=true;				//u

//	skärmbredd/höjd i enheten pixlar
var sWidth = screen.width;				//U
var sHeight = screen.height;				//U

//	delimiters to be used between coordinate in strings in ArcXML request
var coordsDelimiter = " ";
//	delimiters to be used between pairs of coordinates in strings in ArcXML request
var pairsDelimiter = ";";

//	navisgeringsverktyg mm till toolframe
var useFullExtent=true;					//u
var useZoomLast=true;					//u
var useZoomIn=true;					//u
var useZoomOut=true;					//u
var usePan=true;					//u
var useIdentify=true;					//u
var useCreateObject=false;				//u

var canuseIdentify=false;				
var modeBlurb = "Zoom In";
var toolMode = 1;					//u
var panning=false;
var zooming=false;					//u
var selectBox=false;					//u
var showXYs=false;					//u

//Panoreringsfaktor för pilknappar
var panFactor = 0.85;
var zoomFactor = 2;

var ScaleBarPrecision = 2;
var numDecimals = ScaleBarPrecision;

//	variabler för zoom, pan	
var xDistance = Math.abs(right-left);
var yDistance = Math.abs(top-bottom);
var panX = xDistance * panFactor;
var panY = yDistance * panFactor;
var pixelX = xDistance/iWidth;
var pixelY = yDistance/iHeight;
var mapX = left;
var mapY = top;
var lastLeft = left;
var lastRight = right;
var lastTop = top;
var lastBottom = bottom;
							
var xHalf = xDistance/2; 
var yHalf = yDistance/2;

// 	Placering av "laddar karta" banér
var loadBannerLeft = parseInt(iWidth/2)-10;	//U
var loadBannerTop = parseInt(iHeight/2)-15;		//U

//	variabler för att spara musens position
var mouseX=0;						//u
var mouseY=0;						//u
var x1=0;						//u
var y1=0;						//u
var x2=0;						//u
var y2=0;						//u
var zleft=0;						//u
var zright=0;						//u
var ztop=0;						//u
var zbottom=0;
var leftButton =1;
var rightButton = 2;

//	Variabler för förskjutning av karta från övre vänstra hörnet av frame, i enheten pixlar
var hspc = 0;						//u
var vspc = 0;						//u

//	variabler för färger
var mapBackColor = "225,225,225";			//u
var zoomBoxColor = "#ff0000";
var ovBoxSize = 2					//U

var coordsString="";

//	vald adress eller objekt 0=adress, 1-32768=objekt (gruppid), -1=fritt, -2=objekt eller byggnad (ej karta)
var sokTyp = 0;
//	variabler för vald adress
var adressX = 0;					//u
var adressY = 0;
var adressString="";
//	variabler för valt objekt
var objectX = 0;					//u
var objectY = 0;
var objectColor = "";
var objectType = "";
var objectSize = 20;
var objectString="";
//	variabler för kategori
var katX = new Array();
var katY = new Array();
var katColor = new Array();
var katType = new Array();
var katString="";
var katCount = 0;
var katSelected = "";

// storlekar på asetateobjekt för ArcMap server
var circleSize = 8;
var triangleSize = 15;
var squareSize = 7;
var defaultSize = 10;

//	övriga variabler
var toggleVisible = true;				//u
var okToSend = true;					//u
var XMLMode = 1;					//u

var dQuote = '"';

//	index of current active MapService - default is zero - multiples in MultiService sample
var activeMapServiceIndex = 0;

//	array for determining if extent coordinates should have comma instead of point for decimals
var forceCommaInRequest =  false;
//var forceCommaInOV = false;

// encoding for XML header
var charEncoding = "UTF-8";
var localeEncoding = 'encoding="' + charEncoding + '" ';

// charset for dynamic HTML pages - static pages must be changed manually
var charSet = "ISO-8859-1"

// variabler för adressposiotnering
var sokAdress=false;

//	variabler för att visa polygon
var showFeature = true;
var featureString="";
var featureLayer="7";
var featureKey="GAV.GIS.PK_FASTIGHETER.FBET";
var featureValue="";
var featureX = -1;					//centrumpkt för sökt fastighet 
var featureY = -1;					//centrumpkt för sökt fastighet
var sokLayer=7;
var sokField="";
var sokBegrepp="";

// *** Globala variabler för att spara musens position ****************
var mouseX=0;
var mouseY=0;
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var zleft=0;
var zright=0;
var ztop=0;
var zbottom=0;
//
//***********************************************************************
//	funktioner
//***********************************************************************

//-----------------------------------------------------------------------
//	Create a DHTML layer
//-----------------------------------------------------------------------
function createLayer(name, left, top, width, height, visible, content) {
	var layer;
	document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + left + 'px; top:' + top + 'px; width:' + width + 'px; height:' + height + 'px;' + ' visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
	document.writeln(content);
	document.writeln('</div>');
}
//-----------------------------------------------------------------------
//	zoom to envelope
//-----------------------------------------------------------------------
function zoomToEnvelope(minXin,minYin,maxXin,maxYin) {
	left=minXin;
	bottom=minYin;
	right=maxXin;
	top=maxYin;
	//alert("function zoomToEnvelope" + "\nLeft:" + minXin + "\nTop:" + maxYin + "\nRight:" + maxXin + "\nBottom:" + minYin);
	sendMapXML();
}
//-----------------------------------------------------------------------
//	show address
//-----------------------------------------------------------------------
function showAddress(xin,yin) {
	//alert(xin + " " + yin);
	if(!(xin > left && xin < right && yin > bottom && yin < top)) {
		var d1=(right-left)/2;
		//alert(d1);
		left = xin - d1;
		bottom = yin - d1;
		right = xin + d1;
		top = yin + d1;
		checkFullExtent();
	}
	//alert("function showAddress" + "\nLeft:" + left + "\nTop:" + top + "\nRight:" + right + "\nBottom:" + bottom);
	sendMapXML();
}
//-----------------------------------------------------------------------
//	zoom back to last extent
//-----------------------------------------------------------------------
function zoomBack() {
	moveLayer("theMap",hspc,vspc);
	var left1 = left;
	var right1 = right;
	var top1 = top;
	var bottom1 = bottom;
	left = lastLeft;
	right = lastRight;
	top = lastTop;
	bottom = lastBottom;
	lastLeft = left1;
	lastRight = right1;
	lastTop = top1;
	lastBottom = bottom1;
	sendMapXML();	
}
//-----------------------------------------------------------------------
//	kollar att man inte panorerar utanför kartextent
//-----------------------------------------------------------------------
function checkFullExtent() {
	//alert("checkFullExtent");
	if (xDistance>fullWidth) xDistance = fullWidth;
	if (yDistance>fullHeight) yDistance = fullHeight;
	if (enforceFullExtent) {
		if (left < imageLimitLeft) {
			left = imageLimitLeft;
			right = left + xDistance;
		}
		if (top > imageLimitTop) {
			top = imageLimitTop;	
			bottom = top - yDistance;
		}	
		if (right > imageLimitRight) {
			right = imageLimitRight;
			left = right - xDistance;
		}
		if (bottom < imageLimitBottom) {
			bottom = imageLimitBottom;	
			top = bottom + yDistance;
		}	
	}
}
//-----------------------------------------------------------------------
//	move layer to x,y
//-----------------------------------------------------------------------
function moveLayer(name, x, y) {
	var layer = getLayer(name);		
    	layer.left = x;
	layer.top  = y;
}
//-----------------------------------------------------------------------
//	set layer background color
//-----------------------------------------------------------------------
function setLayerBackgroundColor(name, color) {
	//alert("setLayerBackgroundColor");	
  	var layer = getLayer(name);		
    	layer.backgroundColor = color;
}
//-----------------------------------------------------------------------
//	toggle layer to invisible
//-----------------------------------------------------------------------
function hideLayer(name) {		
  	var layer = getLayer(name);		
  	layer.visibility = "hidden";
}
//-----------------------------------------------------------------------
//	toggle layer to visible
//-----------------------------------------------------------------------
function showLayer(name) {		
  	var layer = getLayer(name);		
	layer.visibility = "visible";
}
//-----------------------------------------------------------------------
//	get the layer object called "name"
//-----------------------------------------------------------------------
function getLayer(name) {
	layer = eval('document.all.' + name + '.style');
	return(layer);
}
//-----------------------------------------------------------------------
//	check for existance of layer
//-----------------------------------------------------------------------
function hasLayer(name) {
	var result = false;
	if (eval('document.all.' + name)!=null) result=true;
	return result;
}
//-----------------------------------------------------------------------
//	the starting point
//-----------------------------------------------------------------------
function checkParams() {
	//alert("checkParams()");
	if (!document.layers) {
            	if (hasLayer("theMap")) document.all.theMap.style.cursor = "crosshair";
	}
	if (aimsNavigationPresent) {
		//alert("aimsNavigationPresent=true");
		// Set up event capture for mouse movement
		document.onmousemove = getMouse;
		document.onmousedown = mapTool;
		document.onmouseup = chkMouseUp;
	}
	else {
		//alert("aimsNavigationPresent=false");
		usePan=false;
		useZoomIn=false;
		useZoomOut=false;
		useMata=false;
		useIdentify=false;
	}
}
//-----------------------------------------------------------------------
//	get the coords at mouse position
//-----------------------------------------------------------------------
function getMouse(e) {
	//alert("getMouse");
	window.status="";
	getImageXY(e);	
	if ((mouseX>iWidth) || (mouseY>iHeight) || (mouseX<=0) ||(mouseY<=0)) {
		chkMouseUp(e);
	}
	else {
		if ((zooming) || (selectBox)) {
			//alert("aimsNavigation.js-getMouse_zooming");
			x2=mouseX;
			y2=mouseY;
			setClip();
		}
		else if (panning) {
			//alert("aimsNavigation.js-getMouse_panning");
			x2=mouseX;
			y2=mouseY;
			panMouse();	
		}
		pixelX = xDistance / iWidth;
		mapX = pixelX * mouseX + left;
		var theY = iHeight - mouseY;
		pixelY = yDistance / iHeight;
		mapY = pixelY * theY + bottom;
		if (toolMode==20) {
			calcDistance(mapX,mapY);	
		} 
		else if (showXYs) {
			var u = Math.pow(10,numDecimals);
			var uX = parseInt(mapX * u + 0.5) / u
			var uY= parseInt(mapY * u + 0.5) / u
			var mouseString = "Koord: " + uX + ", " + uY;	
			/*if (toolMode==997) {
				var senaste=matSenaste();
			//	mouseString += "   senaste: ";
			//	mouseString += senaste;
			}*/
			window.status = mouseString;
		}
	}
	return false;
}
//-----------------------------------------------------------------------
//	check for mouseup
//-----------------------------------------------------------------------
function chkMouseUp(e) { 
	if ((toolMode == 1) && (zooming)) {
		stopZoomBox(e);
	}
	if ((toolMode == 2) && (zooming)) {
		stopZoomOutBox(e);
	}
	if ((toolMode == 3) && (panning)) {
		stopPan(e);
	}
	return false;
}
//-----------------------------------------------------------------------
//	get coordinats for image 
//-----------------------------------------------------------------------
function getImageXY(e) {
	mouseX=event.clientX + document.body.scrollLeft;
	mouseY=event.clientY + document.body.scrollTop;
	// subtract offsets from page left and top
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;
   	alert("mouseX= " + mouseX + " mouseY= " + mouseY);
}	
//-----------------------------------------------------------------------
//	box it 
//-----------------------------------------------------------------------
function boxIt(theLeft,theTop,theRight,theBottom) {
	clipLayer("zoomBoxTop",theLeft,theTop,theRight,theTop+ovBoxSize);
	clipLayer("zoomBoxLeft",theLeft,theTop,theLeft+ovBoxSize,theBottom);
	clipLayer("zoomBoxRight",theRight-ovBoxSize,theTop,theRight,theBottom);
	clipLayer("zoomBoxBottom",theLeft,theBottom-ovBoxSize,theRight,theBottom);
}
//-----------------------------------------------------------------------
//	clip layer display to clipleft, cliptip, clipright, clipbottom
//-----------------------------------------------------------------------
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(name);		
	layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
}
//-----------------------------------------------------------------------
//	start zoom in.... box displayed
//-----------------------------------------------------------------------
function startZoomBox(e) {
	//alert("startZoomBox");
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);	
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (zooming) {
			//alert("Gå till stopZoomBox()");
			stopZoomBox(e);
		}
		else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			zleft=x1;
			ztop=y1;
			zbottom=y1;
			zright=x1
			//alert("Har gått vidare");
			
			boxIt(x1,y1,x2,y2);
			//alert("Har utfört boxIt");
			/*
			clipLayer("zoomBoxTop",x1,y1,x2,y2);
			clipLayer("zoomBoxLeft",x1,y1,x2,y2);
			clipLayer("zoomBoxRight",x1,y1,x2,y2);
			clipLayer("zoomBoxBottom",x1,y1,x2,y2);
			*/
			//alert("Skall snart uföra showlayer");
			zooming=true;
			showLayer("zoomBoxTop");
			showLayer("zoomBoxLeft");
			showLayer("zoomBoxRight");
			showLayer("zoomBoxBottom");
			//alert("Har utfört showLayer");
		}
	}
	return false;
}
//-----------------------------------------------------------------------
//	stop zoom box display... zoom in
//-----------------------------------------------------------------------
function stopZoomBox(e) {
	zooming=false;
	if ((zright < zleft+2) && (zbottom < ztop+2)) {
		zoomin(e);
	}
	else {
		var tempLeft=lastLeft;
		var tempRight=lastRight;
		var tempTop=lastTop;
		var tempBottom=lastBottom;
		saveLastExtent();
		pixelX = xDistance / iWidth;
		var theY = iHeight - ztop;
		pixelY = yDistance / iHeight;
		top = pixelY * theY + bottom;
		right = pixelX * zright + left;
		left = pixelX * zleft + left;
		theY = iHeight - zbottom;
		pixelY = yDistance / iHeight;
		bottom = pixelY * theY + bottom;
		window.scrollTo(0,0);
	
        	// funkar ej bra checkFullExtent();
		
		//var theString = writeXML();
		sendMapXML();
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
	}
	return true;
}
//-----------------------------------------------------------------------
//	zoom in around mouse click
//-----------------------------------------------------------------------
function zoomin(e) {
	getMapXY(mouseX,mouseY);
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	saveLastExtent();

	left = mapX - (xHalf/zoomFactor);
	right = mapX + (xHalf/zoomFactor);
	top = mapY + (yHalf/zoomFactor);
	bottom = mapY - (yHalf/zoomFactor);
	
	sendMapXML();
}
//-----------------------------------------------------------------------
//	zoom out from mouse click
//-----------------------------------------------------------------------
function zoomout(e) {
	getMapXY(mouseX,mouseY);
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	saveLastExtent();

	left = mapX - (xDistance*zoomFactor/2);
	right = mapX + (xDistance*zoomFactor/2);
	top = mapY + (yDistance*zoomFactor/2);
	bottom = mapY - (yDistance*zoomFactor/2);
	
	sendMapXML();
}
//-----------------------------------------------------------------------
//	zoom out to full extent
//-----------------------------------------------------------------------
function fullExtent() {
	//alert("fullExtent");
	window.scrollTo(0,0);
	saveLastExtent();
	left = fullLeft;
	right = fullRight;
	top = fullTop;
	bottom = fullBottom;
	//var theString = writeXML();
	sendMapXML();
}
//-----------------------------------------------------------------------
//	zoom to start extent
//-----------------------------------------------------------------------
function startExtent() {
	//alert("startExtent");
	window.scrollTo(0,0);
	left = startLeft;
	right = startRight;
	top = startTop;
	bottom = startBottom;
	//var theString = writeXML();
	sendMapXML();
}
//-----------------------------------------------------------------------
//	positionera till objekt - anropas initialt (?)
//-----------------------------------------------------------------------
function posKarta(varde) {
//Hämta koordinater
	//alert("posKarta " + varde);
	featureValue=varde;
	var fnutt="'";
	var xmlString="";
	sokField="FBET";
	sokBegrepp="Fastighet";
	//alert("sokField och sokBegrepp=" + sokField + sokBegrepp);
	//Create the request. 
	xmlString='<ARCXML version="1.1"><REQUEST><GET_FEATURES outputmode="xml" geometry="false" envelope="true" compact="true"><LAYER id="' + sokLayer + '" /><SPATIALQUERY subfields="#SHAPE#"  where="' + sokField + '= ' + fnutt + varde + fnutt + '" >';
	xmlString=xmlString+'</SPATIALQUERY></GET_FEATURES></REQUEST></ARCXML>';
	//alert("Function posKarta xmlString " + xmlString);			
	sendToServer(qryURL,xmlString,1002);
}

//-----------------------------------------------------------------------
//	save the extent 
//-----------------------------------------------------------------------
function saveLastExtent() {
	//alert("saveLastExtent");
	lastLeft = left;
	lastRight = right;
	lastTop = top;
	lastBottom = bottom;
	//alert("saveLastExtent slut");
}
//-----------------------------------------------------------------------
//	send the created xml request to map server
//-----------------------------------------------------------------------
function sendMapXML() {
	//alert("sendMapXML");
	showRetrieveMap();
	var theText = writeXML();
	//alert("imsURL= " + imsURL + " theText= " + theText);
	sendToServer(imsURL,theText,1);
}
//-----------------------------------------------------------------------
//	prepare the request in xml format for Main Map
//-----------------------------------------------------------------------
function writeXML() {
	//alert("writeXML");
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>\n<ENVELOPE minx="' + forceComma(left) + '" miny="' + forceComma(bottom) + '" maxx="' + forceComma(right) + '" maxy="' + forceComma(top) + '" />\n';
	theString += '<IMAGESIZE height="' + iHeight + '" width="' + iWidth + '" />\n';

	theString += '</PROPERTIES>\n';

	if (objectX > 0) {
		writeObject();
		theString += objectString;
	}
	// highlight polygon
	if (showFeature) {
		writeFeature();
		theString += featureString;
		//alert("showFeature : " + featureString);
	}
	if (adressX > 0) {
		writeAdress();
		theString += adressString;
	}

	theString += '</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
	//alert("writeXML " + theString);

	return theString;
}
//-----------------------------------------------------------------------
//	process the response xml
//-----------------------------------------------------------------------
function processXML(theReplyIn) {
	//alert("processXML"+theReplyIn);
	// ska pil ritas ut
	//ritaPil();
	theReplyIn = replacePlus(theReplyIn);
	var theReply = unescape(theReplyIn);
	//alert("processXML - theReply= " + theReply + "\nXMLMode= " + XMLMode);
	okToSend = true;
	switch(XMLMode) {
		case 1:
			//alert ("Received:\n\n" + theReply);
			var theURL = "";
			theURL = getURL(theReply);
			
			if (theURL != "") {
				getXYs(theReply);
				document.theImage.src = theURL;

			// Scriptet nedan körs vid panorering, (toolMode=3)				
				if (toolMode==3) {
					moveLayer("theMap",hspc,vspc);
					clipLayer("theMap",0,0,iWidth,iHeight);
					window.setTimeout('showLayer("theMap");',1000);
				}
			}
			hideRetrieveMap(); // Egen test
			break			
		case 1001:
			//alert("case 1001 - klick i kartan");
			alert("X= " + mapX + " Y= " + mapY);
			//getMaxMin(theReply);
			break
		case 1002:
			//alert("case 1002 - initilt");
			//alert("aimsXML case 1002 theReply= " + theReply + "\nXMLMode= " + XMLMode);
			getMaxMin(theReply);
			break	
		case 1003:
			//alert("case 1003");
			//alert("theReply= " + theReply + "\nXMLMode= " + XMLMode);
			//writeAdrplString(theReply);
			break
	}
}
//-----------------------------------------------------------------------
//	visa ett lager (?)
//-----------------------------------------------------------------------
/*
function visaLager(lager) {
	if (lager > -1) {  
		LayerVisible[lager]=1;
	}
	var xmlString=writeXML();
	//alert(xmlString);
	sendToServer(imsURL,xmlString,1);
}
*/
//-----------------------------------------------------------------------
//	put up the "RetriveMap" image
//-----------------------------------------------------------------------
function showRetrieveMap() {
	//alert("showRetrieveMap");
	if (hasLayer("LoadMap")) {
		showLayer("LoadMap");
	}
}
//-----------------------------------------------------------------------
//	hide the "RetriveMap" image
//-----------------------------------------------------------------------
function hideRetrieveMap() {
	if (hasLayer("LoadMap")) {
		hideLayer("LoadMap");
	}
}
//-----------------------------------------------------------------------
//	send in XML request and get XML response 
//-----------------------------------------------------------------------
function sendToServer(URLString,XMLRequest,theType) {
	//alert("sendToServer"+URLString+XMLRequest+theType);
	if (parent.PostFrame.document.forms[0]!=null) {
		//okToSend=true;
		if (okToSend) {
			okToSend=false;
			//alert("okToSend");
			XMLMode = theType;
			if (XMLMode==1) showRetrieveMap();
			// default Servlet Connector
			htmlSendToServer(URLString,XMLRequest,theType);
		}
		else {
			alert("Vänta till du fått svar på föregående fråga.");
			hideRetrieveMap();
		}
	}
	else {
		//alert("PostFrame saknas");
		hideRetrieveMap();
	}
}
//-----------------------------------------------------------------------
//	send request so server
//-----------------------------------------------------------------------
function htmlSendToServer(URLString,XMLRequest,theType) {
	//alert("htmlSendToServer "+URLString+XMLRequest+theType);
	// uses default Servlet Connector - requestMethod="Servlet";
	var cVersion = "&ClientVersion=4.0";
	var thePostForm = parent.PostFrame.document.forms[0];
	URLString = URLString + cVersion;
	var requestURL = URLString;
	if (thePostForm.RedirectURL!=null) {
		if (isNotSameHostInURL(URLString, hostName)) {
			requestURL = connectorURL;
			thePostForm.RedirectURL.value = URLString;
		}
		else {
			thePostForm.RedirectURL.value = "";
		}
	}
	thePostForm.action = requestURL + "&Form=True&Encode=False";
	var xmlHeader = '<?xml version="1.0" ' + localeEncoding + '?>';
	thePostForm.ArcXMLRequest.value = xmlHeader + XMLRequest;
	if (thePostForm.FormCharset!=null) thePostForm.FormCharset.value = charSet;
	if (thePostForm.BgColor!=null) thePostForm.BgColor.value = "White";
	//alert("-htmlSendToServer = " + "xmlHeader = " + xmlHeader + "XMLRequest = " + XMLRequest);
	thePostForm.submit();
}
//-----------------------------------------------------------------------
//	forceComma
//-----------------------------------------------------------------------
function forceComma(theNumber) {
	//alert("forceComma");
	var comma = ",";
	var dot = ".";
	var charOut = comma;
	var charIn = dot;
	var numberString = new String(theNumber);
	if (forceCommaInRequest) {
		charOut = dot;
		charIn = comma;
	}
	var pos = numberString.indexOf(charOut);
	if (pos!=-1) {
		var begin = numberString.substring(0,pos);
		var ending = numberString.substring(pos+1, numberString.length);
		numberString = begin + charIn + ending;
	}
	//alert("function forceComma = " + numberString);
	return numberString;
}
//-----------------------------------------------------------------------
//	rita pil   (?)
//-----------------------------------------------------------------------
/*
function ritaPil() {
	if (sokAdress) {
		parent.MapFrame.moveLayer("thePointer",(iWidth/2)+5,iHeight/2);
		sokAdress = false;
	}
	else {
		parent.MapFrame.moveLayer("thePointer",-50,-50);
	}
}
*/
//-----------------------------------------------------------------------
//	replace + in string with space to allow parsing of unescaped xml response
//-----------------------------------------------------------------------
function replacePlus(inText) {
	//alert("function replacePlus");
	var pos=9;
	while (pos != -1) {
		pos = inText.indexOf('+');
		if (pos!=-1) {
			var lastpos = inText.length;
			var midend = pos;
			var midstart = pos + 1;
			var leftSide = inText.substring(0,midend);
			var rightSide = inText.substring(midstart,lastpos);
			inText = leftSide + "%20" + rightSide;
		}
	}
	return inText;
}
//-----------------------------------------------------------------------
//	get the substring between beforeString and afterString, starting at startpos
//	must be found before limitpos (0 for no limit) 
//	caseSensitive = true or false
//-----------------------------------------------------------------------
function getInsideString(inString,beforeString,afterString,startpos,limitpos,caseSensitive) {
	//alert("function getInsideString");
	var returnString = "";
	var ucInString = inString;
	var ucBefore = beforeString;
	var ucAfter = afterString;
	if (limitpos==0) limitpos = inString.length;
	if (!caseSensitive) {
		ucInString = inString.toUpperCase();
		ucBefore = beforeString.toUpperCase();;
		ucAfter = afterString.toUpperCase();;
	}
	pos = ucInString.indexOf(ucBefore,startpos);
	if ((pos != -1) && (pos<limitpos)) {
		pos = pos + ucBefore.length;
		var endpos = ucInString.indexOf(ucAfter,pos);
		returnString = inString.substring(pos,endpos);
	}	
	
	return returnString;
}
//-----------------------------------------------------------------------
//	check if theURL has different host from theHost
//-----------------------------------------------------------------------
function isNotSameHostInURL(theURL, theHost) {
	var startpos = theURL.indexOf("//");
	if (startpos==-1) {
		startpos = 0;
	} else {
		startpos = startpos + 2;
	}
	var endpos = theURL.indexOf("/",startpos);
	if (endpos==-1) endpos = theURL.length;
	var thisHost = theURL.substring(startpos,endpos);
	if (thisHost==theHost) {
		return false;
	} else {
		return true;
	}
}
//-----------------------------------------------------------------------
//	get the map extents from xml reply
//-----------------------------------------------------------------------
function getXYs(theString) {
	var tempStr = "";
	var smallStr = "";
	var startpos = 0;
	var endpos = 0;
	var theReply = theString
	var theXYs =  getEnvelopeXYs(theString, 0)
	left = theXYs[0];
	bottom = theXYs[1];
	right = theXYs[2];
	top = theXYs[3];
	//alert("Detaljkartaextent : " +  "left= " + left + bottom + "bottom= " + "right= " + right + "top= " + top);
	xDistance = Math.abs(right-left);
	yDistance = Math.abs(top-bottom);
	xHalf = xDistance / 2;
	yHalf = yDistance / 2;
	panX = xDistance * panFactor;
	panY = yDistance * panFactor;
	var sFactor = xDistance / iWidth
}
//-----------------------------------------------------------------------
//	get min and max x,y's from xml stream . . . return an array with values
//-----------------------------------------------------------------------
function getEnvelopeXYs(theString, startpos) {
	//alert("getEnvelopeXYs");
	var theEnvelope = new Array();
	theString = theString.toUpperCase();
	var pos = theString.indexOf("ENVELOPE",startpos);
	if (pos!=-1) {
		pos = pos + 8;
		startpos = theString.indexOf("MINX=",pos);
		startpos += 6;
		var endpos = theString.indexOf(dQuote,startpos);
		theEnvelope[0] = parseFloat(theString.substring(startpos,endpos)); 
		startpos = theString.indexOf("MINY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		theEnvelope[1] = parseFloat(theString.substring(startpos,endpos)); 
		startpos = theString.indexOf("MAXX=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		theEnvelope[2] = parseFloat(theString.substring(startpos,endpos)); 
		startpos = theString.indexOf("MAXY=",pos);
		startpos += 6;
		endpos = theString.indexOf(dQuote,startpos);
		theEnvelope[3] = parseFloat(theString.substring(startpos,endpos)); 
		xmlEndPos = endpos;
	}
	return theEnvelope;
}
//-----------------------------------------------------------------------
//	get URL
//-----------------------------------------------------------------------
function getURL(theReply) {
	var theURL = "";
	var startpos = 0;
	var endpos = 0;

	var pos = theReply.indexOf("OUTPUT");
	//alert("pos=" + pos);
	if (pos != -1) {
		theURL = getInsideString(theReply,'url="',dQuote,pos,0,false);
		//alert("theURL to theImage= " + theURL);
		if (theURL!="") {
				endpos = theURL.lastIndexOf("/");
				theImageURLPath = theURL.substring(0,(endpos+1));
				startpos = theURL.lastIndexOf(".");
				var theType = theURL.substring(startpos+1,theURL.length);
				//alert("Image type: " + theType);
				if (theType.toUpperCase()=="GIF") {
					theImageType = "GIF";
				} else {
					theImageType = "PNG";
				}
				if (theType.toUpperCase()=="JPG") {
					noOverlay = true;
				}
				startpos = theReply.indexOf("file=",pos);
				if (startpos != -1) {
					startpos += 6;
					endpos = theReply.indexOf(dQuote,startpos);
					thePath = theReply.substring(startpos,endpos);
					endpos = thePath.lastIndexOf("\\");
					
					theImagePath = thePath.substring(0,(endpos+1));
					startpos = endpos + 1;
					endpos = thePath.indexOf("_",startpos);
					theServiceName = thePath.substring(startpos,endpos);
				}
		}
	}
	//legendImage = getLegendURL(theReply);
	jj_theImagePath = theURL;
	return theURL;
}
//-----------------------------------------------------------------------
//	keep track of currently selected tool, and display it to user
//	set the imsMap cursor tool
//-----------------------------------------------------------------------
function clickFunction (toolName) {
	//alert("aimsClick.js_toolName= " + toolName);

	switch(toolName) {
	// Zooming functions
	case "zoomin":
		// zoom in mode
		toolMode = 1;
		panning=false;
		selectBox=false;
		document.all.theMap.style.cursor = "crosshair";
		theCursor = document.all.theMap.style.cursor;
		modeBlurb = "Zoom In";
		break
	case "zoomout":
		// zoom out mode
		toolMode = 2;
		panning=false;
		selectBox=false;
		document.all.theMap.style.cursor = "crosshair";
		theCursor = document.all.theMap.style.cursor;
		modeBlurb = "Zoom Out";
		break
	case "zoomlast":
		zoomBack();
		panning=false;
		zooming=false;
		selectBox=false;
		break
	case "fullextent":
		fullExtent();
		break
	case "pan":
		// pan mode
		toolMode = 3;
		zooming=false;
		selectBox=false;
		document.all.theMap.style.cursor = "move";
		theCursor = document.all.theMap.style.cursor;
		modeBlurb = "Pan";
		break
	default:
		alert("Function not enabled.");
	}
	modeName=modeBlurb;
}
//-----------------------------------------------------------------------
//	check for mouseup
//-----------------------------------------------------------------------
function chkMouseUp(e) { 
	if ((toolMode == 1) && (zooming)) {
		//alert("aimsClick.js-stopZoomBox");
		stopZoomBox(e);
	}
	if ((toolMode == 2) && (zooming)) {
		stopZoomOutBox(e);
	}
	if ((toolMode == 3) && (panning)) {
		stopPan(e);
	}
	return false;
}
//-----------------------------------------------------------------------
//	perform appropriate action with mapTool
//-----------------------------------------------------------------------
function mapTool (e) {
	//alert("mapTool");
	var theButton=0;
	// get the button pushed... if right, ignore... let browser do the popup... it will anyway
	theButton=window.event.button;
	if (theButton==leftButton) {	
		//alert("aimsClick.js-MapTool-1= " + toolMode);
		getImageXY(e);
		if ((mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight)) {
			//alert("aimsClick.js_mapTool= " + mouseX + "," + mouseY);
			switch(toolMode) {
				case 1:
				        //alert("aimsClick.js-startZoomBox");
					startZoomBox(e);
				        moveLayer("thePointer",-50,-50);
				        return false;
					break
				case 2:
					startZoomOutBox(e);
				        moveLayer("thePointer",-50,-50);
				        return false;
					break	
				case 3:
					startPan(e);
				        moveLayer("thePointer",-50,-50);
				        return false;
					break

				default:
					if (toolMode>=1000) {
						customMapTool(e);
					}
			}
		}
	}
}
//-----------------------------------------------------------------------
//	convert mouse click xy's into map coordinates
//-----------------------------------------------------------------------
function getMapXY(xIn,yIn) {
	//alert("getMapXY");
	mouseX = xIn;
	pixelX = xDistance / iWidth;
	mapX = pixelX * mouseX + left;
	mouseY = iHeight - yIn;
	pixelY = yDistance / iHeight;
	mapY = pixelY * mouseY + bottom;
}
//-----------------------------------------------------------------------
//	getImageXY
//-----------------------------------------------------------------------
function getImageXY(e) {
	mouseX=event.clientX + document.body.scrollLeft;
	mouseY=event.clientY + document.body.scrollTop;
	// subtract offsets from page left and top
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;
   	//alert("mouseX= " + mouseX + " mouseY= " + mouseY);
}	
//-----------------------------------------------------------------------
//	start zoom in.... box displayed
//-----------------------------------------------------------------------
function startZoomBox(e) {
	//alert("startZoomBox");
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);	
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (zooming) {
		    	//alert("Gå till stopZoomBox()");
			stopZoomBox(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);
			zooming=true;
			showLayer("zoomBoxTop");
			showLayer("zoomBoxLeft");
			showLayer("zoomBoxRight");
			showLayer("zoomBoxBottom");
		}
	}
	return false;
}
//-----------------------------------------------------------------------
//	stop zoom box display... zoom in
//-----------------------------------------------------------------------
function stopZoomBox(e) {
	//alert("stopZoomBox");
	zooming=false;
	if ((zright <zleft+2) && (zbottom < ztop+2)) {
		zoomin(e);
	}
	else {
		var tempLeft=lastLeft;
		var tempRight=lastRight;
		var tempTop=lastTop;
		var tempBottom=lastBottom;
		saveLastExtent();
		pixelX = xDistance / iWidth;
		var theY = iHeight - ztop;
		pixelY = yDistance / iHeight;
		top = pixelY * theY + bottom;
		right = pixelX * zright + left;
		left = pixelX * zleft + left;
		theY = iHeight - zbottom;
		pixelY = yDistance / iHeight;
		bottom = pixelY * theY + bottom;
		window.scrollTo(0,0);
        	// funkar ej bra checkFullExtent();
		//var theString = writeXML();
		sendMapXML();
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
	}
	return true;
}
//-----------------------------------------------------------------------
//	start zoom out... box displayed
//-----------------------------------------------------------------------
function startZoomOutBox(e) {
	//alert("startZoomOutBox");
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (zooming) {
			stopZoomOutBox(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);
			zooming=true;
			showLayer("zoomBoxTop");
			showLayer("zoomBoxLeft");
			showLayer("zoomBoxRight");
			showLayer("zoomBoxBottom");
		}
	}
	return false;
}
//-----------------------------------------------------------------------
//	stop zoom out box. . . zoom out
//-----------------------------------------------------------------------
function stopZoomOutBox(e) {
	//alert("stopZoomOutBox");
	zooming=false;
	if ((zright <zleft+2) && (zbottom < ztop+2)) {
		zoomout(e);
	}
	else {	
		var tempLeft=lastLeft;
		var tempRight=lastRight;
		var tempTop=lastTop;
		var tempBottom=lastBottom;
		saveLastExtent();
		var zWidth = Math.abs(zright-zleft);
		var zHeight = Math.abs(ztop-zbottom);
		var xRatio = iWidth / zWidth;
		var yRatio = iHeight / zHeight;
		var xAdd = xRatio * xDistance / 2;
		var yAdd = yRatio * yDistance / 2;
		left = left - xAdd;
		right = right + xAdd;
		top = top + yAdd;
		bottom = bottom - yAdd;
		window.scrollTo(0,0);
		if (enforceFullExtent) {
			if ((right-left)>fullWidth) {
				left = fullLeft;
				right = fullRight;
				top = fullTop;
				bottom = fullBottom;
				lastLeft=tempLeft;
				lastRight=tempRight;
				lastTop=tempTop;
				lastBottom=tempBottom;
			}
		} 
		//var theString = writeXML();
		sendMapXML();
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
	}
	return true;
}
//-----------------------------------------------------------------------
//	clip zoom box layer to mouse coords
//-----------------------------------------------------------------------
function setClip() {	
	//alert("setClip");
	var tempX=x1;
	var tempY=y1;
	if (x1>x2) {
		zright=x1;
		zleft=x2;
	}
	else {
		zleft=x1;
		zright=x2;
	}
	if (y1>y2) {
		zbottom=y1;
		ztop=y2;
	}
	else {
		ztop=y1;
		zbottom=y2;
	}
	
	if ((x1 != x2) && (y1 != y2)) {
		//clipLayer("zoomBox",zleft,ztop,zright,zbottom);
		boxIt(zleft,ztop,zright,zbottom);
	}
}
//-----------------------------------------------------------------------
//	zoom in around mouse click
//-----------------------------------------------------------------------
function zoomin(e) {
	//alert("zoomin");
	getMapXY(mouseX,mouseY);
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	saveLastExtent();
	left = mapX - (xHalf/zoomFactor);
	right = mapX + (xHalf/zoomFactor);
	top = mapY + (yHalf/zoomFactor);
	bottom = mapY - (yHalf/zoomFactor);
	sendMapXML();
}
//-----------------------------------------------------------------------
//	zoom out from mouse click
//-----------------------------------------------------------------------
function zoomout(e) {
	//alert("zoomout");
	getMapXY(mouseX,mouseY);
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	saveLastExtent();
	left = mapX - (xDistance*zoomFactor/2);
	right = mapX + (xDistance*zoomFactor/2);
	top = mapY + (yDistance*zoomFactor/2);
	bottom = mapY - (yDistance*zoomFactor/2);
	sendMapXML();
}	
//-----------------------------------------------------------------------
//	start pan.... image will move
//-----------------------------------------------------------------------
function startPan(e) {
	//alert("startPan");
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (panning) {
			stopPan(e);
		} else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			panning=true;
		}
	}
	return false;
}
//-----------------------------------------------------------------------
//	stop moving image.... pan 
//-----------------------------------------------------------------------
function stopPan(e) {
	//alert("stopPan");
	window.scrollTo(0,0);
	panning=false;
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	saveLastExtent();
	var ixOffset = x2-x1;
	var iyOffset = y1-y2;
	pixelX = xDistance / iWidth;
	var theY = iHeight - ztop;
	pixelY = yDistance / iHeight;
	var xOffset = pixelX * ixOffset;
	var yOffset = pixelY * iyOffset;
	top = top - yOffset;
	right = right - xOffset;
	left = left - xOffset;
	bottom = bottom - yOffset;
	checkFullExtent();
	hideLayer("theMap");
	//var theString = writeXML();
	sendMapXML();
	return true;
}
//-----------------------------------------------------------------------
//	move map image with mouse
//-----------------------------------------------------------------------
function panMouse() {
	//alert("panMouse");
	var xMove = x2-x1;
	var yMove = y2-y1;
	var cLeft = -xMove;
	var cTop = -yMove;
	var cRight = iWidth;
	var cBottom = iHeight;
	if (xMove>0) {
		cLeft = 0;
		cRight = iWidth - xMove;
	}
	if (yMove>0) {
		cTop = 0;
		cBottom = iHeight - yMove;
	}
	clipLayer("theMap",cLeft,cTop,cRight,cBottom);
	moveLayer("theMap",xMove+hspc,yMove+vspc);
}
//-----------------------------------------------------------------------
//	pan to mouse click
//-----------------------------------------------------------------------
function pan(e) {
	getMapXY(mouseX,mouseY);
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	saveLastExtent();
	left = mapX - xHalf;
	right = mapX + xHalf;
	top = mapY + yHalf;
	bottom = mapY - yHalf;
	//var theString = writeXML();
	sendMapXML();
}
//-----------------------------------------------------------------------
//	setZoomColor
//-----------------------------------------------------------------------
function setZoomColor() {
	setLayerBackgroundColor("zoomBoxTop", zoomBoxColor);
	setLayerBackgroundColor("zoomBoxLeft", zoomBoxColor);
	setLayerBackgroundColor("zoomBoxRight", zoomBoxColor);
	setLayerBackgroundColor("zoomBoxBottom", zoomBoxColor);
}
//-----------------------------------------------------------------------
//	markera fastighet(er)
//-----------------------------------------------------------------------
function writeFeature() {
	//alert("writeFeature " + featureValue);
	// kod för highlight av polygon 
	featureString = '<LAYER type="featureclass" name="showFeatures" visible="true"><DATASET fromlayer="';
	featureString += featureLayer;
	featureString += '"/><QUERY where="';
	featureString += featureKey;
	featureString += ' = &apos;';
	featureString += featureValue;
	featureString += '&apos;"/><SIMPLERENDERER><SIMPLEPOLYGONSYMBOL fillcolor="255,128,0" filltype="solid" transparency="0.5" boundary="true" boundarytype="solid" boundarycolor="255,255,0" boundarywidth="2"/></SIMPLERENDERER></LAYER>';

	var x1=-1;
	var y1=-1;

	//visa sökt fastighet
	x1 = ((featureX - left) / (right - left) * iWidth) - 35;
	y1 = ((featureY - bottom) / (top - bottom) * iHeight);
	featureString += '<LAYER type="acetate" name="acetate" id="acetate">';
	featureString += '<OBJECT units="pixel">';
	//featureString += '<TEXT coords="165 200" label="';
	featureString += '<TEXT coords="';
	featureString += x1;
	featureString += ' ';
	featureString += y1;
	featureString += '" label="';
	featureString += featureValue;
	featureString += '"><TEXTMARKERSYMBOL font="Arial" />';
	featureString += '</TEXT></OBJECT></LAYER>';
	//alert(featureString)
	
}
//-----------------------------------------------------------------------
//	get max min in reply
//-----------------------------------------------------------------------
function getMaxMin(theReply) {
	//alert("getMaxMin"+theReply + " XMLMode " + XMLMode );
	var startpos = theReply.indexOf("count=",startpos)+7;
	var endpos = theReply.indexOf("hasmore",startpos)-2;
	var antal = theReply.substring(startpos,endpos);
	//alert("startpos endpos antal= " + startpos +  " " + endpos + " " + antal);
  
	var xmax=limitLeft;
	var xmin=limitRight;
	var ymax=limitBottom;
	var ymin=limitTop;
	var varde;
  
	if (antal>0) {
		startpos=0;
		for (var i=0;i<antal;i++) {
			startpos = theReply.indexOf("minx=",startpos)+6;
			//endpos = theReply.indexOf("miny=",startpos)-2;
			endpos = startpos+7;
			varde = theReply.substring(startpos,endpos);
			//alert("varde= " + varde + "xmin= " + xmin);
 
			if (varde<xmin) {
				xmin=Number(varde)-100;
			}
			startpos = theReply.indexOf("miny=",startpos)+6;
			endpos = startpos+7;
			varde = theReply.substring(startpos,endpos);
			if (varde<ymin) {
				ymin=Number(varde)-100;
			}
			startpos = theReply.indexOf("maxx=",startpos)+6;
			endpos = startpos+7;
			varde = theReply.substring(startpos,endpos);
			if (varde>xmax) {
				xmax=Number(varde)+100;
			}
			startpos = theReply.indexOf("maxy=",startpos)+6;
			endpos = startpos+7;
			varde = theReply.substring(startpos,endpos);
			if (varde>ymax) {
				ymax=Number(varde)+100;
			}
		}

		if (XMLMode==1001) {
			//alert("1001");
			//alert("xmax: " + xmax + " xmin: " + xmin + " ymax: " + ymax + " ymin: " + ymin);		
			if (identifyLeft > xmin){
				identifyLeft = xmin;
			}
			if (identifyRight < xmax){
				identifyRight = xmax;
			}
			if (identifyTop < ymax){
				identifyTop = ymax;
			}
			if (identifyBottom > ymin){
				identifyBottom = ymin;
			}
			// kod för att spara koordinat för utskrift av fbet
			//alert(identifyCount);
			identifyX[identifyCount] = (xmax+xmin)/2;
			identifyY[identifyCount] = (ymax+ymin)/2;
		}
		else {
			//alert("1002");
			//alert("xmax: " + xmax + " xmin: " + xmin + " ymax: " + ymax + " ymin: " + ymin);
			identifyLeft = xmin;
			identifyRight = xmax;
			identifyTop = ymax;
			identifyBottom = ymin;
			featureX = (xmax+xmin)/2; 
			featureY = (ymax+ymin)/2;
			zoomToEnvelope(xmin,ymin,xmax,ymax);
		}
	}
}
//-----------------------------------------------------------------------
//	markera adress   (?) adress hämtas från fdb levererar koordinater
//-----------------------------------------------------------------------
function writeAdress() {
	//alert("writeAdress " + adressX);

	adressString = '<LAYER type="ACETATE" name="theAddress">\n'; 
	adressString += '<OBJECT units="database">\n<POINT coords="' + adressX + ' ' + adressY + '">\n';
	adressString += '<SIMPLEMARKERSYMBOL outline="0,0,0" type="star"';
	adressString += ' color="255,0,0" width="18" />\n</POINT>\n</OBJECT>\n';
	adressString += '</LAYER>\n';

	//alert(adressString)
}
//-----------------------------------------------------------------------
//	markera objekt
//-----------------------------------------------------------------------
function writeObject() {

	if (serviceType == "I") {
		//alert("writeObject ArcIMS image service");
		// markera objekt
		objectString += '<LAYER type="ACETATE" name="theObject">\n'; 
		objectString += '<OBJECT units="database">\n<POINT coords="' + objectX + ' ' + objectY + '">\n';
		objectString += '<SIMPLEMARKERSYMBOL outline="0,0,0" type="' + objectType;
		objectString += '" color="' + objectColor + '" width="' + objectSize + '" />\n</POINT>\n</OBJECT>\n';
		objectString += '</LAYER>\n';

	}
	else {

		//alert("writeObject ArcMap server");
		objectString += '</LAYER>\n';
		// markera objekt
		objectSize = defaultSize;
		switch(objectType) {
		case "circle":
			objectSize = circleSize;
			break
		case "triangle":
			objectSize = triangleSize;
			break
		case "square":
			objectSize = squareSize;
			break
		default:
			objectSize = defaultSize;
			break
		} 
		objectString += '<LAYER type="ACETATE" name="theObject">\n'; 
		objectString += '<OBJECT units="database">\n<POINT coords="' + objectX + ' ' + objectY + '">\n';
		objectString += '<SIMPLEMARKERSYMBOL outline="0,0,0" type="' + objectType;
		objectString += '" color="' + objectColor + '" width="' + objectSize + '" />\n</POINT>\n</OBJECT>\n';
		objectString += '</LAYER>\n';
	}

	//alert(objectString);
}
//-----------------------------------------------------------------------
//	markera kategori   (?)
//-----------------------------------------------------------------------
function writeKat() {
/*
	//alert("writeKat ");
	var objectSize = defaultSize;
	katString = '';
	if (katCount>0) {

		if (serviceType == "I") {
			// ArcIMS image service
			for (var i=0;i<katCount;i++) {

				katString += '<LAYER type="ACETATE" name="kat'+ i + '">\n'; 
				katString += '<OBJECT units="database">\n<POINT coords="' + katX[i] + ' ' + katY[i] + '">\n';
				katString += '<SIMPLEMARKERSYMBOL outline="0,0,0" type="' + katType[i];
				katString += '" color="' + katColor[i] + '" width="9" />\n</POINT>\n</OBJECT>\n';
				katString += '</LAYER>\n';
			}
		}
		else {
			// ArcMap server
			for (var i=0;i<katCount;i++) {

				switch(katType[i]) {
				case "circle":
					objectSize = circleSize;
					break
				case "triangle":
					objectSize = triangleSize;
					break
				case "square":
					objectSize = squareSize;
					break
				default:
					objectSize = defaultSize;
					break
				}

				katString += '<LAYER type="ACETATE" name="kat'+ i + '">\n'; 
				katString += '<OBJECT units="database">\n<POINT coords="' + katX[i] + ' ' + katY[i] + '">\n';
				katString += '<SIMPLEMARKERSYMBOL outline="0,0,0" type="' + katType[i];
				katString += '" color="' + katColor[i] + '" width="' + objectSize + '" />\n</POINT>\n</OBJECT>\n';
				katString += '</LAYER>\n';
			}
		}
	}
	//alert(katString);
*/	
}
//-----------------------------------------------------------------------
//	ladda kategorier   (?)
//-----------------------------------------------------------------------
function loadKat(countIn,xIn,yIn,colorIn,typeIn) {
/*
	//alert("loadKat " + countIn + " " + xIn + " " + yIn + " " + colorIn + " " + typeIn);
  
	var startX = 0;
	var endX = 0;
	var startY = 0;
	var endY = 0;
	var startColor = 0;
	var endColor = 0;
	var startType = 0;
	var endType = 0;

	if (countIn>0) {
		katCount = countIn; 
		for (var i=0;i<countIn;i++) {

			endX = xIn.indexOf(";",startX);
			katX[i] = xIn.substring(startX,endX);
			//alert(startX + '-' + endX + '-' + katX[i]);
			startX = endX + 1;

			endY = yIn.indexOf(";",startY);
			katY[i] = yIn.substring(startY,endY);
			//alert(startY + '-' + endY + '-' + katY[i]);
			startY = endY + 1;

			endColor = colorIn.indexOf(";",startColor);
			katColor[i] = colorIn.substring(startColor,endColor);
			//alert(startColor + '-' + endColor + '-' + katColor[i]);
			startColor = endColor + 1;

			endType = typeIn.indexOf(";",startType);
			katType[i] = typeIn.substring(startType,endType);
			//alert(startType + '-' + endType + '-' + katType[i]);
			startType = endType + 1;
		}
	}
*/
}
//-----------------------------------------------------------------------
//	zoom to identify    (?)
//-----------------------------------------------------------------------
function zoomToIdentify() {
	//alert("zoomToIdentify");
	zoomToEnvelope(identifyLeft,identifyBottom,identifyRight,identifyTop);
}
