function textCounter( field, countfield, maxlimit ) {
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    alert( 'Textarea value can only be 255 characters in length.' );
    return false;
  }
  else
  {
    countfield.value = maxlimit - field.value.length;
  }
}

function validate() {
 var varName = /"|>|</;
 var carriage = /\n/;
 if(varName.exec(document.forms[0].guestmap_name.value))
 {
   window.alert("Invalid input, you must have entered double quotes or html into the form.");
 return false;
 }
 if(varName.exec(document.forms[0].guestmap_phone.value))
 {
   window.alert("Invalid input, you must have entered double quotes or html into the form.");
 return false;
 }
 if(varName.exec(document.forms[0].guestmap_comment.value))
 {
   window.alert("Invalid input, you must have entered double quotes or html into the form.");
 return false;
 }
 if(varName.exec(document.forms [0].guestmap_website.value))
 {
   window.alert("Invalid input, you must have entered double quotes or html into the form.");
 return false;
 }
 if(carriage.exec(document.forms [0].guestmap_comment.value))
 {
   window.alert("You must have hit the 'enter' key in the description text box, creating a line break. Currently this will break the map, but I'm working on it.");
 return false;
 }
 else
 {
   return true;
 }

}

function createMarker(point, html)
	{
	var marker = new GMarker(point, icon);
	var html = '<div align="left" style="white-space:nowrap;"><font color="#000000">' + html + '</font></div>';
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
	return marker;
	};

function reorient() {
	map.setCenter(new GLatLng(slat,slng),szoom);
	}

function find(lat, lng, zoom){
	map.setCenter(new GLatLng(lat,lng),zoom);
}

//Esa's country selector. Parse long, lat, zoom from selector values:
function go() {
	lng = parseFloat(document.getElementById("maahan").value.split(",")[0]);
	lat = parseFloat(document.getElementById("maahan").value.split(",")[1]);
	scale = parseInt(document.getElementById("maahan").value.split(",")[2]);
	map.setCenter(new GLatLng(lat,lng),scale);
	}

// geocoding
function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
            }
          }
        );
      }
    }

// function that load markers from db in ajax way
function getMarkers(){
	var urlstr="modules.php?name=Mappe&file=read";
			var request = GXmlHttp.create();
			request.open('GET', urlstr , true);	// request XML from PHP with AJAX call
			request.onreadystatechange = function () {
				if (request.readyState == 4) {
					var xmlDoc = request.responseXML;
					locations = xmlDoc.documentElement.getElementsByTagName("location");
					markers = [];
					if (locations.length){
						for (var i = 0; i < locations.length; i++) { // cycle thru locations
							markers[i] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lon")), icon);
							markers[i].infowindow = "<div><table width=\'200\'><tr><td style=\"color:black;\">"+locations[i].getAttribute("desc")+"</td></tr></table></div>";
							markers[i].markerindex = i;
							markers[i].db_id = locations[i].getAttribute("id");
							map.addOverlay(markers[i]);

						}
					}
				}
			}
			request.send(null);
	    }


