
/*
** This file was created by Cuesta Systems Inc, June 2004
** It contains functions to handle DHTML display
** Created By: Monica Socol, Cuesta Systems
** On: June 20, 2004
*/


//************************************************************************
//** Create a DHTML layer
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function createLayer(name, inleft, intop, width, height, visible, content) {
	  var layer;

    document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + inleft + 'px; top:' + intop + 'px; width:' + width + 'px; height:' + height + 'px;' + ';visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
    document.writeln(content);
    document.writeln('</div>');

}
//************************************************************************

//************************************************************************
//** Create a DHTML layer, used for drawing rectangle boxes on map: zoom, selection and annotation
//** Created By: Aida Dodo, August 9 2004
//** Modified By: 
//*
function createLayer2(name, border, width, height, visible) {

	    document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; border:' + border + '; width:' + width + 'px; height:' + height + 'px;' + 'visibility:' + (visible ? 'visible;' : 'hidden') +  '">');
	    document.writeln('</div>');
}
//************************************************************************

//************************************************************************
//** get the layer object called "name"
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
/*
function getLayer(name) {
	  if (isNav4)
	    return(document.layers[name]);
	  else if (isNav) // Modified by Ali Sep. 23, 2004
	    return(document.getElementById(name));
	  else if (isIE4) {
	    layer = eval('document.all.' + name + '.style');
	    return(layer);
	  } else if (is5up) {
	             if (isIE){
	             		var theObj = document.getElementById(name);
		              return theObj.style;
		           } else {
		           		var theObj = document[name];
		              return theObj.style;
               } 
  	  }
	  else
	    return(null);
}
*/

function getLayer(name) {
      var theObj = document.getElementById(name);
      return theObj.style;
}

//************************************************************************

//************************************************************************
//** get the layer object called "name" if in OverviewFrame
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function getOVLayer(name) {
	if (parent.OverviewFrame!=null) {
    var theObj = parent.OverviewFrame.document.getElementById(name);
		return theObj.style
	} else {
		return getLayer(name);
	}
		
}
//************************************************************************

//************************************************************************
//** check if the layer is visible
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function isVisible(name) {
   alert(layer.visibility);
   if (layer.visibility == "visible")
	    return(true);

   return(false);
}
//************************************************************************

//************************************************************************
//** move layer to x,y
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function moveLayer(name, x, y) {		
  	var layer = getLayer(name);		
   	layer.moveTo(x, y);

    //layer.left = x + "px";
   	//layer.top  = y + "px";

}
//************************************************************************

//************************************************************************
//** set layer background color
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function setLayerBackgroundColor(name, color) {		
  	var layer = getLayer(name);
    layer.backgroundColor = color;
}
//************************************************************************

//************************************************************************
//** toggle layer to invisible
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function hideLayer(name) {		
  	var layer = getLayer(name);		
    layer.visibility = "hidden";
}
//************************************************************************

//************************************************************************
//** toggle layer to visible
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
/*
function showLayer(name) {		
  	var layer = getLayer(name);		
  	if (isNav4)
    	layer.visibility = "show";
  	//if (document.all)
	else
   	 layer.visibility = "visible";
	 //layer.display="block";
}
*/
function showLayer(name) {		
  	var layer = getLayer(name);
    layer.visibility = "visible";
}

//************************************************************************

//************************************************************************
//** toggle Overview layer to visible - if in OverviewFrame
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function showOVLayer(name) {		
   layer.visibility = "visible";
  }
//************************************************************************

//************************************************************************
//** toggle Overview layer to invisible - if in OverviewFrame
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function hideOVLayer(name) {		
  	var layer = getOVLayer(name);
   	layer.visibility = "hidden";
}
//************************************************************************

//************************************************************************
//** clip layer display to clipleft, cliptip, clipright, clipbottom
//** Not working with Mozilla Milestone 12 (Nav5)
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function clipLayer2(name, clipleft, cliptop, clipright, clipbottom) {		
	  var layer = getLayer(name);		
	  if (isNav4) {
		    layer.clip.left   = clipleft;
		    layer.clip.top    = cliptop;
		    layer.clip.right  = clipright;
		    layer.clip.bottom = clipbottom;
	  }
	  //if (document.all)
	  else if (isIE) {
		    layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	  }
}

function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	  var layer = getLayer(name);
	  if (isNav4) {
		    layer.clip.left   = clipleft;
		    layer.clip.top    = cliptop;
		    layer.clip.right  = clipright;
		    layer.clip.bottom = clipbottom;
	  }	  else {
		    //layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
			var newWidth = clipright - clipleft;
			var newHeight = clipbottom - cliptop;
			layer.height = newHeight;
			layer.width	= newWidth;
			//var theTop = parseInt((parseFloat(cliptop) * 10 + 0.5)/10);
			//var theLeft = parseInt((parseFloat(clipleft) * 10 + 0.5)/10);
			//alert(cliptop + " " + clipleft);
			layer.top	= cliptop  + "px";
			layer.left	= clipleft + "px";
			//layer.display= "none";
		}

}

function clipOVLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	  var layer = getLayer(name);
	  if (parent.OverviewFrame!=null) layer = getOVLayer(name);
	  if (isNav4) {
		    layer.clip.left   = clipleft;
		    layer.clip.top    = cliptop;
		    layer.clip.right  = clipright;
		    layer.clip.bottom = clipbottom;
	  }	  else {
		    //layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
			if ((clipright.isNaN) || (clipleft.isNaN) || (cliptop.isNaN) || (clipbottom.isNaN)) {
				layer.visibility = "hidden";
			} else {
				var newWidth = clipright - clipleft;
				var newHeight = clipbottom - cliptop;
				if ((newWidth<=0) || (newHeight<=0)) {
					layer.visibility = "hidden";
				} else {
					//if (newWidth.isNaN) newWidth = parseInt(i2Width);
					//if (newHeight.isNaN) newHeight = parseInt(i2Height);
					//alert(newWidth + "," + newHeight);
					
					layer.height = newHeight;
					layer.width	= newWidth;
					//var theTop = parseInt((parseFloat(cliptop) * 10 + 0.5)/10);
					//var theLeft = parseInt((parseFloat(clipleft) * 10 + 0.5)/10);
					//alert(cliptop + " " + clipleft);
					layer.top	= cliptop  + "px";
					layer.left	= clipleft + "px";
					//layer.display= "none";
					layer.visibility = "visible";
				}
			}
		}

}
//************************************************************************

//************************************************************************
//** replace layer's content with new content
//** not working with Mozilla Milestone 12 (Nav5)
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function replaceLayerContent(name, content) {

		    var layer = getLayer(name);
		    layer.document.open();
		    layer.document.writeln(content);
		    layer.document.close();

}
//************************************************************************

//************************************************************************
//** check for existance of layer
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function hasLayer(name) {

		var theElements = document.getElementById(name);
		return (theElements.length > 0 || theElements != "null");
		
}
//************************************************************************

//************************************************************************
//** put up the "RetriveMap" image
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function showRetrieveMap() {
	if (hasLayer("LoadMap")) {
		showLayer("LoadMap");
	}
}
//************************************************************************

//************************************************************************
//** hide the "RetriveMap" image
//** Created By: Monica Socol, June 10 2004
//** Modified By: 
//*
function hideRetrieveMap() {
	if (hasLayer("LoadMap")) {
		hideLayer("LoadMap");
	}
}
//************************************************************************

//************************************************************************
//** put up the "OVMap" image
//** Created By: Monica Socol, June 10 2004
//** Modified By: Ali Sep. 24, 2004 "OVMap" to "theOVMap" 
//*
function showOVMap() {
	if (hasLayer("theOVMap")) {
		showLayer("theOVMap");
	}
}
//************************************************************************

//************************************************************************
//** hide the "RetriveMap" image
//** Created By: Monica Socol, June 10 2004
//** Modified By: Ali Sep. 24, 2004 "OVMap" to "theOVMap" 
//*
function hideOVMap() {
	if (hasLayer("theOVMap")) {
		hideLayer("theOVMap");
	}
}
//************************************************************************


//*************************************************************************
//** Function to draw point on map click, used for line/shape annotations
//** Created By: Aida Dodo, July 5, 2004
//** Modified By: 
//*
function drawPoint(x,y,w,lName)
{
          var oPoint  = document.createElement("DIV");
	  var div=document.getElementById(lName);
	  div.visibility = 'visible';

          with (oPoint.style)
          {
               overflow="hidden";
               backgroundColor = "red";
               height=oPoint.style.width=w;
               position="absolute";
               left = x +  window.top.frames["MapFrame"].hspc ;
               top = y +  window.top.frames["MapFrame"].hspc;  //Should this be vspc?
          }
	div.appendChild(oPoint);

}
//************************************************************************


//*************************************************************************
//** Function to draw line between 2 points on map click, used for line/shape annotations
//** Created By: Aida Dodo, Sept 28 2004
//** Modified By: 
//*
function drawLine(x1,y1,x2,y2,w,lName)
{
	dist = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
	dx = (x2-x1)/dist;
	dy = (y2-y1)/dist;
	for(var j=0;j<dist;j++)
	{
		drawPoint(x1,y1,w,lName);
		x1+=dx;
		y1+=dy;
		
	}
}
//************************************************************************


//*************************************************************************
//** Function to dot draw line between first and last points
//** on map click, used for line/shape annotations
//** Created By: Aida Dodo, Sept 28 2004
//** Modified By: 
//*
function drawDotLine(x1,y1,x2,y2,w,lName)
{
	dist = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
	dx = (x2-x1)/dist;
	dy = (y2-y1)/dist;
	maxpts = 3;
	fullpt = 0;
	spacept = maxpts+1;
	
	for(var j=0;j<Math.round(dist);j++)
	{
		if ( (fullpt<maxpts) && (spacept==maxpts+1) ) {
			drawPoint(x1,y1,w,lName);
			x1+=dx;
			y1+=dy;
			fullpt++;
			spacept=0;
		}
		//else if ( (spacept<maxpts) ) && (fullpt==maxpts) ) {
		else {
			x1+=dx;
			y1+=dy;
			spacept++;
			fullpt=0;
		}
	}
}
//************************************************************************

//*************************************************************************
//** Delete the drawing of DHTML point/line for map clicks	
//** Created By: Aida Dodo, Sept 28 2004
//** Modified By: 
//*
function destroyElement(lName)
{
	var div=document.getElementById(lName);
	while (div.childNodes.length != 0)
	{
		div.removeChild(div.lastChild);
	}
}
