
// JavaScript Document

var MouseX=0;
var MouseY=0;
var StartMouseX=0;
var StartMouseY=0;
var RubberBanding = false;
var l=0; t=0; h=0; w=0;

var zoomBoxColor = '#FF0000' //controls colour of zoom box 
var BoxSize = 2              //controls width of zoom box 
var MouseUpX = 0             //current mouse X position 
var MouseUpY = 0             //current mouse Y position 
var MouseDownX = 0           //X position of mouse when user presses button 
var MouseDownY = 0           //Y position of mouse when user presses button 
var zooming = false          //the getMouse function is called every time the mouse moves.
var currentTool = "ZoomIn" 
var hspc = 0;
var vspc = 0;
var sWidth = screen.width; 
var sHeight = screen.height; 

var isNav = (navigator.appName.indexOf("Netscape")>=0);
var isIE = false;
var isNav4 = false;
var isIE4 = false;
var is5up = false;
var ZoomTop = 0;
var ZoomLeft = 0
var ZoomBottom = 0;
var ZoomRight = 0;

	  
var Xoffset;
var Yoffset;
var panning = false ;


function LayerAt(LayerID,x,y,x1,y1)
{ 
  if (x > x1)   { l = x1 ; w = x  - x1 ; }
  else          { l = x  ; w = x1 - x  ; }
  if (y > y1)   { t = y1 ; h = y  - y1 ; }
  else          { t = y  ; h = y1 - y  ; }
  if(document.getElementById)                               
    {
    document.getElementById(LayerID).style.left   =  l ;
    document.getElementById(LayerID).style.top    =  t ;
    document.getElementById(LayerID).style.height =  h ;
    document.getElementById(LayerID).style.width  =  w ;
	}
  else if(document.all)                                     
    { 
    document.all[LayerID].style.left =  l   ;
    document.all[LayerID].style.top    =  t   ;
    document.all[LayerID].style.height =  h;
    document.all[LayerID].style.width  =  w ;  
	}
  else if(document.layers)                                  
    {    
    document.layers[LayerID].left =  l;
    document.layers[LayerID].top    =  t   ;
    document.layers[LayerID].style.height =  h;
    document.layers[LayerID].style.width  =  w ;
	}	
}

function DoFullExtent()
{
  document.theform.Cmd.value = "fullextent";
  SubmitIt();
}

function Find()
{
  document.theform.Cmd.value = "find";
  SubmitIt();
}

function SetTool(thetool)
{
  document.theform.tool.value = thetool
  document.theform.Cmd.value = thetool;
  setToolImage(thetool)
}

function GoProject(ID)
{
  document.theform.Cmd.value = "goproj";
  document.theform.ID.value = ID;
  SubmitIt();
}

function SubmitIt()
{
 // var Str = new String(document.theform.HD.value)
 // document.theform.HD.value = Str.slice(6) + Str.slice(3,5) + Str.slice(0,2);
 // var Str = document.theform.LD.value;
 // document.theform.LD.value = Str.slice(6) + Str.slice(3,5) + Str.slice(0,2)  ;
  document.theform.submit();
 }
 
function engage() { 
  var tool = document.theform.tool.value;
       
  if ((IsOpen == false) && (MouseUpY < ZoomBottom ) && (MouseUpY > ZoomTop) && (MouseUpX < ZoomRight ) && (MouseUpX > ZoomLeft))
    {
    MouseDownX = MouseUpX 
    MouseDownY = MouseUpY
    // ============================= Zoom in Tool  =====================================	  
    if(tool == "zoomin" ) 
      {
      zooming = true 
	  var x2 = MouseDownX + 1 
	  var y2 = MouseDownY + 1 
	  boxIt(MouseDownX,MouseDownY,x2,y2); 
	  showLayer("zoomBoxTop");  
	  showLayer("zoomBoxLeft"); 
	  showLayer("zoomBoxRight"); 
	  showLayer("zoomBoxBottom"); 
	  }  
	else	
    // ============================= Zoom out Tool  =====================================	
	if(tool == "zoomout")	
	  {	   
	  document.theform.X.value = MouseDownX  - 205 ;
      document.theform.Y.value = MouseDownY  - 160 ;	
	  document.theform.Cmd.value = "zoomout";	 
	  SubmitIt()
	  }
	else	
    // ============================= Pan Tool  =====================================	  
	if(tool == "pan")
	  {	
	  panning = true ;
      Xoffset=MouseDownX - 150 ;
      Yoffset=MouseDownY - 204 ; // 190
	  }
	else	    
    // ============================= Identify Tool  =====================================	  
	if(tool == "identify")
	  {	   
	  document.theform.X.value = MouseDownX  - 205 ;
      document.theform.Y.value = MouseDownY  - 160 ;	 
	  document.theform.Cmd.value = "identify";
	  SubmitIt()
	  }
	} 
  else 
	{
	// outside active area
	} 	
	
} 

function getMouse(e) { 
  if (isNav) { 
    MouseUpX=e.pageX; 
    MouseUpY=e.pageY; 
  } else { 
    MouseUpX=event.clientX + document.body.scrollLeft 
    MouseUpY=event.clientY + document.body.scrollTop
  } 
  if ( (MouseUpY < ZoomBottom ) && (MouseUpY > ZoomTop) && (MouseUpX < ZoomRight ) && (MouseUpX > ZoomLeft) )	  
  {	     // inside the zoombox allowed extent    
    if (zooming) {   
        boxIt(((MouseUpX > MouseDownX) ? MouseDownX : MouseUpX),((MouseUpY > MouseDownY) ? MouseDownY : MouseUpY),((MouseUpX > MouseDownX) ? MouseUpX : MouseDownX),((MouseUpY > MouseDownY) ? MouseUpY : MouseDownY)); 
        return(false);
    } 
    if(panning)    {
    // ============================ PANNING =============================
      document.all.MAP.style.pixelLeft=MouseUpX - Xoffset;
      document.all.MAP.style.pixelTop=MouseUpY - Yoffset;
      return(false);
    }
  } else { // we are outside the box !
    if (zooming) {
	disengage() ;
    return(false);
    } //zooming and in box
    if (panning) {
	disengage() ;
    return(false);
    } //panning and in box    
	return(true) ;
	}
} //func

function disengage() {  
  var l=0; t=0; h=0; w=0;
	var tool = document.theform.tool.value;
	if(tool == "zoomin" || tool == "zoomout") {                                  
		//alert("zoom box used")
		var myMouseUpX = MouseUpX
		var myMouseDownX = MouseDownX
		var myMouseUpY = MouseUpY
		var myMouseDownY = MouseDownY
		if((myMouseUpX != myMouseDownX) || (myMouseUpY != myMouseDownY)) //area selected, not just a click
		{
			//alert("area selected: " + myMouseDownX + ":" + myMouseDownY + " , " + myMouseUpX + ":" + myMouseUpY)
			//alert("moustUpX is: " + (myMouseDownY - event.screenY) + ", " + event.screenY)
		}
	}
	if (zooming) { 
	  zooming = false 
	  hideLayer("zoomBoxTop"); 
	  hideLayer("zoomBoxLeft"); 
	  hideLayer("zoomBoxRight"); 
	  hideLayer("zoomBoxBottom");  
	  
	  if (myMouseDownX > myMouseUpX)   { l = myMouseUpX   ; w = myMouseDownX  - myMouseUpX ; }
      else                             { l = myMouseDownX ; w = myMouseUpX    - myMouseDownX   ; }
      if (myMouseDownY > myMouseUpY)   { t = myMouseUpY   ; h = myMouseDownY  - myMouseUpY ; }
      else                             { t = myMouseDownY ; h = myMouseUpY    - myMouseDownY  ; }
      
      if (IsOpen )
      {
      }
      else
      {
	    if (w < 20 && h < 20) 
	    {
	      alert(w + ' by ' + h + ' is too small please select a bigger area  ' + IsOpen)
	    }
	    else 
	    {
	      document.theform.Et.value = t;
          document.theform.El.value = l;	
          document.theform.Ew.value = w;
          document.theform.Eh.value = h;
          SubmitIt();
        }
      }
	} 
	if (panning) {
	  panning = false
	  
	  var shiftx ;
	  var shifty ;
	  shiftx = 400 - MouseUpX + MouseDownX ;
	  shifty = 390 - MouseUpY + MouseDownY ;
	  
	  document.theform.X.value = shiftx - 200 ;
      document.theform.Y.value = shifty - 140 ;	 // up 140 + 14 = 154
	  document.theform.Cmd.value = "pan";	 
	  SubmitIt()
	  
	}
	
	//MouseAction();
} 

function createLayer(name, inleft, intop, width, height, visible, content) {
	  var layer;
	  if (isNav4) {
	    document.writeln('<layer name="' + name + '" left=' + inleft + ' top=' + intop + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');
	    document.writeln(content);
	    document.writeln('</layer>');
	  } else {
	    document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + inleft + 'px; top:' + intop + 'px; width:' + width + 'px; height:' + height + 'px;' + '; z-index:1; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
	    document.writeln(content);
	    document.writeln('</div>');
	  }
}

function hideLayer(name) {		
  	var layer = getLayer(name);		
  	if (isNav4)
    	layer.visibility = "hide";
	else
   		 layer.visibility = "hidden";
}

function showLayer(name) {		
  	var layer = getLayer(name);		
  	if (isNav4)
      layer.visibility = "show";
	else
   	  layer.visibility = "visible";
}

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 {
	var newWidth = clipright - clipleft;
	var newHeight = clipbottom - cliptop;
	layer.height = newHeight;
	layer.width	= newWidth;
	layer.top	= cliptop  + "px";
	layer.left	= clipleft + "px";
  }
}

function boxIt(theLeft,theTop,theRight,theBottom) {
	
	if (!isNav4) {
		theTop = theTop + vspc;
		theBottom = theBottom + vspc;
		theLeft = theLeft + hspc;
		theRight = theRight + vspc;
	}
	clipLayer("zoomBoxTop",theLeft,theTop,theRight,theTop+BoxSize);
	clipLayer("zoomBoxLeft",theLeft,theTop,theLeft+BoxSize,theBottom);
	clipLayer("zoomBoxRight",theRight-BoxSize,theTop,theRight,theBottom);
	clipLayer("zoomBoxBottom",theLeft,theBottom-BoxSize,theRight,theBottom);	
	showLayer("zoomBoxTop");
	showLayer("zoomBoxLeft");
	showLayer("zoomBoxRight");
	showLayer("zoomBoxBottom");

}

function setLayerBackgroundColor(name, color) {	
  var layer = getLayer(name);		
  if (isNav4)
    layer.bgColor = color;
  else
    layer.backgroundColor = color;
}

function getLayer(name) {
  if (isNav4)
	return(document.layers[name]);
  else if (isIE4) {
    layer = eval('document.all.' + name + '.style');
    return(layer);
  } else if (is5up) {
	var theObj = document.getElementById(name);
	return theObj.style
  }
  else
    return(null);
}

function SetZoomBox(x,y,w,h) {
  ZoomTop = y;
  ZoomLeft = x;
  ZoomBottom = y+h;
  ZoomRight = x+w;
  if (isNav) {
    if (parseFloat(navigator.appVersion)<5) {
      isNav4=true;
    } else {
      is5up = true;
    }
  } else {
    isIE4=true;
	isIE=true;
	if (navigator.appVersion.indexOf("MSIE 5")>0) {
      isIE4 = false;
      is5up = true;
	}
  }
  if (isNav) { 
    document.captureEvents(Event.MouseDown | Event.MOUSEUP | Event.MOUSEMOVE) 
  } 
  document.onmousedown = engage 
  document.onmouseup = disengage 
  document.onmousemove = getMouse 

  content = '<img name="zoomImageTop" src="images/pixel.gif" width=1 height=1>';
  createLayer("zoomBoxTop",0,0,sWidth,sHeight,false,content);
  content = '<img name="zoomImageLeft" src="images/pixel.gif" width=1 height=1>';
  createLayer("zoomBoxLeft",0,0,sWidth,sHeight,false,content);
  content = '<img name="zoomImageRight" src="images/pixel.gif" width=1 height=1>';
  createLayer("zoomBoxRight",0,0,sWidth,sHeight,false,content);
  content = '<img name="zoomImageBottom" src="images/pixel.gif" width=1 height=1>';
  createLayer("zoomBoxBottom",0,0,sWidth,sHeight,false,content);
  setLayerBackgroundColor("zoomBoxTop", zoomBoxColor);
  setLayerBackgroundColor("zoomBoxLeft", zoomBoxColor);
  setLayerBackgroundColor("zoomBoxRight", zoomBoxColor);
  setLayerBackgroundColor("zoomBoxBottom", zoomBoxColor);
}

  
function ChangeDay()
  {
  
  var ChangeTo
  var LDate
  var HDate
  ChangeTo = document.theform.DateMenu.options[document.theform.DateMenu.selectedIndex].value
  LDate = ChangeTo.substring(0,10)
  HDate = ChangeTo.substring(11,21)
  
  document.theform.LD.value = LDate
  document.theform.HD.value = HDate
  document.theform.Cmd.value = 'find'
  SubmitIt()
  }
  
function ChangeDates()
  {
  var dateyear
  dateyear = document.theform.DateMenu.options[document.theform.DateMenu.selectedIndex].value
  document.theform.LD.value = '01/01/' + dateyear
  document.theform.HD.value = '31/12/' + dateyear
  document.theform.Cmd.value = 'find'
  SubmitIt()
  }
