﻿//
//  cap.js - javascript gestione della ricerca del cap
//  
//  Author:  Sabrina Cufaro [sabrina.cufaro@produzioneinformatica.com]
//

function init() {
    // azzera tutti i valori            
    document.getElementById("DDLProvincia").selectedIndex = "0";
    document.getElementById("txtLocalita").value = "";
    document.getElementById("DropDownList1").selectedIndex = "0";
    document.getElementById("txtIndirizzo").value = "";
    document.getElementById("txtCap").value = "";
    document.getElementById("tipoRicerca").value = "";
    document.getElementById("MsgErrore").innerHTML = "";
}
function provincia() {
    init();
    document.getElementById("provinciaPanel").style.display = "block";
    document.getElementById("localitaPanel").style.display = "none";
    document.getElementById("indirizzoPanel").style.display = "none";
    document.getElementById("capPanel").style.display = "none";
    document.getElementById("ricercaPanel").style.display = "block";
    document.getElementById("tipoRicerca").value = "provincia";
    document.getElementById("risultatoPanel").style.display = "none";
    document.getElementById("DDLProvincia").disabled = false;
    document.getElementById("MsgErrore").style.display = "none";
}
function localita() {
    init();
    document.getElementById("provinciaPanel").style.display = "none";
    document.getElementById("localitaPanel").style.display = "block";
    document.getElementById("indirizzoPanel").style.display = "none";
    document.getElementById("capPanel").style.display = "none";
    document.getElementById("ricercaPanel").style.display = "block";
    document.getElementById("tipoRicerca").value = "localita";
    document.getElementById("risultatoPanel").style.display = "none";
    document.getElementById("txtLocalita").disabled = false;
    document.getElementById("MsgErrore").style.display = "none";
}
function indirizzo() {
    init();
    document.getElementById("provinciaPanel").style.display = "none";
    document.getElementById("localitaPanel").style.display = "none";
    document.getElementById("indirizzoPanel").style.display = "block";
    document.getElementById("capPanel").style.display = "none";
    document.getElementById("ricercaPanel").style.display = "block";
    document.getElementById("tipoRicerca").value = "indirizzo";
    document.getElementById("risultatoPanel").style.display = "none";
    document.getElementById("DropDownList1").disabled = false;
    document.getElementById("txtIndirizzo").disabled = false;
    document.getElementById("MsgErrore").style.display = "none";
}
function cap() {
    init();
    document.getElementById("provinciaPanel").style.display = "none";
    document.getElementById("localitaPanel").style.display = "none";
    document.getElementById("indirizzoPanel").style.display = "none";
    document.getElementById("capPanel").style.display = "block";
    document.getElementById("ricercaPanel").style.display = "block";
    document.getElementById("tipoRicerca").value = "cap";
    document.getElementById("risultatoPanel").style.display = "none";
    document.getElementById("txtCap").disabled = false;
    document.getElementById("MsgErrore").style.display = "none";
}

function ricerca() {
    if (checkCampi())
        document.form1.submit();
}

function checkCampi() {
    var ric = document.getElementById("tipoRicerca").value;
    if (ric == 'provincia')
        return checkProvincia();
    if (ric == 'localita')
        return checkLocalita();
    if (ric == 'indirizzo')
        return checkIndirizzo();
    if (ric == 'cap')
        return checkCap();
}

function checkProvincia() {
    if (document.getElementById("DDLProvincia").selectedIndex == "0") {
        document.getElementById("MsgErrore").innerHTML = "Scegliere la provincia!";
        document.getElementById("MsgErrore").style.display = "block";
        return false;
    }

    /* Le città suddivise in zone postali sono: 
    * ALESSANDRIA, ANCONA, BARI, BERGAMO, BOLOGNA, BRESCIA, CAGLIARI,  
    * CATANIA, CESENA, FERRARA, FIRENZE, FOGGIA, FORLI', GENOVA,  
    * LA SPEZIA, LIVORNO, MESSINA, MILANO, MODENA, NAPOLI, PADOVA,  
    * PALERMO, PARMA, PERUGIA, PESARO, PESCARA, PIACENZA, PISA, 
    * RAVENNA, REGGIO CALABRIA, REGGIO EMILIA, RIMINI, ROMA, SALERNO, 
    * TARANTO, TORINO,  TRENTO,  TRIESTE,  VENEZIA,  VERBANIA,  VERONA 
    */

    // Pronvincie
    var pr = new Array("AL", "AN", "BA", "BG", "BO", "BS", "CA", "CT",
                                "FE", "FI", "FG", "FC", "GE", "SP", "LI", "ME",
                                "MI", "MO", "NA", "PD", "PA", "PR", "PG", "PU",
                                "PE", "PC", "PI", "RA", "RC", "RE", "RN", "RM",
                                "SA", "TA", "TO", "TN", "TS", "VE", "VB", "VR");
    // ricerca
    var prScelta = document.getElementById("DDLProvincia").value;

    for (indice in pr) {
        if (pr[indice] == prScelta) {
            var msg = "La ricerca potrebbe impiegare molto tempo. \nSi consiglia di effettuare la ricerca per indirizzo.";
            msg += "\nSi vuole comunque continuare?"
            if (window.confirm(msg))
                return true;
            else
                return false;
        }

    }
    return true;
}

function checkLocalita() {
    var obj = document.getElementById("txtLocalita");
    trim(obj);
    if (obj.value == "") {
        document.getElementById("MsgErrore").innerHTML = "Inserire la località!";
        document.getElementById("MsgErrore").style.display = "block";
        return false;
    }
    obj.value = obj.value.toUpperCase();

    /* Le città suddivise in zone postali sono: 
    * ALESSANDRIA, ANCONA, BARI, BERGAMO, BOLOGNA, BRESCIA, CAGLIARI,  
    * CATANIA, CESENA, FERRARA, FIRENZE, FOGGIA, FORLI', GENOVA,  
    * LA SPEZIA, LIVORNO, MESSINA, MILANO, MODENA, NAPOLI, PADOVA,  
    * PALERMO, PARMA, PERUGIA, PESARO, PESCARA, PIACENZA, PISA, 
    * RAVENNA, REGGIO CALABRIA, REGGIO EMILIA, RIMINI, ROMA, SALERNO, 
    * TARANTO, TORINO,  TRENTO,  TRIESTE,  VENEZIA,  VERBANIA,  VERONA 
    */

    // Località
    var loc = new Array("ALESSANDRIA", "ANCONA", "BARI", "BERGAMO", "BOLOGNA",
                                "BRESCIA", "CAGLIARI", "CATANIA", "FERRARA", "FIRENZE",
                                "FOGGIA", "FORLI CESENA", "FORLI'", "CESENA", "GENOVA",
                                "LA SPEZIA", "LIVORNO", "MESSINA", "MILANO", "MODENA",
                                "NAPOLI", "PADOVA", "PALERMO", "PARMA", "PERUGIA",
                                "PESARO E URBINO", "PESARO", "URBINO", "PESCARA",
                                "PIACENZA", "PISA", "RAVENNA", "REGGIO CALABRIA",
                                "REGGIO EMILIA", "RIMINI", "ROMA", "SALERNO", "TARANTO",
                                "TORINO", "TRENTO", "TRIESTE", "VENEZIA", "VERBANO CUSIO OSSOLA",
                                "VERBANO", "CUSIO", "OSSOLA", "VERONA");
    // ricerca
    var locScelta = document.getElementById("txtLocalita").value;

    for (indice in loc) {
        if (loc[indice] == locScelta) {
            var msg = "La ricerca potrebbe impiegare molto tempo. \nSi consiglia di effettuare la ricerca per indirizzo.";
            msg += "\nSi vuole comunque continuare?"
            if (window.confirm(msg))
                return true;
            else
                return false;
        }

    }
    return true;
}

function checkIndirizzo() {
    var obj = document.getElementById("txtIndirizzo");
    trim(obj);
    if (obj.value == "") {
        document.getElementById("MsgErrore").innerHTML = "Inserire l'indirizzo!";
        document.getElementById("MsgErrore").style.display = "block";
        return false;
    }
    obj.value = obj.value.toUpperCase();
    return true;
}

function checkCap() {
    var obj = document.getElementById("txtCap");
    trim(obj);
    if (obj.value == "") {
        document.getElementById("MsgErrore").innerHTML = "Inserire il CAP!";
        document.getElementById("MsgErrore").style.display = "block";
        return false;
    }
    if (isNaN(obj.value) || parseInt(obj.value) < 0) {
        document.getElementById("MsgErrore").innerHTML = "Il CAP deve contenere solo numeri!";
        document.getElementById("MsgErrore").style.display = "block";
        return false;
    }
    return true;
}

// Elimina gli spazi all'inizio e alla fine
function trim(obj) {
    obj.value = obj.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    return true;
}

