// The width of the map at the maximum zoom level (in metres)
var MAX_ZOOM_WIDTH = 500;
// The multiplier used to scale up zoom levels (see function zoomToLevel)
// Increasing this will make bigger jumps between zoom levels and make the map at zoom level 2
// cover a much bigger area. Decreasing it will have the reverse effect. A multiplier of 1 will 
// make everything the same, obviously!
var ZOOM_MULTIPLIER = 2;
var ZOOM_RANGECOUNT = 9;

// This	function is	called on a	mouseup	event
// and events triggered	by the map.
function triggerUpdate()
{
	// Show	the loading icon.
	showPage('visible');
	
	// Get the map form.
	var	mapForm	= document.forms['mapForm'];
			
	// Set the parameters to be passed.
	mapForm.elements['mapTool'].value = toolStatus;
	mapForm.elements['mapButton'].value = buttonStatus;
	
	if(mapForm.elements['SelectedAreasIDs'].value != '')
	{
		mapForm.elements['showHighlight'].value = 'True';
	}
	else
	{
		mapForm.elements['showHighlight'].value = 'False';
	}
	//alert(buttonStatus);
	if(buttonStatus != "false")
	{
		if((zl == 1) && (buttonStatus == "zoomLevel"))
		{
			mapForm.elements['mapMinX'].value = minExtentX;
			mapForm.elements['mapMinY'].value = minExtentY;
			mapForm.elements['mapMaxX'].value = maxExtentX;
			mapForm.elements['mapMaxY'].value = maxExtentY;
		}
		else if(buttonStatus == "clearSelection")
		{
			mapForm.elements['SelectedAreasIDs'].value = "";
			mapForm.elements['selMinX'].value = "";
			mapForm.elements['selMinY'].value = "";
			mapForm.elements['selMaxX'].value = "";
			mapForm.elements['selMaxY'].value = "";
		}
		else
		{
			mapForm.elements['mapMinX'].value = rx1;
			mapForm.elements['mapMinY'].value = ry1;
			mapForm.elements['mapMaxX'].value = rx2;
			mapForm.elements['mapMaxY'].value = ry2;
		}
	}
	else
	{
		if(toolStatus == "SELECT"  || toolStatus == "IDENTIFY")
		{
			
			mapForm.elements['selMinX'].value = rx1;
			mapForm.elements['selMinY'].value = ry1;
			mapForm.elements['selMaxX'].value = rx2;
			mapForm.elements['selMaxY'].value = ry2;
			mapForm.elements['showHighlight'].value = 'True';
			mapForm.submit();
		}
		if(toolStatus == "ZOOM_IN")
		{
			mapForm.elements['mapMinX'].value = rx1;
			mapForm.elements['mapMinY'].value = ry1;
			mapForm.elements['mapMaxX'].value = rx2;
			mapForm.elements['mapMaxY'].value = ry2;
			mapForm.submit();
		}
		if(toolStatus == "SEARCH")
		{
			mapForm.elements['mapMinX'].value = minx;
			mapForm.elements['mapMinY'].value = miny;
			mapForm.elements['mapMaxX'].value = maxx;
			mapForm.elements['mapMaxY'].value = maxy;
			mapForm.submit();
		}
		if(toolStatus == "FULLSCREEN")
		{
			mapForm.elements['mapTool'].value = "ZOOM_IN";
			mapForm.elements['mapButton'].value = "ZOOM_IN";
			mapForm.elements['mapMinX'].value = rx1;
			mapForm.elements['mapMinY'].value = ry1;
			mapForm.elements['mapMaxX'].value = rx2;
			mapForm.elements['mapMaxY'].value = ry2;			
		}		
	}
	//alert("X:" + mapForm.elements['mapMinX'].value + "," + mapForm.elements['mapMaxX'].value + " Y:" + mapForm.elements['mapMinY'].value + ", " + 	mapForm.elements['mapMaxY'].value );
	// Submit the form.	This refreshes the map
	// with	the	new	parameters.
	mapForm.submit();
}

// Removes/Displays the loading page once the map image has been loaded.
function showPage(isVisible)
{
	
	// DOM3	= IE5, NS6
	if (document.getElementById) 
	{  
		document.getElementById('hidepage').style.visibility = isVisible;
	}
	else 
	{
		// Netscape	4
		if (document.layers) 
		{	
			document.hidepage.visibility = isVisible;
		}
		// IE 4
		else 
		{  
			document.all.hidepage.style.visibility = isVisible;
		}
	}
}

/** Updates the global "toolStatus" variable that controls user mouse interaction with 
		the map. Also updates some on screen features (buttons, colours).
		@param tool One of IDENTIFY, SELECT, ZOOM_IN, ZOOM_OUT or PAN.
		*/
function setToolStatus(tool) {
	toolStatus = tool;
	
	document.mapForm.elements['mapTool'].value = toolStatus;

	if (toolStatus == 'ZOOM_OUT') {
		if (document.all) {
			if (document.all['theMap']) document.all['theMap'].style.cursor = 'crosshair';
		}
	}
	else if (toolStatus == 'PAN') {
		if (document.all) {
			if (document.all['theMap']) document.all['theMap'].style.cursor = 'move';
		}
	}
	else if (toolStatus == 'IDENTIFY') {
		MM_nbGroup('down','group2','map_btn_info-off','../images/map_btn_info-on.gif',1)
		if (document.all) {
			if (document.all['theMap']) document.all['theMap'].style.cursor = 'default';
		}
	}
	else if (toolStatus == 'SELECT') {
		MM_nbGroup('down','group2','map_btn_selectoff','../images/map_btn_select-on.gif',1);
		zoomBoxColor = '#0000FF';
		if (document.all) {
			if (document.all['theMap']) document.all['theMap'].style.cursor = 'default';
		}
	}
	else if (toolStatus == 'SEARCH') {
	}
	else if (toolStatus == 'PRINT') {
		toolStatus='ZOOM_IN';
		document.mapForm.mapTool.value ='ZOOM_IN';
		buttonStatus='false';
		rx1=minx;
		rx2=maxx;
		ry1=miny;
		ry2=maxy;
		
							
		triggerUpdate();
	
	}
	else if (toolStatus == 'FULLSCREEN') {
			toolStatus='FULLSCREEN';
			document.mapForm.mapTool.value ='FULLSCREEN';
			buttonStatus='false';
			rx1=minx;
			rx2=maxx;
			ry1=miny;
			ry2=maxy;
			
								
			triggerUpdate();
		
	}
	else if (toolStatus == 'LABEL') {
			
			toolStatus='ZOOM_IN';
			document.mapForm.mapTool.value ='ZOOM_IN';
			buttonStatus='false';
			
			rx1=minx;
			rx2=maxx;
			ry1=miny;
			ry2=maxy;
			toggleLabel();
			triggerUpdate();
		
	}
	
	else {
		toolStatus = 'ZOOM_IN';
		MM_nbGroup('down','group2','map_btn_zoominoff','../images/map_btn_zoomin-on.gif',1)
		if (document.all) {
			if (document.all['theMap']) {
				document.all['theMap'].style.cursor = 'crosshair';
			}
		}
	}
}

/**	Zooms the map to a specific scale level.
		The zoom levels are set by global variables above - MAX_ZOOM_WIDTH and ZOOM_MULTIPLIER.
		At zoom level 9 the map will be MAX_ZOOM_WIDTH across.
		At zoom level 2 the map will be MAX_ZOOM_WIDTH * ZOOM_MULTIPLIER^(9 - 2).
		i.e. if the width at zoom level 9 is 400m and the ZOOM_MULTIPLIER is 2 (the default), 
		then the width at zoom level 2 will be 51200m - 400 * 2^7 or 400 * 2 * 2 * 2 * 2 * 2 * 2 * 2
		@param zoom The zoom level - between 1 (the full extent) and 9 (the maximum zoom or smallest extent)
		*/
function zoomToLevel(zoom) 
{
	var level;

	if ((zoom < 1) || (zoom == 1)) 
	{
		zl = 1;
	}
	else 
	{
		if (zoom > ZOOM_RANGECOUNT) zoom = ZOOM_RANGECOUNT;
		level = MAX_ZOOM_WIDTH * Math.pow(ZOOM_MULTIPLIER, ZOOM_RANGECOUNT - zoom);
			
		// Reset the zoom level;
		zl = zoom;
			
		// Get the centre of the main map.
		var mapCenterX = minx + ((maxx - minx)/2);
		var mapCenterY = miny + ((maxy - miny)/2); 
			
		// Reset the bbox coords.
		// These will be rejigged by ArcIMS to preserve screen resolution.
   		rx1 = mapCenterX - (level / 2); 
		rx2 = mapCenterX + (level / 2); 
		ry1 = mapCenterY - (level / 2); 
		ry2 = mapCenterY + (level / 2); 
	}
	// Set levelZoom to true and update map.
	buttonStatus = "zoomLevel";
	triggerUpdate();
	return false;
}

function findScaleButton() 
{
	var across = Math.abs(maxx - minx);
	var updown = Math.abs(maxy - miny);
	// Now check the zoom level
	var zoomAt;
	zl = 1;

	for (i = ZOOM_RANGECOUNT; i > 0; i--) 
	{
		zoomAt = MAX_ZOOM_WIDTH * Math.pow(ZOOM_MULTIPLIER, ZOOM_RANGECOUNT - i);
		
		if (across <= zoomAt || updown <= zoomAt) 
		{
			zl = i;
			break;
		}
	}
	MM_nbGroup('down','group1','z'+zl,'../images/map_zoom_'+zl+'-on.gif',1);
}

	

// Check whether to show background mapping.
function toggleBackground()
{
	// Get the map form.
	var mapForm = document.forms['mapForm'];
	var sb= mapForm.elements['showBackground'].value;

	if((sb == "true") || (sb == "True") )
	{
		mapForm.elements['showBackground'].value = "false";
	}
	else
	{
		mapForm.elements['showBackground'].value = "true";
	}
}

function toggleLabel()
{
	// Get the map form.
	var mapForm = document.forms['mapForm'];
	var sl= mapForm.elements['showLabel'].value;
	
	if((sl == "true") || (sl == "True") )
	{
		mapForm.elements['showLabel'].value = "False";
	}
	else
	{
		mapForm.elements['showLabel'].value = "True";
	}
}

function getObjCommon(name)
{
	var obj;
	
	if (document.getElementById)
	{
		obj = document.getElementById(name);
	}
	else if (document.all)
	{
		obj = document.all[name];
	}
	
	return obj;	
}

function UpdateSelection(form)
{
	form.elements['UpdateButton'].value ="Update";
	form.submit();
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}

	return sString;
}

function toggleChild(chkValue,strName)
{
	var chks = document.frmAnalyser.getElementsByTagName('input');
	var ilen = chks.length;
	var iPos;
	var schkName;
	for (var i=1; i<ilen; i++)
	{ 
		if (chks[i].type=="checkbox" & chks[i].name.length>8)
		{
			schkName = chks[i].name;
			if (schkName.substring(0,8)=="chkL-IN-")
			{
				iPos = schkName.indexOf(':');
				if (iPos > 0)
				{
					schkName = trimAll(schkName.substring(8, iPos));
					if (schkName == strName)
					{
						chks[i].checked = chkValue;
					}//end of compare names
				}//end of pos checking
			}//end of ckL check
		}//end of type checking
	}//end for loop
}//end function

function toggleParent(chkValue,strName)
{
	var chks = document.frmAnalyser.getElementsByTagName('input');
	var ilen = chks.length;
	var iPos;
	var schkName;
	var bFound;
	var bValue;
	bFound =false;//Initilize the variable
	strName = trimAll(strName.substring(0,strName.indexOf(':')));
	for (var i=1; i<ilen; i++)
	{ 
		if (chks[i].type=="checkbox" & chks[i].name.length>8)
		{
			schkName = chks[i].name;
			if (schkName.substring(0,8)=="chkL-IN-")
			{
				iPos = schkName.indexOf(':');
				if (iPos > 0)
				{
					schkName = trimAll(schkName.substring(8, iPos));
					if (schkName == strName)
					{
						if (bFound==false)
						{
							bValue = chks[i].checked;
							bFound=true;
						}
						else
						{
							if (chks[i].checked != bValue)
							{
								bFound=false;
								break;
							}
						}
					}//end of compare names
				}//end of pos checking
			}//end of ckL check
		}//end of type checking
	}//end for loop
	//if (bFound == true)
	//{
		for (var i=1; i<ilen; i++)
		{ 
			if (chks[i].type=="checkbox" & chks[i].name.length>8)
			{
				schkName = chks[i].name;
				if (schkName.substring(0,8)=="chkL-IH-")
				{
					iPos = schkName.indexOf('~');
					if (iPos > 0)
					{
						schkName = trimAll(schkName.substring(8, iPos));
						if (schkName == strName)
						{
							if (bFound)
								chks[i].checked = bValue;
							else
								chks[i].checked = false;
							break;
						}
					}//end ipos
				}//end chk
			}//end compare
		}//for loop
	//}//end if found

}//end function

function selectDeselectAll(chkValue) 
{ 

    var chks = document.frmAreaTree.getElementsByTagName('input');
	var ilen = chks.length;
	var iPos;
	var schkName;
	for (var i=1; i<ilen; i++)
	{ 
		if (chks[i].type=="checkbox" & chks[i].name.length>3)
		{
			schkName = chks[i].name;
			if (schkName!="chkSelectAll" & schkName.substring(0,3)=="chk")
			{
				chks[i].checked = chkValue;
			}
		}//end of type checking
	}//end for loop

}
