<!-- script -->
<!-- 
// functions called from within the mapimage applet that allow us
// to do box zooms and queries

// document.control.autosubmit = true;









function SetImageXY(i) { 
		var foo = i.split(",");
		x = foo[0];
		y = foo[1];


		if (!document.control) {
			alert("control does not exist");
		}

		document.control.elements['setpoint.x'].value = x;
		document.control.elements['setpoint.y'].value = y;
	    if( document.control.autosubmit )
		{
			document.control.submit();
		}
}


function SetImageBox(i) { /* alert('i is '+i); */ draw(i); }


function pixel_to_geographic(box)
{
	var map_x1,map_y1,map_x2,map_y2,lx1,ly1,lx2,ly2

		// Defines the locator box coordinates by breaking up the box extent variable passed to
		// the function from the marquee applet.

	var extent_list = box.split(",");

	lx1 = extent_list[0];
	ly1 = extent_list[1];
	lx2 = extent_list[2];
	ly2 = extent_list[3];

	// The marquee applet will always return the x,y values in the correct order, no matter
	// what direction the user drags the marquee in.  Thereofre x1,y1 will always represent
	// the top left extreme, and x2,y2 the bottom right extreme.

	// Change at a later date so that can be altered depending on locator map more easily.



		// Calculates the extents of the marquee based on the image coordinates returned.

		// Range of geographic values
	x_range = g_maxx - g_minx;
	y_range = g_miny - g_maxy;



	// Calculates the upper left geographic coordinates of the marquee selection
	scalex1 = lx1/image_x;
	scaley1 = ly1/image_y;

	newx1 = scalex1*x_range;
	newy1 = scaley1*y_range;


	// New map extent for upper left (nb these are in the same format as the image x,y co-ordinates)
	map_x1 = g_minx+newx1;
	map_y1 = g_maxy+newy1; 


	// Calculates the lower right geographic coordinates of the marquee selection
	scalex2 = lx2/image_x;
	scaley2 = ly2/image_y;

	newx2 = scalex2*x_range;
	newy2 = scaley2*y_range;


	// New map extent for lower right (nb these are in the same format as the image x,y co-ordinates)
	map_x2 = g_minx+newx2;
	map_y2 = g_maxy+newy2; 

	// The extents are reassembled for use by the setcontext method.

	x1 = map_x1
	y1 = map_y2
	x2 = map_x2
	y2 = map_y1

	extent = x1 + "," + y1 + "," + x2 + "," + y2 

	return extent;
}


// reverse of pixel_to_geographic
// --jeff
function geographic_to_pixel(box) 
{
	var extent_list = box.split(",");

	x1 = extent_list[0];
	y1 = extent_list[1];
	x2 = extent_list[2];
	y2 = extent_list[3];



		// Calculates the extents of the marquee based on the image coordinates returned.

		// Range of geographic values
	x_range = g_maxx - g_minx;
	y_range = g_miny - g_maxy;

	map_x1=x1;
	map_y1=y2;
	map_x2=x2;
	map_y2=y1;

	newx1 = map_x1 - g_minx;
	newy1 = map_y1 - g_maxy;
	newx2 = map_x2 - g_minx;
	newy2 = map_y2 - g_maxy;

	scalex1 = newx1 / x_range;
	scaley1 = newy1 / y_range;
	scalex2 = newx2 / x_range;
	scaley2 = newy2 / y_range;

	lx1 = scalex1 * image_x;
	ly1 = scaley1 * image_y;
	lx2 = scalex2 * image_x;
	ly2 = scaley2 * image_y;

	extent = lx1+","+ly1+","+lx2+","+ly2;
//	alert("converted pixels to geographical: "+extent);
	return extent;
}


// Reloads the map based on the region selected. Does not update the locator map. 


// This is where the marquee values are used to place geographic cordinates in to the text search box.

function draw(box)
{
	var extent = pixel_to_geographic(box);
	//alert("locator map x,y equates to geographic coordinates " + extent);

	var definedregion = extent.split(",");
	var x1reg = definedregion[0];
	var y1reg = definedregion[1];
	var x2reg = definedregion[2];
	var y2reg = definedregion[3];

	document.FGDC.North.value = y2reg;
	document.FGDC.South.value = y1reg;
	document.FGDC.East.value = x2reg;
	document.FGDC.West.value = x1reg;


// Updates the locator map on a change being made by the user
	updateLocator(document.applets['mapimage'], extent);


	return true;

}



function updateLocator(applet, box) {

	if (!document.applets['mapimage']) {
    alert("updateLocator: can't find the applet");
		return false;
	}

	//alert("updating locator");
	var extent_list = geographic_to_pixel(box).split(",");

	x1 = extent_list[0];
	y1 = extent_list[1];
	x2 = extent_list[2];
	y2 = extent_list[3];

  // updates locator map to highlight selected area (state/country) from pulldown list
	//applet.setPermBox(x1-0,y1-0,x2-0,y2-0, 2);
	return true;
}

//-->
<!-- /script-->


