var map;

function addImage(src) {
	var newImg = document.createElement("img");
	newImg.style.margin = "5px";
	newImg.setAttribute("class", "PopBoxImageSmall");
	newImg.setAttribute("onclick", "Pop(this,50,'PopBoxImageLarge')");
	newImg.setAttribute("pbsrc", src.replace(/\/thumbnail/,'/view'));
	newImg.onclick = function() {Pop(this,50,'PopBoxImageLarge')};
	newImg.pbsrc = src.replace(/\/thumbnail/,'/view');
	newImg.className = "PopBoxImageSmall";

	document.getElementById("thumbnails").appendChild(newImg);
	newImg.src = src;
}

function load() {
	for (i = 0; i < 10; i++) {
		var field = document.getElementById("foto" + i);
		if (field.value != "") {
			addImage("/image/thumbnail?id=" + field.value);
		}
	}
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setMapType(G_HYBRID_MAP);
    map.addControl(new GSmallMapControl());
    map.addControl(new GScaleControl()); 
    var lat = document.getElementById("lat");
    var lng = document.getElementById("lng");
    var location = new GLatLng(parseFloat(lat.value.substr(0, 8)), parseFloat(lng.value.substr(0, 8)));
    map.setCenter(location, 16);
    pc_marker = new GMarker(location);
   	if (document.getElementById("source").value=='Mobile') {
   	   	map.addOverlay(pc_marker);
   	} else if (document.getElementById("source").value=='Glie') {
   		restore();
	}
  }
  
}

function restore() {
	  		var lpoints = new Array();

			//draw the polygone
			var va = new Array();
			for (i = 0; i < 9; i++) { //maximum 9 vertexes
				var vertex = document.getElementById('vertex' + i).value;
				if (vertex != "") {
					var lat = parseFloat(vertex.split(";")[0]);
					var lng = parseFloat(vertex.split(";")[1]);
					va.push(new GLatLng(lat, lng));
				}
			}
			if (va.length > 0) {
				polyline = new GPolygon(va, "#ffff00", 3, 0.5, '#00ff00', 0.4);
				map.addOverlay(polyline);
			}

}

function goPrevFrom(page) {
	var crrPage = document.getElementById('currentFormStage');
	crrPage.value = page;
	var direction = document.getElementById('direction');
	direction.value = "prev";
	var form = document.forms[0];
	form.submit();
}
