/* The author of this code is Geir K. Engdahl, and can be reached
 * at geir.engdahl (at) gmail.com
 * 
 * If you intend to use the code or derive code from it, please
 * consult with the author.
 */

// Converts the specified latitude or longitude to its corresponding value
// in the WGS84 format (100000ths of a degree).
function getWGS84Degrees(latOrLng) {
  return Math.round(latOrLng * 100000);
}

// Creates a TomTom-compatible itinerary file from the GDirections object.
// dir should be a valid GDirections object, filled with at least one route.
function createTomTomItineraryItn(gdir, addr) {
  if (gdir.getNumRoutes() < 1) {
    // This is likely a programmatical error. Alert the user.
    alert("createTomTomItineraryXml(): The driving directions are invalid!");
    return null;
  }
  var startLatLng = gdir.getRoute(0).getStep(0).getLatLng();
  var label = "Start";
  if (addr[0] != null)
    label = addr[0];
  var itnStr = getWGS84Degrees(startLatLng.lng()) + "|"
    + getWGS84Degrees(startLatLng.lat()) + "|" + label + "|4|\n";
  for (var i = 0; i < gdir.getNumRoutes(); i++) {
    var latLng = gdir.getRoute(i).getEndLatLng();
    label = "Destination " + (i+1);
    if (addr[(i+1) % addr.length] != null)
      label = addr[(i+1) % addr.length];
    itnStr += getWGS84Degrees(latLng.lng()) + "|"
      + getWGS84Degrees(latLng.lat()) + "|" + label + "|2|\n";
  }
  return itnStr;
}
