﻿function RicercaGeocoder(livello, idIndirizzo, idComune, idcap, nazione, idEsito, idLatitudine, idLongitudine, idDatiRicerca, txtAbilitato1, txtAbilitato2, txtAbilitato3, txtAbilitato4) {

    var indirizzo = document.getElementById(idIndirizzo).value;
    var comune = document.getElementById(idComune).value;
    var cap = document.getElementById(idcap).value;
    testoConferma = "";
    document.getElementById(idEsito).value = "";
    document.getElementById(idLatitudine).value = "";
    document.getElementById(idLongitudine).value = "";

    var datiRicerca = document.getElementById(idDatiRicerca);
    datiRicerca.value = "";
    search = "";
    
    if (cap != "") {
        search = nazione + " " + cap;
        ultimoLivello = 1;
    }

    if (comune != "") {
        search = nazione + " " + comune;
        ultimoLivello = 1;
    }

    if (indirizzo != "") {
        search = nazione + " " + indirizzo;
        ultimoLivello = 1;
    }
        
    if (cap != "" && comune != "") {
        switch (livello) {
            case 1:
                search = nazione + " " + comune + " " + cap;
                break;
            case 2:
                search = nazione + " " + cap;
                break;
            case 3:
                search = nazione + " " + comune;
                break;
        }
        ultimoLivello = 3;
    }

    if (indirizzo != "" && comune != "") {
        switch (livello) {
            case 1:
                search = nazione + " " + indirizzo + " " + comune;
                break;
            case 2:
                search = nazione + " " + comune;
                break;
            case 3:
                search = nazione + " " + indirizzo;
                break;
        }
        ultimoLivello = 3;
    }

    if (indirizzo != "" && cap != "") {
        switch (livello) {
            case 1:
                search = nazione + " " + indirizzo + " " + cap;
                break;
            case 2:
                search = nazione + " " + cap;
                break;
            case 3:
                search = nazione + " " + indirizzo;
                break;
        }
        ultimoLivello = 3;
    }

    if (cap != "" && comune != "" && indirizzo != "") {
        switch (livello) {
            case 1:
                search = nazione + " " + indirizzo + " " + comune + " " + cap;
                break;
            case 2:
                search = nazione + " " + comune + " " + cap;
                break;
            case 3:
                search = nazione + " " + indirizzo + " " + cap;
                break;
            case 4:
                search = nazione + " " + indirizzo + " " + comune;
                break;
            case 5:
                search = nazione + " " + cap;
                break;
            case 6:
                search = nazione + " " + comune;
                break;
            case 7:
                search = nazione + " " + indirizzo;
                break;
        }
        ultimoLivello = 7;
    }

    //Memorizzo il valore in una textbox (Utile per test)
    datiRicerca.value = search;
    //Non esistono campi su cui fare la geolocalizzazione.
    if (search == "") {
        AvviaRicerca(txtAbilitato1, txtAbilitato2, txtAbilitato3, txtAbilitato4, idEsito);
        return false;
    }

    //Chiamo google

    geo.getLocations(search, function (result) {
        document.getElementById(idEsito).value = status[result.Status.code];
        if (result.Status.code == G_GEO_SUCCESS) {
            //Esito Positivo: Memorizzo i dati e termino le chiamate
            if (result.Placemark.length > 0) {
                document.getElementById(idLatitudine).value = result.Placemark[0].Point.coordinates[1];
                document.getElementById(idLongitudine).value = result.Placemark[0].Point.coordinates[0];

                if (result.Placemark[0] != null && result.Placemark[0].AddressDetails != null && result.Placemark[0].AddressDetails.Country != null && result.Placemark[0].AddressDetails.Country.AdministrativeArea != null) {

                    var IndG = result.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea;
                    var coerente = true;
                    document.getElementById(idDatiRicerca).value = "";
                    if (IndG != null) {

                        if (IndG.Locality != null) {
                            if (IndG.Locality.Thoroughfare != null) {
                                document.getElementById(idDatiRicerca).value += "#1" + IndG.Locality.Thoroughfare.ThoroughfareName + ", ";
                                var indMy = document.getElementById(idIndirizzo).value.toUpperCase();
                                var indGG = IndG.Locality.Thoroughfare.ThoroughfareName.toUpperCase();

                                indMy = indMy.replace("CORSO", "").replace("VIA", "").replace("VICOLO", "").replace("PIAZZA", "").replace(",", "").replace(" ", "").replace(" ", "");
                                indGG = indGG.replace("CORSO", "").replace("VIA", "").replace("VICOLO", "").replace("PIAZZA", "").replace(",", "").replace(" ", "").replace(" ", "");

                                if (indGG.indexOf(indMy) == -1 && indMy != "")
                                    coerente = false;
                            }
                            if (IndG.Locality.PostalCode != null) {
                                document.getElementById(idDatiRicerca).value += "#2" + IndG.Locality.PostalCode.PostalCodeNumber + ", ";
                                if (IndG.Locality.PostalCode.PostalCodeNumber.toUpperCase() != document.getElementById(idcap).value.toUpperCase() && document.getElementById(idcap).value != "")
                                    coerente = false;
                            }
                            if (IndG.Locality.LocalityName != null) {
                                document.getElementById(idDatiRicerca).value += "#3" + IndG.Locality.LocalityName;
                                var comMy = document.getElementById(idComune).value.toUpperCase();
                                var comGG = IndG.Locality.LocalityName.toUpperCase();

                                comMy = comMy.replace(",", "").replace(" ", "").replace(" ", "");
                                comGG = comGG.replace(",", "").replace(" ", "").replace(" ", "");

                                if (comGG.indexOf(comMy) == -1 && comMy != "")
                                    coerente = false;
                            }
                        }
                        if (IndG.SubAdministrativeAreaName != null)
                            document.getElementById(idDatiRicerca).value += "#4" + " (" + IndG.SubAdministrativeAreaName + ")";
                    }
                    else 
                    {
                        document.getElementById(idLatitudine).value = "";
                        document.getElementById(idLongitudine).value = "";
                    }
                }
                // Controllo coerenza dati
                if (coerente == true)
                    document.getElementById(idDatiRicerca).value = "1" + document.getElementById(idDatiRicerca).value;
                else
                    document.getElementById(idDatiRicerca).value = "2" + document.getElementById(idDatiRicerca).value;

                // Sigla Prov   document.getElementById(idDatiRicerca).value = result.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
                // indirizzo    document.getElementById(idDatiRicerca).value = result.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;    
                // Cap          document.getElementById(idDatiRicerca).value = result.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                // comune       document.getElementById(idDatiRicerca).value = result.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
                // stato        document.getElementById(idDatiRicerca).value = result.Placemark[0].AddressDetails.Country.CountryName;
            }

            //Ho trovato i valori di longitudine e latitudine. Procedo con la ricerca dei prodotti.
            AvviaRicerca(txtAbilitato1, txtAbilitato2, txtAbilitato3, txtAbilitato4, idEsito)

        } else {
            //Esito negativo: nuova ricerca o terminazione

            // Se il livello ha un testo visualizzo il confirm
            flagProcedi = true;
            if (testoConferma != "") {
                if (confirm(testoConferma)) {
                    flagProcedi = true;
                } else {
                    flagProcedi = false;
                }
            }

            //Chiamata al livello seguente   (Il livello 5 è l'ultimo mi fermo)
            if (flagProcedi) {
                if (livello != ultimoLivello) {
                    RicercaGeocoder(livello + 1, idIndirizzo, idComune, idcap, nazione, idEsito, idLatitudine, idLongitudine, idDatiRicerca, txtAbilitato1, txtAbilitato2, txtAbilitato3, txtAbilitato4);
                } else {
                    // return true;  Sono arrivato all'ultimo livello di ricerca e ho deciso di procedere alla ricerca dei prodotti senza Geocoder
                    AvviaRicerca(txtAbilitato1, txtAbilitato2, txtAbilitato3, txtAbilitato4, idEsito);
                }
            } else {
                //Ho deciso di fermarmi per sistemare i dati di residenza
                document.getElementById(idIndirizzo).focus();
            }

        }
    }
    );

}

function AvviaRicerca(txtAbilitato1, txtAbilitato2, txtAbilitato3, txtAbilitato4, idEsito) {
    //Abilitazione campi per poterli usare nella ricerca
    if (txtAbilitato1 != null)
        txtAbilitato1.disabled = false;
    if (txtAbilitato2 != null)
        txtAbilitato2.disabled = false;
    if (txtAbilitato3 != null)
        txtAbilitato3.disabled = false;
    if (txtAbilitato4 != null)
        txtAbilitato4.disabled = false;

    //Se avvio la ricerca l'esito deve essere "Success" anche senza le coordinate

    document.getElementById(idEsito).value = "Success";

    if (document.getElementById('divAttendere') != null) {
        document.getElementById('divAttendere').style.display = 'block';
        if (navigator.appVersion.indexOf("Chrome") < 0)
            AnimateAttendere();
    }

    document.aspnetForm.submit();
}


// codice per l'icona animata "Attendere..."
var animationCount = 1;

function AnimateAttendere() {
    document.getElementById("imgAttendere").src = "/image/attesa_" + animationCount + ".gif?v=1";
    if (animationCount == 8) {
        animationCount = 1;
    }
    else {
        animationCount++;
    }

    setTimeout('AnimateAttendere()', 100);
}
