function new_location_entry() {
	$('#location-name-input').text('');
	$('#location-name-input').show();
	$('#mapfile-input').show();
	init_Location_edit(-1);
	overlay_in();
}

function init_Location_edit(locationID) {

	$('table#details tr#datasheet td input').show();

	var newButton = '<div class="round-button-left"><a href="#" onclick="saveLocation(' + locationID + ')"><i class="fa fa-check" aria-hidden="true"></i></a></div>';
	$('.round-button-left').replaceWith(newButton);
}

function update_clickable_map() {
	var selected_map_file = $('#mapfile-input').val();
	$('#clickablemap').attr('src', 'map/' + selected_map_file);
}


function saveLocation(locationID) {
	var map_v = $('#clickablemap').attr('src').substring(4);
	var name_v = $('#location-name-input').val();

	if(name_v.length > 100) {
		alert('Name too long (max 100 characters).')
		return;
	}
	var data = new FormData();

	data.append('map', map_v);
	data.append('name', name_v)

	$.ajax({
	    url: rootURL + 'alterLocation/' + locationID,
	    type: 'POST',
      data: data,
      cache: false,
      contentType: false,
      processData: false,
			success: function(data) {
				alert("k.");
			},
			error: function() {
				alert("Couldn't update the part information. Please retry.");
			}
	  });

	end_edit();
	$('#edit-button').click(function() {
		init_edit(partID);
	});
}

function placeMarker(locationID){
	var $img = $('#clickablemap');
	var currentClickPosX = event.pageX - $img.offset().left;
	var currentClickPosY = event.pageY - $img.offset().top;

    var correctX = (($img.prop('naturalWidth') / $img.width()) * currentClickPosX).toFixed(0);
    var correctY = (($img.prop('naturalHeight') / $img.height()) * currentClickPosY).toFixed(0);

    // $("#mapURL").html("elab.png?x=" + correctX + "&y=" + correctY);
    $("#clickablemap").attr("src", "map/" + $("#mapfile-input").val() + "?x=" + correctX + "&y=" + correctY);
}