/*********************************************************************************************************
	 * 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) {
		// 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);
	}
	
	
	/**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 = formatEN(objeGGP.pointerX());
			yCoord = formatEN(objeGGP.pointerY());
			getId('coords').innerHTML = xCoord + "E<br/>" + yCoord + "N" + "<br/>";
		}
	}
	
	function mapCentre() {
		if (objeGGP.script()!="") {
			cpx = formatEN(objeGGP.centreX());
			cpy = formatEN(objeGGP.centreY());
			getId('coords').innerHTML = cpx + "E<br/>" + cpy + "N" +"<br/>(centre)";
		}
	}
	
	function formatEN(coord, optDecimalPlaces){
        //coord value is expected to be in millimeters.
	    
	    var decimalPlacesRequired=0;
	    if(optDecimalPlaces){decimalPlacesRequired=optDecimalPlaces};
	    
	    var multiplier=Math.pow(10, decimalPlacesRequired);
	    coord =(Math.round(coord * multiplier)/multiplier).toString();
	    
	    //String tidy:
	    var decimals=coord.split('.')[1];
	    if(decimals){
	       while (decimals.length < decimalPlacesRequired){
	        decimals+='0';
	       }
	       coord =coord.split('.')[0]+'.'+decimals;
	    }
	    return coord;
	}
	
	function displayAllHuds(){
	    $('#allHudsHeader, #allHUDs').show();
	}
	
	function hideAllHuds(){
	    $('#allHudsHeader, #allHUDs').hide();
	}
	
	function displayHUD(HUDId){
	    displayAllHuds();
	    if($('#allHudsHeader')[0].freefloating===false){
	        $('#allHudsMax').click();
	    }
	    minimizeAllHUDSwatches();	    

	    getId("allHUDs").style.display="block";
	    getId("allHudsHeader").style.display="block";
	    
	    getId(HUDId).style.display="block";
	    getId("sw-"+HUDId+"-body").style.display="block";
	}
	
	var AllHUDids=new Array('getInfoHUD', 'lyrFormResults', 'gzFormResults', 'nrFormResults');
	function minimizeAllHUDs(){
	    $('#allHudsMin').click();
	}
	function minimizeAllHUDSwatches(){
	    for(var i=0; i<AllHUDids.length;i++){
	        getId("sw-"+AllHUDids[i]+"-body").style.display="none";
	    }
	}
	

	/*********************************************************************************************************
	 * 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);
	}

