/*********************
 DIRECTIONS FUNCTIONS
 ********************/

function getDirections() {
    // check they are not empty
    var from = $('#from_address').val();
    var to = $('#to_address').val();
    if (from == "") { alert("Please enter a starting address"); $('#from_address').focus(); return false; }
    if (to == "") { alert("Please enter a finishing address"); $('#to_address').focus(); return false; }
    directions_from = from;
    directions_to = to;
    
    // clear everything from the map
    map.clearOverlays();
    
    // geocode from and to so we can pick from a list if needed
    //getAddress('from_address');
    
    if (directions == null) {
        directions = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(directions, "error", handleErrors);
    }
    from += ", UK"; to += ", UK";
    showDirections(from, to);
    
    return false;
}

function showDirections(from, to) {
    //logDirections(from, to);
    toggleMapNav("directions");
    directions.load("from: "+from+" to: "+to);
}

function clearDirections() {
    toggleMapNav("options", true);
    directions.clear();
}

function handleErrors(){
	if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
		alert("No corresponding geographic location could be found for the specified address. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code);
	}
	else if (directions.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: " + directions.getStatus().code);
	}
	else if (directions.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: " + directions.getStatus().code);
	}
	else if (directions.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: " + directions.getStatus().code);
	}
	else if (directions.getStatus().code == G_GEO_BAD_REQUEST) {
		alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
	}
	else {
		alert("An unknown error occurred.");
	}
}