var isNav = (navigator.appName.indexOf("Netscape")>=0);
var isNav4 = false;
var isIE = false;
var x1;
var	y1;
var x2;
var y2;
var zleft;
var ztop;
var zbottom;
var zright;
var isNav5up=false;
var startx;
var starty;
var endx;
var endy;
var mouseX;
var mouseY;

if (navigator.appName == "Netscape") {
	isNav =true;
	if (navigator.appVersion.search("5.0") == -1)
   		isNav4 = true;
   		
	else isNav5up = true;

	}
else 
	isIE = true;
	

function setLayerBackgroundColor(name, color) {		
  	var layer = getLayer(name);		
 	 if (isNav4)
    	layer.bgColor = color;
  	//else if (document.all)
	else
    	layer.backgroundColor = color;
}	
	
// get the layer object called "name"
function getLayer(name) {
	if (isNav4)
		return(document.layers[name]);
	else if (isIE) {
		layer = eval('document.all.' + name + '.style');
		return(layer);
	} else if (isNav) {
		var theElements = document.getElementsByTagName("DIV");
		var theObj;
		var j = -1;
		for (i=0;i<theElements.length;i++) {
			if (theElements[i].id==name) theObj = theElements[i].style;
			}
			return theObj
		}
	else
		return(null);
}

function getMapWidth(){
	var mapwidth = parent.MapFrame.window.innerwidth;
	if (mapwidth == null) {
	mapwidth = parent.MapFrame.document.body.clientWidth;
	}
	return mapwidth;
}
function getMapHeight(){
var mapheight = parent.MapFrame.window.innerheight;
	if(mapheight == null){
	mapheight = parent.MapFrame.document.body.clientHeight;
	}
	return mapheight;
}
// check for existance of layer
function hasLayer(name) {
	var result = false;
	if (isNav4) {
		if (document.layers[name]!=null) result=true;
	}  else if (isIE) {
		if (eval('document.all.' + name)!=null) result=true;
	} else if (isNav) {
		var theElements = document.getElementsByTagName("DIV");
		var theObj;
		var j = -1;
		for (i=0;i<theElements.length;i++) {
			if (theElements[i].id==name) result=true;
		}
    }
	return result;
}

// toggle layer to invisible
function hideLayer(name) {		
  	var layer = getLayer(name);		
  	if (isNav4)
    	layer.visibility = "hide";
  	//if (document.all)
	else
   		 layer.visibility = "hidden";
}

// toggle layer to visible
function showLayer(name) {		
  	var layer = getLayer(name);		
  	if (isNav4)
    	layer.visibility = "show";
  	//if (document.all)
	else
   	 layer.visibility = "visible";
}

// put up the "RetriveData" image
function showRetrieveData() {
	if (hasLayer("LoadData")) {
		showLayer("LoadData");
	}
}

// hide the "RetriveData" image
function hideRetrieveData() {
	if (hasLayer("LoadData")) {
		hideLayer("LoadData");
	}
}

// put up the "RetriveMap" image
function showRetrieveMap() {
	if (hasLayer("LoadMap")) {
		showLayer("LoadMap");
	}
}

// hide the "RetriveMap" image
function hideRetrieveMap() {
	if (hasLayer("LoadMap")) {
		hideLayer("LoadMap");
	}
}

function moveLayer(name, x, y) {		
  	var layer = getLayer(name);	
  	if (isNav4)
    	layer.moveTo(x, y);
  	//if (document.all) {
	 else {
    	layer.left = x;
   		 layer.top  = y;
  	}
}
function getImageXY(e) {

	if (isNav) {
	    mouseX=e.pageX;
		mouseY=e.pageY;


	} else {
		mouseX=event.clientX + document.body.scrollLeft;
		mouseY=event.clientY + document.body.scrollTop;
	}
	// subtract offsets from page left and top
	mouseX = mouseX-5;
	mouseY = mouseY-5;


}
function getMouse(e) {
if (zooming) {
getImageXY(e);
x2=mouseX;
y2=mouseY;
setClip();
}

}
function 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 ((zright-zleft)>5) {
	nosub=true;
	}
	if ((x1 != x2) && (y1 != y2)) {
		boxIt(zleft,ztop,zright,zbottom);
	}	
}

function boxIt(theLeft,theTop,theRight,theBottom) {
	if (isNav5up) {
	    var mylayer;
		moveLayer("zoomBoxTop",theLeft,theTop);
		//parent.MapFrame.document.layers["zoomBoxTop"].clip.width=(theRight-theLeft);
		//parent.MapFrame.document.layers["zoomBoxTop"].clip.height=2;
        mylayer = getLayer("zoomBoxTop");	
        mylayer.width=(theRight-theLeft);
        
		moveLayer("zoomBoxBottom",theLeft,theBottom);
		clipLayer("zoomBoxBottom",theLeft,theBottom-2,theRight,theBottom);
		//parent.MapFrame.document.layers["zoomBoxBottom"].clip.width=(theRight-theLeft);
		//parent.MapFrame.document.layers["zoomBoxBottom"].clip.height=2;
        mylayer = getLayer("zoomBoxBottom");	
        mylayer.width=(theRight-theLeft);
        
        
		moveLayer("zoomBoxLeft",theLeft,theTop);
		clipLayer("zoomBoxLeft",theLeft,theTop,theLeft+2,theBottom);
		//parent.MapFrame.document.layers["zoomBoxLeft"].clip.height=(theBottom-theTop);
		//parent.MapFrame.document.layers["zoomBoxLeft"].clip.width=2;
        mylayer = getLayer("zoomBoxLeft");	
        mylayer.height=(theBottom-theTop);

		moveLayer("zoomBoxRight",theRight-2,theTop);
		clipLayer("zoomBoxRight",theRight-2,theTop,theRight,theBottom);
		//parent.MapFrame.document.layers["zoomBoxRight"].clip.height=(theBottom-theTop);
		//parent.MapFrame.document.layers["zoomBoxRight"].clip.width=2;
       mylayer = getLayer("zoomBoxRight");	
        mylayer.height=(theBottom-theTop);
	
	} else {
		clipLayer("zoomBoxTop",theLeft,theTop,theRight,theTop+2);
		clipLayer("zoomBoxLeft",theLeft,theTop,theLeft+2,theBottom);
		clipLayer("zoomBoxRight",theRight-2,theTop,theRight,theBottom);
		clipLayer("zoomBoxBottom",theLeft,theBottom-2,theRight,theBottom);

	}

}



// start zoom in.... box displayed
function startZoomBox(e) {
if ((parent.MapFrame.document.mapcontrol.action.value=='ZoomIn')|| (parent.MapFrame.document.mapcontrol.action.value=='ZoomOut')) {
if (zooming==false) {
  zooming=true;
   	getImageXY(e);
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		
			startx=mouseX;
			starty=mouseY;
			x1=mouseX;			
			y1=mouseY;
			x2=x1+1;
			y2=y1+1;
			
			boxIt(x1,y1,x2,y2);
			
			showLayer("zoomBoxTop");
			showLayer("zoomBoxLeft");
			showLayer("zoomBoxRight");
			showLayer("zoomBoxBottom");	
	}
	}
	}
}

// stop zoom box display... zoom in
function stopZoomBox(e) {

        getImageXY(e);	
        if ((mouseX<iWidth) && (mouseY<iHeight)) {
			endx=mouseX;
			endy=mouseY;
			}
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
		if (zooming) {
		parent.MapFrame.document.mapcontrol.mx1.value=startx;
		parent.MapFrame.document.mapcontrol.mx2.value=endx;
		parent.MapFrame.document.mapcontrol.my1.value=starty;
		parent.MapFrame.document.mapcontrol.my2.value=endy;
		parent.MapFrame.showRetrieveMap();
		parent.MapFrame.document.mapcontrol.submit();
		}
	    zooming=false;

}


// Create a DHTML layer
function createLayer(name, left, top, width, height, visible, content) {
			  var layer;
			  if (isNav4) {
			    document.writeln('<layer name="' + name + '" left=' + left + ' top=' + top + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');
			    document.writeln(content);
			    document.writeln('</layer>');
			    //layer = getLayer(name);
			   // layer.width = width;
			   // layer.height = height;
			  //}
			  } else {
			 // if (isNav) {
			    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>');
			  }
			  //clipLayer(name, 0, 0, width, height);
		}


function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	  var layer = getLayer(name);		
	  if (isNav) {
		    layer.clip.left   = clipleft;
		    layer.clip.top    = cliptop;
		    layer.clip.right  = clipright;
		    layer.clip.bottom = clipbottom;
	 }
	  //if (document.all)
	  else {
	    layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	    }
}

function chkifout(){
if (zooming){
  if((x2>545)||(x2<5)||(y2<5)||(y2>435)) {
        zooming=false;
        hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
		}
  }
}

