function routingFuncs(oKaMap){
	this.kaMap = oKaMap;
	this.kaMap.routingFuncs = this;
}

routingFuncs.prototype.initRouting = function(){
	// remove old addresses
	if(typeof(foundLocationsListArray) != "undefined")
		foundLocationsListArray.splice(0, foundLocationsListArray.length);
	
	if(typeof(foundLocations) != "undefined")
		foundLocations.splice(0, foundLocations.length);
		
	if(typeof(locDefinitionList) != "undefined")
		locDefinitionList.splice(0, locDefinitionList.length);
	
	var infoPOI = this.kaMap.getRawObject("r_goRouting").innerInfo;
	
	var dep = new Array();
	dep['adr'] = this.kaMap.getRawObject("r_streetName").value + " " + this.kaMap.getRawObject("r_streetNo").value;
	dep['npa'] = this.kaMap.getRawObject("r_NPA").value;
	dep['loc'] = this.kaMap.getRawObject("r_city").value;
	dep['region'] = "";
	dep['pays'] = this.kaMap.getRawObject("r_country").options[this.kaMap.getRawObject("r_country").selectedIndex].value;
	dep['coordX']='';
	dep['coordY']='';
	
	var arr = new Array();
	arr['adr'] = infoPOI.NOM_RUE + infoPOI.NO_MAISON;
	arr['npa'] = infoPOI.NPA;
	arr['loc'] = infoPOI.LOCALITE_18;
	arr['region'] = "";
	arr['pays'] = "CHE"; // Switzerland
	arr['coordX']= (typeof infoPOI.COORDX != "undefined")?infoPOI.COORDX:infoPOI.objectidx;
	arr['coordY']= (typeof infoPOI.COORDY != "undefined")?infoPOI.COORDY:infoPOI.objectidy;
	
	this.infoPOI = infoPOI;
	
	var res = this.getAddress(dep, "ambiguousLoc1");
	if(!res){
		alert("l'adresse " + dep['adr'] + ", " + dep['npa'] + " " + dep['loc'] + " est introuvable");
		return false;
	}
	
	res = this.getAddress(arr, "ambiguousLoc2");
	if(!res){
		alert("l'adresse " + dep['adr'] + ", " + dep['npa'] + " " + dep['loc'] + " est introuvable");
		return false;
	}
	
	if(this.kaMap.getRawObject("ambiguousLoc1").loc.length == 1 && this.kaMap.getRawObject("ambiguousLoc2").loc.length == 1){
		this.validateLocations();
	}
	
	return;
};

// retrun true if address was found
routingFuncs.prototype.getAddress = function(input, ambi){
	
	var inputAddress = new InputAddress(input['adr'], input['loc'], input['region'], input['npa'], input['pays']);
	inputAddress.coordX = input['coordX'];
	inputAddress.coordY = input['coordY'];
	searchAddresse(inputAddress, 0, ambi);
	
	if(foundLocationsListArray != null && foundLocationsListArray.length > 0){
		if(foundLocationsListArray[0].size == 0){
			alert("no result");
			return false;
		}
		
		if(foundLocationsListArray[0].size == 1){
			this.kaMap.getRawObject(ambi).loc = foundLocationsListArray[0].foundLocations;
			//var locDef = new LocDefinition(null, foundLocationsListArray[0].foundLocations[0].locDesc.locid, null);
			//locDefinitionList[locDefinitionList.length] = locDef;
			return true;
		}
		
		if(foundLocationsListArray[0].size > 1){
			this.kaMap.getRawObject(ambi).loc = foundLocationsListArray[0].foundLocations;
			this.displayChoicesForLocation();
			return true;
		}
	}
	else{
		return false;
	}
};

routingFuncs.prototype.displayChoicesForLocation = function(){
	this.kaMap.getRawObject("ambiguousLocationsDiv").style.display = "block";
	this.kaMap.getRawObject("r_table1").style.display = "none";
	this.kaMap.getRawObject("r_table2").style.display = "none";
};

routingFuncs.prototype.validateLocations = function(){

	addLocDefinition(this.kaMap.getRawObject("ambiguousLoc1").loc[this.kaMap.getRawObject("ambiguousLoc1").selectedIndex].locDesc.locid, 'StopoversTable');
	addLocDefinition(this.kaMap.getRawObject("ambiguousLoc2").loc[this.kaMap.getRawObject("ambiguousLoc2").selectedIndex].locDesc.locid, 'StopoversTable');

	this.kaMap.getRawObject("ambiguousLocationsDiv").style.display = "none";
	this.kaMap.getRawObject("r_table1").style.display = "block";
	this.kaMap.getRawObject("r_table2").style.display = "block";
	this.kaMap.getRawObject("routingDiv").style.display = "none";
	
	// sablier
	this.kaMap.getRawObject("sablier1").style.display = "block";
	
	this.getRoute();
	/*	
	}
	catch(e){
		alert('general_erreur_departOuDestIntrouvable');
	}
	*/
};

routingFuncs.prototype.getRoute = function() {
	
	var d = new Date();
	var jour = d.getDate();
	var mois = d.getMonth() + 1;
	var annee = d.getYear() + 1900;
	if(annee > 3000)
		annee = annee - 1900;
	var itiDate = jour + "/" + mois + "/" + annee;

	
	//=================================//
	
	/*
	<option value="0">Light vehicle</option>
	<option value="1">Truck</option>
	<option value="2">Pedestrian</option>
	<option value="3">Cycle</option>				
	*/	
	var vehicleType = 0;
	
	//=================================//
	
	/*	
	<option value="0">simple</option>
	<option value="1">fastest</option>
	<option value="2">shortest</option>
	<option value="3">economic</option>
	<option value="4">wrong value</option>
	*/
	var itiType = 0;
	
	//=================================//
	
	// preferences
	var favourMotorways = false;
	var avoidCrossingBorders = false;
	var avoidTolls = false;
	var avoidRoadTaxAreas = false;
	var avoidOffroadConnections = false;
	var avoidMountainPass = false;
	var itiPref = new ItineraryPreferences(favourMotorways, avoidCrossingBorders, avoidTolls, avoidRoadTaxAreas, avoidOffroadConnections, avoidMountainPass);
	
	//=================================//
	
	var fuelCost = new Cost("", "");
	var fuelConsumption = new Array();
	fuelConsumption[fuelConsumption.length] = "";
	fuelConsumption[fuelConsumption.length] = "";
	fuelConsumption[fuelConsumption.length] = "";
	
	var itiOptions = new ItineraryOptions(itiDate, vehicleType, itiType, itiPref, fuelCost, fuelConsumption);
	
	//=================================//
	
	/*
	<option value="1">Normal</option>
	<option value="0">Synthesis</option>
	*/
	var detailLevel = 1;
	
	//=================================//
	
	/*		
	<option value="eng">English</option>
	<option value="fra">French</option>
	<option value="deu">German</option>
	<option value="spa">Spanish</option>
	<option value="ita">Italian</option>
	<option value="nld">Dutch</option>
	<option value="por">Portuguese</option>
	<option value="dan">Danish</option>
	<option value="fin">Finnish</option>
	<option value="swe">Swedish</option>
	<option value="nor">Norwegian</option>
	*/
	
	switch(appLanguage){
		case "fr" : var language = "fra"; break;
		case "de" : var language = "deu"; break;
		case "it" : var language = "ita"; break;
		case "en" : var language = "eng"; break;
	}
		
	
	//=================================//
	
	/*
	<option value="0">HTML</option>
	<option value="1">Text</option>
	*/
	var instructionsFormat = 0;
	
	//=================================//
	
	/*
	<option value="1">Car</option>
	<option value="2">Moto</option>
	<option value="3">Caravan</option>
	<option value="4">Truck (2 axle)</option>
	<option value="5">Truck (3 axle)</option>
	<option value="6">Truck (4 axle)</option>
	<option value="7">Truck (5 axle)</option>
	*/
	var tollCategory = 1;
	
	var presentationOptions = new ExtendedPresentationOptions(detailLevel, language, instructionsFormat, tollCategory);
	
	//=================================//
	/*
	<option value="3">Itinerary roadmap & Itinerary trace</option>
	<option value="2">Itinerary roadmap</option>
	<option value="1">Itinerary trace</option>
	*/
	var responseElts = 3;
	
	//=================================//
	
	/*		
	<option value="0">No map definition</option>
	<option value="1">Main map definition only</option>
	<option value="2">Detail map definition only</option>
	<option value="3">All map definition only</option>
	*/
				
	var mapDefCalc = 1;
	
	//=================================//
	
	var mainMapWidth = 600;
	var mainMapHeight = 600;
	var blocMapWidth = 200;
	var blocMapHeight = 200;
	
	var responseOptions = new ResponseOptions(responseElts, mapDefCalc, mainMapWidth, mainMapHeight, blocMapWidth, blocMapHeight);
	
	
	var itiRequest = new ItineraryRequest(locDefinitionList, itiOptions, presentationOptions, responseOptions);
	
	
	if ((vehicleType == "2") || (vehicleType == "3")) {		
		routeCalculation_getRouteNonMotorized(itiRequest);
	}
	else {
		routeCalculation_getRoute(itiRequest);
	}
};

routingFuncs.prototype.routingCallback = function(calculatedRoute, itiRequest){
	locDefinitionList.splice(0, locDefinitionList.length);
	
	if((typeof(calculatedRoute) != "undefined") && (calculatedRoute != null)) {
		if(calculatedRoute.roadmap != null){
			this.itiHtmlCode = calculatedRoute.roadmap;
		}
		if(calculatedRoute.mapId != null){
			this.getItineraryMap(calculatedRoute.mapId, itiRequest.responseOptions.mainMapWidth, itiRequest.responseOptions.mainMapHeight, calculatedRoute.ititrace);
		}
	}
	
};


routingFuncs.prototype.getItineraryMap = function(mapId, mapWidth, mapHeight, oititraceList) {
	ititraceList = oititraceList;
	mapManagement_getMapByID(mapId, mapWidth, mapHeight, null, 0, true, null, null, oititraceList);
};


routingFuncs.prototype.getGeneratedMap = function(map){
	this.kaMap.getRawObject("sablier1").style.display = "none";
	var VMPopUp = new kaMapPopUp(this.kaMap, "VMPopUp", 15);
	
	var imgUrl = map.mapURL;
	var descrIti = this.itiHtmlCode.replace(/src=\"img\/iti/g, "src=\"/OPO/tools/MichelinRoutingAPI/img/iti");
	
    var winH = getInsideWindowHeight();
    var height = winH - 15 - 80;
	
	var widthTitle = 800;
	// add some pixels for scrollbar in IE (is added outside of the div)
	if(navigator.userAgent.toLowerCase().indexOf("msie 6.")!= -1){
		widthTitle = 816;
	}
	
	
	var content = "<div class='title' style='background:rgb(102,115,175); color:white;font-family:arial; position:relative;width:"+widthTitle+"px'><span style='margin-left:5px'>"+tra['routing_itineraire']+"</span><a href='javascript:myKaMap.getRawObject(\"mainContainer\").style.display=\"block\"; myKaMap.kaMapPopUp.destroyKaMapPopUp()'><img style='border:none; position:absolute; right:3px; top:2px; width:14px; height:14px;' src='/OPO/images/popUpSchliessen.gif'></a></div>";
 	content += "<div id='VMItinerary' style='height:"+height+"px;'>";
 	content += 		"<div id='mapContainer'>";
 	content += 			"<img id='dynCarte' src='"+imgUrl+"'>";
	if(typeof this.infoPOI.NOM != "undefined" || typeof this.infoPOI.LOCALITE_18 != "undefined"){
		content += 			"<div id='detailsPOI'>";
		content += 				"<ins>"+tra['routing_summary_destination']+"</ins><br>";
		if(typeof this.infoPOI.NOM != undefined)
			content += 			"<strong>"+this.infoPOI.NOM+"</strong><br>";
		if(typeof this.infoPOI.NOM_RUE != "undefined" && typeof this.infoPOI.NO_MAISON != "undefined")
			content += 			this.infoPOI.NOM_RUE+" "+this.infoPOI.NO_MAISON+"<br>";
		if(typeof this.infoPOI.NPA != "undefined" && typeof this.infoPOI.LOCALITE_18 != "undefined")
			content += 			this.infoPOI.NPA+ " " +this.infoPOI.LOCALITE_18;
		content += 			"</div>";
	}
	content += 			"<a id='r_print' href='javascript:myKaMap.routingFuncs.print()'>"+tra['general_imprimer']+"&nbsp;";
 	content += 			"<img style='border:none' src='/OPO/images/printit.png'></a><br>";
 	content += 		"</div>";
 	content += 		"<div id='itiContainer'>" + descrIti + "</div>";
 	content += "</div>";
 	
	VMPopUp.setText(content);
};

// hide some elements when printing routing (others are hidden with CSS too!)
routingFuncs.prototype.print = function(){
	this.kaMap.getRawObject("mainContainer").style.display = "none";
	window.print();
}