

// ----- Event-Handling -----

var isNav4 = (document.layers)? true:false;
var isNav6 = (document.getElementById)? true:false;
var isIE4 = (document.all)? true:false;
var is5up = false;
if (isIE4) { if (navigator.userAgent.indexOf('MSIE 5')>0) is5up = true; if (isNav6) isNav6 = false; }



var fmodus = false;
var clickFirst	= new pKobj();
var moveLast 		= new pKobj();
var crdPosX	= new Array();
var crdPosY	= new Array();
var crdGssR = new Array();
var crdGssH = new Array();
var winMapEv;
var winZoomEv;
var nmapWidth;
var nmapHeight;
var simgSize;
var smapSize;
var bClicked	= false;
var bDrag 		= false;
var bMessclick = false;
var jg;
var messID	= 'disabled';
var gaussID = 'disabled';
var nPoints = 0;
var nallWay = 0;
var nclicks = 0;
var MAX_CLICKS = 20;
//////////////////////////////////////////////////////////////////////
var dRw;
var dHw;
var plGCoords = null;
var narrCount = 0;
//////////////////////////////////////////////////////////////////////


// ----- Window Handles -----

// Map 
var winMap	= parent.MapFrame;
// Layer 
var winLay	= parent.LayFrame;
// Legende 
var winLeg	= parent.LegFrame;
var winLegTwo	= parent.LegFrameTwo;
// Toolbar 
var winTool	= parent.ToolFrame;
// Statuswindow 
var winStat	= parent.StatFrame;
// Suchwindow 
var winSearch	= parent.SearchFrame;


function init(picName)
{
	nmapWidth=Number(document.mapserv.mapwidth.value)
	nmapHeight=Number(document.mapserv.mapheight.value)

	winMapEv = getPicData(document.images[picName], '[mapsize]');
	window.objContainer		= getLayerbez('mapLayID');
	window.objZoomSquare	= getLayerbez('zoomLayID');
	window.objMove				= getLayerbez('moveLayID');	
	window.objScalebar		= getLayerbez('scaleLayID');	

	window.objCross				= getLayerbez('crossLayID');
	pFobj_set(objScalebar, new pFobj(20, nmapHeight-50));
	pFobj_setVisibility(objScalebar, true);	

	pFobj_set(objContainer, winMapEv);
	pFobj_set(objScalebar, new pFobj(20, nmapHeight-30));
	pFobj_setVisibility(objContainer, true);

	var tmp = winTool.document.getElementById("streetID").value;
	if(tmp== "enabled")
	{
		pFobj_set(objCross, new pFobj((nmapWidth / 2) - 10, (nmapHeight / 2) - 10, 10, 10));
 		pFobj_setVisibility(objCross, true);
	}
	winTool.document.getElementById("streetID").value = "disabled";
	tmp = "disabled";
 	if (isNav4)
 	{
  	objContainer.captureEvents(Event.MOUSEDOWN)
  	document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
 	}
	objContainer.onmousedown = clickIt;
 	document.onmousemove = showMousePos;
 	document.onmouseup = sendIt;
 	document.onkeydown = messUp;
	document.ondblclick = messUp;
}


function initLeg()
{
	var pseudo =1;
	if(!winMap.document.getElementById("divRefID")) alert('no');
	window.objRef = winMap.document.getElementById("divRefID");
 	objRef.onmousedown = refClicked;
	
}


function refClicked(evt)
{
	e = evt || winMap.event;
	if(!e) 	
	{	alert('null');
		return null; 
	}
	
 	var clickRef = getCoords (e);	
	
	var width =  winMap.document.getElementById("refID").width;
	var Height = winMap.document.getElementById("refID").height;	

	var img = winMap.document.getElementById("refID");
	var oLeft = winMap.document.getElementById("refID").offsetLeft;	
	var oTop = winMap.document.getElementById("refID").offsetTop;		
	var x  = (clickRef.x);
	var y  = (clickRef.y);	
    loadLayer('statLayID','visible');	
 	winMap.document.mapserv.imgbox.value = "";	
 	winMap.document.mapserv.imgxy.value =  "";
    winMap.document.mapserv.mapxy.value =  "";	
 	winMap.document.mapserv.refxy.value =  x + " " + y;
 	winMap.document.mapserv.zoom.value = 1;
 	winMap.document.mapserv.submit();

}

function submitIt()
{ 

	doResize();	
	loadLayer('statLayID','visible');	
 	winMap.document.mapserv.mode.value = "browse";	
 	winMap.document.mapserv.refxy.value = ""; 	
 	winMap.document.mapserv.zoomdir.value = 1;
 	winMap.document.mapserv.imgbox.value = "";
 	winMap.document.mapserv.imgxy.value = "";
	winMap.document.mapserv.mapxy.value =  ""; 	
	winMap.document.mapserv.scale.value = "";    	
 	winMap.document.mapserv.mapext.value = "";	
 	winMap.document.mapserv.submit();


}

function submitMess()
{ 

	winStat.document.getElementById("c3").value = "";	
	winStat.document.getElementById("c4").value = "";	
	winStat.document.getElementById("c5").value = "";	

	winMap.jg.clear();

}

function setLayercheck(layername)
{	
if (winLegTwo.document.getElementById(layername).checked == false)
 	{
 	winMap.document.getElementById(layername).checked = false;	
 	}
 	else
 	{
 	winMap.document.getElementById(layername).checked = true;	
 	}
	changescale();
}

function setLayercheckthema(layername)
{	
var pLayer = winMap.document.getElementsByName("layer");
	for(var i=0, n=pLayer.length; i < n; i++)
		pLayer[i].checked = false;		
	winMap.document.getElementById(layername).checked = true;	
	changescale();
 	}



function searchLoc(x1, y1)
{
	doResize();	
	loadLayer('statLayID','visible');
	winTool.document.getElementById("streetID").value = "enabled";	
 	winMap.document.mapserv.mode.value = "browse";
	winMap.pm.changeImg('imgZoomin_p')
	winMap.pm.changeDoc('docEmpty')	
 	winMap.document.mapserv.refxy.value = ""; 	
 	winMap.document.mapserv.zoomdir.value = 1;
 	winMap.document.mapserv.imgbox.value = "";
 	winMap.document.mapserv.imgxy.value = "";
    winMap.document.mapserv.mapxy.value = x1 + " " +y1;	
 	winMap.document.mapserv.scale.value = winSearch.document.getElementById("scaleID").value / 1.3333333333;
  	winMap.document.mapserv.submit();

}

function checkRef()
{
if (winTool.document.getElementById("refhiddenID").value == 'visible')
	{
	winMap.document.getElementById("refLayerID").style.visibility = 'visible';
	}
}

function showref()
{
if (winTool.document.getElementById("refhiddenID").value == 'hidden')
	{
	winMap.document.getElementById("refLayerID").style.visibility = 'visible';
	winTool.document.getElementById("refhiddenID").value = 'visible';
	}
	else if (winTool.document.getElementById("refhiddenID").value == 'visible')
	{
	winMap.document.getElementById("refLayerID").style.visibility = 'hidden';
	winTool.document.getElementById("refhiddenID").value = 'hidden';
	}
}



function changescale()
{
	doResize();
	loadLayer('statLayID','visible');
	winMap.document.mapserv.mode.value = "browse";
	winMap.pm.changeImg('imgZoomin_p')
	winMap.pm.changeDoc('docEmpty')	
 	winMap.document.mapserv.refxy.value = ""; 	
 	winMap.document.mapserv.zoomdir.value = 1;
 	winMap.document.mapserv.imgbox.value = "";
 	winMap.document.mapserv.imgxy.value = "";
    var RW1= winMap.document.mapserv.imgext.value.substring(0,14);
 	var HW1= winMap.document.mapserv.imgext.value.substring(15,29);
 	var RW2= winMap.document.mapserv.imgext.value.substring(30,44);
 	var HW2= winMap.document.mapserv.imgext.value.substring(45,59); 
 	var RWNEU = (RW2*1 + RW1*1)/2;
 	var HWNEU = (HW2*1 + HW1*1)/2;
	winMap.document.mapserv.mapxy.value = RWNEU + " " + HWNEU;
 	winMap.document.mapserv.scale.value = winTool.document.getElementById("scaleID").value / 1.3333333333;
 	winMap.document.mapserv.submit();
}

function scaleaktuell()
{
winTool.document.getElementById("scaleID").value = Math.round(winMap.document.mapserv.scale.value * 1.3333333333);

}

function moveMapNow(bildext)
{
	loadLayer('statLayID','visible');
	winMap.document.mapserv.mapext.value = bildext;
 	winMap.document.mapserv.refxy.value = ""; 		
	winMap.document.mapserv.mode.value = "browse";
	winMap.document.mapserv.zoomdir.value = 1;
	winMap.document.mapserv.zoom.value = 1;	
	winMap.document.mapserv.imgbox.value = "";  
	winMap.document.mapserv.imgxy.value = "";
    winMap.document.mapserv.mapxy.value =  "";	
	winMap.document.mapserv.submit();
}


function getPicData(img, mapsize)
{
	massarray = mapsize.split(' ');
 	if (isNav4) return new pFobj(img.x, img.y, massarray[0], massarray[1]);
 	else 
 	{
  	var canvas = new pFobj(0, 0, massarray[0], massarray[1]);
  	while(img) 
  	{
   		canvas.x += img.offsetLeft;
   		canvas.y += img.offsetTop;
   		img = img.offsetParent;
   		
  	}
  	canvas.width = nmapWidth;
  	canvas.height = nmapHeight;
  	return canvas;
 	}
}


function getLayerbez(bildid, document)
{
	if (!document) document = winMap.document;
 	if (isNav4) 
 	{
 		for (var l = 0; l < document.layers.length; l++)
  		if (document.layers[l].id == bildid) return document.layers[l];
  	for (var l = 0; l < document.layers.length; l++) 
  	{
  		var result = getLayerbez(bildid, document.layers[l].document);
   		if (result) return result;
  	}
  	return null;
 	}
	else if (isIE4) return document.all[bildid];
	else if (isNav6) return document.getElementById(bildid);
	else return null;
}


function showMousePos(evt)
{
	e = evt || window.event;
	if(!e) 	return null; 
	comPos = getMousePos(e);
	var gsCoord = getGauss(comPos);
	window.status = 'Gauss-Krueger Zone 2 / ' + gsCoord[0] + ', ' + gsCoord[1] + ' /';
		
	pullIt(evt);
	return true;
}


function getMousePos(evt)
{
	e = evt || window.event;
	if(!e) 	return null;
	
	if(document.layers)	return new pCoords (e.pageX, e.pageY);
	else if (window.opera)	return new pCoords (e.clientX, e.clientY);
	else if(document.all)		return new pCoords (e.x +document.body.scrollLeft, e.y + document.body.scrollTop);
	else if(document.getElementById)		return new pCoords (e.pageX, e.pageY);

}


function clickIt(evt)
{
	var modstatus = winMap.document.mapserv.mode.value;
 	gaussID = winTool.document.getElementById("gaussID").value;
 	gaussSearchID = winTool.document.getElementById("gaussSearchID").value; 	
 	messID = winTool.document.getElementById("messID").value; 	
 	e = evt || window.event;
	if(!e) 	return null; 

 	winMap.clickFirst = getCoords (e);
 	bClicked = true;
 	if ((modstatus == 'browse') && (gaussID=='disabled')&& (gaussSearchID=='disabled')&& (messID=='disabled'))
 	{
  	var zoomstatus = winMap.document.mapserv.zoomdir.value;
  	if ((zoomstatus == 1) || (zoomstatus==0))
  	{
  		if(winTool.document.getElementById("cursorID").value == 'crosshair')
   			pFobj_set(objZoomSquare, new pFobj (clickFirst.x, clickFirst.y, 1, 1));
   		else
   		{
   			bDrag = true;
		    winMap.document.getElementById("imgID").src = "img/dot.gif";   			
   			var pCoord = getCoords (E);
   			pFobj_set(objMove, new pFobj (20, 20,  nmapWidth, nmapHeight));   			
    		pFobj_setVisibility(objMove, true);   			   			
			}
   		winMap.fmodus = true;

  	}
  	else
  	{
   		var x1 = window.clickFirst.x - winMapEv.x;
   		var y1 = window.clickFirst.y - winMapEv.y;
   		loadLayer ('statLayID','visible');
   		window.document.mapserv.imgbox.value = "";
		window.document.mapserv.refxy.value = ""; 	   		
   		window.document.mapserv.imgxy.value = x1 + " " + y1;
		winMap.document.mapserv.mapxy.value =  "";	

  	}
  	
   if((zoomstatus == -1) && (winMap.fmodus == false)) winMap.fmodus = true;
  	
  }	
  else if((gaussID == 'enabled') || (gaussSearchID=='enabled'))	
 	{
 		pFobj_set(objCross, new pFobj(clickFirst.x-10, clickFirst.y-10, 10, 10));
		pFobj_setVisibility(objCross, true);
		var comPos = getMousePos(e);
		var gsCoord = getGauss(comPos);
		if(gaussID =='enabled')
		{
			winStat.document.getElementById("c1").value = gsCoord[0];
			winStat.document.getElementById("c2").value = gsCoord[1];			
		}
		else if(gaussSearchID=='enabled') 
		{
			winSearch.document.getElementById("c1").value = gsCoord[0];
			winSearch.document.getElementById("c2").value = gsCoord[1];			
			
		}	
 	}

  else if(messID == 'enabled')	
 	{
 		if(nclicks < MAX_CLICKS) 		sumGauss();
 		else
 		{	 
 			alert('Maximale Anzahl von Punkten erreicht, schliesse Polygon...');
 			messUp();
 		}
 	} 	
 	else if (modstatus == 'query')
 	{

  	var x1 = window.clickFirst.x - winMapEv.x;
  	var y1 = window.clickFirst.y - winMapEv.y;	
  	loadLayer ('statLayID','visible');
  	window.document.mapserv.imgbox.value = "";
  	window.document.mapserv.savequery.value = "true";
	window.document.mapserv.refxy.value = ""; 	  	
  	window.document.mapserv.imgxy.value = x1 + " " + y1;	
	
	e = evt || window.event;
	if(!e) 	return null; 
	comPos = getMousePos(e);
	var gsCoord = getGauss(comPos);
	winMap.document.mapserv.mapxy.value =  gsCoord[0] + " " + gsCoord[1];

  	window.document.mapserv.submit();
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////////	
//Zeichenfunktionen	
	
	  else if(gaussID == 'addpoly')
 	{
 		pFobj_set(objCross, new pFobj(clickFirst.x-10, clickFirst.y-10, 10, 10));
		var comPos = getMousePos(e);
		var gsCoord = getGauss(comPos);
		if(gaussID =='addpoly')
		{
			var str = "";	
			str = String(gsCoord[0]) + " " + String(gsCoord[1]) + "-";
       		winSearch.document.getElementById("coordsID").value = winSearch.document.getElementById("coordsID").value + str;
			var xyfirst = winSearch.document.getElementById("coordsID").value.substring(0,15);			
			drawPoly();
		}		
 	}
	
	else if(gaussID == 'addpoint')
 	{
 		pFobj_set(objCross, new pFobj(clickFirst.x-10, clickFirst.y-10, 10, 10));
		pFobj_setVisibility(objCross, true);
		var comPos = getMousePos(e);
		var gsCoord = getGauss(comPos);
		if(gaussID =='addpoint')
		{
			winSearch.document.getElementById("RW").value = gsCoord[0].toString();
			winSearch.document.getElementById("HW").value = gsCoord[1].toString();	
			
		}		
 	}
	
	else if(gaussID == 'addlink')
 	{
 		pFobj_set(objCross, new pFobj(clickFirst.x-10, clickFirst.y-10, 10, 10));
		pFobj_setVisibility(objCross, true);
		var comPos = getMousePos(e);
		var gsCoord = getGauss(comPos);
		if(gaussID =='addlink')
		{
			winStat.document.getElementById("RW").value = gsCoord[0].toString();
			winStat.document.getElementById("HW").value = gsCoord[1].toString();	
			winStat.document.getElementById("mapxy").value = gsCoord[0].toString()+ " " + gsCoord[1].toString();
			
		}		
 	}
	
}	

function cutLine()
{
	var coordlaenge = winSearch.document.getElementById("coordsID").value.length -1;
	winSearch.document.getElementById("coordsID").value = winSearch.document.getElementById("coordsID").value.substring(0,coordlaenge);
}

function closePoly()
{
	var xyfirst = winSearch.document.getElementById("coordsID").value.substring(0,15);
	winSearch.document.getElementById("coordsID").value = winSearch.document.getElementById("coordsID").value + xyfirst;	

}

function drawPoly()
{
		var comPos = getMousePos(e);
		var gaussPos = getGauss(comPos);
 		
		if(!bMessclick)
		{
			nPoints = 0;
			crdPosX[nPoints] = window.clickFirst.x;
			crdPosY[nPoints] = window.clickFirst.y;
			crdGssR[nPoints] = gaussPos[0];
			crdGssH[nPoints] = gaussPos[1];
				
			jg = new jsGraphics("mapLayID")					
	  	jg.setColor("#FF00FF"); // schwarz	
	  	jg.setStroke(2); // schwarz	 			
			bMessclick = true;
			nPoints++;
		}
		else
		{		
			crdPosX[nPoints] = window.clickFirst.x;
			crdPosY[nPoints] = window.clickFirst.y;
			crdGssR[nPoints] = gaussPos[0];
			crdGssH[nPoints] = gaussPos[1];	
				
	  	jg.drawLine(crdPosX[nPoints-1], crdPosY[nPoints-1], crdPosX[nPoints], crdPosY[nPoints]); // Koordinaten auf Zeichenfläche bezogen
	  	jg.paint();
	  	nPoints++;	  	
		}
		nclicks++;
	
}

//Ende Zeichenfunktionen
/////////////////////////////////////////////////////////////////////////////////////////////////////////

function sumGauss()
{
		var comPos = getMousePos(e);
		var gaussPos = getGauss(comPos);
 		
		if(!bMessclick)
		{
			nPoints = 0;
			crdPosX[nPoints] = window.clickFirst.x;
			crdPosY[nPoints] = window.clickFirst.y;
			crdGssR[nPoints] = gaussPos[0];
			crdGssH[nPoints] = gaussPos[1];
				
			jg = new jsGraphics("mapLayID")					
	  	jg.setColor("#ff0000"); // rot	
	  	jg.setStroke(2); // rot	  			
			bMessclick = true;
			nPoints++;
		}
		else
		{		
			crdPosX[nPoints] = window.clickFirst.x;
			crdPosY[nPoints] = window.clickFirst.y;
			crdGssR[nPoints] = gaussPos[0];
			crdGssH[nPoints] = gaussPos[1];	
				
	  	jg.drawLine(crdPosX[nPoints-1], crdPosY[nPoints-1], crdPosX[nPoints], crdPosY[nPoints]); // Koordinaten auf Zeichenfläche bezogen
	  	jg.paint();
	  	
	  	var a = crdGssR[nPoints] > crdGssR[nPoints-1] ? (crdGssR[nPoints] - crdGssR[nPoints-1]) : (crdGssR[nPoints-1] - crdGssR[nPoints]);
	  	var b = crdGssH[nPoints] > crdGssH[nPoints-1] ? (crdGssH[nPoints] - crdGssH[nPoints-1]) : (crdGssH[nPoints-1] - crdGssH[nPoints]);
	  	a = a * a;
	  	b = b * b;
	  	var c = Math.sqrt(a+b);
	  	var str = String(c);
	  	nallWay += c;
	  	if(str.indexOf(".") > 0) 	str = str.substring(0, str.indexOf(".") + 3);
	  	winStat.document.getElementById("c3").value = str;
			str = String(nallWay);
	  	if(str.indexOf(".") > 0) 	str = str.substring(0, str.indexOf(".") + 3);
	  	winStat.document.getElementById("c4").value = str;

	  	nPoints++;	  	
		}
		winStat.document.getElementById("c1").value = gaussPos[0];
		winStat.document.getElementById("c2").value = gaussPos[1];	
		nclicks++;
	
}

function pullIt(evt)
{
	var modstatus = window.document.mapserv.mode.value;
 	var zoomstatus = window.document.mapserv.zoomdir.value;
  var tmp = winTool.document.getElementById("cursorID").value;

	E = evt || window.event; 	 		
	if(!e) return null; 
	
	
	var pCoord = getCoords (E);
 	var dx = pCoord.x - window.clickFirst.x;
 	var dy = pCoord.y - window.clickFirst.y;
	var x = dx;
	var y = dy;
	

	if(messID=='enabled')
	{
		var kx = (pCoord.x<window.clickFirst.x) ? 1 : 1;
		var ky = (pCoord.y<window.clickFirst.y) ? 1 : 1;		
		if (isNav4)
		{
  		for (i = 0; i < 8; i++)
				with(layerbez('punkt'+(i+1)) ) 
				{ left = window.clickFirst.x+((dx/8)*i)*kx; 
					top = window.clickFirst.y+((dy/8)*i)*ky; 
					visibility = 'show';
				}

		}
		else
		{
			for (i = 0; i < 8; i++)
			{
				with(getLayerbez('punkt'+(i+1)) ) 
				{ style.left = window.clickFirst.x+((dx/8)*i)*kx; 
					style.top = window.clickFirst.y+((dy/8)*i)*ky;
					style.visibility = 'visible';  
				}
			}		
		}
		

	}
	
	else if ((modstatus == 'browse') && (zoomstatus == 1) && (window.fmodus) && (tmp=='crosshair'))
 	{
	
   		if(Math.abs(dx) > 10 && Math.abs(dy) > 10)
   		{
    		pFobj_setVisibility(objZoomSquare, true);
    		if(isNav4) objZoomSquare.background.src="img/s.gif";
   		}
   		else
   		{
    		pFobj_setVisibility(objZoomSquare, false);
    		if(isNav4) objZoomSquare.background.src="";
   		}
   		winZoomEv = new pFobj(window.clickFirst.x, window.clickFirst.y);
   		if((dx) < 0) winZoomEv.x = (window.clickFirst.x + dx);
   		if((dy) < 0) winZoomEv.y = (window.clickFirst.y + dy );
   		winZoomEv .height = Math.abs(dy);
   		winZoomEv .width = Math.abs(dx);
   		if(	winZoomEv .x < winMapEv.x || winZoomEv .y < winMapEv.y
					|| winMapEv.width < (winZoomEv .width + winZoomEv .x - winMapEv.x) 
					|| winMapEv.height < (winZoomEv .height + winZoomEv .y - winMapEv.y)) return; 
   		else pFobj_set (objZoomSquare, winZoomEv );

 	}
	
	else if(bClicked && (modstatus == 'browse'))
	{
		
		window.moveLast.x= dx; window.moveLast.y = dy;


		if( ((window.clickFirst.x + x) > nmapWidth) || 
				((window.clickFirst.x + x) < 0) ||
				((window.clickFirst.y + y) > nmapHeight) || 
				((window.clickFirst.y + y) < 0) 				 ) 	sendIt();
		x= dx; y = dy;

		pFobj_set(objMove, new pFobj (x, y, nmapWidth, nmapHeight));   			
		
	}
}


function sendIt()
{
	var modstatus = window.document.mapserv.mode.value;
 	if (modstatus == 'browse')
 	{
  	var x1, y1, x2, y2;
  	var midX, midY;
  	if(fmodus == true)
  	{
   		if((!winZoomEv || (winZoomEv.width < 10 && winZoomEv.height < 10)) && !bDrag)
   		{
    		winMap.pm.changeDoc('docEmpty');
			x1 = window.clickFirst.x - winMapEv.x;
    		y1 = window.clickFirst.y - winMapEv.y;
    		window.document.mapserv.imgbox.value = "";
    		window.document.mapserv.imgxy.value = x1 + " " + y1;
   		}
   		else if(!bDrag)
   		{
			winMap.pm.changeDoc('docEmpty');
    		x1 = winZoomEv.x - winMapEv.x; y1 = winZoomEv.y - winMapEv.y;
    		x1 = x1 < 0 ? 0 : x1; y1 = y1 < 0 ? 0 : y1;
    		x2 = x1 + winZoomEv.width; y2 = y1 + winZoomEv.height;
    		x2 = x2 > (winMapEv.width + winMapEv.x) ? winMapEv.width + winMapEv.x : x2; y2 = y2 > (winMapEv.height + winMapEv.y) ? winMapEv.height + winMapEv.y : y2;
    		window.document.mapserv.imgbox.value = x1 + " " + y1 + " " + x2 + " " + y2;
    		window.document.mapserv.imgxy.value = "";
   		}
   		else
   		{
  			bDrag = false;
    		winMap.document.getElementById("zoomID").value = 0;   			
  			midX = nmapWidth / 2; midY = nmapHeight / 2;
    		x1 = midX - window.moveLast.x;
    		y1 = midY - window.moveLast.y;
    		window.document.mapserv.imgbox.value = "";
    		window.document.mapserv.imgxy.value = x1 + " " + y1;
   			
   		}
   		bClicked = false;
   		loadLayer ('statLayID','visible');
   		document.onmousemove = dummy;
 		winMap.document.mapserv.refxy.value = "";			
		winMap.document.mapserv.mapxy.value ="";
		winMap.document.mapserv.scale.value =""; 					
   		window.document.mapserv.submit();
  	}
 	}
 	
}


function messUp()
{

	if(messID == 'enabled')
	{
		messID = 'disabled';
		winTool.document.getElementById("messID").value = 'disabled';
		
	  jg.drawLine(crdPosX[nPoints-1], crdPosY[nPoints-1], crdPosX[0], crdPosY[0]); // Koordinaten auf Zeichenfläche bezogen
	  jg.paint();		
	  
  	var a = crdGssR[nPoints-1] > crdGssR[0] ? (crdGssR[nPoints-1] - crdGssR[0]) : (crdGssR[0] - crdGssR[nPoints-1]);
  	var b = crdGssH[nPoints-1] > crdGssH[0] ? (crdGssH[nPoints-1] - crdGssH[0]) : (crdGssH[0] - crdGssH[nPoints-1]);
  	a = a * a;
  	b = b * b;
  	var c = Math.sqrt(a+b);
  	var str = String(c);
  	nallWay += c;
  	if(str.indexOf(".") > 0) 	str = str.substring(0, str.indexOf(".") + 3);
  	winStat.document.getElementById("c3").value = str;
		str = String(nallWay);
  	if(str.indexOf(".") > 0) 	str = str.substring(0, str.indexOf(".") + 3);
  	winStat.document.getElementById("c4").value = str;
	  
	  
		
  	var nallSqr = 0;

  	for (i = 0; i < crdGssR.length - 1; i++)
   		nallSqr += (((crdGssR[i] + crdGssR[i+1]) * (crdGssH[i] - crdGssH[i+1])) / 2);
  	nallSqr += (((crdGssR[nPoints-1] + crdGssR[0]) * (crdGssH[nPoints-1] - crdGssH[0])) / 2);

  	
		var str = String(Math.abs(nallSqr));
	  if(str.indexOf(".") > 0) 	str = str.substring(0, str.indexOf(".") + 3);
	  winStat.document.getElementById("c5").value = str;
  			
		
		if (isNav4)
		{
  		for (var i = 0; i < 8; i++) with(layerbez('punkt'+(i+1)) )	visibility = 'hide';

		}
		else
		{
			for (var i = 0; i < 8; i++)
			{
				with(getLayerbez('punkt'+(i+1)) )	style.visibility = 'hidden';  
			}		
		}		
		
		for(var i=0; i < crdPosX.length; i++)
		{
			crdPosX[i] = 0; crdPosY[i] = 0; crdGssR[i] = 0; crdGssH[i] = 0;
		}
		nPoints = 0;
		nallWay = 0;
		nclicks = 0;
		bMessclick = false;
		
		
	}
}



function dummy()
{
	var pseudo =1;
}


function getCoords (evt) 
{
	E = evt || window.event;
 	if(!e) return null;
 	if(isNav4) return new pKobj(e.pageX, e.pageY);
 	else if(window.opera) return new pKobj(e.clientX, e.clientY);
 	else if(isIE4)
 	{
 		if(is5up) return new pKobj(e.x + document.body.scrollLeft, e.y + document.body.scrollTop);
  	else return new pKobj(e.x, e.y);
 	}
 	else if(isNav6) return new pKobj(e.pageX , e.pageY);
}


function loadLayer(ladung, zustand)
{
	var statuslayer = getLayerbez(ladung);
 	if (isNav4) statuslayer.visibility = zustand;
 	else statuslayer.style.visibility = zustand;
}


function setMode(mod,wert)
{

	winMap.document.mapserv.mode.value = mod;

}


function doResize()
{

	var cx = getMapWidth();
	var cy = getMapHeight();
	simgSize = String(cx) + ' ' + String(cy);
	smapSize = String(cx) + ' ' + String(cy);

	var tmp;	
	tmp = winMap.document.getElementById("imgsizeID").value;
	if(tmp != null) tmp = simgSize; 
	tmp = winMap.document.getElementById("mapsizeID").value;
 	if(tmp != null) tmp = smapSize; 
 	
 	winMap.document.getElementById("imgsizeID").value = simgSize;
 	winMap.document.getElementById("mapsizeID").value = smapSize;

}


function getMapWidth () 
{
	var nWidth = winMap.innerWidth;
	if (nWidth == null) 
	{
		nWidth = winMap.document.body.clientWidth;
	}
	return nWidth;
}


function getMapHeight () 
{
	var nHeight = winMap.innerHeight;
	if (nHeight == null) 
	{
		nHeight = winMap.document.body.clientHeight;
	}
	return nHeight;
}


function MM_reloadPage(init) 
{
  if (init==true) with (navigator) 
  {
  	if ((appName=="Netscape")&&(parseInt(appVersion)==4)) 
  	{
    	document.MM_pgW=innerWidth; 
    	document.MM_pgH=innerHeight; 
    	onresize=MM_reloadPage; 
    }
	}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);



function moveMap (fx, fy)
{
	loadLayer('statLayID','visible');
	s = winMap.document.mapserv.imgext.value;
	x1 = parseInt(s.substr(0, s.indexOf(" ")));
	s  = s.substr(s.indexOf(" ")+1);
	y1 = parseInt(s.substr(0, s.indexOf(" ")));
	s  = s.substr(s.indexOf(" ")+1);
	x2 = parseInt(s.substr(0, s.indexOf(" ")));
	y2 = parseInt(s.substr(s.indexOf(" ")+1));
	dx = (x2-x1)*fx;
	dy = (y2-y1)*fy;
	s = (" "+(x1+dx)+" "+(y1+dy)+" "+(x2+dx)+" "+(y2+dy)).substr(1);
	moveMapNow(s);
}



function getGauss(plCoord)
{
	var s = document.mapserv.imgext.value;

	
	var x1 = parseFloat(s.substr(0, s.indexOf(" ")));
	s  = s.substr(s.indexOf(" ")+1);
	var y1 = parseFloat(s.substr(0, s.indexOf(" ")));
	s  = s.substr(s.indexOf(" ")+1);
	var x2 = parseFloat(s.substr(0, s.indexOf(" ")));
	var y2 = parseFloat(s.substr(s.indexOf(" ")+1));

	var dGx = x1 + ((plCoord.getX() *  (x2 - x1 )) / nmapWidth);
	var dGy = y2 - ((plCoord.getY() *  (y2 - y1 )) / nmapHeight);


	var gsCoord = new Array(Math.round(dGx), Math.round(dGy));

	return gsCoord;
		
	
}	




// ----- Objekte und Objektmethoden -----

// ----- class pKobj -----
function pKobj(x,y)
{
	this.x=(!x)?0:x; this.y=(!y)?0:y;
	this.equals=pKobj_equals;
}


function pKobj_equals(c)
{
	return(this.x == c.x && this.y == c.y);
}





// ----- class pFobj -----
function pFobj(x, y, breite, hoehe)
{
 this.width=(!breite)?0:breite; 
 this.height=(!hoehe)?0:hoehe;
 this.Kobj= pKobj; 
 this.Kobj(x, y);
 this.equalsKobj=this.equals; 
 this.equals=pFobj_equals;
}


function pFobj_equals(c)
{
	return (this.equalsKobj == c.equalsKobj && this.width == c.width && this.height == c.height);
}


function pFobj_set (objLayer, canvas)
{
	if (isNav4) 
 	{
  	objLayer.top = canvas.y;
  	objLayer.left = canvas.x;
  	objLayer.clip.width = canvas.width;
  	objLayer.clip.height = canvas.height;
 	}
 	else if (window.opera) 
 	{
  	objLayer.style.top = canvas.y;
  	objLayer.style.left = canvas.x;
  	objLayer.style.height = canvas.height;
  	objLayer.style.width = canvas.width;
 	}
 	else if (isIE4) 
 	{
  	objLayer.style.top = canvas.y;
  	objLayer.style.pixelLeft = canvas.x;
  	objLayer.style.height = canvas.height;
 	objLayer.style.width = canvas.width;
  	
  }
 	else if (isNav6) 
 	{
  	objLayer.style.top = canvas.y + 'px'; 
  	objLayer.style.left = canvas.x + 'px';
  	objLayer.style.height = canvas.height;
  	objLayer.style.width = canvas.width;
 	}
}


function pFobj_setVisibility(objLayer, visible) 
{
	if(isNav4) objLayer.visibility  = (visible == true) ? 'show' : 'hide';
 	else objLayer.style.visibility = (visible == true) ? 'visible' : 'hidden';
}



// ----- class pCoords -----
function pCoords(x, y)
{

	this.x = ((!x) || (x < 0) || (y < 0) || (x >= nmapWidth) || (y >= nmapHeight)) ? 0 : x ;
	this.y = ((!y) || (x < 0) || (y < 0) || (x >= nmapWidth) || (y >= nmapHeight)) ? 0 : y ;	
	
	this.toString = pCoords_objToString;
	this.equals = pCoords_equalsCoord;
	this.getX = pCoords_getX;
	this.getY = pCoords_getY;	
}


function pCoords_getX()
{
	return this.x
}


function pCoords_getY()
{
	return this.y;
}	


function pCoords_equalsCoord(/*Coord*/ c)
{
	return (this.x == c.x && this.y ==c.y);
}


function pCoords_objToString()
{
 var ret = "/";
 for(p in this)
 {
  if (typeof this[p] == "function" || typeof this[p] == "object") continue;
  if(ret.length > 1) ret += ",";
  ret += p + ":" + this[p];
 }
 return ret + "/";
}


/* This notice must be untouched at all times.

wz_jsgraphics.js    v. 2.02
The latest version is available at
http://www.walterzorn.com
or http://www.devira.com
or http://www.walterzorn.de

Copyright (c) 2002-2003 Walter Zorn. All rights reserved.
Created 3. 11. 2002 by Walter Zorn <walter@kreuzotter.de>
Last modified: 12. 5. 2003

High Performance JavaScript Graphics Library.
Provides methods
- to draw lines, rectangles, ellipses, polygons
  with specifiable line thickness,
- to fill rectangles and ellipses
- to draw text.
NOTE: Operations, functions and branching have rather been optimized
to efficiency and speed than to shortness of source code.

This program is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License
at http://www.gnu.org/copyleft/gpl.html for more details.
*/





var jg_ihtm, jg_ie, jg_dom,
jg_n4 = (document.layers && typeof document.classes != "undefined");





function chkDHTM(x, i)
{
	x = document.body || null;
	jg_ie = (x && typeof x.insertAdjacentHTML != "undefined");
	jg_dom = (x && !jg_ie &&
		typeof x.appendChild != "undefined" &&
		typeof document.createRange != "undefined" &&
		typeof (i = document.createRange()).setStartBefore != "undefined" &&
		typeof i.createContextualFragment != "undefined");
	jg_ihtm = (!jg_ie && !jg_dom &&	x && typeof x.innerHTML != "undefined");
}





function pntDoc()
{
	document.write(this.htm);
	this.htm = '';
}





function pntCnvDom()
{
	var x = document.createRange();
	x.setStartBefore(this.cnv);
	x = x.createContextualFragment(this.htm);
	this.cnv.appendChild(x);
	this.htm = '';
}





function pntCnvIe()
{
	this.cnv.insertAdjacentHTML("BeforeEnd", this.htm);
	this.htm = '';
}





function pntCnvIhtm()
{
	this.cnv.innerHTML += this.htm;
	this.htm = '';
}





function pntCnv()
{
	this.htm = '';
}





function mkDiv(x, y, w, h)
{
	this.htm += '<div style="position:absolute;'+
		'left:' + x + 'px;'+
		'top:' + y + 'px;'+
		'width:' + w + 'px;'+
		'height:' + h + 'px;'+
		'clip:rect(0,'+w+'px,'+h+'px,0);'+
		'overflow:hidden;background-color:' + this.color + ';'+
		'"><\/div>';
}





function mkDivPrint(x, y, w, h)
{
	this.htm += '<div style="position:absolute;'+
		'border-left:' + w + 'px solid ' + this.color + ';'+
		'left:' + x + 'px;'+
		'top:' + y + 'px;'+
		'width:' + w + 'px;'+
		'height:' + h + 'px;'+
		'clip:rect(0,'+w+'px,'+h+'px,0);'+
		'overflow:hidden;background-color:' + this.color + ';'+
		'"><\/div>';
}





function mkLyr(x, y, w, h)
{
	this.htm += '<layer '+
		'left="' + x + '" '+
		'top="' + y + '" '+
		'width="' + w + '" '+
		'height="' + h + '" '+
		'bgcolor="' + this.color + '"><\/layer>';
}





function mkLbl(txt, x, y)
{
	this.htm += '<div style="position:absolute;white-space:nowrap;'+
		'left:' + x + 'px;'+
		'top:' + y + 'px;'+
		'font-family:' +  this.ftFam + ';'+
		'font-size:' + this.ftSz + ';'+
		'color:' + this.color + ';' + this.ftSty + '">'+
		txt +
		'<\/div>';
}





function mkLin(x1, y1, x2, y2)
{
	if (x1 > x2)
	{
		var _x2 = x2;
		var _y2 = y2;
		x2 = x1;
		y2 = y1;
		x1 = _x2;
		y1 = _y2;
	}
	var dx = x2-x1, dy = Math.abs(y2-y1),
	x = x1, y = y1,
	yIncr = (y1 > y2)? -1 : 1;

	if (dx >= dy)
	{
		var pr = dy<<1,
		pru = pr - (dx<<1),
		p = pr-dx,
		ox = x;
		while ((dx--) > 0)
		{
			++x;
			if (p > 0)
			{
				this.mkDiv(ox, y, x-ox, 1);
				y += yIncr;
				p += pru;
				ox = x;
			}
			else p += pr;
		}
		this.mkDiv(ox, y, x2-ox+1, 1);
	}

	else
	{
		var pr = dx<<1,
		pru = pr - (dy<<1),
		p = pr-dy,
		oy = y;
		if (y2 <= y1)
		{
			while ((dy--) > 0)
			{
				if (p > 0)
				{
					this.mkDiv(x++, y, 1, oy-y+1);
					y += yIncr;
					p += pru;
					oy = y;
				}
				else
				{
					y += yIncr;
					p += pr;
				}
			}
			this.mkDiv(x2, y2, 1, oy-y2+1);
		}
		else
		{
			while ((dy--) > 0)
			{
				y += yIncr;
				if (p > 0)
				{
					this.mkDiv(x++, oy, 1, y-oy);
					p += pru;
					oy = y;
				}
				else p += pr;
			}
			this.mkDiv(x2, oy, 1, y2-oy+1);
		}
	}
}





function mkLin2D(x1, y1, x2, y2)
{
	if (x1 > x2)
	{
		var _x2 = x2;
		var _y2 = y2;
		x2 = x1;
		y2 = y1;
		x1 = _x2;
		y1 = _y2;
	}
	var dx = x2-x1, dy = Math.abs(y2-y1),
	x = x1, y = y1,
	yIncr = (y1 > y2)? -1 : 1;


	var s = this.stroke;
	if (dx >= dy)
	{
		if (s-0x03 > 0)
		{
			var _s = (s*dx*Math.sqrt(1+dy*dy/(dx*dx))-dx-(s>>1)*dy) / dx;
			_s = (!(s-0x04)? Math.ceil(_s) : Math.round(_s)) + 1;
		}
		else var _s = s;
		var ad = Math.ceil(s/2);

		var pr = dy<<1,
		pru = pr - (dx<<1),
		p = pr-dx,
		ox = x;
		while ((dx--) > 0)
		{
			++x;
			if (p > 0)
			{
				this.mkDiv(ox, y, x-ox+ad, _s);
				y += yIncr;
				p += pru;
				ox = x;
			}
			else p += pr;
		}
		this.mkDiv(ox, y, x2-ox+ad+1, _s);
	}

	else
	{
		if (s-0x03 > 0)
		{
			var _s = (s*dy*Math.sqrt(1+dx*dx/(dy*dy))-(s>>1)*dx-dy) / dy;
			_s = (!(s-0x04)? Math.ceil(_s) : Math.round(_s)) + 1;
		}
		else var _s = s;
		var ad = Math.round(s/2);

		var pr = dx<<1,
		pru = pr - (dy<<1),
		p = pr-dy,
		oy = y;
		if (y2 <= y1)
		{
			++ad;
			while ((dy--) > 0)
			{
				if (p > 0)
				{
					this.mkDiv(x++, y, _s, oy-y+ad);
					y += yIncr;
					p += pru;
					oy = y;
				}
				else
				{
					y += yIncr;
					p += pr;
				}
			}
			this.mkDiv(x2, y2, _s, oy-y2+ad);
		}
		else
		{
			while ((dy--) > 0)
			{
				y += yIncr;
				if (p > 0)
				{
					this.mkDiv(x++, oy, _s, y-oy+ad);
					p += pru;
					oy = y;
				}
				else p += pr;
			}
			this.mkDiv(x2, oy, _s, y2-oy+ad+1);
		}
	}
}





function mkLinDott(x1, y1, x2, y2)
{
	if (x1 > x2)
	{
		var _x2 = x2;
		var _y2 = y2;
		x2 = x1;
		y2 = y1;
		x1 = _x2;
		y1 = _y2;
	}
	var dx = x2-x1, dy = Math.abs(y2-y1),
	x = x1, y = y1,
	yIncr = (y1 > y2)? -1 : 1,
	drw = true;
	if (dx >= dy)
	{
		var pr = dy<<1,
		pru = pr - (dx<<1),
		p = pr-dx;
		while ((dx--) > 0)
		{
			if (drw) this.mkDiv(x, y, 1, 1);
			drw = !drw;
			if (p > 0)
			{
				y += yIncr;
				p += pru;
			}
			else p += pr;
			++x;
		}
		if (drw) this.mkDiv(x, y, 1, 1);
	}

	else
	{
		var pr = dx<<1,
		pru = pr - (dy<<1),
		p = pr-dy;
		while ((dy--) > 0)
		{
			if (drw) this.mkDiv(x, y, 1, 1);
			drw = !drw;
			y += yIncr;
			if (p > 0)
			{
				++x;
				p += pru;
			}
			else p += pr;
		}
		if (drw) this.mkDiv(x, y, 1, 1);
	}
}





function mkOv(left, top, width, height)
{
	var a = width>>1, b = height>>1,
	wod = width&1, hod = (height&1)+1,
	cx = left+a, cy = top+b,
	x = 0, y = b,
	ox = 0, oy = b,
	aa = (a*a)<<1, bb = (b*b)<<1,
	st = (aa>>1)*(1-(b<<1)) + bb,
	tt = (bb>>1) - aa*((b<<1)-1),
	w, h;
	do
	{
		if (st < 0)
		{
			st += bb*((x<<1)+0x03);
			tt += (bb<<1)*(++x);
		}
		else if (tt < 0)
		{
			st += bb*((x<<1)+0x03) - (aa<<1)*(y-1);
			tt += (bb<<1)*(++x) - aa*(((y--)<<1)-0x03);
			w = x-ox;
			h = oy-y;
			if (w&0x02 && h&0x02)
			{
				this.mkOvQds(cx, cy, -x+1, ox+wod, -oy, oy-h+1+hod, w, 1);
				this.mkOvQds(cx, cy, -x+1, x-1+wod, -y-1, y+hod, 1, 1);
			}
			else this.mkOvQds(cx, cy, -x+1, ox+wod, -oy, oy-h+hod, w, h);
			ox = x;
			oy = y;
		}
		else
		{
			tt -= aa*((y<<1)-0x03);
			st -= (aa<<1)*(--y);
		}
	}
	while (y > 0);
	this.mkDiv(cx-a, cy-oy, a-ox+1, (oy<<1)+hod);
	this.mkDiv(cx+ox+wod, cy-oy, a-ox+1, (oy<<1)+hod);
}





function mkOv2D(left, top, width, height)
{
	var s = this.stroke;
	width += s-1;
	height += s-1;
	var a = width>>1, b = height>>1,
	wod = width&1, hod = (height&1)+1,
	cx = left+a, cy = top+b,
	x = 0, y = b,
	aa = (a*a)<<1, bb = (b*b)<<1,
	st = (aa>>1)*(1-(b<<1)) + bb,
	tt = (bb>>1) - aa*((b<<1)-1);


	if (s-0x04 < 0 && (!(s-0x02) || width-0x33 > 0 && height-0x33 > 0))
	{
		var ox = 0, oy = b,
		w, h,
		pxl, pxr, pxt, pxb, pxw;
		do
		{
			if (st < 0)
			{
				st += bb*((x<<1)+0x03);
				tt += (bb<<1)*(++x);
			}
			else if (tt < 0)
			{
				st += bb*((x<<1)+0x03) - (aa<<1)*(y-1);
				tt += (bb<<1)*(++x) - aa*(((y--)<<1)-0x03);
				w = x-ox;
				h = oy-y;

				if (w-1)
				{
					pxw = w+1+(s&1);
					h = s;
				}
				else if (h-1)
				{
					pxw = s;
					h += 1+(s&1);
				}
				else pxw = h = s;
				this.mkOvQds(cx, cy, -x+1, ox-pxw+w+wod, -oy, -h+oy+hod, pxw, h);
				ox = x;
				oy = y;
			}
			else
			{
				tt -= aa*((y<<1)-0x03);
				st -= (aa<<1)*(--y);
			}
		}
		while (y > 0);
		this.mkDiv(cx-a, cy-oy, s, (oy<<1)+hod);
		this.mkDiv(cx+a+wod-s+1, cy-oy, s, (oy<<1)+hod);
	}


	else
	{
		var _a = (width-((s-1)<<1))>>1,
		_b = (height-((s-1)<<1))>>1,
		_x = 0, _y = _b,
		_aa = (_a*_a)<<1, _bb = (_b*_b)<<1,
		_st = (_aa>>1)*(1-(_b<<1)) + _bb,
		_tt = (_bb>>1) - _aa*((_b<<1)-1),

		pxl = new Array(),
		pxt = new Array(),
		_pxb = new Array();
		pxl[0] = 0;
		pxt[0] = b;
		_pxb[0] = _b-1;
		do
		{
			if (st < 0)
			{
				st += bb*((x<<1)+0x03);
				tt += (bb<<1)*(++x);
				pxl[pxl.length] = x;
				pxt[pxt.length] = y;
			}
			else if (tt < 0)
			{
				st += bb*((x<<1)+0x03) - (aa<<1)*(y-1);
				tt += (bb<<1)*(++x) - aa*(((y--)<<1)-0x03);
				pxl[pxl.length] = x;
				pxt[pxt.length] = y;
			}
			else
			{
				tt -= aa*((y<<1)-0x03);
				st -= (aa<<1)*(--y);
			}

			if (_y > 0)
			{
				if (_st < 0)
				{
					_st += _bb*((_x<<1)+0x03);
					_tt += (_bb<<1)*(++_x);
					_pxb[_pxb.length] = _y-1;
				}
				else if (_tt < 0)
				{
					_st += _bb*((_x<<1)+0x03) - (_aa<<1)*(_y-1);
					_tt += (_bb<<1)*(++_x) - _aa*(((_y--)<<1)-0x03);
					_pxb[_pxb.length] = _y-1;
				}
				else
				{
					_tt -= _aa*((_y<<1)-0x03);
					_st -= (_aa<<1)*(--_y);
					_pxb[_pxb.length-1]--;
				}
			}
		}
		while (y > 0);

		var ox = 0, oy = b,
		_oy = _pxb[0],
		l = pxl.length,
		w, h;
		for (var i = 0; i < l; i++)
		{
			if (typeof _pxb[i] != "undefined")
			{
				if (_pxb[i] < _oy || pxt[i] < oy)
				{
					x = pxl[i];
					this.mkOvQds(cx, cy, -x+1, ox+wod, -oy, _oy+hod, x-ox, oy-_oy);
					ox = x;
					oy = pxt[i];
					_oy = _pxb[i];
				}
			}
			else
			{
				x = pxl[i];
				this.mkDiv(cx-x+1, cy-oy, 1, (oy<<1)+hod);
				this.mkDiv(cx+ox+wod, cy-oy, 1, (oy<<1)+hod);
				ox = x;
				oy = pxt[i];
			}
		}
		this.mkDiv(cx-a, cy-oy, 1, (oy<<1)+hod);
		this.mkDiv(cx+ox+wod, cy-oy, 1, (oy<<1)+hod);
	}
}





function mkOvDott(left, top, width, height)
{
	var a = width>>1, b = height>>1,
	wod = width&1, hod = height&1,
	cx = left+a, cy = top+b,
	x = 0, y = b,
	aa = (a*a)<<1, bb = (b*b)<<1,
	st = (aa>>1)*(1-(b<<1)) + bb,
	tt = (bb>>1) - aa*((b<<1)-1),
	drw = true;
	do
	{
		if (st < 0)
		{
			st += bb*((x<<1)+0x03);
			tt += (bb<<1)*(++x);
		}
		else if (tt < 0)
		{
			st += bb*((x<<1)+0x03) - (aa<<1)*(y-1);
			tt += (bb<<1)*(++x) - aa*(((y--)<<1)-0x03);
		}
		else
		{
			tt -= aa*((y<<1)-0x03);
			st -= (aa<<1)*(--y);
		}
		if (drw) this.mkOvQds(cx, cy, -x, x+wod, -y, y+hod, 1, 1);
		drw = !drw;
	}
	while (y > 0);
}





function mkOvFill(left, top, width, height)
{
	var a = width>>1, b = height>>1,
	wod = (width&1)+1, hod = (height&1)+1,
	cx = left+a, cy = top+b,
	x = 0, y = b,
	ox = 0, oy = b,
	aa = (a*a)<<1, bb = (b*b)<<1,
	st = (aa>>1)*(1-(b<<1)) + bb,
	tt = (bb>>1) - aa*((b<<1)-1),
	pxl, w, h;
	do
	{
		if (st < 0)
		{
			st += bb*((x<<1)+0x03);
			tt += (bb<<1)*(++x);
		}
		else if (tt < 0)
		{
			st += bb*((x<<1)+0x03) - (aa<<1)*(y-1);
			pxl = cx-x;
			w = (x<<1)+wod;
			tt += (bb<<1)*(++x) - aa*(((y--)<<1)-0x03);
			h = oy-y;
			this.mkDiv(pxl, cy-oy, w, h);
			this.mkDiv(pxl, cy+oy-h+hod, w, h);
			ox = x;
			oy = y;
		}
		else
		{
			tt -= aa*((y<<1)-0x03);
			st -= (aa<<1)*(--y);
		}
	}
	while (y > 0);
	this.mkDiv(cx-a, cy-oy, width+1, (oy<<1)+hod);
}





function mkRect(x, y, w, h)
{
	var s = this.stroke || 1;
	this.mkDiv(x, y, w, s);
	this.mkDiv(x+w, y, s, h);
	this.mkDiv(x, y+h, w+s, s);
	this.mkDiv(x, y+s, s, h-s);
}





function mkRectDott(x, y, w, h)
{
	this.drawLine(x, y, x+w, y);
	this.drawLine(x+w, y, x+w, y+h);
	this.drawLine(x, y+h, x+w, y+h);
	this.drawLine(x, y, x, y+h);
}





function jsgFont()
{
	this.PLAIN = 'font-weight:normal;';
	this.BOLD = 'font-weight:bold;';
	this.ITALIC = 'font-style:italic;';
	this.ITALIC_BOLD = this.ITALIC + this.BOLD;
	this.BOLD_ITALIC = this.ITALIC_BOLD;
}
var Font = new jsgFont();





function jsgStroke()
{
	this.DOTTED = -1;
}
var Stroke = new jsgStroke();





function jsGraphics(id)
{
	this.setColor = new Function('arg', 'this.color = arg;');



	this.setStroke = function()
	{
		for (var i = 0; i < arguments.length; i++)
		{
			if (typeof arguments[i] == "number") this.stroke = arguments[i];
		}
		if (!(this.stroke+1))
		{
			this.drawLine = mkLinDott;
			this.mkOv = mkOvDott;
			this.drawRect = mkRectDott;
		}
		else if (this.stroke-1 > 0)
		{
			this.drawLine = mkLin2D;
			this.mkOv = mkOv2D;
			this.drawRect = mkRect;
		}
		else
		{
			this.drawLine = mkLin;
			this.mkOv = mkOv;
			this.drawRect = mkRect;
		}
	}



	this.setPrintable = function(arg)
	{
		this.printable = arg;
		this.mkDiv = jg_n4? mkLyr : arg? mkDivPrint : mkDiv;
	}



	this.setFont = function(fam, sz, sty)
	{
		this.ftFam = fam;
		this.ftSz = sz;
		this.ftSty = sty || Font.PLAIN;
	}



	this.drawPolyline = this.drawPolyLine = function(x, y, s)
	{
		var z = x.length-1; while (z >= 0)
			this.drawLine(x[z], y[z], x[--z], y[z]);
	}



	this.fillRect = function(x, y, w, h)
	{
		this.mkDiv(x, y, w, h);
	}



	this.drawPolygon = function(x, y)
	{
		this.drawPolyline(x, y);
		this.drawLine(x[x.length-1], y[x.length-1], x[0], y[0]);
	}



	this.drawEllipse = this.drawOval = function(x, y, w, h)
	{
		this.mkOv(x, y, w, h);
	}



	this.fillEllipse = this.fillOval = function(x, y, w, h)
	{
		this.mkOvFill(x, y, w-1, h-1)
	}



	this.drawString = mkLbl;



	this.clear = function()
	{
		this.htm = "";
		if (this.cnv) this.cnv.innerHTML = this.defhtm;
	}



	this.mkOvQds = function(cx, cy, xl, xr, yt, yb, w, h)
	{
		this.mkDiv(xr+cx, yt+cy, w, h);
		this.mkDiv(xr+cx, yb+cy, w, h);
		this.mkDiv(xl+cx, yb+cy, w, h);
		this.mkDiv(xl+cx, yt+cy, w, h);
	}


	this.mkOvFill = mkOvFill;
	this.setStroke(1);
	this.setPrintable(false);
	this.setFont('verdana,geneva,helvetica,sans-serif', String.fromCharCode(0x31, 0x32, 0x70, 0x78), Font.PLAIN);
	this.color = '#000000';
	this.htm = '';


	if (!(jg_ie || jg_dom || jg_ihtm)) chkDHTM();
	if (id == document) this.paint = pntDoc;
	else
	{
		this.cnv = document.all? (document.all[id] || null)
			: document.getElementById? (document.getElementById(id) || null)
			: null;
		this.defhtm = (this.cnv && this.cnv.innerHTML)? this.cnv.innerHTML : '';
		this.paint = jg_dom? pntCnvDom : jg_ie? pntCnvIe : jg_ihtm? pntCnvIhtm : pntCnv;
	}
}



