// JavaScript Document
var map, point, gdir, marker, geocoder = null;

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nDid you insert address in format: Street, City, Country?");
	} 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_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("An error occurred. Did you insert both addresses?");
	}   
}

function setDirections() {
	var fromvalue = document.getElementById("fromAddress").value; 
	var tovalue = document.getElementById("toAddress").value;
	var locale = document.getElementById("locale").value;
	if(fromvalue != "" && tovalue != ""){
		gdir.load("from: " + fromvalue + " to: " + tovalue, { "locale": locale });
	}
}

function windowHeight() {
	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight) {
		return self.innerHeight;
	}
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	}
	// IE 5
	if (document.body) {
		return document.body.clientHeight;
	}
	// Just in case. 
	return 0;
}

function handleResize() {
	var height = windowHeight() - document.getElementById('toolbar').offsetHeight - 22;
	document.getElementById('map').style.height = height + 'px';
	document.getElementById('sidebar').style.height = height + 'px';
}

function onGDirectionsLoad() { }

function SwitchDirection() {
	var fromvalue = document.getElementById("fromAddress").value; 
	var tovalue = document.getElementById("toAddress").value;
	document.getElementById("fromAddress").value = tovalue;
	document.getElementById("toAddress").value = fromvalue;
	setDirections();
}

function submitenter(e) {
	var keycode;
	if (window.event) { 
		keycode = window.event.keyCode; 
	} else if (e) { 
		keycode = e.which; 
	} else { 
		return true; 
	} 
	if (keycode == 13) {
		setDirections();
		return false;
	} else { return true; }
}
