/*Mapsearch functionality in a client-cacheable file.
*Copyright MMI - MMV Velocity Databank, Inc.
*Author: Chris Frazier
*LastModified:12/01/04
*/
var xmouse, ymouse, xfrom, yfrom, xabs, yabs, xdrag, ydrag, x, y;
var xmin, ymin, xmax, ymax, mapwidth, mapheight;
var mousebutton, dragmode;
var select, zoom, maplayer,mouseover, buttons, maplayers, ieZoom;
var boxtop, boxbottom, boxleft, boxright;

function init(){
	//grab the various inline elements as javascript objects.
	if(document.layers == null){ //Microsoft / DOM2 version
		select = document.getElementById("divSelect");
		zoom = document.getElementById("divZoom");
		maplayer = document.getElementById("maplayer");
		boxtop = document.getElementById("boxtop");
		boxbottom = document.getElementById("boxbottom");
		boxleft = document.getElementById("boxleft");
		boxright = document.getElementById("boxright");
		mouseover = document.getElementById("mouseOver");
		buttons = document.getElementById("divButtons");
		maplayers = document.getElementById("mapLayers");
		
		ieZoom = new ie_object(zoom);
	}
	
	document.onmousedown=mousedown;
	document.onmousemove=mousemove;
	document.onmouseup=mouseup;
	xmin=Number(document.mapper.xmin.value);
	ymin=Number(document.mapper.ymin.value);
	xmax=Number(document.mapper.xmax.value);
	ymax=Number(document.mapper.ymax.value);
	mapwidth=Number(document.mapper.mapwidth.value);
	mapheight=Number(document.mapper.mapheight.value);	
	
	if(document.mapper.operation.value == "zoom" || document.mapper.operation.value == "select"){
		//alert('setMapMode(' + document.mapper.operation.value + ')');
		setMapMode(document.mapper.operation.value);
	}
	
	zoom.style.cursor = "crosshair";
	select.style.cursor = "pointer";
	resize();
	hideLoading();
}

function resize() {
	if (document.layers == null) {
      // Microsoft / DOM2 version
      document.mapper.clientwidth.value = document.body.clientWidth
      document.mapper.clientheight.value = document.body.clientHeight }
    else {
      // Netscape version
      document.mapper.clientwidth.value = (window.innerWidth - 16)
      document.mapper.clientheight.value = window.innerHeight 
    }
}

function mousedown(evt)
{
	//alert(evt)
  if (evt == null)
  {
    // microsoft version
	mousebutton = window.event.button;
  }else{
	mousebutton = evt.which;
  }
 
  if (mousebutton <= 1) 
  {
    xdrag = xabs;
    ydrag = yabs;
    mousemove(evt);
    
    if((xmouse != null) & (ymouse != null)){
    
      xfrom = xmouse;
      yfrom = ymouse;
      
		//alert(document.mapper.operation.value);
      // enable dragging of zoom window
      if ((document.mapper.operation.value == "zoom") || (document.mapper.operation.value == "select")) 
      {
        if (document.layers == null) 
        {
          // microsoft / DOM2 version
			boxleft.style.left = -2;
			boxleft.style.height = 1;
			boxleft.style.visibility = 'visible';
			boxright.style.left = -2;
			boxright.style.height = 1;
			boxright.style.visibility = 'visible';
			boxtop.style.top = -2;
			boxtop.style.width = 1;
			boxtop.style.visibility = 'visible';
			boxbottom.style.top = -2;
			boxbottom.style.width = 1;
			boxbottom.style.visibility = 'visible';
        }
        dragmode='box';
      }
      return false;
    }else{
      return true;
    }
  }     
}//end mousedown()

function mousemove(evt)
{
	if (evt == null) 
	{
		xabs = window.event.clientX + getScrollX();
		yabs = window.event.clientY + getScrollY();
		
		x = xabs - ieZoom.objGetPageLeft(maplayer);
		y = yabs - ieZoom.objGetPageTop(maplayer);
	}else{
		xabs = evt.pageX - 1;
		yabs = evt.pageY - 1;
		
		x = xabs - maplayer.offsetLeft;
		y = yabs - maplayer.offsetTop;
	}
	
	//alert(x + "," + y); 
	
	if ((x <= -1) && (dragmode == 'box') && (xdrag != null))
	{
		xmouse = 0;
		ymouse = y;
		x = -1;
		return false;
		
	}
  
	if ((y <= -1) && (dragmode == 'box') && (xdrag != null))
	{
		ymouse = 0;
		xmouse = x;
		y = -1;
		return false;
		
	}

	if ((x > mapwidth) && (dragmode == 'box') && (xdrag != null))
	{
		xmouse = mapwidth;
		ymouse = y;
		x = -1;
		return false;
	}

	if (x > mapwidth)
	{
		if ((dragmode == 'box') && (xdrag != null))
		{
			xmouse = mapheight;
			ymouse = y;
			x = -1;
			return false;	
		}
		else
		{
			x = -1;
		}
	}
	    
	if (y > mapheight)
	{
		if ((dragmode == 'box') && (xdrag != null))
		{
			ymouse = mapwidth;
			xmouse = x;
			y = -1;
			return false;	
		}
		else
		{
			y = -1;
		}
	}
	    
	if ((x >= 0) && (y >= 0))
	{
		xmouse = x;
		ymouse = y;
		
		if ((dragmode == 'box') && (xdrag != null))
		{
			dragbox();
			
		}
			return false;
	}
	else
	{
		xmouse = null;
		ymouse = null;
		return true;
	}
}//end mousemove()

function mouseup(evt)
{

  if (evt == null)
  {
	mousebutton = window.event.button;
  }else{
	mousebutton = evt.which;
  }
   
  if (mousebutton <= 1)
  {
		mousemove(evt);
		if (xmouse != null)
		{
				if ((xfrom != null) && (xmouse != null))
				{
		
					document.mapper.x1.value = Math.min(xfrom, xmouse);
					document.mapper.y1.value = Math.min(yfrom, ymouse);
					//debugging!
					//alert("y1 Math.min: \r\n\r\nyfrom: "+ yfrom + "\r\nymouse: " + ymouse);
					//alert("actual: " + document.mapper.y1.value);
					
					document.mapper.x2.value = Math.max(xmouse, xfrom);
					document.mapper.y2.value = Math.max(ymouse, yfrom);
					//debugging!
					//alert("y2 Math.max: \r\n\r\nymouse: " + ymouse + "\r\nyfrom: " + yfrom);
					//alert("actual: " + document.mapper.y2.value);

					if (document.mapper.operation.value == "zoom")
					{
						doZoom();
					}
					else if (document.mapper.operation.value == "select")
					{
						doSelect();
					}
				}
				else
				{
				yfrom = null;
				yfrom = null;
				xmouse = null;
				ymouse = null;
				}
				if (dragmode == 'box')
				{
				if (document.layers == null)
				{
					// microsoft / DOM2 version
  					//hide the bounding box
  						boxleft.style.visibility = 'hidden';
  						boxright.style.visibility = 'hidden';
  						boxtop.style.visibility = 'hidden';
  						boxbottom.style.visibility = 'hidden';		 			
				}
			}
		}
  }
  
  dragmode = null;
  return true;
}//end mouseup()

function setMapMode(strMode)
{
	if (strMode == "select")
	{
		document.mapper.operation.value = "select";
		zoom.style.display = 'none';
		maplayers.style.visibility = 'hidden';
		buttons.style.visibility = 'hidden';
		mouseover.style.visibiliy = 'visible';
		mouseover.style.zIndex = 26;
		select.style.display = 'inline';
	}
	else if (strMode == "zoom")
	{
		document.mapper.operation.value = "zoom";
		select.style.display = 'none';
		mouseover.style.visibility = 'hidden';
		maplayers.style.zIndex = -1;
		maplayers.style.visibility = 'hidden';
		buttons.style.visibility = 'visible';
		zoom.style.display = 'inline';
	}
	else if (strMode == "update")
	{
		document.mapper.operation.value = "update";
		showLoading();
		document.mapper.submit();
	}
	else if	(strMode == "resize")	//user resizing map
	{
		document.mapper.operation.value = "resize";
	}
	else if (strMode == "recenter")
	{
		document.mapper.operation.value = "recenter";
	}
	else if(strMode == "addtocart")
	{
		document.mapper.operation.value = "addtocart";
		document.mapper.submit();
		return true;
	}
	else
	{
	    document.mapper.operation.value = 'undefined';
	    alert("bad input parameter - setMapMode(" + strMode + ")");
	}
    return true;	
}//end setmapmode()

function dragbox()
{		
    if (xabs > xdrag) 
    {
		boxleft.style.left = xdrag;
		boxright.style.left = xabs;
		boxtop.style.left = xdrag;
		boxtop.style.width = (xabs - xdrag);
		boxbottom.style.left = xdrag;
		boxbottom.style.width = (xabs - xdrag);
    }
    if (xabs < xdrag)
    {
  		boxleft.style.left = xabs;
  		boxright.style.left = xdrag;
  		boxtop.style.left = xabs;
  		boxtop.style.width = (xdrag - xabs);
  		boxbottom.style.left = xabs;
  		boxbottom.style.width = (xdrag - xabs);
    }
    if (yabs > ydrag)
    {
  		boxtop.style.top = ydrag;
  		boxbottom.style.top = yabs;
  		boxleft.style.top = ydrag;
  		boxleft.style.height = (yabs - ydrag);
  		boxright.style.top = ydrag;
  		boxright.style.height = (yabs - ydrag);
    }
    if (yabs < ydrag)
    {
  		boxtop.style.top = yabs;
  		boxbottom.style.top = ydrag;
  		boxleft.style.top = yabs;
  		boxleft.style.height = (ydrag - yabs);
  		boxright.style.top = yabs;
  		boxright.style.height = (ydrag - yabs);
    }   
  
	return true;
}//end dragbox()

var iMinZoomWindowSize = 3;

function doZoom()
{
	//set the mode
	if ((Math.abs(document.mapper.x1.value - document.mapper.x2.value) <= iMinZoomWindowSize) || (Math.abs(document.mapper.y1.value - document.mapper.y2.value) <= iMinZoomWindowSize))
	{
		setMapMode("recenter");
	}
	else
	{
		document.mapper.operation.value = "zoom";
	}
	showLoading();
	document.mapper.submit();
	//return true;	
}

function doSelect()
{
		BoxMinX = document.mapper.x1.value;
		BoxMinY = document.mapper.y1.value;
	
		BoxMaxX = document.mapper.x2.value;
		BoxMaxY = document.mapper.y2.value;

		for (var i in surveys)
		{
			 bIconIn = false;
			 bLabelIn = false;
			 Xin = false;
			 Yin = false;
			 
			// If the width or height of the drawn bounding box is greater then the minimum size required for the rubber band zoom then execute it
			if (((BoxMaxX - BoxMinX) > iMinZoomWindowSize) || ((BoxMaxY - BoxMinY) > iMinZoomWindowSize))
			{
				 //is survey.icon within select box
				 Xin = ((BoxMaxX > parseInt(surveys[i].icon.minX)) && (BoxMinX < parseInt(surveys[i].icon.maxX)));
				 if (!Xin)
				 {
				    Xin = ((BoxMaxX > parseInt(surveys[i].icon.maxX)) && (BoxMinX < parseInt(surveys[i].icon.maxX)));
				 }
				 if (Xin)
				 {
				    Yin = ((BoxMaxY > parseInt(surveys[i].icon.minY)) && (BoxMinY < parseInt(surveys[i].icon.maxY)))
				    if (!Yin)
				    {
				        Yin = ((BoxMaxY > parseInt(surveys[i].icon.minY)) && (BoxMinY < parseInt(surveys[i].icon.minY)))
				    }
				 }
				 if ((Xin) && (Yin))
				 {
				    bIconIn = true;
				 }
				 
				//reset and see if label is within box
				Xin = false;
				Yin = false;
			     
			     if (!bIconIn)
			     {
				    Xin = ((BoxMaxX > parseInt(surveys[i].label.minX)) && (BoxMinX < parseInt(surveys[i].label.minX)))
				    if (!Xin)
				    {
				       Xin = ((BoxMaxX > parseInt(surveys[i].label.maxX)) && (BoxMinX < parseInt(surveys[i].label.maxX)))
				    }
				    if (Xin)
				    {
				       Yin = ((BoxMaxY > parseInt(surveys[i].label.maxY)) && (BoxMinY < parseInt(surveys[i].label.maxY)))
				       if (!Yin)
				       {
				           Yin = ((BoxMaxY > parseInt(surveys[i].label.minY)) && (BoxMinY < parseInt(surveys[i].label.minY)))
				       }
				    }
				    if ((Xin) && (Yin))
				    {
				       bLabelIn = true;
				    }
				}
			}
			else	//is a click
			{
				if ((parseInt(surveys[i].icon.maxX) >= BoxMinX) && (parseInt(surveys[i].icon.minX) <= BoxMinX))
				{
					Xin = true;
					
					if ((parseInt(surveys[i].icon.maxY) <= BoxMinY) && (parseInt(surveys[i].icon.minY) >= BoxMinY))
					{
						Yin = true;
					}
				}
				if ((Xin) && (Yin))
				{
					bIconIn = true;
				}

				//reset and see if click is within label
				Xin = false;
				Yin = false;

				if (!bIconIn)
				{
					if ((parseInt(surveys[i].label.maxX) >= BoxMinX) && (parseInt(surveys[i].label.minX) <= BoxMinX))
					{
						Xin = true;
						
						if ((parseInt(surveys[i].label.maxY) <= BoxMinY) && (parseInt(surveys[i].label.minY) >= BoxMinY))
						{
							Yin = true;
						}
					}
					if ((Xin) && (Yin))
					{
						bLabelIn = true;
					}
				}
			}
			if ((bIconIn) || (bLabelIn))
			{
				
			   /*var oCheck = eval('chk' + surveys[i].vdbuwi);
			   if (!oCheck.checked)
			   {
			       oCheck.click();
			   }*/
			   
			   var name = 'chk' + surveys[i].vdbuwi;
			   
			   var elem = document.forms[0].elements;
			   
			   for(var j = 0; j < elem.length; j++){
					if(elem[j].type == "checkbox" && elem[j].name == name){
						elem[j].checked = true;
						break;
					}
			   }
			}
		}			 
		
		//clear x1,x2,y1,y2
		document.mapper.x1.value = "";
		document.mapper.y1.value = "";
		document.mapper.x2.value = "";
		document.mapper.y2.value = "";
		
		return true;
}

function getScrollY() 
{
	return document.body.scrollTop;
}

function getScrollX(){
	//get the right scroll content
	return document.body.scrollLeft;
}

function getScrollH()
{
	return document.body.scrollHeight;
}

function showLoading()
{
	var loading = document.getElementById("loading");

    loading.style.top =350;
    loading.style.left = 150;
    loading.style.visibility = 'visible';

    return true;
}

function hideLoading()
{
	var loading = document.getElementById("loading");
	
	loading.style.visibility = 'hidden';
	
	return true;
}

function showMapLayers(){
	buttons.style.visibility = 'hidden';
	maplayers.style.zIndex = 25;
	maplayers.style.visibility = 'visible';
}

function hideMapLayers(){
	maplayers.style.zIndex = -1;
	maplayers.style.visibility = 'hidden';
	buttons.style.visibility = 'visible';
}

function doMouseLayerText(strInput)
{	
	if(document.layers == null){
		mouseover.innerHTML = strInput;			
	}
}

function showInstructions()
{
	var strTmp = "";
		
	strTmp += "<SPAN STYLE=\"FONT-SIZE:12pt; COLOR:#ffffff; FONT-FAMILY:Arial,sans-serif,helvetica;\"><u><b>&nbsp;&nbsp;Location</b>&nbsp;Information&nbsp;&nbsp;&nbsp;</u></span><BR><SPAN CLASS=tiny>&nbsp;</SPAN><BR>"
	strTmp += "<SPAN STYLE=\"FONT-SIZE:8pt; COLOR:#ffffff; FONT-FAMILY:Arial,sans-serif,helvetica;\">"
	strTmp += "&nbsp;&nbsp;&nbsp;When the map is in &#34;Select&#34; mode<BR> ";
	strTmp += "&nbsp;&nbsp;&nbsp;move your mouse over any well<BR> ";
	strTmp += "&nbsp;&nbsp;&nbsp;location on the map and information<BR> ";
	strTmp += "&nbsp;&nbsp;&nbsp;regarding that well will appear here.<BR><BR>";
	strTmp += "&nbsp;&nbsp;&nbsp;Click on a well icon or label and it <BR>";
	strTmp += "&nbsp;&nbsp;&nbsp;will be checked off in the list below.<BR><BR>";
	strTmp += "&nbsp;&nbsp;&nbsp;Draw a box on the map and the wells<BR>";
	strTmp += "&nbsp;&nbsp;&nbsp;within its boundaries will be <BR>";
	strTmp += "&nbsp;&nbsp;&nbsp;selected in the list below.<BR><BR>";
	strTmp += "&nbsp;&nbsp;&nbsp;To start a new search or further<BR>";
	strTmp += "&nbsp;&nbsp;&nbsp;refine your Zoom parameters, please <BR>";
	strTmp += "&nbsp;&nbsp;&nbsp;click 'Zoom Mode' above.<BR>";
	strTmp += "</SPAN>";
	
	buttons.style.visibility = 'hidden';
	
	doMouseLayerText(strTmp);	
}

function showMe(objMarker)
{
		var strTmp = "&nbsp;";
		if (objMarker != 'none')
		{
			strTmp = "";
			
			strTmp += "<SPAN STYLE=\"FONT-SIZE:12pt; COLOR:#ffffff; FONT-FAMILY:Arial,sans-serif,helvetica;\"><u><b>&nbsp;&nbsp;Location</b>&nbsp;Information&nbsp;&nbsp;&nbsp;</u></span><BR><SPAN CLASS=tiny>&nbsp;</SPAN><BR>"
			strTmp += "<SPAN STYLE=\"FONT-SIZE: 8pt; COLOR: #ffffff; FONT-FAMILY: Arial,sans-serif,helvetica; \"><B>&nbsp;&nbsp;" + objMarker.wellname + ", Well Number " + objMarker.wellnumber + "</B></SPAN><BR>"
			strTmp += "<SPAN STYLE=\"FONT-SIZE: 8pt; COLOR: #dddddd; FONT-FAMILY: Arial,sans-serif,helvetica; \">&nbsp;&nbsp;" + objMarker.operator + "<BR>"
			strTmp += "&nbsp;&nbsp;" + objMarker.location + "<BR>"
			strTmp += "&nbsp;&nbsp;" + objMarker.area + ", " + objMarker.state + "<BR><BR>"
			strTmp += "&nbsp;&nbsp;" + "Measured Depth: <B>" + objMarker.mdepth + "</B><BR>"
			strTmp += "&nbsp;&nbsp;" + "Log Start: <B>" + objMarker.logstart + "</B><BR>"
			strTmp += "&nbsp;&nbsp;" + "Log Stop: <B>" + objMarker.logstop + "</B><BR>"
			strTmp += "&nbsp;&nbsp;" + "VDB#:<B> " + objMarker.vdbuwi + "</B></SPAN><BR>\n"				
				
					
			//Display address info in status bar
			var strStatusLine = objMarker.wellname + ", Well Number: " + objMarker.wellnumber + ", " + objMarker.operator;
			
			//output string info			
			doMouseLayerText(strTmp);
			setStatus(strStatusLine);
	}
}

function GetKeyPress(evt){
	//test for enter
	if(evt == null){
		if(event.keyCode == 13){
			setMapMode("update");
		}
	}else{
		if(evt.keyCode == 13){
			setMapMode("update");
		}
	}
}  

