
var activeLinkImage = ""

function getLayerIndexPosn(layerName)
{
	/*
		specific function comments :-

		loops through all layers in the mapservice. If the layer name, supplied as a parameter, exists within
		the mapservice, the function returns the index position of the maplayer.
	*/

	var t;
		var tName = "";
		if (opener) 
		{
			if (opener.name=="MapFrame") 
			{
				t = opener.parent.MapFrame;
				tName = "opener.parent.MapFrame.";
			} 
			else 
			{
				t = opener;
				tName = "opener.";
			}
		} 
		else 
		{
			if (parent.MapFrame) 
			{
				t = parent.MapFrame;
				tName = "parent.MapFrame.";
			} 
			else 
			{
				t=document;
			}
		}

		//loop through all layers in the mapservice.
		var theCount = t.layerCount;

		for (var i=0;i<theCount;i++) 
			{
				//check to see if the layer in the loop is the layer we want the index position of.
				if (parent.MapFrame.LayerName[i] == layerName) 
				{
					return i
					break
				}
			}
			
			//if the map layer was not found.
			alert("Map layer not found. Please wait for the map to load, before choosing a map layer to draw")
			parent.MapFrame.reloadApp();
}

//-------------------------------------------------------------------------------------------------------------------
function getAttributeValue(theXMLString, theAttributeRequired)
{
	//Return the attribute reqd
	var Pos=theXMLString.indexOf(theAttributeRequired);
	if (Pos!=-1)
	{
		var startpos = Pos + theAttributeRequired.length + 1;
		var endpos = theXMLString.indexOf('"', startpos);
		tempString = theXMLString.substring(startpos,endpos);
		
		//alert("Extracted " + tempString)
		return(tempString);
	}
	else
	{
		//alert("Unable to locate '" + theAttributeRequired + "' in '" + theXMLString + "'");
		return("");
	}
}

function getPRNs(theString, startpos)
{
	//alert("Starting to read at position:\n\n" + startpos)

	var theNumber = "";
	theString = theString.toUpperCase();
	var tempString = "";

	startpos = theString.indexOf("PRN=",startpos);
	startpos += 5;
	
	endpos = theString.indexOf(dQuote,startpos);
	if (endpos==-1)
	{
		endpos = theString.length
	}
	
	tempString = theString.substring(startpos, endpos);
	theNumber = tempString; 
	xmlEndPos = endpos;

	return theNumber;
}

function cycleScale(zoomIn)
{
	/*
		specific function comments :-
	*/

	//WE CAN ONLY ZOOM IN IF WE HAVE A MAP LAYER DRAWN
	if (zoomIn==true)
	{
		selectBox=false;
		modeBlurb = modeList[0];
			
		//get the current scale
		var currentScale = GetMapScale();	

		if (currentScale > 750000)
		{
			//set scale to 750,000 if current scale is greater
			parent.ToolFrame.document.forms[0].txtScale.value = "750000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale > 500000)
		{
			//set scale to 500,000
			parent.ToolFrame.document.forms[0].txtScale.value = "500000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale > 350000)
		{
			//set scale to 350,000
			parent.ToolFrame.document.forms[0].txtScale.value = "350000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale > 200000)
		{
			//set scale to 200,000
			parent.ToolFrame.document.forms[0].txtScale.value = "200000"
			RecalculateExtent();
			sendMapXML();
			return
		}
		
		if (currentScale > 100000)
		{
			//set scale to 100,000
			parent.ToolFrame.document.forms[0].txtScale.value = "100000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale > 50000)
		{
			//set scale to 50,000
			parent.ToolFrame.document.forms[0].txtScale.value = "50000"
			RecalculateExtent();
			sendMapXML();
			return
		}
		
		if (currentScale > 25000)
		{
			//set scale to 25,000
			parent.ToolFrame.document.forms[0].txtScale.value = "25000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale > 10000)
		{
			//set scale to 10,000
			parent.ToolFrame.document.forms[0].txtScale.value = "10000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale > 5000)
		{
			//set scale to 5,000
			parent.ToolFrame.document.forms[0].txtScale.value = "5000"
			RecalculateExtent();
			sendMapXML();
			return
		}
		
		if (currentScale > 2500)
		{
			//set scale to 2,500
			parent.ToolFrame.document.forms[0].txtScale.value = "2500"
			RecalculateExtent();
			sendMapXML();
			return
		}
		
		if (currentScale <= 2500)
		{
			alert("It is not possible to zoom in closer than 1: 2,500 scale.")
			return
		}
	}
	else
	{
		//zoom out
		selectBox=false;
		modeBlurb = modeList[1];

		//get the current scale
		var currentScale = GetMapScale();

		if (currentScale >= 1000000)
		{		
			alert("It is not possible to zoom out further than 1: 1,000,000 scale.")
			return
		}

		if (currentScale >= 750000)
		{
			//set scale to 1000000
			parent.ToolFrame.document.forms[0].txtScale.value = "1000000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale >= 500000)
		{
			//set scale to 350,000
			parent.ToolFrame.document.forms[0].txtScale.value = "750000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale >= 350000)
		{
			//set scale to 350,000
			parent.ToolFrame.document.forms[0].txtScale.value = "500000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale >= 200000)
		{
			//set scale to 350,000
			parent.ToolFrame.document.forms[0].txtScale.value = "350000"
			RecalculateExtent();
			sendMapXML();
			return
		}
		
		if (currentScale >= 100000)
		{
			//set scale to 200,000
			parent.ToolFrame.document.forms[0].txtScale.value = "200000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale >= 50000)
		{
			//set scale to 100,000
			parent.ToolFrame.document.forms[0].txtScale.value = "100000"
			RecalculateExtent();
			sendMapXML();
			return
		}
		
		if (currentScale >= 25000)
		{
			//set scale to 50,000
			parent.ToolFrame.document.forms[0].txtScale.value = "50000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale >= 10000)
		{
			//set scale to 25,000
			parent.ToolFrame.document.forms[0].txtScale.value = "25000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale >= 5000)
		{
			//set scale to 10,000
			parent.ToolFrame.document.forms[0].txtScale.value = "10000"
			RecalculateExtent();
			sendMapXML();
			return
		}
		
		if (currentScale >= 2500)
		{
			//set scale to 5,000
			parent.ToolFrame.document.forms[0].txtScale.value = "5000"
			RecalculateExtent();
			sendMapXML();
			return
		}

		if (currentScale < 2500)
		{
			//set scale to 5,000
			parent.ToolFrame.document.forms[0].txtScale.value = "2500"
			RecalculateExtent();
			sendMapXML();
			return
		}
	}
}

//-------------------------------------------------------------------------------------------------------------------
function RecalculateExtent()
{	
	var oldScale = GetMapScale();
	var newScale = parseInt(parent.ToolFrame.document.forms[0].txtScale.value);

	if((oldScale == 0) || (isNaN(oldScale) || isNaN(newScale)) || (newScale < 2500) || (newScale > 1000050))
	{
		//If we have just zoomed here as a result of a NOTES query, we do not want to display this message
		if (parent.MapFrame.justLinked == true)
		{
			parent.MapFrame.justLinked = false;
			return
		}
		alert("This is not a valid scale.\n\nValid scales range between 1:2,500 and 1:1,00,0000.\n\nPlease try again.");
		return;
	}

	var factorOldToNew = newScale / oldScale;

	var oldWidth = eRight - eLeft;
	var oldHeight = eTop - eBottom;
	var newWidth = oldWidth * factorOldToNew;
	var newHeight = oldHeight * factorOldToNew;

	var horIncrement = 0.5 * (newWidth - oldWidth);
	var verIncrement = 0.5 * (newHeight - oldHeight);

	eLeft -= horIncrement;
	eRight += horIncrement;
	eBottom -= verIncrement;
	eTop += verIncrement;		

}
//-----------------------------------------------------------------------------------------------------------------
function GetMapScale()
{
	/*
		specific function comments :-

	*/

	var tempScale = (eRight - eLeft) / iWidth;

	var tempFactor = 96;
	var inchPerMeter = 39.4;
	return parseInt((tempScale * tempFactor * inchPerMeter) + 0.5);
}

//-----------------------------------------------------------------------------------------------------------------

function CopyrightScale()
{
	/*
		Checks scale and rights out relevant copyright information
	*/
	var scale = GetMapScale();

	if (scale <= 40000)
	{
		CopyrightText = "© Crown Copyright and database right 2012. Ordnance Survey LA 100049055"
	}

	if ((scale >= 40001) && (scale < 80000))
	{
		CopyrightText = "2012 Philips Ltd."
	}


	if ((scale >= 80001) && (scale < 150000))
	{
		CopyrightText = "© Crown Copyright and database right 2012. Ordnance Survey LA 100049055"
	}

	if (scale > 150000) 
	{
		CopyrightText = "2012 Philips Ltd."
	}
}

//-----------------------------------------------------------------------------------------------------------------

function reloadApp() 
{
	var reloadTimer=0;
	var cmdString = parent.document.location.search;
	
	var theStr = "http://" + hostName + "website/k2p/viewer.htm" + cmdString
	//var theStr = "http://195.216.4.104/website/tourism/viewer.htm" + cmdString

	parent.document.location.href = theStr;
}

function printMap()
{
	var theTitle = "Keys to the Past."
	  
	var mWidth = parent.MapFrame.getMapWidth();
	var mHeight = parent.MapFrame.getMapHeight();

	  
	parent.MapFrame.getPrintMap(theTitle,mWidth,mHeight,1.0);
}

function processDCCLayer(theRef)
{
	
	if (theRef=="K2P")
	{
		var i = 1 // getLayerIndexPosn(theRef)
		return i
	}
	else
	{
		alert("Not found " + theRef)
		return 0
	}
}

//-----------------------------------------------------------------------------
function splitScreen()
{
	parent.ToolFrame.document.forms[0].txtScale.value = GetMapScale();
	var CurrentScale = parseInt(parent.ToolFrame.document.forms[0].txtScale.value);

	if((CurrentScale > 10000)) 
	{
		alert("This function is only available at 1:10,000 scale and below")
		return
	}
	else
	{
		//show split screen
		//open new window, passing url and box coords		
		var pointCoords = startRight + "," + startBottom		
		var theURL = "http://" + hostName + "/website/k2pSplitScreen" + "?STARTTOP=" + parent.MapFrame.eTop + "&STARTBOTTOM=" + parent.MapFrame.eBottom + "&STARTLEFT=" + parent.MapFrame.eLeft + "&STARTRIGHT=" + parent.MapFrame.eRight + "&DRAWPOINT=" + pointCoords + "&POINTLABEL=" + escape(pointLabelText)
		
		//alert(theURL)

		//get screen resolution
		var screenWidth = screen.width
		var screenHeight = screen.height

		//check resolution of user machine is OK for GIS
		if (screenWidth < 640 || screenHeight < 480) 
		{
			alert("To use the Interactive Mapping Service, your screen resolution should be set to 640 x 480 or greater")
		} 
		else 
		{
			//set sizes for the GIS window, depending upon users screen resolution
			var windowWidth=screenWidth * 0.8
			var windowHeight=screenHeight * 0.88
			
			//open the GIS in a window
			splitWin=open(theURL, "splitwindow", "width=" + windowWidth + ",height="+windowHeight+",top=0,left=0,resizable=no,status=no,toolbar=no,menubar=no")
		}
	}
}
//-----------------------------------------------------------------------------
function changeMapping(type)
{
	if ((type=="") || (type==null))
	{
		alert("Error getting map date. Date is: " + type)
		return
	}
	else
	{
		//Which layer are we going to display
		var theLayerToDisplay = 99
		
		if (type=="modern")
		{
			theLayerToDisplay = getLayerIndexPosn("10000");
		}
		else if (type=="Epoch1")
		{
			theLayerToDisplay = getLayerIndexPosn("Epoch1");
		}
		else if (type=="Epoch2")
		{
			theLayerToDisplay = getLayerIndexPosn("Epoch2");
		}
		else if (type=="Epoch3")
		{
			theLayerToDisplay = getLayerIndexPosn("Epoch3");
		}
		else if (type=="Epoch4")
		{
			theLayerToDisplay = getLayerIndexPosn("Epoch4");
		}
		else if (type=="Epoch5")
		{
			theLayerToDisplay = getLayerIndexPosn("Epoch5");
		}
		else if (type=="Epoch6")
		{
			theLayerToDisplay = getLayerIndexPosn("Epoch6");
		}
		else if (type=="Epoch7")
		{
			theLayerToDisplay = getLayerIndexPosn("Epoch7");
		}
		else if (type=="Epoch8")
		{
			theLayerToDisplay = getLayerIndexPosn("Epoch8");
		}
		else
		{
			alert("Error getting map date");
			return;
		}

		//Make the changes ...
		if (theLayerToDisplay != 99)
		{
			//Make layers invisible
			LayerVisible[getLayerIndexPosn("10000")]=0;
			LayerVisible[getLayerIndexPosn("Epoch1")]=0;
			LayerVisible[getLayerIndexPosn("Epoch2")]=0;
			LayerVisible[getLayerIndexPosn("Epoch3")]=0;
			LayerVisible[getLayerIndexPosn("Epoch4")]=0;
			LayerVisible[getLayerIndexPosn("Epoch5")]=0;
			LayerVisible[getLayerIndexPosn("Epoch6")]=0;
			LayerVisible[getLayerIndexPosn("Epoch7")]=0;
			LayerVisible[getLayerIndexPosn("Epoch8")]=0;

			//Make layer visible
			LayerVisible[theLayerToDisplay]=1;

			//Scale
			parent.ToolFrame.document.forms[0].txtScale.value = GetMapScale();
			var CurrentScale = parseInt(parent.ToolFrame.document.forms[0].txtScale.value);

			//If scale too big
			if(CurrentScale > 10000)
			{
				alert("Please zoom in to 1:10,000 scale or below to see the changes.")
			}
			else
			{
				//Refresh the map
				sendMapXML();
			}
		}
		else
		{
			alert("Error, unable to make the new layer visible");
			return;
		}
	}
}
//-----------------------------------------------------------------------------
function linker(theString)
{
	parent.opener.location.href=theString;
	parent.opener.focus();
}


