﻿var LOCATIONS = new Array();
var GoogleMapCitySearch = '';
function InitMap(){
    if (GoogleMapCitySearch != undefined) {
        GoogleMapCitySearch = new Dackia.GoogleMap({
        MapHolder: 'GoogleMapCitySearch',
        DefaultLat: '62.492027730426905',
        DefaultLng: '14.853515625'
        });
    }
}            
function CheckFindGeoGarage(txt, garage, e) {
    var keyCode = e.keyCode.toString();
    if (keyCode == "13") {
        e.returnValue = false;
        e.cancel = false;
        if (navigator.appName != 'Microsoft Internet Explorer') {
            e.preventDefault();
        }
        FindGeoGarage(txt.value, garage);
    }
}
function FindGeoGarage(search, garage) {
    if (document.getElementById(search) != undefined) {
        search = document.getElementById(search).value;
    }
    search = search.replace('Ort / postnummer', '');
    var oldSearch = getFromQueryString(document.location, "geosrc");
    var redirect = search != oldSearch;
    if (search.length > 2) {
        InitMap();
        googlesearch = search + ',Sweden';
            GoogleMapCitySearch.GeoEncoder.getLatLng(
            googlesearch,
            function (latlng) {
                if (latlng) {
                    latLong = latlng.toString();
                    if (redirect) {
                        RedirectWithGeoCoordinates(search, latLong, garage);
                    }
                } else if (redirect) {
                    RedirectWithGeoCoordinates(search, "unknown,unknown", garage);
                }
            }
            );
    }
}
function RedirectWithGeoCoordinates(search, latLong, garage) {
    latLong = latLong.replace('(', '').replace(')', '').replace(' ', '');
    var newLocation = setToQueryString(document.location, "geosrc", search);
    newLocation = setToQueryString(newLocation, "latitude", latLong.split(',')[0]);
    newLocation = setToQueryString(newLocation, "longitude", latLong.split(',')[1]);
    newLocation = setToQueryString(newLocation, "garage", garage);
    document.location = newLocation;
}
