<!--
// all scripts related to using the map tools on the map are contained
// within this file.

// define variables...
var currentToolCmd = -1;
var dragging = false;
var depressed = false;
var mouseX1, mouseY1, mouseX2, mouseY2, minX, minY, maxX, maxY;

document.onmousedown = handleMouseDown;
document.onmouseup = handleMouseUp;



// ###########################################################
// function handleMouseDown(e)
// This function is triggered by the onMousedown event and
// performs various tasks on the map based on the current tool
// selected.

function handleMouseDown(e) {
	// get X/Y value of mousedown
	resetDragBox();
	setX1Y1(e);
	// set mouse state to depressed
	depressed = true;
	validPoint = checkBounds(mouseX1,mouseY1);
	// get active tool from toolbar panel
	currentToolCmd = activeBtnNum;
	
	if (currentToolCmd!=-1 && validPoint) {
		// start drag box if zoom or recselect tool is active
		if (currentToolCmd == 0) {
			if (document.all) shiftTo('dragBox',(mouseX1-7),(mouseY1-7));
			else shiftTo('dragBox',mouseX1,mouseY1);
			showObject('dragBox');
			document.onmousemove = drawBox;
		}
		else if (currentToolCmd == 2) {
	// document.onmousemove = panMap;
			placePoint(mouseX1,mouseY1);
		}
	
	} // if (currentToolCmd!=-1 && validPoint)
	
} // end function handleMouseDown(e)

// ###########################################################


// function handleMouseUp(e)
// This function is triggered by the onMouseup event. Records
// the x/y coordinates of the mouseup event. Sets depressed
// variable, and then calls submitMapCommands().

function handleMouseUp(e) {
	// get X/Y value of mouseup
	setX2Y2(e);
	// reset variables
	depressed = false;
	validPoint = checkBounds(mouseX2,mouseY2);	
	// set mapAttributes form values
	if (currentToolCmd!=-1 && validPoint) setMapAttributes();
	else resetDragBox();
		
} // end function handleMouseUp(e)
// ###########################################################

// ###########################################################
// function setX1Y1(e)
// This function is called on all mousedown events and sets
// the value of mouseX1 and mouseY1 to the mouse's XY coordinates
// within the browser window.

function setX1Y1(e) {
	mouseX1 = (document.all) ? event.clientX : e.pageX;
    mouseY1 = (document.all) ? event.clientY : e.pageY;
	return true;
} // end function setXY(e)
// ###########################################################

// ###########################################################
// function setX2Y2(e)
// This function is called on all mouseup events and sets
// the value of mouseX2 and mouseY2 to the mouse's XY coordinates
// within the browser window.

function setX2Y2(e) {
	mouseX2 = (document.all) ? event.clientX : e.pageX;
	mouseY2 = (document.all) ? event.clientY : e.pageY;
	return true;
} // end function setX2Y2(e)
// ###########################################################

// ###########################################################
// function drawBox(evt)
// This function is triggered by the onMousemove event and
// draws a selection box on the screen if the active tool is
// zoomin or recselect and depressed=true;

function drawBox(e) {

	boxObj=findDOM('dragBox',1);
	if (currentToolCmd == 5) boxObj.border = '2px dashed red';

	xDIM = (document.all) ? event.clientX : e.pageX;
	yDIM = (document.all) ? event.clientY : e.pageY;
	validDragPoint = checkBounds(xDIM,yDIM);

	if (depressed && validDragPoint) {

		if ((eval(xDIM) > eval(mouseX1)) && (eval(yDIM) > eval(mouseY1))) {
		newWidth = (eval(xDIM) - eval(mouseX1));
		newHeight = (eval(yDIM) - eval(mouseY1));
		resizeBox('dragBox',newWidth,newHeight);
		}
		else if ((eval(xDIM) < eval(mouseX1)) && (eval(yDIM) > eval(mouseY1))) {
		newWidth = (eval(mouseX1) - eval(xDIM));
		newHeight = (eval(yDIM) - eval(mouseY1));
		resizeBox('dragBox',newWidth,newHeight);
		shiftTo('dragBox',xDIM,mouseY1);
		}
		else if ((eval(xDIM) < eval(mouseX1)) && (eval(yDIM) < eval(mouseY1))) {
		newWidth = (eval(mouseX1) - eval(xDIM));
		newHeight = (eval(mouseY1) - eval(yDIM));
		resizeBox('dragBox',newWidth,newHeight);
		shiftTo('dragBox',xDIM,yDIM);
		}
		else if ((eval(xDIM) > eval(mouseX1)) && (eval(yDIM) < eval(mouseY1))) {
		newWidth = (eval(xDIM) - eval(mouseX1));
		newHeight = (eval(mouseY1) - eval(yDIM));
		resizeBox('dragBox',newWidth,newHeight);
		shiftTo('dragBox',mouseX1,yDIM);
		}

	} // end if (depressed)

} // end function drawBox(evt)
// ###########################################################

// ###########################################################

function panMap(e) {

	// get current mouse coordinates
	currentX = (document.all) ? event.clientX : e.pageX;
	currentY = (document.all) ? event.clientY : e.pageY;
	validDragPoint = checkBounds(currentX,currentY);

	mapObj=findDOM('map',1);

	if (depressed && validDragPoint) {
	
		deltaX = currentX - mouseX1;
		deltaY = currentY - mouseY1;
		
		mapObj.left = minX + deltaX;
		mapObj.top = minY + deltaY;
		
		mapObjRight = findRight('map');
		mapObjBottom = findBottom('map');
		
		if (parseInt(mapObjRight) > maxX) clipRt = maxX - (mapObjRight - maxX) - minX;
		else clipRt = maxX;
		if (parseInt(mapObj.left) < minX) clipLt = (minX - parseInt(mapObj.left));
		else clipLt = 0;
		if (parseInt(mapObj.top) < minY) clipTop = (minY - parseInt(mapObj.top));
		else clipTop = 0;
		if (mapObjBottom > maxY) clipBottom = maxY - (mapObjBottom - maxY) - minY;
		else clipBottom = maxY;
		setClip('map',clipTop,clipRt,clipBottom,clipLt);
	
	} // if (depressed && dragging && validDragPoint)
	
} // end function panMap(e)
// ###########################################################

// ###########################################################
// function submitMapCommands()
// determines active tool area of screen based on visible map

function getBoundaries() {
	minX = findLeft('map');
	maxX = findRight('map');
	minY = findTop('map');
	maxY = findBottom('map');
} // end function getBoundaries()
// ###########################################################

// ###########################################################
// function resizeBox(object,x,y)
// this function receives an object name, x and y value, and
// resizes the object to the new dimensions.

function resizeBox(object,x,y) {
	boxObj=findDOM(object,1);
	boxObj.width = x;
	boxObj.height = y;
} // end function resizeBox(object,x,y)
// ###########################################################

// ###########################################################
// function setClip(objectID, clipTop, clipRight, clipBottom, clipLeft)
// this function receives an object name and 4 clipping values
// and resets the visible portion of the object to those values...

function setClip(objectID, clipTop, clipRight, clipBottom, clipLeft) {
	var dom = findDOM(objectID,1);
	if (dom.clip.left) {
		dom.clip.top = clipTop;
		dom.clip.right = clipRight;
		dom.clip.bottom = clipBottom;
		dom.clip.left = clipLeft;
	}
	dom.clip = 'rect('+clipTop+' '+clipRight+' '+clipBottom+' '+clipLeft+')';
} // end function setClip()
// ###########################################################

// ###########################################################
// function resetDragBox()
// function resets the red zoomin box to 1 x 1 and hides it

function resetDragBox() {
	hideObject('dragBox');
	resizeBox('dragBox',1,1);
} // end function resetDragBox
// ###########################################################

// ###########################################################

function setMapAttributes() {
	showObject('updateBox');
	formObj = findDOM('mapAttributes',0);
	formObj.command.value = commandsArray[currentToolCmd];
	// set mouseDown value...
	formObj.mouseDownPoint.value = mouseX1+','+mouseY1;
	// set mouseUp value...
	formObj.mouseUpPoint.value = mouseX2+','+mouseY2;
	formObj.submit();
} // end function setMapAttributes()
// ###########################################################

// ###########################################################

function checkBounds(x,y) {
	if (x < minX || x > maxX || y < minY || y > maxY) return false;
	else return true;
} // end function checkBounds(x,y)
// ###########################################################

// ###########################################################

function resetExtents() {
	showObject('updateBox');
	formObj = findDOM('mapAttributes',0);
	formObj.command.value = "reset";
	formObj.submit();
} // end function resetExtents()
// ###########################################################

// ###########################################################
function fnSetValues(cityval,citytext)
{
   
   if (cityval != "Choose a Place")
       {
	    document.queryForm.city.value = cityval;
          submitMyQuery();
	    }
}//End of function getInfo()

//############################################################

//############################################################
function setGaz(cityval,citytext)
  {
    formObj = findDOM('queryForm',0);
   if ( formObj.searchType.value="parcel")
    {
     formObj.gazetteerTypeName.value = 'MA_GAZ_MDC_WSPA_PARCELS';
     formObj.city.value  = cityval;
    }
   else if ( formObj.searchType.value="town") 
     {
     formObj.gazetteerTypeName.value = 'MA_GAZ';
     formObj.city.value  = citytext;

     }
   }



//############################################################

//############################################################

function selectTown(value,name)
  {
   formObj = findDOM('queryForm',0);
   formObj.townID.value=value;
   formObj.locatorCity.value =name;
  }
//############################################################

//############################################################

 function submitMyQuery() {
    
	formObj = findDOM('queryForm',0);
	formObj.command.value = "query";

      if( formObj.searchType.value="town") {
            
                formObj.search.value=formObj.locatorCity.value;
                }
            else if (formObj.searchType.value="parcel")
           {
           formObj.gazetteerTypeName.value="MA_GAZ_MDC_WSPA_PARCELS";
           }
           
          showObject('updateBox');
		      formObj.submit();
      
  }

    
//############################################################

//############################################################ 
	
function submitOnEnter1(evt) {
  var keyCode = document.all ? event.keyCode :
                evt.which ? evt.which :
                evt.keyCode ? evt.keyCode :
                evt.charcode;
  if (keyCode == 13) {
    submitMyQuery();
    return false;
  }
  else
    return true;
} // end function submitOnEnter (evt)	

// ###########################################################

// ###########################################################
function submitViewQuery() {

	formObj = findDOM('queryForm',0);
	formObj.command.value = "listplaces";
	
	// ensure one of the radios is checked...
	checkedIndex = -1;
	for (i=0;i<formObj.searchType.length;i++) {
		if (formObj.searchType[i].checked) {
			checkedIndex = i;
			criteriaVal = formObj.searchType[i].value;
		} // if (formObj.searchType[i].checked)
	} // for loop
	if (checkedIndex == -1) {
		alert('Please select your query criteria.');
		return
	}
		
	formObj.gazetteerTypeName.value = setGAZType(criteriaVal);
	showObject('updateBox');
	formObj.submit();
	return;
} // end function submitQuery()
// ###########################################################

// ###########################################################
function validateInput(criteria,value) {
	if (criteria == "zip") {
		regExp = /\d{5}(-\d{4})?/;
		return regExp.test(value);
	} // if (criteria == "zip")
	return true;
} // end function validateInput(criteria,value)
// ###########################################################

// ###########################################################
function submitOnEnter (evt) {
  var keyCode = document.all ? event.keyCode :
                evt.which ? evt.which :
                evt.keyCode ? evt.keyCode :
                evt.charcode;
  if (keyCode == 13) {
    submitQuery();
    return false;
  }
  else
    return true;
} // end function submitOnEnter (evt)
// ###########################################################

// ###########################################################
function submitListItemHS(queryStr) {
  // get obj ref for query form...
  formObj = findDOM('queryForm',0);
  // set form values...
  formObj.command.value = "hsquery";
  for (i=0;i<formObj.searchType.length;i++) {
  	if (formObj.searchType[i].value == lastSearch) {
		formObj.searchType[i].checked = true;
	} // if statement
  } // for loop
  formObj.search.value = queryStr;
  formObj.gazetteerTypeName.value = lastGAZ;
  showObject('updateBox');
  formObj.submit();
} // end function submitListItem(queryStr)
// ###########################################################

// ###########################################################
function submitListItem(queryStr) {
  // get obj ref for query form...
  formObj = findDOM('queryForm',0);
  // set form values...
  formObj.command.value = "query";
  for (i=0;i<formObj.searchType.length;i++) {
  	if (formObj.searchType[i].value == lastSearch) {
		formObj.searchType[i].checked = true;
	} // if statement
  } // for loop
  formObj.search.value = queryStr;
  formObj.gazetteerTypeName.value = lastGAZ;
  showObject('updateBox');
  formObj.submit();
} // end function submitListItem(queryStr)
// ###########################################################

// ###########################################################
function setGAZType(criteriaVal) {
	if (criteriaVal == "parcel") return 'MA_GAZ_MDC_WSPA_PARCELS';
	else if (criteriaVal == "town") return 'MA_GAZ';
	else return null;
} // end function setGAZType(criteriaVal)
// ###########################################################

// ###########################################################
function setMapCursor(number) {
	mapObj = findDOM('map',1);
	if (number == 0 || number == 1)	mapObj.cursor = "crosshair";
	else if (number == 2) mapObj.cursor = "move";
	else if (number == 4) {
		if (document.all) mapObj.cursor = "hand";
		else mapObj.cursor = "pointer";
	}
	else return;
} // end function setMapCursor(commandNum)
// ###########################################################

// ###########################################################
// placePoint(mouseX1,mouseY1) - this function places a marker
// on the map at the mousedown x,y coordinate if the info tool
// is being used.
 
function placePoint(mouseX1,mouseY1) {

	shiftTo('redPoint',mouseX1,mouseY1);
	showObject('redPoint');

} // end function placePoint(mouseX1,mouseY1)

// ###########################################################

//-->

