function map_location (count) {
  var address = "";

  if (document.getElementById("edit-locations-"+count+"-country") &&
    document.getElementById("edit-locations-"+count+"-country").type != 'hidden')
  {
    var country_object = document.getElementById("edit-locations-"+count+"-country");
    switch (country_object.options[country_object.selectedIndex].value)
    {
    case 'uk':
      address = address_to_string_uk(count);
      break
    case 'at':
      address = address_to_string_country_override(count, 'Austria');
      break
    case 'li':
      address = address_to_string_country_override(count, 'Liechtenstein');
      break
    default:
      address = address_to_string_default(count);
    }
  }
  else {
    address = address_to_string_default(count);
  }
  
  place_marker_at_address (address, count);
}

function place_marker_at_address (address, count) {
  var locnr = count+1;
  var map = Drupal.gmap.getMap('loc'+locnr).map;
  //var map = new GMap2(document.getElementById("gmap-loc1-gmap"+count));
  var geocoder = new GClientGeocoder();

  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
//        map.addControl(new GSmallMapControl());
//        map.addControl(new GMapTypeControl());
        map.clearOverlays();
        zoom = map.getZoom();
        map.setCenter(point, zoom);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        var coords = marker.getPoint();
        document.getElementById('gmap-loc'+locnr+'-locpick_latitude0').value = coords.lat();
        document.getElementById('gmap-loc'+locnr+'-locpick_longitude0').value = coords.lng();
        /*marker.openInfoWindowHtml(address);*/
        GEvent.addListener(map, "click", function(click_marker, click_point) {
          if (click_marker) {
            document.getElementById("gmap-loc"+locnr+"-locpick_latitude0").value = '';
            document.getElementById("gmap-loc"+locnr+"-locpick_longitude0").value = '';
            map.removeOverlay(click_marker);
            marker = null;
          } else {
            if (marker != null) {
              map.removeOverlay(marker);
            }
//            marker = new GMarker(click_point);
//            map.addOverlay(marker);
//            var click_coords = marker.getPoint();
//            document.getElementById("gmap-loc"+locnr+"-locpick_latitude0").value = click_coords.lat();
//            document.getElementById("gmap-loc"+locnr+"-locpick_longitude0").value = click_coords.lng();
          }
        });
      }
    }
  );
}



function translate_province(province_token) {
  var province_name;
    var a = get_province_array();
    for (var i = 1; i < a.length; ++i)
      if (a[i][1] == province_token) {
        province_name = a[i][2];
      }
    if (!province_name) {
      province_name = province_token.slice(3,5);  
    }
  return province_name;
}


function address_to_string_default(count) {
  var address = '';

  if (document.getElementById("edit-locations-"+count+"-street") &&
    document.getElementById("edit-locations-"+count+"-street").type != 'hidden' &&
    document.getElementById("edit-locations-"+count+"-street").value != '') {
      address = address+document.getElementById("edit-locations-"+count+"-street").value;
  }

  if (document.getElementById("edit-locations-"+count+"-city") &&
    document.getElementById("edit-locations-"+count+"-city").type != 'hidden' &&
    document.getElementById("edit-locations-"+count+"-city").value != '') {
    if (address != '') {
      address = address+", ";
    }
    address = address+document.getElementById("edit-locations-"+count+"-city").value;
  }

  if (document.getElementById("edit-locations-"+count+"-province") &&
    document.getElementById("edit-locations-"+count+"-province").type != 'hidden') {
    var province_object = document.getElementById("edit-locations-"+count+"-province");
    if (province_object.options[province_object.selectedIndex].value != '') {
      if (address != '') {
        address = address+", ";
      }
      var province_old = province_object.options[province_object.selectedIndex].value;
      var province = translate_province(province_old);
      address = address+province;
    }
  }

  if (document.getElementById("edit-locations-"+count+"-postal-code") &&
    document.getElementById("edit-locations-"+count+"-postal-code").type != 'hidden' &&
    document.getElementById("edit-locations-"+count+"-postal-code").value != '') {
      if (address != '') {
        address = address+" ";
      }
      address = address+document.getElementById("edit-locations-"+count+"-postal-code").value;
  }

  if (document.getElementById("edit-locations-"+count+"-country") &&
    document.getElementById("edit-locations-"+count+"-country").type != 'hidden') {
      var country_object = document.getElementById("edit-locations-"+count+"-country");
      if (country_object.options[country_object.selectedIndex].value != '') {
        address = address+", "+country_object.options[country_object.selectedIndex].value;
      }
  }

  return address;
}

function address_to_string_country_override(count, country) {
  var address = '';

  if (document.getElementById("edit-locations-"+count+"-street") &&
    document.getElementById("edit-locations-"+count+"-street").type != 'hidden' &&
    document.getElementById("edit-locations-"+count+"-street").value != '') {
      address = address+document.getElementById("edit-locations-"+count+"-street").value;
  }

  if (document.getElementById("edit-locations-"+count+"-city") &&
    document.getElementById("edit-locations-"+count+"-city").type != 'hidden' &&
    document.getElementById("edit-locations-"+count+"-city").value != '') {
    if (address != '') {
      address = address+", ";
    }
    address = address+document.getElementById("edit-locations-"+count+"-city").value;
  }

  if (document.getElementById("edit-locations-"+count+"-province") &&
    document.getElementById("edit-locations-"+count+"-province").type != 'hidden') {
    var province_object = document.getElementById("edit-locations-"+count+"-province");
    if (province_object.options[province_object.selectedIndex].value != '') {
      if (address != '') {
        address = address+", ";
      }
      var province_old = province_object.options[province_object.selectedIndex].value;
      var province = translate_province(province_old);
      address = address+province;
    }
  }

  if (document.getElementById("edit-locations-"+count+"-postal-code") &&
    document.getElementById("edit-locations-"+count+"-postal-code").type != 'hidden' &&
    document.getElementById("edit-locations-"+count+"-postal-code").value != '') {
      if (address != '') {
        address = address+" ";
      }
      address = address+document.getElementById("edit-locations-"+count+"-postal-code").value;
  }

  address = address+", "+country;

  return address;
}

function address_to_string_uk(count) {
  var address = '';


  if (document.getElementById("edit-locations-"+count+"-street") &&
    document.getElementById("edit-locations-"+count+"-street").type != 'hidden' &&
    document.getElementById("edit-locations-"+count+"-street").value != '') {
      address = address+document.getElementById("edit-locations-"+count+"-street").value;
  }

  if (document.getElementById("edit-locations-"+count+"-city") &&
    document.getElementById("edit-locations-"+count+"-city").type != 'hidden' &&
    document.getElementById("edit-locations-"+count+"-city").value != '') {
    if (address != '') {
      address = address+", ";
    }
    address = address+document.getElementById("edit-locations-"+count+"-city").value;
  }

  if (document.getElementById("edit-locations-"+count+"-province") &&
    document.getElementById("edit-locations-"+count+"-province").type != 'hidden') {
    var province_object = document.getElementById("edit-locations-"+count+"-province");
    if (province_object.options[province_object.selectedIndex].value != '') {
      if (address != '') {
        address = address+", ";
      }
      var province_old = province_object.options[province_object.selectedIndex].value;
      var province = translate_province(province_old);
      address = address+province;
    }
  }

  if (document.getElementById("edit-locations-"+count+"-country") &&
    document.getElementById("edit-locations-"+count+"-country").type != 'hidden') {
      var country_object = document.getElementById("edit-locations-"+count+"-country");
      if (country_object.options[country_object.selectedIndex].value != '') {
        address = address+", "+country_object.options[country_object.selectedIndex].value;
      }
  }

  return address;
}

