﻿// Google Maps JScript file //

var map;
var geocoder = new GClientGeocoder();
var divGoogleResults;

function initialize(div_name, x, y, points, zoom) {
    map = new GMap2(document.getElementById(div_name));
    // Add 10 markers to the map at random locations

    var bounds = new GLatLngBounds();
    if (points != null) {

        var j;

        j = points.length;

        for (var i = 0; i < j; i++) {
            //map.addOverlay(new GMarker(points[i],markerOptions));  
            bounds.extend(points[i]);
        }
    }

    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds) + zoom);
    map.setUIToDefault();
}

function AddPushpin(location, title, description, iconurl) {
    var markerIcon = new GIcon();
    if (iconurl != '') {
        markerIcon.image = iconurl;
        //markerIcon.iconSize = new GSize(10, 10);  
        markerIcon.iconAnchor = new GPoint(0, 0);
        markerIcon.infoWindowAnchor = new GPoint(2, 2);
    }
    else
        markerIcon = new GIcon(G_DEFAULT_ICON);

    var html;
    html = '<div class="ITOmni_Salespoints_InfoBox"><strong>' + title + '</strong><br>' + description + '</div>';

    // Set up our GMarkerOptions object  
    markerOptions = { icon: markerIcon };
    var marker = new GMarker(location, markerOptions);
    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
    });
    map.addOverlay(marker);
}

function AddListPushpin(name, description, x, y, url, iconurl) {

    var markerIcon = new GIcon();
    if (iconurl != '') {
        markerIcon.image = iconurl;
        //markerIcon.iconSize = new GSize(10, 10);  
        markerIcon.iconAnchor = new GPoint(0, 0);
        markerIcon.infoWindowAnchor = new GPoint(2, 2);
    }
    else
        markerIcon = new GIcon(G_DEFAULT_ICON);

    var html;
    html = '<div class="ITOmni_Salespoints_InfoBox"><strong><a href=\'' + url + '\'>' + name + '</a></strong><br>' + description + '</div>';

    // Set up our GMarkerOptions object  
    markerOptions = { icon: markerIcon };
    var marker = new GMarker(new GLatLng(y, x), markerOptions);
    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
    });
    map.addOverlay(marker);

}


function showAddress(txtCity, txtStreet, txtNumber, msg_notfound, msg_requiredfields) {
    var address;
    //geocoder.setBaseCountryCode("NL");

    var City = document.getElementById(txtCity).value;
    var Street = document.getElementById(txtStreet).value;
    var Number = document.getElementById(txtNumber).value;

    if (City == '' || Street == '' || Number == '')
        alert(msg_requiredfields);
    else {
        address = City + ', ' + Street + ' ' + Number;

        if (geocoder) {
            geocoder.getLatLng(
          address,
          function(point) {
              if (!point) {
                  alert(msg_notfound);
              } else {
                  map.setCenter(point, 10);
                  var marker = new GMarker(point);
                  map.addOverlay(marker);

                  var html;
                  html = '<div class="ITOmni_Salespoints_InfoBox Refine">' + address + '</div>';

                  marker.openInfoWindowHtml(html);
              }
          }
        );


        }

    }

    return false;

}

function FindLocation(country, location, divResults) {


    var address = location;
    divGoogleResults = document.getElementById(divResults);

    var countryFilter;
    countryFilter = country;

    if (countryFilter != '') {
        geocoder = null;
        geocoder = new GClientGeocoder();
        geocoder.setBaseCountryCode(countryFilter);
    }
    geocoder.getLocations(address, ShowResults);

}

function FindLocation2(textboxLocation, divResults, trCountries, defaultcountry, backlink, searchtable) {
    //Show back to search hyperlink
    var hyperlinkBackToSearch;
    hyperlinkBackToSearch = document.getElementById(backlink);
    hyperlinkBackToSearch.style.display = '';

    //Hide search table
    var tableSearch;
    tableSearch = document.getElementById(searchtable);
    tableSearch.style.display = 'none';

    var address = document.getElementById(textboxLocation).value;
    divGoogleResults = document.getElementById(divResults);

    var countryFilter;
    var tmpCountry;

    countryFilter = '';

    // Iterate through all the input elements to get the selected country
    var inputs = document.getElementsByTagName('INPUT');
    for (i = 0; i < inputs.length; i++) {
    
        // The custom attribute of the radiobutton gets added to the span surrounding the input
        if (inputs[i].parentNode && inputs[i].parentNode.tagName == "SPAN") {
            tmpCountry = inputs[i].parentNode.getAttribute("CountryCode");
            if (tmpCountry != null) {
                //Valid radioButton found
                if (inputs[i].checked) {
                    countryFilter = tmpCountry;
                }
            }
        }
    }

    if ((countryFilter == '') && (defaultcountry != ''))
        countryFilter = defaultcountry;

    if (countryFilter != '') {
        geocoder = null;
        geocoder = new GClientGeocoder();
        geocoder.setBaseCountryCode(countryFilter);
    }
    geocoder.getLocations(address, ShowResults);

}



function ShowResults(response) {

    if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
    } else {
        var tmpPlace;
        if (response.Placemark.length > 1) {

            //Multiple results, let user select location

            divGoogleResults.innerHTML = ''
            for (var i = 0; i < response.Placemark.length; i++) {
                tmpPlace = response.Placemark[i];
                divGoogleResults.innerHTML += '<a class="GMapsResult" href="#" onclick="SendCoordinates(' + tmpPlace.Point.coordinates[0] + ',' + tmpPlace.Point.coordinates[1] + ')">' + tmpPlace.address + '</a><br>';
            }
        }
        else {
            tmpPlace = response.Placemark[0];
            SendCoordinates(tmpPlace.Point.coordinates[0], tmpPlace.Point.coordinates[1]);
            //divGoogleResults.innerHTML += '<a  class="GMapsResult" href="#" onclick="SendCoordinates(' + tmpPlace.Point.coordinates[0] + ',' + tmpPlace.Point.coordinates[1] + ')">' + tmpPlace.address + '</a><br>';
        }

    }

}

function SendCoordinates(x, y) {
    __doPostBack('GMapsScript', x + '|' + y);
}
