	function addOptions(elm, obj) {

		for (var count = elm.options.length-1; count >= 0; count--)
			elm.options[count] = null;
	
		obj.each(function(optn) {
			//var optionItem = new Option(optn.name, optn.id, false, false);
			if (optn.name) {
				var item = new Element('option', {'html': optn.name});
				item.value = optn.id;
				item.inject(elm);
			}
		});	
	}

	function startDrawing(poly, name, onUpdate, color) {
		map.addOverlay(poly);
		poly.enableEditing({onEvent: "mouseover"});
		poly.disableEditing({onEvent: "mouseout"});
		GEvent.addListener(poly, "lineupdated", function(latlng, index) {
			updateLength(poly);
			createVertices(poly);
		});
		GEvent.addListener(poly, "endline", function() {
			if (formVal) {
				formVal.reset();
				formVal.ignoreField('trail_added');
			}
			GEvent.addListener(poly, "click", function(latlng, index) {
				if (typeof index == "number") {
					poly.deleteVertex(index);
				} 
				else
					createVertices(poly);
			});

		});
	}
	
	function createVertices(poly) {
		// Get all existing hidden vars and delete
		var vertices = $$('.vertex');
		vertices.each(function(vertex) { 
			vertex.destroy();
		});

		// Loop the points, encode into nasty hidden variable
		var vcount = poly.getVertexCount();
		for (var i =0; i < vcount; i++) {
			var vertex = poly.getVertex(i);
			var lon = vertex.lng();
			var lat = vertex.lat();
			
			// Now add the new
			var hidden = new Element('input', {'type': 'hidden', 'class': 'vertex', 'name': 'tra_vertices[]', 'id': 'vertex_'+i, 'value': lon+','+lat});
			hidden.inject('trail', 'inside');
		}
	}

	function updateLength(line) {
		var len = line.getLength();
		$('lngth').setProperty('html', (Math.round(len / 10) / 100) + "km");
		//$('tra_length').innerHTML = (Math.round(len / 10) / 100) + "km";
		$('length').value = len / 1000;
	}
