﻿function MostraCampiPrecompilati(idRbBassa, idRbMedia, idRbAlta,
                            idRbPerc0, idRbPerc90, idRbPerc100,
                            idTxtSaldoMedioAttivo, idrbNecessitaScopertoNo,
                            idrbAccreditoSi, idTxtBonificiRicevuti, idTxtVersamenti,
                            idTxtPrelContanteSportello, idTxtPrelPropriaBanca, idTxtPrelAltraBanca, idTxtPrelCircuitiInternazionali,
                            idTxtPagBancomat, idTxtPagAssegni, idTxtBonOnline, idTxtBonSportello, idTxtNumUtenzeDomiciliate,
                            idTxtNumCarteBancomat, idTxtNumCarteCredito,
                            idSpanSoloOnline1 ,idSpanSoloOnline2, idSpanSoloOnline3, idSpanSoloFiliale1) {

    var rbBassa = document.getElementById(idRbBassa);
    var rbMedia = document.getElementById(idRbMedia);
    var rbAlta = document.getElementById(idRbAlta);
    var rbPerc0 = document.getElementById(idRbPerc0);
    var rbPerc90 = document.getElementById(idRbPerc90);
    var rbPerc100 = document.getElementById(idRbPerc100);

    var operativita;
    var percentuale;

    operativita = -1;
    percentuale = -1;
    
    if (rbBassa.checked) operativita = 1;
    if (rbMedia.checked) operativita = 2;
    if (rbAlta.checked) operativita = 3;

    if (rbPerc0.checked) percentuale = 0;
    if (rbPerc90.checked) percentuale = 90;
    if (rbPerc100.checked) percentuale = 100;

    //Bonifici ricevuti
    var txtSaldoMedioAttivo = document.getElementById(idTxtSaldoMedioAttivo);
    var rbNecessitaScopertoNo = document.getElementById(idrbNecessitaScopertoNo);
    var rbAccreditoSi = document.getElementById(idrbAccreditoSi);
    var txtBonificiRicevuti = document.getElementById(idTxtBonificiRicevuti);
    var txtVersamenti = document.getElementById(idTxtVersamenti);
    var txtPrelContanteSportello = document.getElementById(idTxtPrelContanteSportello);
    var txtPrelPropriaBanca = document.getElementById(idTxtPrelPropriaBanca);
    var txtPrelAltraBanca = document.getElementById(idTxtPrelAltraBanca);
    var txtPrelCircuitiInternazionali = document.getElementById(idTxtPrelCircuitiInternazionali);
    var txtPagBancomat = document.getElementById(idTxtPagBancomat);
    var txtPagAssegni = document.getElementById(idTxtPagAssegni);
    var txtBonOnline = document.getElementById(idTxtBonOnline);
    var txtBonSportello = document.getElementById(idTxtBonSportello);
    var txtNumUtenzeDomiciliate = document.getElementById(idTxtNumUtenzeDomiciliate);
    var txtNumCarteBancomat = document.getElementById(idTxtNumCarteBancomat);
    var txtNumCarteCredito = document.getElementById(idTxtNumCarteCredito);

    var spanSoloOnline1 = document.getElementById(idSpanSoloOnline1);
    var spanSoloOnline2 = document.getElementById(idSpanSoloOnline2);
    var spanSoloOnline3 = document.getElementById(idSpanSoloOnline3);
    var spanSoloFiliale1 = document.getElementById(idSpanSoloFiliale1);


    //Reset campi che possono essere bloccati
    txtBonOnline.disabled = false;   
    txtBonOnline.value = "";  
    txtVersamenti.disabled = false;
    txtPrelContanteSportello.disabled = false;
    txtBonSportello.disabled = false;
    txtVersamenti.value = ""
    txtPrelContanteSportello.value = ""
    txtBonSportello.value = ""
    spanSoloOnline1.style.visibility = "hidden";
    spanSoloOnline2.style.visibility = "hidden";
    spanSoloOnline3.style.visibility = "hidden";
    spanSoloFiliale1.style.visibility = "hidden";

    
    if (percentuale != -1) {
        switch (percentuale) {
            case 0:
                txtBonOnline.value = 0;
                txtBonOnline.disabled = true;
                spanSoloFiliale1.style.visibility = "visible";
                
                if (operativita != -1) {
                    switch (operativita) {
                        case 1:
                            txtBonificiRicevuti.value = 1;
                            txtVersamenti.value = 4;
                            txtPrelContanteSportello.value = 3;
                            txtPrelPropriaBanca.value = 3;
                            txtPrelAltraBanca.value = 1;
                            txtPrelCircuitiInternazionali.value = 0;
                            txtPagBancomat.value = 3;
                            txtPagAssegni.value = 2;
                            txtBonSportello.value = 3;
                            txtNumUtenzeDomiciliate.value = 0;
                            txtNumCarteBancomat.value = 0;
                            break;
                        case 2:
                            txtBonificiRicevuti.value = 2;
                            txtVersamenti.value = 7;
                            txtPrelContanteSportello.value = 5;
                            txtPrelPropriaBanca.value = 7;
                            txtPrelAltraBanca.value = 2;
                            txtPrelCircuitiInternazionali.value = 1;
                            txtPagBancomat.value = 25;
                            txtPagAssegni.value = 5;
                            txtBonSportello.value = 6;
                            txtNumUtenzeDomiciliate.value = 1;
                            txtNumCarteBancomat.value = 1;
                            break;
                        case 3:
                            txtBonificiRicevuti.value = 6;
                            txtVersamenti.value = 12;
                            txtPrelContanteSportello.value = 15;
                            txtPrelPropriaBanca.value = 21;
                            txtPrelAltraBanca.value = 11;
                            txtPrelCircuitiInternazionali.value = 3;
                            txtPagBancomat.value = 60;
                            txtPagAssegni.value = 14;
                            txtBonSportello.value = 8;
                            txtNumUtenzeDomiciliate.value = 2;
                            txtNumCarteBancomat.value = 2;
                            break;
                    }
                }
                break;
            case 90:
                if (operativita != -1) {
                    switch (operativita) {
                        case 1:
                            txtBonificiRicevuti.value = 4;
                            txtVersamenti.value = 1;
                            txtPrelContanteSportello.value = 1;
                            txtPrelPropriaBanca.value = 5;
                            txtPrelAltraBanca.value = 1;
                            txtPrelCircuitiInternazionali.value = 0;
                            txtPagBancomat.value = 3;
                            txtPagAssegni.value = 2;
                            txtBonOnline.value = 2;
                            txtBonSportello.value = 1;
                            txtNumUtenzeDomiciliate.value = 2;
                            txtNumCarteBancomat.value = 1;
                            break;
                        case 2:
                            txtBonificiRicevuti.value = 7;
                            txtVersamenti.value = 2;
                            txtPrelContanteSportello.value = 2;
                            txtPrelPropriaBanca.value = 8;
                            txtPrelAltraBanca.value = 4;
                            txtPrelCircuitiInternazionali.value = 1;
                            txtPagBancomat.value = 25;
                            txtPagAssegni.value = 4;
                            txtBonOnline.value = 6;
                            txtBonSportello.value = 1;
                            txtNumUtenzeDomiciliate.value = 2;
                            txtNumCarteBancomat.value = 1;
                            break;
                        case 3:
                            txtBonificiRicevuti.value = 15;
                            txtVersamenti.value = 3;
                            txtPrelContanteSportello.value = 4;
                            txtPrelPropriaBanca.value = 28;
                            txtPrelAltraBanca.value = 15;
                            txtPrelCircuitiInternazionali.value = 3;
                            txtPagBancomat.value = 60;
                            txtPagAssegni.value = 5;
                            txtBonOnline.value = 16;
                            txtBonSportello.value = 1;
                            txtNumUtenzeDomiciliate.value = 3;
                            txtNumCarteBancomat.value = 2;
                            break;
                    }
                }
                break;
            case 100:
                
                txtVersamenti.value = 0;
                txtVersamenti.disabled = true;
                txtPrelContanteSportello.value = 0;
                txtPrelContanteSportello.disabled = true;
                txtBonSportello.value = 0;
                txtBonSportello.disabled = true;
                spanSoloOnline1.style.visibility = "visible";
                spanSoloOnline2.style.visibility = "visible";
                spanSoloOnline3.style.visibility = "visible";
                
                if (operativita != -1) {
                    switch (operativita) {
                        case 1:
                            txtBonificiRicevuti.value = 5;
                            txtPrelPropriaBanca.value = 5;
                            txtPrelAltraBanca.value = 2;
                            txtPrelCircuitiInternazionali.value = 0;
                            txtPagBancomat.value = 3;
                            txtPagAssegni.value = 2;
                            txtBonOnline.value = 3;
                            txtNumUtenzeDomiciliate.value = 1;
                            txtNumCarteBancomat.value = 0;
                            break;
                        case 2:
                            txtBonificiRicevuti.value = 9;
                            txtPrelPropriaBanca.value = 9;
                            txtPrelAltraBanca.value = 5;
                            txtPrelCircuitiInternazionali.value = 1;
                            txtPagBancomat.value = 25;
                            txtPagAssegni.value = 3;
                            txtBonOnline.value = 8;
                            txtNumUtenzeDomiciliate.value = 2;
                            txtNumCarteBancomat.value = 1;
                            break;
                        case 3:
                            txtBonificiRicevuti.value = 18
                            txtPrelPropriaBanca.value = 17;
                            txtPrelAltraBanca.value = 30;
                            txtPrelCircuitiInternazionali.value = 3;
                            txtPagBancomat.value = 60;
                            txtPagAssegni.value = 2;
                            txtBonOnline.value = 20;
                            txtNumUtenzeDomiciliate.value = 3;
                            txtNumCarteBancomat.value = 2;
                            break;
                    }
                }
                break;
        }
    }
}

function ValidazioneCampi(idEsitoGeo,
                            idRbBassa, idRbMedia, idRbAlta,
                            idRbPerc0, idRbPerc90, idRbPerc100,
                            idTxtDataNascitaGiorno, idTxtDataNascitaMese, idTxtDataNascitaAnno,
                            idTxtSaldoMedioAttivo,
                            idRbNecessitaScopertoSi, idRbNecessitaScopertoNo,
                            idTxtSaldoMedioPassivo, idTxtPercentualeAnnoSaldoPassivo,
                            idRbAccreditoSi, idRbAccreditoNo,
                            idTxtBonificiRicevuti, idTxtVersamenti,
                            idTxtPrelContanteSportello, idTxtPrelPropriaBanca, idTxtPrelAltraBanca, idTxtPrelCircuitiInternazionali,
                            idTxtPagBancomat, idTxtPagAssegni, idTxtBonOnline, idTxtBonSportello, idTxtNumUtenzeDomiciliate,
                            idTxtNumCarteBancomat, idTxtNumCarteCredito,
                            idTxtIndirizzoResidenza, idTXtComuneResidenza, idTxtCAPResidenza,
                            idLatitudine, idLongitudine, idDatiRicerca,
                            idBtnRicerca) {

    //Validazione Data nascita
    if (!ValidateDate(idTxtDataNascitaGiorno, idTxtDataNascitaMese, idTxtDataNascitaAnno, "Data di nascita"))
        return false;
    if (!DataPassata(idTxtDataNascitaGiorno, idTxtDataNascitaMese, idTxtDataNascitaAnno, "Data di nascita"))
        return false;
    if (document.getElementById(idTxtDataNascitaAnno).value * 1 < 1900) {
        alert("Indicare un anno di nascita supereiore al 1900.");
        document.getElementById(idTxtDataNascitaAnno).focus();
        return false;
    }

    //Validazione dati di residenza
    var ctrlIndirizzo = document.getElementById(idTxtIndirizzoResidenza);
    var ctrlComune = document.getElementById(idTXtComuneResidenza);
    var ctrlCAP = document.getElementById(idTxtCAPResidenza);

    //Indirizzo
    if (ctrlIndirizzo.value == "Via e numero civico")
        ctrlIndirizzo.value = "";
    if (ctrlIndirizzo.value != "") {
        if (check_indirizzo(ctrlIndirizzo, true, "Indicare l'indirizzo e il numero civico, anche se incompleto, nell'apposito campo.") == false) return false;
        if (non_soloInt(ctrlIndirizzo, "Indirizzo e numero civico", "") == true) return false;
    }

    //Comune
    if (ctrlComune.value == "Località / Comune")
        ctrlComune.value = "";
    if (ctrlComune.value != "") {
        if (check_indirizzo(ctrlComune, true, "Indicare il comune, anche se incompleto, nell'apposito campo.") == false) return false;
        if (non_soloInt(ctrlComune, "Comune", "") == true) return false;
    }

    //CAP
    if (ctrlCAP.value == "C.A.P.")
        ctrlCAP.value = "";
    if (ctrlCAP.value) {
        if (check_integerNonMex(ctrlCAP, true, true) == false || ctrlCAP.value.length != 5) {
            alert("Valore non corretto nel campo C.A.P.");
            ctrlCAP.focus();  
            return false;
        }
    }
    
    //Validazione Accredito stipendio/pensione
    var rbAccreditoSi = document.getElementById(idRbAccreditoSi);
    var rbAccreditoNo = document.getElementById(idRbAccreditoNo);

    if (!rbAccreditoSi.checked && !rbAccreditoNo.checked) {
        alert("Devi scegliere un valore per campo 'Accredito stipendio/pensione?'");
        rbAccreditoSi.focus();
        return false;
    }

    //Validazione Saldo medio attivo
    var txtSaldoMedioAttivo = document.getElementById(idTxtSaldoMedioAttivo);
    if (!ValidateConRegularExpression(txtSaldoMedioAttivo.value) || txtSaldoMedioAttivo.value == "") {
        alert("Devi inserire un valore decimale per campo 'Saldo medio attivo'.");
        txtSaldoMedioAttivo.focus();
        return false;
    }

    //Validazione Necessità scoperto
    var rbNecessitaScopertoSi = document.getElementById(idRbNecessitaScopertoSi);
    var rbNecessitaScopertoNo = document.getElementById(idRbNecessitaScopertoNo);

    if (!rbNecessitaScopertoSi.checked && !rbNecessitaScopertoNo.checked) {
        alert("Devi scegliere un valore per campo 'Necessità scoperto di conto?'");
        rbNecessitaScopertoSi.focus();
        return false;
    }

    if (rbNecessitaScopertoSi.checked) {
        //Validazione Percentuale anno con saldo passivo
        if (!ValidaPercentuale(idTxtPercentualeAnnoSaldoPassivo) || idTxtPercentualeAnnoSaldoPassivo.value == "") {
            alert("Il valore inserito nel campo 'Porzione di anno con saldo passivo'\ndeve essere un valore decimale compreso tra 0 e 100.");
            return false;
        }

        //Validazione Saldo medio passivo
        var txtSaldoMedioPassivo = document.getElementById(idTxtSaldoMedioPassivo);
        if (!ValidateConRegularExpression(txtSaldoMedioPassivo.value) || txtSaldoMedioPassivo.value == "") {
            alert("Devi inserire un valore decimale per campo 'Saldo medio passivo'.");
            txtSaldoMedioPassivo.focus();
            return false;
        }
    }

    //Validazione Numero carte di credito
    var txtNumCarteCredito = document.getElementById(idTxtNumCarteCredito);
    if (!ValidateInteroConRegularExpression(txtNumCarteCredito.value) || txtNumCarteCredito.value == "") {
        alert("Devi inserire un valore intero per campo 'Carte di credito (Visa/Mastercard)'.");
        txtNumCarteCredito.focus();
        return false;
    }

    //Validazione Percentuale Operazioni Online
    var rbPerc0 = document.getElementById(idRbPerc0);
    var rbPerc90 = document.getElementById(idRbPerc90);
    var rbPerc100 = document.getElementById(idRbPerc100);

    if (!rbPerc0.checked && !rbPerc90.checked && !rbPerc100.checked) {
        alert("Devi scegliere un valore per campo 'Canale di ultilizzo'.");
        rbPerc0.focus();
        return false;
    }


    //Validazione operativita
    var rbBassa = document.getElementById(idRbBassa);
    var rbMedia = document.getElementById(idRbMedia);
    var rbAlta = document.getElementById(idRbAlta);

    if (!rbBassa.checked && !rbMedia.checked && !rbAlta.checked) {
        alert("Devi scegliere un valore per campo 'Intensità di utilizzo'.");
        rbBassa.focus();
        return false;
    }

    //Validazione Bonifici ricevuti (diversi da accredito stipendio/pensione)
    var txtBonificiRicevuti = document.getElementById(idTxtBonificiRicevuti);
    if (!ValidateInteroConRegularExpression(txtBonificiRicevuti.value) || txtBonificiRicevuti.value == "") {
        alert("Devi inserire un valore intero per campo 'Bonifici ricevuti'.");
        txtBonificiRicevuti.focus();
        return false;
    }

    //Validazione Versamenti (contanti o assegni)
    var txtVersamenti = document.getElementById(idTxtVersamenti);
    if (!ValidateInteroConRegularExpression(txtVersamenti.value) || txtVersamenti.value == "") {
        alert("Devi inserire un valore intero per campo 'Versamenti contanti o assegni'.");
        txtVersamenti.focus();
        return false;
    }

    //Validazione Prelievo contante allo sportello
    var txtPrelContanteSportello = document.getElementById(idTxtPrelContanteSportello);
    if (!ValidateInteroConRegularExpression(txtPrelContanteSportello.value) || txtPrelContanteSportello.value == "") {
        alert("Devi inserire un valore intero per campo 'Prelievi di contante in filiale'.");
        txtPrelContanteSportello.focus();
        return false;
    }

    //Validazione Prelievo bancomat presso propria banca
    var txtPrelPropriaBanca = document.getElementById(idTxtPrelPropriaBanca);
    if (!ValidateInteroConRegularExpression(txtPrelPropriaBanca.value) || txtPrelPropriaBanca.value == "") {
        alert("Devi inserire un valore intero per campo 'Prelievi bancomat propria banca'.");
        txtPrelPropriaBanca.focus();
        return false;
    }

    //Validazione Prelievo bancomat presso altra banca
    var txtPrelAltraBanca = document.getElementById(idTxtPrelAltraBanca);
    if (!ValidateInteroConRegularExpression(txtPrelAltraBanca.value) || txtPrelAltraBanca.value == "") {
        alert("Devi inserire un valore intero per campo 'Prelievi bancomat altre banche'.");
        txtPrelAltraBanca.focus();
        return false;
    }

    //Validazione Prelievo bancomat circuiti internazionali
    var txtPrelCircuitiInternazionali = document.getElementById(idTxtPrelCircuitiInternazionali);
    if (!ValidateInteroConRegularExpression(txtPrelCircuitiInternazionali.value) || txtPrelCircuitiInternazionali.value == "") {
        alert("Devi inserire un valore intero per campo 'Prelievi bancomat all’estero'.");
        txtPrelCircuitiInternazionali.focus();
        return false;
    }

    //Validazione Pagamenti con bancomat
    var txtPagBancomat = document.getElementById(idTxtPagBancomat);
    if (!ValidateInteroConRegularExpression(txtPagBancomat.value) || txtPagBancomat.value == "") {
        alert("Devi inserire un valore intero per campo 'Pagamenti con bancomat'.");
        txtPagBancomat.focus();
        return false;
    }

    //Validazione Pagamenti con assegni
    var txtPagAssegni = document.getElementById(idTxtPagAssegni);
    if (!ValidateInteroConRegularExpression(txtPagAssegni.value) || txtPagAssegni.value == "") {
        alert("Devi inserire un valore intero per campo 'Pagamenti con assegni'.");
        txtPagAssegni.focus();
        return false;
    }

    //Validazione Bonifici online
    var txtBonOnline = document.getElementById(idTxtBonOnline);
    if (!ValidateInteroConRegularExpression(txtBonOnline.value) || txtBonOnline.value == "") {
        alert("Devi inserire un valore intero per campo 'Bonifici online'.");
        txtBonOnline.focus();
        return false;
    }

    //Validazione Bonifici allo sportello
    var txtBonSportello = document.getElementById(idTxtBonSportello);
    if (!ValidateInteroConRegularExpression(txtBonSportello.value) || txtBonSportello.value == "") {
        alert("Devi inserire un valore intero per campo 'Bonifici in filiale'.");
        txtBonSportello.focus();
        return false;
    }

    //Validazione Numero di utenze domiciliate
    var txtNumUtenzeDomiciliate = document.getElementById(idTxtNumUtenzeDomiciliate);
    if (!ValidateInteroConRegularExpression(txtNumUtenzeDomiciliate.value) || txtNumUtenzeDomiciliate.value == "") {
        alert("Devi inserire un valore intero per campo 'Utenze domiciliate'.");
        txtNumUtenzeDomiciliate.focus();
        return false;
    }

    //Validazione Numero carte bancomat
    var txtNumCarteBancomat = document.getElementById(idTxtNumCarteBancomat);
    if (!ValidateInteroConRegularExpression(txtNumCarteBancomat.value) || txtNumCarteBancomat.value == "") {
        alert("Devi inserire un valore intero per campo 'Carte bancomat'.");
        txtNumCarteBancomat.focus();
        return false;
    }
    
    window.scrollTo(0, document.body.scrollHeight);
    RicercaGeocoder(1, idTxtIndirizzoResidenza, idTXtComuneResidenza, idTxtCAPResidenza, 'Italia', idEsitoGeo, idLatitudine, idLongitudine, idDatiRicerca, txtBonOnline, txtVersamenti, txtPrelContanteSportello, txtBonSportello);
    
    return false;
}
