	var distances;		//Array of variables and their distance to the fromAddress
    var geocoder = null;
    var formId;
    var hiddenId;

	function load() {
		//General Google Map Loader
 		if (GBrowserIsCompatible()) {  
			//document.body.innerHTML = document.body.innerHTML + '<div id="map_info"></div>';
            geocoder = new GClientGeocoder();
		}
	}
  
	function setPostal(fromPostal, hidden_id, form_id) {
        hiddenId = hidden_id;
        formId = form_id;
        //First do an initial test to see if postal code entered is correct using regular expression
        var re = /^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s*$/i
        if (!re.test(fromPostal)) {
            //if postal code test fail, output error and stop the application
            alert ("Veuillez taper un code postal valide.");
            return;
        }
		//initializing variables and starting the loadDirection function
		distances = new Array();
        geocoder.getLatLng(fromPostal, 
            function(point) {
                if (!point) { 
                    alert (fromPostal + " est introuvable."); 
                } 
                else {
                    loadDistances(point.latRadians(), point.lngRadians());
                }
            }
        );
   
	}

    function loadDistances(fromLat, fromLng) {
        //var el = document.getElementById("map_info");
        var el = document.getElementById(hiddenId);
        //el.innerHTML = "";
        for (var y in company_postals) {
            distances[y] = {map_id:company_postals[y].map_id, postalCode:company_postals[y].postal_code, distance:calcDistance(fromLat, fromLng, company_postals[y].lat, company_postals[y].lng)};
        }
        distances = distances.sort(callbackFunc);
        for (var y = 0; y < numReturn; y++) {
            //el.innerHTML = el.innerHTML + "Map id: " + distances[y].map_id + " Postal Code: " + distances[y].postalCode + " - Distance: " + (distances[y].distance / 1000) + "km<BR>";
            el.value = (el.value + " " + distances[y].map_id).ltrim();
        }
        document.getElementById(formId).submit();
        //Debug only
        //for (var y in distances) {
            //el.innerHTML = el.innerHTML + "#" + (1+Number(y)) + " Postal Code: " + distances[y].postalCode + " - Distance:" + (distances[y].distance / 1000) + "km<BR>";
        //}
        
    }

	//Extra stuff not related to Google

	String.prototype.ltrim = function() {
		//Custom Left Trim function
		return this.replace(/^\s+/,"");
	}


	function callbackFunc(a,b){
		//This function is use for the sort only

		if(a.distance == b.distance){

			if(a.postalCode == b.postalCode){
				return 0;
			}

			return (a.postalCode < b.postalCode) ? -1 : 1;
		}

		return (a.distance < b.distance) ? -1 : 1;
	}

    function calcDistance(lat1, long1, lat2, long2) {
        //all input must be in radian
        //return distance in meter
        var radSphere = 6370997;
        var distance;
        distance = radSphere * Math.acos(Math.sin(lat1)*Math.sin(lat2) + Math.cos(lat1)*Math.cos(lat2)*Math.cos(long1-long2))
        return distance;
    }

