  var num = 1;
  var map;
  var markers = new Array();
  var points = new Array();
  var geocoder = new GClientGeocoder();

  function init() {
    var mapdiv = document.getElementById("map");
    map = new GMap2(mapdiv);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(40.433633, -86.901366), 7);
    if (typeof prefill=='undefined')
      addRow();
    else
      prefill();
  }

  function chooseFunction(obj) {
    var id = extractId(obj);
    map.closeInfoWindow();
    if (obj.value == "Show") {
      showAddress(obj);
    }
    else if (obj.value == "Go To") {
      map.panTo(points[extractId(obj)]);
      var div = document.getElementById("markerElement" + id);
      GEvent.trigger(markers[id], "click");
    }
    else if (obj.value == "Move To" || obj.value == "Loading..." || obj.value == "Error") {
      map.removeOverlay(markers[id]);
      showAddress(obj);
    }
    obj.blur();
  }

  function showAddress(obj) {
     var id = extractId(obj);
     var address = document.getElementById("addressElement" + id).value;
     var btn = document.getElementById("showBtnElement" + id);
     var lat = 0;
     var lon = 0;
     geocoder.getLatLng(
         address,
         function(point) {
           if (!point) {
             alert(address + " not found");
           } else {
             map.setCenter(point);
             showMarker(point, id);
           }
         }
       );
  }

  function showMarker(point, id) {
    var lbl = document.getElementById("labelElement" + id);
    var add = document.getElementById("addressElement" + id);
    var btn = document.getElementById("showBtnElement" + id);
    points[id] = point;
    markers[id] = createMarker(point, lbl.value, add.value, id);
    map.addOverlay(markers[id]);
    map.closeInfoWindow();
    btn.value = "Go To";
  }

  function createMarker(point, text, address, id) {
     var marker = new GMarker(point);
     var div = document.getElementById("markerElement").cloneNode(true);
     div.id = div.id + id;
     div.firstChild.innerHTML = text;
     div.lastChild.innerHTML = address;
     GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(div.innerHTML); });
     return marker;
  }

  function addRow() {
    var row = document.getElementById("blankRowElement");
    var newrow = row.cloneNode(true);
    appendId(newrow, num);
    newrow.id = newrow.id + num;
    num = num + 1;
    newrow.style.display = "";
    row.parentNode.appendChild(newrow);
    return newrow; 
  }

  function appendId(obj, n) {
    var child = obj.firstChild;
    while (child != null) {
      if (child.tagName == "INPUT")
        child.id = child.id + n;
      else if (child.tagName == "TD") {
          appendId(child, n);
      }
      child = child.nextSibling;
    }
  }

  function labelChange(obj) {
    var div = document.getElementById("markerElement" + extractId(obj));
    if (div != null)
      div.firstChild.innerHTML = obj.value;
  }

  function extractId(obj) {
    var id = obj.id.substr(obj.id.indexOf("Element")+7, obj.id.length);
    return id;
  }

  function removeAddress(obj) {
    var id = extractId(obj);
    map.removeOverlay(markers[id]);
    document.getElementById("blankRowElement" + id).style.display = "none";
  }

  function clearAddresses() {
    map.clearOverlays();
    var tbody = document.getElementById("addresses");
    var row = tbody.firstChild;
    while (row != null) {
      if (row.id != null && row.id != "blankRowElement") {
        row.style.display = "none";
      }
      row = row.nextSibling;
    }
    addRow();
  }

  function addressChange(obj) {
    var btn = document.getElementById("showBtnElement" + extractId(obj));
    if (btn != null && btn.value != "Show")
      btn.value = "Move To";
  }

  function generateLink() {
    var text = "http://www.mindflowsystems.com/maps/maps.html?";
    var point = map.getCenter();
    text = text + "&lat=" + point.y;
    text = text + "&long=" + point.x;
    text = text + "&zoom=" + map.getZoom();
    var tbody = document.getElementById("addresses");
    var row = tbody.firstChild;
    while (row != null) {
      if (row.id != null && row.id != "blankRowElement" && row.style.display != "none") {
        var id = extractId(row);
        if (points[id] != null) {
          text = text + "&lbl:list=" + document.getElementById("labelElement" + id).value;
          text = text + "&add:list=" + document.getElementById("addressElement" + id).value;
          text = text + "&x:list=" + points[id].x;
          text = text + "&y:list=" + points[id].y;
        }
      }
      row = row.nextSibling;
    }
    while (text.indexOf(" ") != -1) {
      text = text.replace(" ", "%20");
    }
    var permaLink = document.getElementById("permaLink");
    permaLink.value = text;
    permaLink.scrollIntoView();
    permaLink.focus();
    permaLink.select();
  }