//******************************************************************************************
// Copyright (C) Enterprise Information Solutions, Inc.
// All Rights Reserved.
// PROPRIETARY AND CONFIDENTIAL.  FOR INTERNAL USE ONLY.
// Application specific Javascript functions
//		Author:		Weilin Sung
//		Used By:	MapProcess.asp
//		History:	2/7/2002 WS Created
//******************************************************************************************/


var isNav = (navigator.appName.indexOf("Netscape")>=0);
var isNav4 = false;
var isIE = false;
var lStartX;				//Rubberband Start Point X Coordinate
var	lStartY;				//Rubberband Start Point Y Coordinate
var lEndX;					//Rubberband End Point X Coordinate
var lEndY;					//Rubberband End Point Y Coordinate
var zleft;
var ztop;
var zbottom;
var zright;
var isNav5up=false;
var startx;
var starty;
var endx;
var endy;
var mouseX;
var mouseY;

var bRubberBanding=false;

//*******************************************************************************
//To Do: Set map border width
//*******************************************************************************
var iBorderWidth = 0;				//Map Border width

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-iBorderWidth;
	mouseY = mouseY-iBorderWidth;
}

function getMouseXY(e) {
	
	if (bRubberBanding) {
		getImageXY(e);
		lEndX=mouseX;
		lEndY=mouseY;
		setClip();
		
	}
}

function setClip() {	
    var tempX=lStartX;
	var tempY=lStartY;
	
	if (lStartX>lEndX) {
		zright=lStartX;
		zleft=lEndX;
	} else {
		zleft=lStartX;
		zright=lEndX;
	}
	
	if (lStartY>lEndY) {
		zbottom=lStartY;
		ztop=lEndY;
	} else {
		ztop=lStartY;
		zbottom=lEndY;
	}
	
	if ((zright-zleft) > 5) {
		nosub=true;
	}
	if ((lStartX != lEndX) && (lStartY != lEndY)) {
		displayRubberBandBox(zleft,ztop,zright,zbottom);
	}	
}

function displayRubberBandBox(theLeft,theTop,theRight,theBottom) {
	if (isNav5up) {
	  var mylayer;
		moveLayer("RubberBandTop",theLeft,theTop);
		mylayer = getLayer("RubberBandTop");	
    mylayer.width=(theRight-theLeft);
        
		moveLayer("RubberBandBottom",theLeft,theBottom);
		clipLayer("RubberBandBottom",theLeft,theBottom-2,theRight,theBottom);
		mylayer = getLayer("RubberBandBottom");	
    mylayer.width=(theRight-theLeft);
        
        
		moveLayer("RubberBandLeft",theLeft,theTop);
		clipLayer("RubberBandLeft",theLeft,theTop,theLeft+2,theBottom);
		mylayer = getLayer("RubberBandLeft");	
    mylayer.height=(theBottom-theTop);

		moveLayer("RubberBandRight",theRight-2,theTop);
		clipLayer("RubberBandRight",theRight-2,theTop,theRight,theBottom);
		mylayer = getLayer("RubberBandRight");	
		mylayer.height=(theBottom-theTop);
	
	} else {
		clipLayer("RubberBandTop",theLeft,theTop,theRight,theTop+2);
		clipLayer("RubberBandLeft",theLeft,theTop,theLeft+2,theBottom);
		clipLayer("RubberBandRight",theRight-2,theTop,theRight,theBottom);
		clipLayer("RubberBandBottom",theLeft,theBottom-2,theRight,theBottom);

	}
}


// start zoom in.... box displayed
function startRubberBand(e) {
	
if ((document.mapcontrol.action.value=='ZoomIn')|| (document.mapcontrol.action.value=='ZoomOut')|| (document.mapcontrol.action.value=='SelectSRect')|| (document.mapcontrol.action.value=='SelectMRect')) {
		if (bRubberBanding==false) {
		  bRubberBanding=true;
		  getImageXY(e);
			
			
			// keep it within the MapImage
			if ((mouseX<iWidth) && (mouseY<iHeight)) {
				
					startx=mouseX;
					starty=mouseY;
					lStartX=mouseX;			
					lStartY=mouseY;
					lEndX=lStartX+1;
					lEndY=lStartY+1;
					
					displayRubberBandBox(lStartX,lStartY,lEndX,lEndY);
					
					showLayer("RubberBandTop");
					showLayer("RubberBandLeft");
					showLayer("RubberBandRight");
					showLayer("RubberBandBottom");	
					
			}
		}
	}
}

// stop zoom box display... zoom in
function stopRubberBand(e) {
	getImageXY(e);	
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		endx=mouseX;
		endy=mouseY;
	}
	hideLayer("RubberBandTop");
	hideLayer("RubberBandLeft");
	hideLayer("RubberBandRight");
	hideLayer("RubberBandBottom");
	if (bRubberBanding) {
		
		if (startx==null) {
			startx=-1;
		}
		if (starty==null) {
			starty=-1;
		}
		if (endx==null) {
			endx=-1;
		}
		if (endy==null) {
			endy=-1;
		}
						
		parent.MapFrame.document.mapcontrol.lStartX.value=startx;
		parent.MapFrame.document.mapcontrol.lEndX.value=endx;
		parent.MapFrame.document.mapcontrol.lStartY.value=starty;
		parent.MapFrame.document.mapcontrol.lEndY.value=endy;
		
		if ((endx!=-1) || (endy!=-1)) {
			parent.MapFrame.showRetrieveMap();
	  }
		
		if ((endx>=startx) && ((endx-startx)<2))	{
		  parent.MapFrame.document.mapcontrol.submit();
		}
		
		if ((endx<startx) && ((startx-endx)<2))		
		  parent.MapFrame.document.mapcontrol.submit();
		}
		
		bRubberBanding=false;
		
		

}


// Create a DHTML layer
function createLayer(name, left, top, width, height, visible, content) {
			  var layer;
			  //window.alert(isNav4);
			  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 checkIfMouseIsOutOfMapBound(){

	if (bRubberBanding){
		//if(( lEndX > iMapWidth + iBorderWidth)||(lEndX < iBorderWidth)
		if(( lEndX > iWidth)||(lEndX < iBorderWidth)
									||(lEndY < iBorderWidth)||(lEndY > iMapHeight)) 
			{
			  ///parent.MapFrame.document.mapcontrol.lStartX.value=-1;
		    //parent.MapFrame.document.mapcontrol.lEndX.value=-1;
		    //parent.MapFrame.document.mapcontrol.lStartY.value=-1;
		    //parent.MapFrame.document.mapcontrol.lEndY.value=-1;
		    //*/
			  bRubberBanding=false;
			  hideLayer("RubberBandTop");
				hideLayer("RubberBandLeft");
				hideLayer("RubberBandRight");
				hideLayer("RubberBandBottom");
			}
  }
}




//------------------------------------------------------------------>
// General functions - popup window, etc...



	function ShowPopup(tURL, windowname) {
 	
 		if (! window.focus)return true;
    		var href;
    		if (typeof(tURL) == 'string')
    			href=tURL;
    		else
    			href=tURL.href;
    			window.open(href, windowname, 'top=20,left=20,width=800,height=630,resizable=yes,scrollbars=yes,toolbar=yes');
	   		return false;
		}



	function ShowPopup2(tURL, windowname,iTop, iLeft, iWidth, iHeight) {
 	
 		if (! window.focus)return true;
    		var href;
    		if (typeof(tURL) == 'string')
    			href=tURL;
    		else
    			href=tURL.href;
    			window.open(href, windowname, 'top=' + iTop + ',left=' + iLeft + ',width=' + iWidth + 'height=' + iHeight + ',resizable=yes,scrollbars=yes,toolbar=yes');
	   		return false;
		}








//------------------------------------------------------------------>
// Functions for map operations

function zoomToPoint(dX, dY, bDoLabel, tLabel) { 
  
  // author:  T. Moen
  // date:    3/3/2002
  
	// show message
	parent.MapFrame.showRetrieveMap();
 		 
	// pass ZOOMTOPNT action to mapprocess
  parent.ToolFrame.action='ZOOMTOPNT';
  parent.MapFrame.location.href="MapProcess.asp?action=ZOOMTOPNT&dX=" + dX + "&dY=" + dY + "&bDoLabel=" + bDoLabel + "&tLabel=" + tLabel;
		
      
}





