/*********************************************************************************************************
	 * Supporting functions to help with shortcuts
	 * Parameters: ID of each object from the DOM
	 *********************************************************************************************************/
	
	function Trim(str)
	{  
		while(str.charAt(0) == (" ") ) {  
			str = str.substring(1);
		}
		while(str.charAt(str.length-1) == " " ) {
			str = str.substring(0,str.length-1);
		}
		return str;
	}
	
	function isNumeric(intString) {
		// It's a bit daft that JavaScript does not have a function that checks whether a specified
		// input contains valid numbers.  Had a problem using pattern matching.
		// The input needs to be trimmed first
		
		var strValidChars = "0123456789";
		var strChar;
		var numFlag = true;
		
		if (intString.length == 0) return false;
		for (i = 0; i < intString.length && numFlag == true; i++) {
			strChar = intString.charAt(i);
			if (strValidChars.indexOf(strChar) == -1) { numFlag = false;	}
		}
		return numFlag;
	}
	
	function currDisplay(spanId) {
		return getId(spanId).style.display;
	}
	
	function showItem(spanId) {
		getId(spanId).style.display = 'block';
	}
	
	function hideItem(spanId) {
		getId(spanId).style.display = 'none';
	}
	
	function toggleItem(spanId) {
		if (currDisplay(spanId) != "none") {
			hideItem(spanId);
		}
		else {
			showItem(spanId);
		}
	}
	
	/**Generic function to return handle to a DOM object
	 * Parameters: Object ID as a String
	 */
	function getId(id){
		return document.getElementById(id);
	}
	
	/**Used when calling a function from the eGGP object
	 *This is to prevent form objects or URLS from firing off and creating a new page
	 */
	function returnFalse() {
		return false;
	}
	
	/**Prevent other functions from executing
	 * Used when closing a HUD to stop the toggling of its status
	 */
	function cancelBubble(e) {
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	}
	
	function updateEN() {
		if (objeGGP.script()!="") {
			// Get the eastings and northings and show it in metres.
			xCoord = objeGGP.pointerX() / 1000;
			yCoord = objeGGP.pointerY() / 1000;
			getId('coords').innerHTML = "<b>Easting: </b>" + xCoord + "<br/> <b>Northing:</b> " + yCoord;
		}
	}
	
	function mapCentre() {
		if (objeGGP.script()!="") {
			cpx = objeGGP.centreX() / 1000;
			cpy = objeGGP.centreY() / 1000;
			getId('coords').innerHTML = "<b>Map Centre (metres):</b><br/>" + cpx + "E , " + cpy + "N";
		}
	}
	
	/* Function to close the Get Info results HUD
	 */
	function closePanel(e){
		getId("sw-getInfoHUD").style.display="none";
		getId("sw-getInfoHUD-body").style.display="none";
		cancelBubble(e);
	}
	
	/* Function to close the Layer Search results HUD
	 */
	function closeLyrSearchPanel(e){
		getId("sw-lyrFormResults").style.display="none";
		getId("sw-lyrFormResults-body").style.display="none";
		cancelBubble(e);
	}
	
	/* Function to close the Address Search results HUD
	 */
	function closeGazSearchPanel(e){
		getId("sw-gzFormResults").style.display="none";
		getId("sw-gzFormResults-body").style.display="none";
		cancelBubble(e);
	}
	/* Function to close the Add Point form
	 */
	function closeAddPointForm(formDiv) {
		getId('successAddPoint').style.display = 'none';
		getId(formDiv).style.display = 'none';
	}
	
	/* Function to close the Layer search form
	 */
	function closeLayerSearchForm(formDiv) {
		hideItem(formDiv);
		//getId(formDiv).style.display = 'none';
	}
	
	/* Function to close the find nearest results HUD
	 */
	function closeNrSearchPanel(e){
		getId("sw-nrFormResults").style.display="none";
		getId("sw-nrFormResults-body").style.display="none";
		cancelBubble(e);
	}
	
  /*********************************************************************************************************
	 * Function to toggle between the state of panels (expanded/collapsed)
	 * Parameters: Root object ID as a String
	 *********************************************************************************************************/
	function toggleSwatch(objSwatchHead){
		try {
			var idPinSym = objSwatchHead.id + "-pin";
			var objPinSym = getId(idPinSym);
			if ((objPinSym.alt != "Unlock panel")||(objPinSym.alt != "Unlock panel")) {	
				toggleHUD(objSwatchHead);
			}
			closeOthers(objSwatchHead);
		}
		catch(e) {
			toggleHUD(objSwatchHead);
		}
	}
	
	function closeOthers(objKeep) {
		var swatchArea = getId('leftBlock').getElementsByTagName('div');
		for (var i = 0; i < swatchArea.length; i++) {
			if (swatchArea[i].className == "swatchHead") {
				var currId = swatchArea[i].id;
				if (currId != objKeep.id) {
					var pinId = getId(currId + "-pin");
					if ((pinId.alt != "Unlock panel")||(pinId.alt != "Unlock panel")) {	
						if (currDisplay(currId + "-body") != "none") {
							toggleHUD(swatchArea[i]);
						}
					}
				}
			}
		}
	}
	 	
  /*********************************************************************************************************
	 * Function to toggle between the state of panels (expanded/collapsed)
	 * Parameters: Root object ID as a String
	 *********************************************************************************************************/
	function toggleHUD(objSwatchHead){
		var idPanelBody =objSwatchHead.id + "-body";
		var objPanelBody =getId(idPanelBody);		
		objPanelBody.style.display=(objPanelBody.style.display=="none")?"block":"none";
		
		var idMinMaxSym = objSwatchHead.id + "-mm";
		var objMinMaxSym = getId(idMinMaxSym);
		objMinMaxSym .setAttribute("alt",(objPanelBody.style.display != "block") ? "Expand" : "Collapse");
		objMinMaxSym .setAttribute("title",(objPanelBody.style.display != "block") ? "Expand" : "Collapse");
		objMinMaxSym .setAttribute("src",(objPanelBody.style.display != "block") ? "assets/new buttons/expand.gif" : "assets/new buttons/collapse.gif");
	}
	
	/*********************************************************************************************************
	 * Function to handle errors in the backend and surface to the interface
	 * Parameters: e as event
	 *********************************************************************************************************/
	function errorHandler(e) {
		alert(e.message);
	}
	
	function convertToMetres(from, selUnit) {
		//What unit of measure did they choose
		var newMetre = 0;
		
		switch (selUnit){
			case "metres": 
				newMetre = from;
				break;
			case "kmetres":
				newMetre = from * 1000;
				break;
			case "feet":
				newMetre = from / 0.3048;
				break;
			case "miles":
				newMetre = from * 1609.3;
				break;
			case "yards":
				newMetre = from * 0.9144;
				break;
			default:
				alert("please select a unit of measure!");
		}
		return parseInt(newMetre);
	}
	
	
    /*********************************************************************************************************
	 * Function to fix the open state of panels (expanded/collapsed)
	 * Parameters: Root object ID as a String
	 *********************************************************************************************************/
	function togglePin(objSwatchHead){
		var idPinSym = objSwatchHead.id + "-pin";
		var objPinSym = getId(idPinSym);
		objPinSym.setAttribute("alt",(objPinSym.alt != "Lock panel") ? "Lock panel" : "Unlock panel");
		objPinSym.setAttribute("title",(objPinSym.title != "Lock panel") ? "Lock panel" : "Unlock panel");
		objPinSym.setAttribute("src",(objPinSym.title != "Lock panel") ? "assets/new buttons/pinned.gif" : "assets/new buttons/unpinned.gif");
		cancelBubble();
	}
