var map;
var gdir;
var geocoder = null;
var addressMarker;

var height = 0;
var frm = '';
var val_directions = '';

function setDirections(fromAddress, toAddress, locale) {
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}

function handleErrors() {
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("En af adresserne kunne ikke findes");
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

	else alert("OBS! Fejl i adresse-opslag");
}


function afrund(tal) {
	if(isNaN(tal)) return "0,00";

	tal = ""+(Math.round(tal*100))/100;
	punktum = tal.indexOf(".");

	if (punktum==-1) return tal+",00";

	if (tal.length-punktum==2) {
		return tal.replace('.',',') + "0";
	}

	if (tal.length-punktum==3) {
		return tal.replace('.',',');
	}

	else return tal + "0";
}

function DHMS(secTotal) {
	var signum = (secTotal >= 0) ? +1 : -1;
	secTotal *= signum;

	var sec = signum*secTotal % 60;

	secTotal /= 60;
	secTotal = Math.floor(secTotal);
	var min = signum*secTotal % 60;

	secTotal /= 60;
	secTotal = Math.floor(secTotal);
	var hour = signum*secTotal % 24;

	secTotal /= 24;
	secTotal = Math.floor(secTotal);
	var day = signum*secTotal;

	var returnFormat = '';

	if (day > 0) returnFormat += day + " dage ";
	if (hour > 0) returnFormat += hour + " timer ";
	if (sec > 30) min += 1;
	if (min > 0) returnFormat += min + " minutter";

	return returnFormat;
	//return  + hour + " timer " + min + " minutter " + sec + " sekunder";
}


function onGDirectionsLoad() {

	var seconds = gdir.getDuration().seconds;

	document.getElementById("distance").innerHTML = 'ca. ' + Math.round(gdir.getDistance().meters / 1000) + " km.";

	if (seconds > 0) {
		document.getElementById("duration").innerHTML = DHMS(seconds) + "";
	}

}


function reset_search() {
	frm.sAddress.value = '';
	frm.sZip.value = '';
	frm.vAddress.value = '';
	frm.vZip.value = '';
	frm.submit();
}



function toggleBetween(diva, divb, div_height, e) {

	A= document.getElementById(diva);
	B= document.getElementById(divb);
	C= document.getElementById('org_directions');

	D= document.getElementById('distance');
	E= document.getElementById('duration');

	if(A.style.display == "none") {
		A.style.display = "block";
		B.style.display = "none";
		D.style.display = "block";
		E.style.display = "block";
		e.value = "Rute";
		B.innerHTML = '';
	} else {
		A.style.display = "none";
		B.style.display = "block";
		D.style.display = "none";
		E.style.display = "none";
		e.value = "Kort";
		B.innerHTML = C.innerHTML;
	}      
}
