var objMap = null;
var objGeocoder = null;
var objMarker = null;
var strLastAddress = '';
var blnSearchInProgress = false;

var objLocalSearch = new GlocalSearch();

function initialiseMap() {
	
  if (GBrowserIsCompatible()) {
    objMap = new GMap2(document.getElementById("google_map"));
    objMap.setCenter(new GLatLng(54.5,-3.475601), 13);
    objMap.disableDragging();
    objMap.setZoom(4);
    objMap.disableInfoWindow();
    
    
    updateMapStatusText("Enter your postcode", false);
    checkAndSubmitAddress();
  }
}

function updateMapStatusText(strText, blnOK) {
	var objSpan = document.getElementById('spnMapStatus');
	if (objSpan) {
		objSpan.innerHTML = "Status: " + strText;
	}
	
}

function checkAndSubmitAddress() {
	var blnDo = true;

	
	if (document.getElementById('strPostCode').value.length < 1) {
		blnDo = false;
	}
	
	if (blnDo) {
		var strAddress = makeAddress();
		
		if (strAddress != strLastAddress) {
			strLastAddress = strAddress;
			showAddress(strAddress);
		}
	}
}

function makeAddress() {
	var strAddress = '';
	/*if (!blnUser) {
		strAddress = concatValue('strAddress1', '');
		strAddress = concatValue('strAddress2', strAddress);
		strAddress = concatValue('strAddress3', strAddress);
		strAddress = concatValue('strTown', strAddress);
		strAddress = concatValue('strCounty', strAddress);
	}*/
	strAddress = concatValue('strPostCode', strAddress);
	
	return(strAddress);
}

function concatValue(strItemID, strCurrent) {
	var objValue = document.getElementById(strItemID);
	if (objValue) {
		if (objValue.value.length > 0) {
			if (strCurrent.length > 0) {
				strCurrent = strCurrent + ', ' + objValue.value;
			} else {
				strCurrent = objValue.value;
			}
		}
	}	
	return(strCurrent);
}

function showAddress(strAddress) {
	if (!blnSearchInProgress) {
		
		blnSearchInProgress = true;
		strAddress = strAddress + ", UK";
		/*objGeocoder = new GClientGeocoder();
		objGeocoder.setBaseCountryCode('uk');
		objGeocoder.getLatLng( strAddress, pointReturned);*/
		
		objLocalSearch.setSearchCompleteCallback(null, searchPointReturned);
		
		objLocalSearch.execute(strAddress);
		
		updateMapStatusText("Searching...", true);
	}
	
}

function searchPointReturned() {
	var objValidAddress = document.getElementById('blnValidAddress');
	if (objLocalSearch.results[0]) {
		var resultLat = objLocalSearch.results[0].lat;
        var resultLng = objLocalSearch.results[0].lng;
        var objPoint = new GLatLng(resultLat,resultLng);
        
        if (blnUser) {
        	var strTown = objLocalSearch.results[0].city;
        	if (strTown.length > 0) {
        		var objTown = document.getElementById('strTown');
        		if (objTown) {
        			objTown.value = strTown;
        		}
        	}
        }
        
        objMap.setCenter(objPoint, 13);
        
        if (objMarker) {
        	objMap.removeOverlay(objMarker);
        }
        
        objMarker = new GMarker(objPoint);
        
        objMap.addOverlay(objMarker);
        objMarker.openInfoWindowHtml('');
        
        document.getElementById('fltLong').value = objPoint.lng();
      	document.getElementById('fltLat').value = objPoint.lat();
      	
      	updateMapStatusText("Address found", true);
      	
      	if (objValidAddress) {
      		objValidAddress.value = 1;
      	}
        
	} else {
		updateMapStatusText("Address not found", false);
		if (objValidAddress) {
      		objValidAddress.value = 0;
      	}
	}
	blnSearchInProgress = false;
}
