var map = null;
var geocoder = null;
var submitted = false;
var marker = null;
var clickEv;
var icon = new GIcon();
	icon.image = "images/mm_20_red.png";
	icon.shadow = "images/mm_20_shadow.png";
	icon.iconSize = new GSize(12, 20);
	icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	
	
    function loadmap() {
    	if (GBrowserIsCompatible()) {
        	map = new GMap2(document.getElementById("map"));
			map.enableDoubleClickZoom();
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			geocoder = new GClientGeocoder();
		
			//if we are not on tthe search page then allow the user to click and move the marker 
	   		if(!(/(.)*searchresults.php/.test(window.location)))
	     		clickEv = GEvent.addListener(map, "click", function(overlay, point) {
            		//if(!submitted){
						map.clearOverlays(); 
						markerOptions = { icon:icon, draggable: false};
						marker = new GMarker(point, markerOptions);
		            	map.addOverlay(marker); 
						$("#long").val(point.x);
						$("#lat").val(point.y);
						$("#address").val("");
					//}  
        		});
	   
	    	map.setCenter(new GLatLng(62.5, 15.7), 4);
      	}else 
	  		alert("Your Browser is not compatible, unfortunately you will not be able to make a report with our online tool. Please email us your data and we will insert it for you");
    }
	
	function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
			  map.clearOverlays(); 
              map.setCenter(point, 13);
              marker = new GMarker(point, icon);
              map.addOverlay(marker);
			  
			  //set the decimal values with lat long values
			  get("long").value = point.x;
			  get("lat").value = point.y;
			  
            }
          }
        );
      }
    }
	
	function convertDMS(dd, mm, ss){
		return parseInt(dd) + (mm/60) + (ss/3600);
	}
	
	function convertDecimal(value){
		var dd = parseInt(value);
		var tmp = 60*(value - dd);
		var mm = Math.abs(parseInt(tmp));
		var ss = 60*(Math.abs(tmp) - mm);
		
		return new Array(dd,mm,ss);
	}
	
	//called when the ddmmss submit button of the form is clicked
	//converts and sets the decimal values of the lat and long elements on the page and set the value of ddmmmss for lat and long 
	function showLatLong(type){
		var la = $('#lat').val();
		var lo = $('#long').val();
				
		var point = new GLatLng(la, lo);
		map.clearOverlays(); 
		map.setCenter(point, 13);
        marker = new GMarker(point, icon);
        map.addOverlay(marker);
	}

