function addMarker(latlng) {
    markersDir.push(new google.maps.Marker({
      position:latlng, 
      map:map,
      icon:"http://maps.google.com/mapfiles/marker" + String.fromCharCode(markersDir.length + 65) + ".png"
    }));    
}

function calcRoute(){
    if(origin == null){Ext.Msg.alert('Info', 'Click on the map to add a start point');return;}
    if(destination == null){Ext.Msg.alert('Info', 'You have not click the direction point');return;}
    var mode;
    switch(Ext.getCmp('hidVal').getValue()){
      case "bicycling":
        mode = google.maps.DirectionsTravelMode.BICYCLING;
      break;
      case "driving":
        mode = google.maps.DirectionsTravelMode.DRIVING;
      break;
      case "walking":
        mode = google.maps.DirectionsTravelMode.WALKING;
      break;
      default:
      	mode = google.maps.DirectionsTravelMode.DRIVING;
      break;
    }
    
    var request = {
    	origin:origin, destination:destination, waypoints:waypoints, travelMode:mode, optimizeWaypoints:Ext.getCmp('optimize').getValue(),
        avoidHighways:Ext.getCmp('highWays').getValue(), avoidTolls:Ext.getCmp('tolls').getValue()
    };
    //var directionsService = directionsService;
    directionsService.route(request, function(response, status){
      if (status == google.maps.DirectionsStatus.OK){
        	directionsDisplay.setDirections(response);
      }
    });
    clearMarkers();
    directionsVisible = true;
}

function updateMode(){
    if (directionsVisible) {
      calcRoute();
    }
}
  
function clearMarkers(){
    for (var i = 0; i < markersDir.length; i++) {
      markersDir[i].setMap(null);
    }
}
  
function clearWaypoints(){
    markersDir = [];
    origin = null;
    destination = null;
    waypoints = [];
    directionsVisible = false;
}

function reset(){
	//var directionsDisplay = directionsDisplay;
	//var map = map;
  	clearMarkers();
  	clearWaypoints();
  	directionsDisplay.setMap(null);
  	directionsDisplay.setPanel(null);
  	directionsDisplay = new google.maps.DirectionsRenderer();
  	directionsDisplay.setMap(map);
  	directionsDisplay.setPanel(document.getElementById("directionsPanel"));
}
