function langGeocoder(lang){
	if (lang == 'fr' || lang == 'en' || lang == 'de' || lang == 'it'){  //photon only supports fr en de it, the rest will be en by default
		return lang
	}
	else return "en"
}

// Expediteur
var expeLocList = []
var expeLoc;
$('#expeGeocoderPhoton').typeahead({
    source: function (query, process) {
		var timer;
		clearTimeout(timer);
		timer = setTimeout(function() {  // using a timer of 400 ms to troubleshot people typing super fast
			expeLocList = []
			var l = langGeocoder(lang)
			var url = "https://photon.komoot.io/api?q="+query+"&lang="+l+"&limit=4&osm_tag=place:city&osm_tag=place:town&osm_tag=place:village&osm_tag=place:municipality&osm_tag=place:suburb&osm_tag=place:neighbourhood&osm_tag=place:hamlet&osm_tag=highway";
			//var url = "https://photon.komoot.io/api?q="+query+"&lang="+l+"&limit=4";
			var res = []
			return $.getJSON(url,function(data){
				for (var i in data.features){
					if(data.features[i].properties.type == "street"){
						res.push(data.features[i].properties.name+" ("+data.features[i].properties.city+", "+data.features[i].properties.country+")")
						expeLocList.push({
							name: data.features[i].properties.city,
							center: data.features[i].geometry.coordinates,
							fullname: data.features[i].properties.name+" ("+data.features[i].properties.city+", "+data.features[i].properties.country+")"
						})
					}
					else if(data.features[i].properties.osm_value == "suburb" || data.features[i].properties.osm_value == "neighbourhood"){
						res.push(data.features[i].properties.name+" ("+data.features[i].properties.city+", "+data.features[i].properties.country+")")
						expeLocList.push({
							name: data.features[i].properties.city,
							center: data.features[i].geometry.coordinates,
							fullname: data.features[i].properties.name+" ("+data.features[i].properties.city+", "+data.features[i].properties.country+")"
						})
					}
					else{
						res.push(data.features[i].properties.name+" ("+data.features[i].properties.state+", "+data.features[i].properties.country+")")
						expeLocList.push({
							name: data.features[i].properties.name,
							center: data.features[i].geometry.coordinates,
							fullname: data.features[i].properties.name+" ("+data.features[i].properties.state+", "+data.features[i].properties.country+")"
						})
					}
					
				}
				return process(res)
			})
			},
			400  
		)
    },
	matcher: function(item){ //needed when spelling not exact
     return true
	},

    afterSelect: function (obj) {
      for (var i in expeLocList){
		  if (expeLocList[i].fullname == obj){
			expeLoc = expeLocList[i]
			validExpeLoc = true
			checkFormValid()
		  }
	  }
    }
})

//Destinataire
var destLocList = []
var destLoc;
$('#destGeocoderPhoton').typeahead({
    source: function (query, process) {
		var timer;
		clearTimeout(timer);
		timer = setTimeout(function() {  // using a timer of 400 ms to troubleshot people typing super fast
			destLocList = []
			var l = langGeocoder(lang)
			var url = "https://photon.komoot.io/api?q="+query+"&lang="+l+"&limit=4&osm_tag=place:city&osm_tag=place:town&osm_tag=place:village&osm_tag=place:municipality&osm_tag=place:suburb&osm_tag=place:neighbourhood&osm_tag=place:hamlet&osm_tag=highway";
			var res = []
			return $.getJSON(url,function(data){
				for (var i in data.features){
					if(data.features[i].properties.type == "street"){
						res.push(data.features[i].properties.name+" ("+data.features[i].properties.city+", "+data.features[i].properties.country+")")
						destLocList.push({
							name: data.features[i].properties.city,
							center: data.features[i].geometry.coordinates,
							fullname: data.features[i].properties.name+" ("+data.features[i].properties.city+", "+data.features[i].properties.country+")"
						})
					}
					else if(data.features[i].properties.osm_value == "suburb" || data.features[i].properties.osm_value == "neighbourhood"){
						res.push(data.features[i].properties.name+" ("+data.features[i].properties.city+", "+data.features[i].properties.country+")")
						destLocList.push({
							name: data.features[i].properties.city,
							center: data.features[i].geometry.coordinates,
							fullname: data.features[i].properties.name+" ("+data.features[i].properties.city+", "+data.features[i].properties.country+")"
						})
					}
					else{
						res.push(data.features[i].properties.name+" ("+data.features[i].properties.state+", "+data.features[i].properties.country+")")
						destLocList.push({
							name: data.features[i].properties.name,
							center: data.features[i].geometry.coordinates,
							fullname: data.features[i].properties.name+" ("+data.features[i].properties.state+", "+data.features[i].properties.country+")"
						})
					}
					
				}
				return process(res)
			})
		},
			400  
		)
    },
	matcher: function(item){ //needed when spelling not exact
     return true
	},

    afterSelect: function (obj) {
      for (var i in destLocList){
		  if (destLocList[i].fullname == obj){
			destLoc = destLocList[i]
			validDestLoc = true
			checkFormValid()			
		  }
	  }
    }
})