/****************** Implementation by Felixone.it *******************/

var map = null;
var localSearch = new GlocalSearch();

function loadMap() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    if (mctrl) map.addControl(mapctrl);
    if (mtype) map.addControl(new GMapTypeControl());
    if (mscale) map.addControl(new GScaleControl());
    if (moview) map.addControl(new GOverviewMapControl());
    map.setCenter(mcenter, mzoom);
  }
}

function usePointFromPostcode(address, callbackFunction) {
  map.clearOverlays();
  localSearch.setSearchCompleteCallback(null, function () {
    if (localSearch.results[0]) {
      var resultLat = localSearch.results[0].lat;
      var resultLng = localSearch.results[0].lng;
      var point = new GLatLng(resultLat,resultLng);
      callbackFunction(point,address);
    } else {
      alert("\""+address+"\"" + " not found!"); // You may edit this message 
    }
  });
  if (mcountry != "") mcountry = ", " + mcountry;
  localSearch.execute(address + mcountry);
}

function placeMarkerAtPoint(point,address) {
  map.setCenter(point, 13);
  var marker = new GMarker(point, {title: "Click"});
  map.addOverlay(marker);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(address);
  });
}

function concAddress() {
  var address = document.getElementById('GMaddress').value;
  if (document.getElementById('GMtown')) {
    address += ", " + document.getElementById('GMtown').value;
    if (document.getElementById('GMzip')) address += ", " + document.getElementById('GMzip').value;
  }
  return address;
}

function GMcheckFields() {
  var msg = "";
  if (document.getElementById('GMaddress').value.replace(/ /g,"") == "") {
    msg += "The Address is required.\n"; // You may edit this message 
  } else if (document.getElementById('GMtown') && document.getElementById('GMaddress').value.indexOf(",") != -1) {
    msg += "Please remove the comma from the address field, write only the address and use only the space as separator.\n"; // You may edit this message 
  }
  if (document.getElementById('GMtown') && document.getElementById('GMtown').value.replace(/ /g,"") == "") {
    msg += "The Town is required.\n"; // You may edit this message 
  }
  if (msg != "") {
    alert(msg);
  } else usePointFromPostcode(concAddress(), placeMarkerAtPoint);
}
